cleanup work
This commit is contained in:
@@ -7,7 +7,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class CharToStringTest {
|
||||
|
||||
@Test
|
||||
public void givenChar_whenCallingStringValueOf_shouldConvertToString(){
|
||||
public void givenChar_whenCallingStringValueOf_shouldConvertToString() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = String.valueOf(givenChar);
|
||||
@@ -16,7 +16,7 @@ public class CharToStringTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenCallingToStringOnCharacter_shouldConvertToString(){
|
||||
public void givenChar_whenCallingToStringOnCharacter_shouldConvertToString() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = Character.toString(givenChar);
|
||||
@@ -25,7 +25,7 @@ public class CharToStringTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenCallingCharacterConstructor_shouldConvertToString3(){
|
||||
public void givenChar_whenCallingCharacterConstructor_shouldConvertToString3() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = new Character(givenChar).toString();
|
||||
@@ -34,7 +34,7 @@ public class CharToStringTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenConcatenated_shouldConvertToString4(){
|
||||
public void givenChar_whenConcatenated_shouldConvertToString4() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = givenChar + "";
|
||||
@@ -43,7 +43,7 @@ public class CharToStringTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChar_whenFormated_shouldConvertToString5(){
|
||||
public void givenChar_whenFormated_shouldConvertToString5() {
|
||||
final char givenChar = 'x';
|
||||
|
||||
final String result = String.format("%c", givenChar);
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.junit.Test;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class RandomListElementTest {
|
||||
public class RandomListElementUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenList_whenRandomIndexChosen_shouldReturnARandomElementUsingRandom() {
|
||||
@@ -20,7 +20,7 @@ public class RandomListElementTest {
|
||||
public void givenList_whenRandomIndexChosen_shouldReturnARandomElementUsingMathRandom() {
|
||||
List<Integer> givenList = Lists.newArrayList(1, 2, 3);
|
||||
|
||||
givenList.get((int)(Math.random() * givenList.size()));
|
||||
givenList.get((int) (Math.random() * givenList.size()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringToIntOrIntegerTest {
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
public class StringToIntOrIntegerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenParsingInt_shouldConvertToInt() {
|
||||
@@ -16,7 +17,6 @@ public class StringToIntOrIntegerTest {
|
||||
assertThat(result).isEqualTo(42);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingIntegerValueOf_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
@@ -42,192 +42,137 @@ public class Java8CollectorsTest {
|
||||
|
||||
@Test
|
||||
public void whenCollectingToList_shouldCollectToList() throws Exception {
|
||||
final List<String> result = givenList.stream()
|
||||
.collect(toList());
|
||||
final List<String> result = givenList.stream().collect(toList());
|
||||
|
||||
assertThat(result)
|
||||
.containsAll(givenList);
|
||||
assertThat(result).containsAll(givenList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectingToList_shouldCollectToSet() throws Exception {
|
||||
final Set<String> result = givenList.stream()
|
||||
.collect(toSet());
|
||||
final Set<String> result = givenList.stream().collect(toSet());
|
||||
|
||||
assertThat(result)
|
||||
.containsAll(givenList);
|
||||
assertThat(result).containsAll(givenList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectingToCollection_shouldCollectToCollection() throws Exception {
|
||||
final List<String> result = givenList.stream()
|
||||
.collect(toCollection(LinkedList::new));
|
||||
final List<String> result = givenList.stream().collect(toCollection(LinkedList::new));
|
||||
|
||||
assertThat(result)
|
||||
.containsAll(givenList)
|
||||
.isInstanceOf(LinkedList.class);
|
||||
assertThat(result).containsAll(givenList).isInstanceOf(LinkedList.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception {
|
||||
assertThatThrownBy(() -> {
|
||||
givenList.stream()
|
||||
.collect(toCollection(ImmutableList::of));
|
||||
givenList.stream().collect(toCollection(ImmutableList::of));
|
||||
}).isInstanceOf(UnsupportedOperationException.class);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectingToMap_shouldCollectToMap() throws Exception {
|
||||
final Map<String, Integer> result = givenList.stream()
|
||||
.collect(toMap(Function.identity(), String::length));
|
||||
final Map<String, Integer> result = givenList.stream().collect(toMap(Function.identity(), String::length));
|
||||
|
||||
assertThat(result)
|
||||
.containsEntry("a", 1)
|
||||
.containsEntry("bb", 2)
|
||||
.containsEntry("ccc", 3)
|
||||
.containsEntry("dd", 2);
|
||||
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectingToMap_shouldCollectToMapMerging() throws Exception {
|
||||
final Map<String, Integer> result = givenList.stream()
|
||||
.collect(toMap(Function.identity(), String::length, (i1, i2) -> i1));
|
||||
final Map<String, Integer> result = givenList.stream().collect(toMap(Function.identity(), String::length, (i1, i2) -> i1));
|
||||
|
||||
assertThat(result)
|
||||
.containsEntry("a", 1)
|
||||
.containsEntry("bb", 2)
|
||||
.containsEntry("ccc", 3)
|
||||
.containsEntry("dd", 2);
|
||||
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectingAndThen_shouldCollect() throws Exception {
|
||||
final List<String> result = givenList.stream()
|
||||
.collect(collectingAndThen(toList(), ImmutableList::copyOf));
|
||||
final List<String> result = givenList.stream().collect(collectingAndThen(toList(), ImmutableList::copyOf));
|
||||
|
||||
assertThat(result)
|
||||
.containsAll(givenList)
|
||||
.isInstanceOf(ImmutableList.class);
|
||||
assertThat(result).containsAll(givenList).isInstanceOf(ImmutableList.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJoining_shouldJoin() throws Exception {
|
||||
final String result = givenList.stream()
|
||||
.collect(joining());
|
||||
final String result = givenList.stream().collect(joining());
|
||||
|
||||
assertThat(result)
|
||||
.isEqualTo("abbcccdd");
|
||||
assertThat(result).isEqualTo("abbcccdd");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJoiningWithSeparator_shouldJoinWithSeparator() throws Exception {
|
||||
final String result = givenList.stream()
|
||||
.collect(joining(" "));
|
||||
final String result = givenList.stream().collect(joining(" "));
|
||||
|
||||
assertThat(result)
|
||||
.isEqualTo("a bb ccc dd");
|
||||
assertThat(result).isEqualTo("a bb ccc dd");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() throws Exception {
|
||||
final String result = givenList.stream()
|
||||
.collect(joining(" ", "PRE-", "-POST"));
|
||||
final String result = givenList.stream().collect(joining(" ", "PRE-", "-POST"));
|
||||
|
||||
assertThat(result)
|
||||
.isEqualTo("PRE-a bb ccc dd-POST");
|
||||
assertThat(result).isEqualTo("PRE-a bb ccc dd-POST");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPartitioningBy_shouldPartition() throws Exception {
|
||||
final Map<Boolean, List<String>> result = givenList.stream()
|
||||
.collect(partitioningBy(s -> s.length() > 2));
|
||||
final Map<Boolean, List<String>> result = givenList.stream().collect(partitioningBy(s -> s.length() > 2));
|
||||
|
||||
assertThat(result)
|
||||
.containsKeys(true, false)
|
||||
.satisfies(booleanListMap -> {
|
||||
assertThat(booleanListMap.get(true))
|
||||
.contains("ccc");
|
||||
assertThat(result).containsKeys(true, false).satisfies(booleanListMap -> {
|
||||
assertThat(booleanListMap.get(true)).contains("ccc");
|
||||
|
||||
assertThat(booleanListMap.get(false))
|
||||
.contains("a", "bb", "dd");
|
||||
});
|
||||
assertThat(booleanListMap.get(false)).contains("a", "bb", "dd");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCounting_shouldCount() throws Exception {
|
||||
final Long result = givenList.stream()
|
||||
.collect(counting());
|
||||
final Long result = givenList.stream().collect(counting());
|
||||
|
||||
assertThat(result)
|
||||
.isEqualTo(4);
|
||||
assertThat(result).isEqualTo(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSummarizing_shouldSummarize() throws Exception {
|
||||
final DoubleSummaryStatistics result = givenList.stream()
|
||||
.collect(summarizingDouble(String::length));
|
||||
final DoubleSummaryStatistics result = givenList.stream().collect(summarizingDouble(String::length));
|
||||
|
||||
assertThat(result.getAverage())
|
||||
.isEqualTo(2);
|
||||
assertThat(result.getCount())
|
||||
.isEqualTo(4);
|
||||
assertThat(result.getMax())
|
||||
.isEqualTo(3);
|
||||
assertThat(result.getMin())
|
||||
.isEqualTo(1);
|
||||
assertThat(result.getSum())
|
||||
.isEqualTo(8);
|
||||
assertThat(result.getAverage()).isEqualTo(2);
|
||||
assertThat(result.getCount()).isEqualTo(4);
|
||||
assertThat(result.getMax()).isEqualTo(3);
|
||||
assertThat(result.getMin()).isEqualTo(1);
|
||||
assertThat(result.getSum()).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAveraging_shouldAverage() throws Exception {
|
||||
final Double result = givenList.stream()
|
||||
.collect(averagingDouble(String::length));
|
||||
final Double result = givenList.stream().collect(averagingDouble(String::length));
|
||||
|
||||
assertThat(result)
|
||||
.isEqualTo(2);
|
||||
assertThat(result).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSumming_shouldSum() throws Exception {
|
||||
final Double result = givenList.stream()
|
||||
.collect(summingDouble(String::length));
|
||||
final Double result = givenList.stream().collect(summingDouble(String::length));
|
||||
|
||||
assertThat(result)
|
||||
.isEqualTo(8);
|
||||
assertThat(result).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMaxingBy_shouldMaxBy() throws Exception {
|
||||
final Optional<String> result = givenList.stream()
|
||||
.collect(maxBy(Comparator.naturalOrder()));
|
||||
final Optional<String> result = givenList.stream().collect(maxBy(Comparator.naturalOrder()));
|
||||
|
||||
assertThat(result)
|
||||
.isPresent()
|
||||
.hasValue("dd");
|
||||
assertThat(result).isPresent().hasValue("dd");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroupingBy_shouldGroupBy() throws Exception {
|
||||
final Map<Integer, Set<String>> result = givenList.stream()
|
||||
.collect(groupingBy(String::length, toSet()));
|
||||
final Map<Integer, Set<String>> result = givenList.stream().collect(groupingBy(String::length, toSet()));
|
||||
|
||||
assertThat(result)
|
||||
.containsEntry(1, newHashSet("a"))
|
||||
.containsEntry(2, newHashSet("bb", "dd"))
|
||||
.containsEntry(3, newHashSet("ccc"));
|
||||
assertThat(result).containsEntry(1, newHashSet("a")).containsEntry(2, newHashSet("bb", "dd")).containsEntry(3, newHashSet("ccc"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCreatingCustomCollector_shouldCollect() throws Exception {
|
||||
final ImmutableSet<String> result = givenList.stream()
|
||||
.collect(toImmutableSet());
|
||||
final ImmutableSet<String> result = givenList.stream().collect(toImmutableSet());
|
||||
|
||||
assertThat(result)
|
||||
.isInstanceOf(ImmutableSet.class)
|
||||
.contains("a", "bb", "ccc", "dd");
|
||||
assertThat(result).isInstanceOf(ImmutableSet.class).contains("a", "bb", "ccc", "dd");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,6 @@ public class CompletableFutureTest {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Future<String> calculateAsyncWithCancellation() throws InterruptedException {
|
||||
CompletableFuture<String> completableFuture = new CompletableFuture<>();
|
||||
|
||||
@@ -56,7 +55,6 @@ public class CompletableFutureTest {
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = CancellationException.class)
|
||||
public void whenCancelingTheFuture_thenThrowsCancellationException() throws ExecutionException, InterruptedException {
|
||||
|
||||
@@ -110,8 +108,7 @@ public class CompletableFutureTest {
|
||||
@Test
|
||||
public void whenUsingThenCompose_thenFuturesExecuteSequentially() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello")
|
||||
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello").thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
|
||||
|
||||
assertEquals("Hello World", completableFuture.get());
|
||||
|
||||
@@ -120,9 +117,7 @@ public class CompletableFutureTest {
|
||||
@Test
|
||||
public void whenUsingThenCombine_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello")
|
||||
.thenCombine(CompletableFuture.supplyAsync(() -> " World"),
|
||||
(s1, s2) -> s1 + s2);
|
||||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello").thenCombine(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> s1 + s2);
|
||||
|
||||
assertEquals("Hello World", completableFuture.get());
|
||||
|
||||
@@ -131,9 +126,7 @@ public class CompletableFutureTest {
|
||||
@Test
|
||||
public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
|
||||
|
||||
CompletableFuture.supplyAsync(() -> "Hello")
|
||||
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"),
|
||||
(s1, s2) -> System.out.println(s1 + s2));
|
||||
CompletableFuture.supplyAsync(() -> "Hello").thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> System.out.println(s1 + s2));
|
||||
|
||||
}
|
||||
|
||||
@@ -154,9 +147,7 @@ public class CompletableFutureTest {
|
||||
assertTrue(future2.isDone());
|
||||
assertTrue(future3.isDone());
|
||||
|
||||
String combined = Stream.of(future1, future2, future3)
|
||||
.map(CompletableFuture::join)
|
||||
.collect(Collectors.joining(" "));
|
||||
String combined = Stream.of(future1, future2, future3).map(CompletableFuture::join).collect(Collectors.joining(" "));
|
||||
|
||||
assertEquals("Hello Beautiful World", combined);
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JavaUtilTimeTest {
|
||||
public class JavaUtilTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void currentTime() {
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.baeldung.functionalinterface;
|
||||
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@@ -13,54 +14,43 @@ import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FunctionalInterfaceTest {
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
|
||||
public class FunctionalInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenPassingLambdaToComputeIfAbsent_thenTheValueGetsComputedAndPutIntoMap() {
|
||||
|
||||
Map<String, Integer> nameMap = new HashMap<>();
|
||||
Integer value = nameMap.computeIfAbsent("John", s -> s.length());
|
||||
|
||||
assertEquals(new Integer(4), nameMap.get("John"));
|
||||
assertEquals(new Integer(4), value);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingMethodReferenceToComputeIfAbsent_thenTheValueGetsComputedAndPutIntoMap() {
|
||||
|
||||
Map<String, Integer> nameMap = new HashMap<>();
|
||||
Integer value = nameMap.computeIfAbsent("John", String::length);
|
||||
|
||||
assertEquals(new Integer(4), nameMap.get("John"));
|
||||
assertEquals(new Integer(4), value);
|
||||
|
||||
}
|
||||
|
||||
public byte[] transformArray(short[] array, ShortToByteFunction function) {
|
||||
byte[] transformedArray = new byte[array.length];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
transformedArray[i] = function.applyAsByte(array[i]);
|
||||
}
|
||||
return transformedArray;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCustomFunctionalInterfaceForPrimitives_thenCanUseItAsLambda() {
|
||||
|
||||
short[] array = {(short) 1, (short) 2, (short) 3};
|
||||
short[] array = { (short) 1, (short) 2, (short) 3 };
|
||||
byte[] transformedArray = transformArray(array, s -> (byte) (s * 2));
|
||||
|
||||
byte[] expectedArray = {(byte) 2, (byte) 4, (byte) 6};
|
||||
byte[] expectedArray = { (byte) 2, (byte) 4, (byte) 6 };
|
||||
assertArrayEquals(expectedArray, transformedArray);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBiFunction_thenCanUseItToReplaceMapValues() {
|
||||
|
||||
Map<String, Integer> salaries = new HashMap<>();
|
||||
salaries.put("John", 40000);
|
||||
salaries.put("Freddy", 30000);
|
||||
@@ -71,22 +61,18 @@ public class FunctionalInterfaceTest {
|
||||
assertEquals(new Integer(50000), salaries.get("John"));
|
||||
assertEquals(new Integer(30000), salaries.get("Freddy"));
|
||||
assertEquals(new Integer(60000), salaries.get("Samuel"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenPassingLambdaToThreadConstructor_thenLambdaInferredToRunnable() {
|
||||
|
||||
Thread thread = new Thread(() -> System.out.println("Hello From Another Thread"));
|
||||
thread.start();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingSupplierToGenerateNumbers_thenCanUseItInStreamGenerate() {
|
||||
|
||||
int[] fibs = {0, 1};
|
||||
int[] fibs = { 0, 1 };
|
||||
Stream<Integer> fibonacci = Stream.generate(() -> {
|
||||
int result = fibs[1];
|
||||
int fib3 = fibs[0] + fibs[1];
|
||||
@@ -95,55 +81,44 @@ public class FunctionalInterfaceTest {
|
||||
return result;
|
||||
});
|
||||
|
||||
List<Integer> fibonacci5 = fibonacci.limit(5)
|
||||
.collect(Collectors.toList());
|
||||
List<Integer> fibonacci5 = fibonacci.limit(5).collect(Collectors.toList());
|
||||
|
||||
assertEquals(new Integer(1), fibonacci5.get(0));
|
||||
assertEquals(new Integer(1), fibonacci5.get(1));
|
||||
assertEquals(new Integer(2), fibonacci5.get(2));
|
||||
assertEquals(new Integer(3), fibonacci5.get(3));
|
||||
assertEquals(new Integer(5), fibonacci5.get(4));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingConsumerInForEach_thenConsumerExecutesForEachListElement() {
|
||||
|
||||
List<String> names = Arrays.asList("John", "Freddy", "Samuel");
|
||||
names.forEach(name -> System.out.println("Hello, " + name));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBiConsumerInForEach_thenConsumerExecutesForEachMapElement() {
|
||||
|
||||
Map<String, Integer> ages = new HashMap<>();
|
||||
ages.put("John", 25);
|
||||
ages.put("Freddy", 24);
|
||||
ages.put("Samuel", 30);
|
||||
|
||||
ages.forEach((name, age) -> System.out.println(name + " is " + age + " years old"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingPredicateInFilter_thenListValuesAreFilteredOut() {
|
||||
|
||||
List<String> names = Arrays.asList("Angela", "Aaron", "Bob", "Claire", "David");
|
||||
|
||||
List<String> namesWithA = names.stream()
|
||||
.filter(name -> name.startsWith("A"))
|
||||
.collect(Collectors.toList());
|
||||
List<String> namesWithA = names.stream().filter(name -> name.startsWith("A")).collect(Collectors.toList());
|
||||
|
||||
assertEquals(2, namesWithA.size());
|
||||
assertTrue(namesWithA.contains("Angela"));
|
||||
assertTrue(namesWithA.contains("Aaron"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingUnaryOperatorWithReplaceAll_thenAllValuesInTheListAreReplaced() {
|
||||
|
||||
List<String> names = Arrays.asList("bob", "josh", "megan");
|
||||
|
||||
names.replaceAll(String::toUpperCase);
|
||||
@@ -151,7 +126,6 @@ public class FunctionalInterfaceTest {
|
||||
assertEquals("BOB", names.get(0));
|
||||
assertEquals("JOSH", names.get(1));
|
||||
assertEquals("MEGAN", names.get(2));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,8 +133,7 @@ public class FunctionalInterfaceTest {
|
||||
|
||||
List<Integer> values = Arrays.asList(3, 5, 8, 9, 12);
|
||||
|
||||
int sum = values.stream()
|
||||
.reduce(0, (i1, i2) -> i1 + i2);
|
||||
int sum = values.stream().reduce(0, (i1, i2) -> i1 + i2);
|
||||
|
||||
assertEquals(37, sum);
|
||||
|
||||
@@ -178,10 +151,6 @@ public class FunctionalInterfaceTest {
|
||||
|
||||
}
|
||||
|
||||
public double squareLazy(Supplier<Double> lazyValue) {
|
||||
return Math.pow(lazyValue.get(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingSupplierToGenerateValue_thenValueIsGeneratedLazily() {
|
||||
|
||||
@@ -196,4 +165,18 @@ public class FunctionalInterfaceTest {
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public double squareLazy(Supplier<Double> lazyValue) {
|
||||
return Math.pow(lazyValue.get(), 2);
|
||||
}
|
||||
|
||||
public byte[] transformArray(short[] array, ShortToByteFunction function) {
|
||||
byte[] transformedArray = new byte[array.length];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
transformedArray[i] = function.applyAsByte(array[i]);
|
||||
}
|
||||
return transformedArray;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ReflectionTest {
|
||||
public class ReflectionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObject_whenGetsFieldNamesAtRuntime_thenCorrect() {
|
||||
@@ -20,8 +20,7 @@ public class ReflectionTest {
|
||||
|
||||
List<String> actualFieldNames = getFieldNames(fields);
|
||||
|
||||
assertTrue(Arrays.asList("name", "age")
|
||||
.containsAll(actualFieldNames));
|
||||
assertTrue(Arrays.asList("name", "age").containsAll(actualFieldNames));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -35,8 +34,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassName_whenCreatesObject_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> clazz = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
|
||||
assertEquals("Goat", clazz.getSimpleName());
|
||||
@@ -45,8 +43,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenRecognisesModifiers_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenRecognisesModifiers_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
int goatMods = goatClass.getModifiers();
|
||||
@@ -80,8 +77,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsImplementedInterfaces_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsImplementedInterfaces_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
Class<?>[] goatInterfaces = goatClass.getInterfaces();
|
||||
@@ -94,8 +90,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsConstructor_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsConstructor_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
Constructor<?>[] constructors = goatClass.getConstructors();
|
||||
|
||||
@@ -104,8 +99,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsFields_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsFields_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
Field[] fields = animalClass.getDeclaredFields();
|
||||
|
||||
@@ -116,20 +110,17 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsMethods_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsMethods_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
Method[] methods = animalClass.getDeclaredMethods();
|
||||
List<String> actualMethods = getMethodNames(methods);
|
||||
|
||||
assertEquals(4, actualMethods.size());
|
||||
assertTrue(actualMethods.containsAll(Arrays.asList("getName",
|
||||
"setName", "getSound")));
|
||||
assertTrue(actualMethods.containsAll(Arrays.asList("getName", "setName", "getSound")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsAllConstructors_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsAllConstructors_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Constructor<?>[] constructors = birdClass.getConstructors();
|
||||
|
||||
@@ -137,24 +128,20 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsEachConstructorByParamTypes_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenClass_whenGetsEachConstructorByParamTypes_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Constructor<?> cons1 = birdClass.getConstructor();
|
||||
Constructor<?> cons2 = birdClass.getConstructor(String.class);
|
||||
Constructor<?> cons3 = birdClass.getConstructor(String.class,
|
||||
boolean.class);
|
||||
Constructor<?> cons3 = birdClass.getConstructor(String.class, boolean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
|
||||
Constructor<?> cons1 = birdClass.getConstructor();
|
||||
Constructor<?> cons2 = birdClass.getConstructor(String.class);
|
||||
Constructor<?> cons3 = birdClass.getConstructor(String.class,
|
||||
boolean.class);
|
||||
Constructor<?> cons3 = birdClass.getConstructor(String.class, boolean.class);
|
||||
|
||||
Bird bird1 = (Bird) cons1.newInstance();
|
||||
Bird bird2 = (Bird) cons2.newInstance("Weaver bird");
|
||||
@@ -168,8 +155,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsPublicFields_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsPublicFields_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Field[] fields = birdClass.getFields();
|
||||
assertEquals(1, fields.length);
|
||||
@@ -178,8 +164,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsPublicFieldByName_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenClass_whenGetsPublicFieldByName_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Field field = birdClass.getField("CATEGORY");
|
||||
assertEquals("CATEGORY", field.getName());
|
||||
@@ -187,8 +172,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsDeclaredFields_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsDeclaredFields_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Field[] fields = birdClass.getDeclaredFields();
|
||||
assertEquals(1, fields.length);
|
||||
@@ -196,8 +180,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsFieldsByName_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Field field = birdClass.getDeclaredField("walks");
|
||||
assertEquals("walks", field.getName());
|
||||
@@ -205,17 +188,14 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenGetsType_thenCorrect()
|
||||
throws Exception {
|
||||
Field field = Class.forName("com.baeldung.java.reflection.Bird")
|
||||
.getDeclaredField("walks");
|
||||
public void givenClassField_whenGetsType_thenCorrect() throws Exception {
|
||||
Field field = Class.forName("com.baeldung.java.reflection.Bird").getDeclaredField("walks");
|
||||
Class<?> fieldClass = field.getType();
|
||||
assertEquals("boolean", fieldClass.getSimpleName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenSetsAndGetsValue_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Bird bird = (Bird) birdClass.newInstance();
|
||||
Field field = birdClass.getDeclaredField("walks");
|
||||
@@ -232,8 +212,7 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenGetsAndSetsWithNull_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenClassField_whenGetsAndSetsWithNull_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Field field = birdClass.getField("CATEGORY");
|
||||
field.setAccessible(true);
|
||||
@@ -242,21 +221,17 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsAllPublicMethods_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsAllPublicMethods_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Method[] methods = birdClass.getMethods();
|
||||
List<String> methodNames = getMethodNames(methods);
|
||||
|
||||
assertTrue(methodNames.containsAll(Arrays
|
||||
.asList("equals", "notifyAll", "hashCode",
|
||||
"walks", "eats", "toString")));
|
||||
assertTrue(methodNames.containsAll(Arrays.asList("equals", "notifyAll", "hashCode", "walks", "eats", "toString")));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsOnlyDeclaredMethods_thenCorrect()
|
||||
throws ClassNotFoundException {
|
||||
public void givenClass_whenGetsOnlyDeclaredMethods_thenCorrect() throws ClassNotFoundException {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
List<String> actualMethodNames = getMethodNames(birdClass.getDeclaredMethods());
|
||||
|
||||
@@ -269,12 +244,10 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethodName_whenGetsMethod_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenMethodName_whenGetsMethod_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||
Method setWalksMethod = birdClass.getDeclaredMethod("setWalks",
|
||||
boolean.class);
|
||||
Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
|
||||
assertFalse(walksMethod.isAccessible());
|
||||
assertFalse(setWalksMethod.isAccessible());
|
||||
@@ -288,12 +261,10 @@ public class ReflectionTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethod_whenInvokes_thenCorrect()
|
||||
throws Exception {
|
||||
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
|
||||
Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
Bird bird = (Bird) birdClass.newInstance();
|
||||
Method setWalksMethod = birdClass.getDeclaredMethod("setWalks",
|
||||
boolean.class);
|
||||
Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||
boolean walks = (boolean) walksMethod.invoke(bird);
|
||||
|
||||
@@ -308,19 +279,19 @@ public class ReflectionTest {
|
||||
|
||||
}
|
||||
|
||||
private static List<String> getFieldNames(Field[] fields) {
|
||||
List<String> fieldNames = new ArrayList<>();
|
||||
for (Field field : fields)
|
||||
fieldNames.add(field.getName());
|
||||
return fieldNames;
|
||||
private static List<String> getFieldNames(Field[] fields) {
|
||||
List<String> fieldNames = new ArrayList<>();
|
||||
for (Field field : fields)
|
||||
fieldNames.add(field.getName());
|
||||
return fieldNames;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> getMethodNames(Method[] methods) {
|
||||
List<String> methodNames = new ArrayList<>();
|
||||
for (Method method : methods)
|
||||
methodNames.add(method.getName());
|
||||
return methodNames;
|
||||
}
|
||||
private static List<String> getMethodNames(Method[] methods) {
|
||||
List<String> methodNames = new ArrayList<>();
|
||||
for (Method method : methods)
|
||||
methodNames.add(method.getName());
|
||||
return methodNames;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegexTest {
|
||||
public class RegexUnitTest {
|
||||
private static Pattern pattern;
|
||||
private static Matcher matcher;
|
||||
|
||||
@@ -499,4 +499,3 @@ public class RegexTest {
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class ScreenshotTest {
|
||||
public class ScreenshotUnitTest {
|
||||
|
||||
private Screenshot screenshot = new Screenshot("Screenshot.jpg");
|
||||
private File file = new File("Screenshot.jpg");
|
||||
@@ -17,43 +17,43 @@ public class EchoMultiTest {
|
||||
Thread.sleep(500);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient1_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage(".");
|
||||
@Test
|
||||
public void givenClient1_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage(".");
|
||||
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient2_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage(".");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
@Test
|
||||
public void givenClient2_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage(".");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient3_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage(".");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
@Test
|
||||
public void givenClient3_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage(".");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,28 +18,28 @@ public class EchoTest {
|
||||
Thread.sleep(500);
|
||||
}
|
||||
|
||||
private EchoClient client = new EchoClient();
|
||||
private EchoClient client = new EchoClient();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
}
|
||||
@Before
|
||||
public void init() {
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient_whenServerEchosMessage_thenCorrect() {
|
||||
@Test
|
||||
public void givenClient_whenServerEchosMessage_thenCorrect() {
|
||||
|
||||
String resp1 = client.sendMessage("hello");
|
||||
String resp2 = client.sendMessage("world");
|
||||
String resp3 = client.sendMessage("!");
|
||||
String resp4 = client.sendMessage(".");
|
||||
assertEquals("hello", resp1);
|
||||
assertEquals("world", resp2);
|
||||
assertEquals("!", resp3);
|
||||
assertEquals("good bye", resp4);
|
||||
}
|
||||
String resp1 = client.sendMessage("hello");
|
||||
String resp2 = client.sendMessage("world");
|
||||
String resp3 = client.sendMessage("!");
|
||||
String resp4 = client.sendMessage(".");
|
||||
assertEquals("hello", resp1);
|
||||
assertEquals("world", resp2);
|
||||
assertEquals("!", resp3);
|
||||
assertEquals("good bye", resp4);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
client.stopConnection();
|
||||
}
|
||||
@After
|
||||
public void tearDown() {
|
||||
client.stopConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.concurrent.Executors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GreetServerTest {
|
||||
public class GreetServerIntegrationTest {
|
||||
|
||||
private GreetClient client;
|
||||
|
||||
@@ -24,7 +24,7 @@ public class GreetServerTest {
|
||||
@Before
|
||||
public void init() {
|
||||
client = new GreetClient();
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
client.startConnection("127.0.0.1", PORT);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user