Merge origin/BAEL-2565 into BAEL-2565
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
- [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing)
|
||||
- [Guide To Java 8 Optional](http://www.baeldung.com/java-optional)
|
||||
- [Guide to the Java 8 forEach](http://www.baeldung.com/foreach-java)
|
||||
- [Java Base64 Encoding and Decoding](http://www.baeldung.com/java-base64-encode-and-decode)
|
||||
- [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap)
|
||||
- [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods)
|
||||
- [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency)
|
||||
@@ -34,3 +33,8 @@
|
||||
- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects)
|
||||
- [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic)
|
||||
- [How to Replace Many if Statements in Java](https://www.baeldung.com/java-replace-if-statements)
|
||||
- [Java @Override Annotation](https://www.baeldung.com/java-override)
|
||||
- [Java @SuppressWarnings Annotation](https://www.baeldung.com/java-suppresswarnings)
|
||||
- [Java @SafeVarargs Annotation](https://www.baeldung.com/java-safevarargs)
|
||||
- [Java @Deprecated Annotation](https://www.baeldung.com/java-deprecated)
|
||||
- [Java 8 Predicate Chain](https://www.baeldung.com/java-predicate-chain)
|
||||
|
||||
+35
-2
@@ -1,5 +1,5 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-8</artifactId>
|
||||
@@ -104,6 +104,24 @@
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${asspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>${powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jmockit</groupId>
|
||||
<artifactId>jmockit</artifactId>
|
||||
<version>${jmockit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -119,7 +137,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
@@ -142,6 +160,16 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
|
||||
</argLine>
|
||||
<disableXmlReport>true</disableXmlReport>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -159,9 +187,14 @@
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<asspectj.version>1.8.9</asspectj.version>
|
||||
<powermock.version>2.0.0-RC.4</powermock.version>
|
||||
<jmockit.version>1.44</jmockit.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
<jmh-generator.version>1.19</jmh-generator.version>
|
||||
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
|
||||
<!-- plugins -->
|
||||
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.list;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private int petals;
|
||||
|
||||
public Flower(String name, int petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(int petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.java8;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class Java8CollectionCleanupUnitTest {
|
||||
|
||||
// tests -
|
||||
|
||||
@Test
|
||||
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {
|
||||
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
|
||||
final List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {
|
||||
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
|
||||
final List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {
|
||||
final List<Integer> listWithoutNulls = Lists.newArrayList(null, 1, 2, null, 3, null);
|
||||
listWithoutNulls.removeIf(Objects::isNull);
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
|
||||
final List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
|
||||
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream().distinct().collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutDuplicates, hasSize(3));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.baeldung.java8;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java8PredicateChainUnitTest {
|
||||
|
||||
private List<String> names = Arrays.asList("Adam", "Alexander", "John", "Tom");
|
||||
|
||||
@Test
|
||||
public void whenFilterList_thenSuccess() {
|
||||
List<String> result = names.stream()
|
||||
.filter(name -> name.startsWith("A"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertThat(result, contains("Adam", "Alexander"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithMultipleFilters_thenSuccess() {
|
||||
List<String> result = names.stream()
|
||||
.filter(name -> name.startsWith("A"))
|
||||
.filter(name -> name.length() < 5)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertThat(result, contains("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithComplexPredicate_thenSuccess() {
|
||||
List<String> result = names.stream()
|
||||
.filter(name -> name.startsWith("A") && name.length() < 5)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertThat(result, contains("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithCombinedPredicatesInline_thenSuccess() {
|
||||
List<String> result = names.stream()
|
||||
.filter(((Predicate<String>) name -> name.startsWith("A")).and(name -> name.length() < 5))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertThat(result, contains("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithCombinedPredicatesUsingAnd_thenSuccess() {
|
||||
Predicate<String> predicate1 = str -> str.startsWith("A");
|
||||
Predicate<String> predicate2 = str -> str.length() < 5;
|
||||
|
||||
List<String> result = names.stream()
|
||||
.filter(predicate1.and(predicate2))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertThat(result, contains("Adam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithCombinedPredicatesUsingOr_thenSuccess() {
|
||||
Predicate<String> predicate1 = str -> str.startsWith("J");
|
||||
Predicate<String> predicate2 = str -> str.length() < 4;
|
||||
|
||||
List<String> result = names.stream()
|
||||
.filter(predicate1.or(predicate2))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertThat(result, contains("John", "Tom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithCombinedPredicatesUsingOrAndNegate_thenSuccess() {
|
||||
Predicate<String> predicate1 = str -> str.startsWith("J");
|
||||
Predicate<String> predicate2 = str -> str.length() < 4;
|
||||
|
||||
List<String> result = names.stream()
|
||||
.filter(predicate1.or(predicate2.negate()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(3, result.size());
|
||||
assertThat(result, contains("Adam", "Alexander", "John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithCollectionOfPredicatesUsingAnd_thenSuccess() {
|
||||
List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>();
|
||||
allPredicates.add(str -> str.startsWith("A"));
|
||||
allPredicates.add(str -> str.contains("d"));
|
||||
allPredicates.add(str -> str.length() > 4);
|
||||
|
||||
List<String> result = names.stream()
|
||||
.filter(allPredicates.stream()
|
||||
.reduce(x -> true, Predicate::and))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertThat(result, contains("Alexander"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterListWithCollectionOfPredicatesUsingOr_thenSuccess() {
|
||||
List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>();
|
||||
allPredicates.add(str -> str.startsWith("A"));
|
||||
allPredicates.add(str -> str.contains("d"));
|
||||
allPredicates.add(str -> str.length() > 4);
|
||||
|
||||
List<String> result = names.stream()
|
||||
.filter(allPredicates.stream()
|
||||
.reduce(x -> false, Predicate::or))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertThat(result, contains("Adam", "Alexander"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package com.baeldung.list;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AddElementsUnitTest {
|
||||
|
||||
List<Flower> flowers;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.flowers = new ArrayList<>(Arrays.asList(
|
||||
new Flower("Poppy", 12),
|
||||
new Flower("Anemone", 8),
|
||||
new Flower("Catmint", 12)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
|
||||
List<Flower> anotherList = new ArrayList<>();
|
||||
anotherList.addAll(flowers);
|
||||
|
||||
assertEquals(anotherList.size(), flowers.size());
|
||||
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
|
||||
List<Flower> anotherList = new ArrayList<>();
|
||||
anotherList.addAll(flowers);
|
||||
|
||||
Flower flower = anotherList.get(0);
|
||||
flower.setPetals(flowers.get(0).getPetals() * 3);
|
||||
|
||||
assertEquals(anotherList.size(), flowers.size());
|
||||
Assert.assertTrue(anotherList.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
|
||||
List<Flower> target = new ArrayList<>();
|
||||
|
||||
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
|
||||
|
||||
assertEquals(target.size(), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
|
||||
List<Flower> flowerVase = new ArrayList<>();
|
||||
|
||||
flowers.stream()
|
||||
.skip(1)
|
||||
.forEachOrdered(flowerVase::add);
|
||||
|
||||
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||
assertFalse(flowerVase.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
|
||||
List<Flower> flowerVase = new ArrayList<>();
|
||||
|
||||
flowers.stream()
|
||||
.filter(f -> f.getPetals() > 10)
|
||||
.forEachOrdered(flowerVase::add);
|
||||
|
||||
assertEquals(flowerVase.size() + 1, flowers.size());
|
||||
assertFalse(flowerVase.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
|
||||
List<Flower> target = new ArrayList<>();
|
||||
|
||||
Optional.ofNullable(flowers)
|
||||
.ifPresent(target::addAll);
|
||||
|
||||
assertNotNull(target);
|
||||
assertEquals(target.size(), 3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ LocalDateTime.class })
|
||||
public class LocalDateTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeMock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
LocalDateTime dateTime = LocalDateTime.now(clock);
|
||||
mockStatic(LocalDateTime.class);
|
||||
when(LocalDateTime.now()).thenReturn(dateTime);
|
||||
String dateTimeExpected = "2014-12-22T10:15:30";
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFixedClock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
String dateTimeExpected = "2014-12-22T10:15:30";
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.now(clock);
|
||||
|
||||
assertThat(dateTime).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Mock;
|
||||
import mockit.MockUp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LocalDateTimeWithJMockUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeWithJMock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-21T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
new MockUp<LocalDateTime>() {
|
||||
@Mock
|
||||
public LocalDateTime now() {
|
||||
return LocalDateTime.now(clock);
|
||||
}
|
||||
};
|
||||
String dateTimeExpected = "2014-12-21T10:15:30";
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeWithExpectations_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
LocalDateTime dateTimeExpected = LocalDateTime.now(clock);
|
||||
new Expectations(LocalDateTime.class) {
|
||||
{
|
||||
LocalDateTime.now();
|
||||
result = dateTimeExpected;
|
||||
}
|
||||
};
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user