Skip to content

Commit a6ac119

Browse files
authored
Merge branch 'master' into BAEL_679_reactive_streams
2 parents 6def745 + 092d883 commit a6ac119

75 files changed

Lines changed: 1524 additions & 1072 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.travis.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: java
22

3-
install: travis_wait 60 mvn -q clean install
3+
install: travis_wait 60 mvn -q test
44

55
before_script:
66
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:-UseGCOverheadLimit'" > ~/.mavenrc
@@ -17,4 +17,5 @@ cache:
1717
directories:
1818
- .autoconf
1919
- $HOME/.m2
20+
2021

‎apache-poi/temp.xlsx‎

0 Bytes
Binary file not shown.

‎core-java/pom.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@
198198
<plugin>
199199
<groupId>org.apache.maven.plugins</groupId>
200200
<artifactId>maven-surefire-plugin</artifactId>
201-
<version>${maven-surefire-plugin.version}</version>
202201
<configuration>
203202
<excludes>
204203
<exclude>**/*IntegrationTest.java</exclude>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.arraycopy.model;
2+
3+
public class Address implements Cloneable {
4+
private String country;
5+
private String state;
6+
private String city;
7+
private String street;
8+
private String zipcode;
9+
10+
public String getCountry() {
11+
return country;
12+
}
13+
14+
public void setCountry(String country) {
15+
this.country = country;
16+
}
17+
18+
public String getState() {
19+
return state;
20+
}
21+
22+
public void setState(String state) {
23+
this.state = state;
24+
}
25+
26+
public String getCity() {
27+
return city;
28+
}
29+
30+
public void setCity(String city) {
31+
this.city = city;
32+
}
33+
34+
public String getStreet() {
35+
return street;
36+
}
37+
38+
public void setStreet(String street) {
39+
this.street = street;
40+
}
41+
42+
public String getZipcode() {
43+
return zipcode;
44+
}
45+
46+
public void setZipcode(String zipcode) {
47+
this.zipcode = zipcode;
48+
}
49+
50+
@Override
51+
protected Object clone() throws CloneNotSupportedException {
52+
super.clone();
53+
Address address = new Address();
54+
address.setCity(this.city);
55+
address.setCountry(this.country);
56+
address.setState(this.state);
57+
address.setStreet(this.street);
58+
address.setZipcode(this.zipcode);
59+
return address;
60+
}
61+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.arraycopy.model;
2+
3+
import java.io.Serializable;
4+
5+
public class Employee implements Serializable {
6+
private static final long serialVersionUID = -2454619097207585825L;
7+
private int id;
8+
private String name;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.baeldung.arraycopy;
2+
3+
import com.baeldung.arraycopy.model.Address;
4+
import com.baeldung.arraycopy.model.Employee;
5+
import org.apache.commons.lang3.SerializationUtils;
6+
import org.junit.Assert;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import java.util.Arrays;
11+
12+
public class ArrayCopyUtilTest {
13+
private static Employee[] employees;
14+
private static final int MAX = 2;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
employees = new Employee[MAX];
19+
Employee employee;
20+
for(int i = 0; i < MAX; i++) {
21+
employee = new Employee();
22+
employee.setName("Emp"+i);
23+
employee.setId(i);
24+
employees[i] = employee;
25+
}
26+
}
27+
28+
@Test
29+
public void givenArrayOfPrimitiveType_whenCopiedViaSystemsArrayCopy_thenSuccessful(){
30+
int[] array = {23, 43, 55};
31+
int[] copiedArray = new int[3];
32+
33+
System.arraycopy(array, 0, copiedArray, 0, 3);
34+
35+
Assert.assertTrue(array.length == copiedArray.length);
36+
Assert.assertTrue(copiedArray[0] == array[0]);
37+
Assert.assertTrue(copiedArray[1] == array[1]);
38+
Assert.assertTrue(copiedArray[2] == array[2]);
39+
}
40+
41+
@Test
42+
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaSystemsArrayCopy_thenSuccessful(){
43+
int[] array = {23, 43, 55, 12, 65, 88, 92};
44+
int[] copiedArray = new int[3];
45+
46+
System.arraycopy(array, 2, copiedArray, 0, 3);
47+
48+
Assert.assertTrue(3 == copiedArray.length);
49+
Assert.assertTrue(copiedArray[0] == array[2]);
50+
Assert.assertTrue(copiedArray[1] == array[3]);
51+
Assert.assertTrue(copiedArray[2] == array[4]);
52+
}
53+
54+
@Test
55+
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaArraysCopyOfRange_thenSuccessful(){
56+
int[] array = {23, 43, 55, 12, 65, 88, 92};
57+
58+
int[] copiedArray = Arrays.copyOfRange(array, 1, 4);
59+
60+
Assert.assertTrue(3 == copiedArray.length);
61+
Assert.assertTrue(copiedArray[0] == array[1]);
62+
Assert.assertTrue(copiedArray[1] == array[2]);
63+
Assert.assertTrue(copiedArray[2] == array[3]);
64+
}
65+
66+
@Test
67+
public void givenArrayOfPrimitiveType_whenCopiedViaArraysCopyOf_thenValueChangeIsSuccessful(){
68+
int[] array = {23, 43, 55, 12};
69+
int newLength = array.length;
70+
71+
int[] copiedArray = Arrays.copyOf(array, newLength);
72+
73+
Assert.assertNotNull(copiedArray);
74+
Assert.assertTrue(copiedArray.length == array.length);
75+
Assert.assertTrue(copiedArray[0] == array[0]);
76+
Assert.assertTrue(copiedArray[1] == array[1]);
77+
Assert.assertTrue(copiedArray[2] == array[2]);
78+
Assert.assertTrue(copiedArray[3] == array[3]);
79+
array[0] = 9;
80+
Assert.assertTrue(copiedArray[0] != array[0]);
81+
copiedArray[1] = 12;
82+
Assert.assertTrue(copiedArray[1] != array[1]);
83+
}
84+
85+
@Test
86+
public void givenArrayOfNonPrimitiveType_whenCopiedViaArraysCopyOf_thenDoShallowCopy(){
87+
Employee[] copiedArray = Arrays.copyOf(employees, employees.length);
88+
89+
Assert.assertNotNull(copiedArray);
90+
Assert.assertTrue(copiedArray.length == employees.length);
91+
employees[0].setName(employees[0].getName()+"_Changed");
92+
//change in employees' element caused change in the copied array
93+
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
94+
}
95+
96+
@Test
97+
public void givenArrayOfPrimitiveType_whenCopiedViaArrayClone_thenValueChangeIsSuccessful(){
98+
int[] array = {23, 43, 55, 12};
99+
100+
int[] copiedArray = array.clone();
101+
102+
Assert.assertNotNull(copiedArray);
103+
Assert.assertTrue(copiedArray.length == array.length);
104+
Assert.assertTrue(copiedArray[0] == array[0]);
105+
Assert.assertTrue(copiedArray[1] == array[1]);
106+
Assert.assertTrue(copiedArray[2] == array[2]);
107+
Assert.assertTrue(copiedArray[3] == array[3]);
108+
array[0] = 9;
109+
Assert.assertTrue(copiedArray[0] != array[0]);
110+
copiedArray[1] = 12;
111+
Assert.assertTrue(copiedArray[1] != array[1]);
112+
}
113+
114+
@Test
115+
public void givenArraysOfNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
116+
Employee[] copiedArray = employees.clone();
117+
118+
Assert.assertNotNull(copiedArray);
119+
Assert.assertTrue(copiedArray.length == employees.length);
120+
employees[0].setName(employees[0].getName()+"_Changed");
121+
//change in employees' element changed the copied array
122+
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
123+
}
124+
125+
@Test
126+
public void givenArraysOfCloneableNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
127+
Address[] addresses = createAddressArray();
128+
129+
Address[] copiedArray = addresses.clone();
130+
131+
Assert.assertNotNull(copiedArray);
132+
Assert.assertTrue(copiedArray.length == addresses.length);
133+
addresses[0].setCity(addresses[0].getCity()+"_Changed");
134+
Assert.assertTrue(copiedArray[0].getCity().equals(addresses[0].getCity()));
135+
}
136+
137+
@Test
138+
public void givenArraysOfSerializableNonPrimitiveType_whenCopiedViaSerializationUtils_thenDoDeepCopy(){
139+
Employee[] copiedArray = SerializationUtils.clone(employees);
140+
141+
Assert.assertNotNull(copiedArray);
142+
Assert.assertTrue(copiedArray.length == employees.length);
143+
employees[0].setName(employees[0].getName()+"_Changed");
144+
//change in employees' element didn't change in the copied array
145+
Assert.assertFalse(copiedArray[0].getName().equals(employees[0].getName()));
146+
}
147+
148+
private Address[] createAddressArray(){
149+
Address[] addresses = new Address[1];
150+
addresses[0] = createAddress();
151+
return addresses;
152+
}
153+
154+
private Address createAddress() {
155+
Address address = new Address();
156+
address.setCountry("USA");
157+
address.setState("CA");
158+
address.setCity("San Francisco");
159+
address.setStreet("Street 1");
160+
address.setZipcode("59999");
161+
return address;
162+
}
163+
}

core-java/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleTest.java renamed to core-java/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import static java.util.stream.Collectors.toList;
1313
import static org.assertj.core.api.Assertions.assertThat;
1414

15-
public class CountdownLatchExampleTest {
15+
public class CountdownLatchExampleIntegrationTest {
1616
@Test
1717
public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion() throws InterruptedException {
1818
// Given

core-java/src/test/java/org/baeldung/java/shell/JavaProcessUnitTest.java renamed to core-java/src/test/java/org/baeldung/java/shell/JavaProcessUnitIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.concurrent.Executors;
88
import java.util.function.Consumer;
99

10-
public class JavaProcessUnitTest {
10+
public class JavaProcessUnitIntegrationTest {
1111
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
1212

1313
private static class StreamGobbler implements Runnable {

core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java renamed to core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
package org.baeldung.java.streams;
2-
3-
import org.junit.Test;
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
import java.util.concurrent.ExecutionException;
8-
import java.util.concurrent.ForkJoinPool;
9-
import java.util.stream.Collectors;
10-
import java.util.stream.LongStream;
11-
import java.util.stream.Stream;
12-
13-
import static org.junit.Assert.assertEquals;
14-
import static org.junit.Assert.assertTrue;
15-
16-
public class ThreadPoolInParallelStreamTest {
17-
18-
@Test
19-
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
20-
long firstNum = 1;
21-
long lastNum = 1_000_000;
22-
23-
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
24-
25-
ForkJoinPool customThreadPool = new ForkJoinPool(4);
26-
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
27-
28-
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
29-
}
30-
31-
@Test
32-
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
33-
List<Long> aList = new ArrayList<>();
34-
Stream<Long> parallelStream = aList.parallelStream();
35-
36-
assertTrue(parallelStream.isParallel());
37-
}
38-
}
1+
package org.baeldung.java.streams;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.ForkJoinPool;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.LongStream;
11+
import java.util.stream.Stream;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertTrue;
15+
16+
public class ThreadPoolInParallelStreamIntegrationTest {
17+
18+
@Test
19+
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
20+
long firstNum = 1;
21+
long lastNum = 1_000_000;
22+
23+
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
24+
25+
ForkJoinPool customThreadPool = new ForkJoinPool(4);
26+
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
27+
28+
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
29+
}
30+
31+
@Test
32+
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
33+
List<Long> aList = new ArrayList<>();
34+
Stream<Long> parallelStream = aList.parallelStream();
35+
36+
assertTrue(parallelStream.isParallel());
37+
}
38+
}

0 commit comments

Comments
 (0)