formatting work
This commit is contained in:
parent
455df4e499
commit
8ce7e1f588
@ -32,7 +32,6 @@ public class EncoderDecoderUnitTest {
|
|||||||
return encoded;
|
return encoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String decode(String value) {
|
private String decode(String value) {
|
||||||
String decoded = null;
|
String decoded = null;
|
||||||
try {
|
try {
|
||||||
@ -59,9 +58,7 @@ public class EncoderDecoderUnitTest {
|
|||||||
requestParams.put("key2", "value@!$2");
|
requestParams.put("key2", "value@!$2");
|
||||||
requestParams.put("key3", "value%3");
|
requestParams.put("key3", "value%3");
|
||||||
|
|
||||||
String encodedURL = requestParams.keySet().stream()
|
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", ""));
|
||||||
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
|
|
||||||
.collect(joining("&", "http://www.baeldung.com?", ""));
|
|
||||||
|
|
||||||
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
|
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
|
||||||
}
|
}
|
||||||
@ -72,12 +69,9 @@ public class EncoderDecoderUnitTest {
|
|||||||
|
|
||||||
String query = url.getQuery();
|
String query = url.getQuery();
|
||||||
|
|
||||||
String decodedQuery = Arrays.stream(query.split("&"))
|
String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&"));
|
||||||
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
|
|
||||||
.collect(joining("&"));
|
|
||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals("http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
|
||||||
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.enums;
|
package com.baeldung.enums;
|
||||||
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.java.networking.udp;
|
package com.baeldung.java.networking.udp;
|
||||||
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -27,18 +27,14 @@ public class Java8StreamApiUnitTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
productList = Arrays.asList(
|
productList = Arrays.asList(new Product(23, "potatoes"), new Product(14, "orange"), new Product(13, "lemon"), new Product(23, "bread"), new Product(13, "sugar"));
|
||||||
new Product(23, "potatoes"), new Product(14, "orange"),
|
|
||||||
new Product(13, "lemon"), new Product(23, "bread"),
|
|
||||||
new Product(13, "sugar"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkPipeline_whenStreamOneElementShorter_thenCorrect() {
|
public void checkPipeline_whenStreamOneElementShorter_thenCorrect() {
|
||||||
|
|
||||||
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
||||||
long size = list.stream().skip(1)
|
long size = list.stream().skip(1).map(element -> element.substring(0, 3)).count();
|
||||||
.map(element -> element.substring(0, 3)).count();
|
|
||||||
assertEquals(list.size() - 1, size);
|
assertEquals(list.size() - 1, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,8 +44,7 @@ public class Java8StreamApiUnitTest {
|
|||||||
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
||||||
|
|
||||||
counter = 0;
|
counter = 0;
|
||||||
long sizeFirst = list.stream()
|
long sizeFirst = list.stream().skip(2).map(element -> {
|
||||||
.skip(2).map(element -> {
|
|
||||||
wasCalled();
|
wasCalled();
|
||||||
return element.substring(0, 3);
|
return element.substring(0, 3);
|
||||||
}).count();
|
}).count();
|
||||||
@ -126,8 +121,7 @@ public class Java8StreamApiUnitTest {
|
|||||||
public void runStreamPipeline_whenOrderIsRight_thenCorrect() {
|
public void runStreamPipeline_whenOrderIsRight_thenCorrect() {
|
||||||
|
|
||||||
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
||||||
Optional<String> stream = list.stream()
|
Optional<String> stream = list.stream().filter(element -> {
|
||||||
.filter(element -> {
|
|
||||||
log.info("filter() was called");
|
log.info("filter() was called");
|
||||||
return element.contains("2");
|
return element.contains("2");
|
||||||
}).map(element -> {
|
}).map(element -> {
|
||||||
@ -145,15 +139,13 @@ public class Java8StreamApiUnitTest {
|
|||||||
int reducedTwoParams = IntStream.range(1, 4).reduce(10, (a, b) -> a + b);
|
int reducedTwoParams = IntStream.range(1, 4).reduce(10, (a, b) -> a + b);
|
||||||
assertEquals(16, reducedTwoParams);
|
assertEquals(16, reducedTwoParams);
|
||||||
|
|
||||||
int reducedThreeParams = Stream.of(1, 2, 3)
|
int reducedThreeParams = Stream.of(1, 2, 3).reduce(10, (a, b) -> a + b, (a, b) -> {
|
||||||
.reduce(10, (a, b) -> a + b, (a, b) -> {
|
|
||||||
log.info("combiner was called");
|
log.info("combiner was called");
|
||||||
return a + b;
|
return a + b;
|
||||||
});
|
});
|
||||||
assertEquals(16, reducedThreeParams);
|
assertEquals(16, reducedThreeParams);
|
||||||
|
|
||||||
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream()
|
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream().reduce(10, (a, b) -> a + b, (a, b) -> {
|
||||||
.reduce(10, (a, b) -> a + b, (a, b) -> {
|
|
||||||
log.info("combiner was called");
|
log.info("combiner was called");
|
||||||
return a + b;
|
return a + b;
|
||||||
});
|
});
|
||||||
@ -163,14 +155,12 @@ public class Java8StreamApiUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void collecting_whenAsExpected_thenCorrect() {
|
public void collecting_whenAsExpected_thenCorrect() {
|
||||||
|
|
||||||
List<String> collectorCollection = productList.stream()
|
List<String> collectorCollection = productList.stream().map(Product::getName).collect(Collectors.toList());
|
||||||
.map(Product::getName).collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertTrue(collectorCollection instanceof List);
|
assertTrue(collectorCollection instanceof List);
|
||||||
assertEquals(5, collectorCollection.size());
|
assertEquals(5, collectorCollection.size());
|
||||||
|
|
||||||
String listToString = productList.stream().map(Product::getName)
|
String listToString = productList.stream().map(Product::getName).collect(Collectors.joining(", ", "[", "]"));
|
||||||
.collect(Collectors.joining(", ", "[", "]"));
|
|
||||||
|
|
||||||
assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]"));
|
assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]"));
|
||||||
|
|
||||||
@ -180,33 +170,26 @@ public class Java8StreamApiUnitTest {
|
|||||||
int summingPrice = productList.stream().collect(Collectors.summingInt(Product::getPrice));
|
int summingPrice = productList.stream().collect(Collectors.summingInt(Product::getPrice));
|
||||||
assertEquals(86, summingPrice);
|
assertEquals(86, summingPrice);
|
||||||
|
|
||||||
IntSummaryStatistics statistics = productList.stream()
|
IntSummaryStatistics statistics = productList.stream().collect(Collectors.summarizingInt(Product::getPrice));
|
||||||
.collect(Collectors.summarizingInt(Product::getPrice));
|
|
||||||
assertEquals(23, statistics.getMax());
|
assertEquals(23, statistics.getMax());
|
||||||
|
|
||||||
Map<Integer, List<Product>> collectorMapOfLists = productList.stream()
|
Map<Integer, List<Product>> collectorMapOfLists = productList.stream().collect(Collectors.groupingBy(Product::getPrice));
|
||||||
.collect(Collectors.groupingBy(Product::getPrice));
|
|
||||||
assertEquals(3, collectorMapOfLists.keySet().size());
|
assertEquals(3, collectorMapOfLists.keySet().size());
|
||||||
|
|
||||||
Map<Boolean, List<Product>> mapPartioned = productList.stream()
|
Map<Boolean, List<Product>> mapPartioned = productList.stream().collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
|
||||||
.collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
|
|
||||||
assertEquals(2, mapPartioned.keySet().size());
|
assertEquals(2, mapPartioned.keySet().size());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
public void collect_whenThrows_thenCorrect() {
|
public void collect_whenThrows_thenCorrect() {
|
||||||
Set<Product> unmodifiableSet = productList.stream()
|
Set<Product> unmodifiableSet = productList.stream().collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet));
|
||||||
.collect(Collectors.collectingAndThen(Collectors.toSet(),
|
|
||||||
Collections::unmodifiableSet));
|
|
||||||
unmodifiableSet.add(new Product(4, "tea"));
|
unmodifiableSet.add(new Product(4, "tea"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() {
|
public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() {
|
||||||
Collector<Product, ?, LinkedList<Product>> toLinkedList =
|
Collector<Product, ?, LinkedList<Product>> toLinkedList = Collector.of(LinkedList::new, LinkedList::add, (first, second) -> {
|
||||||
Collector.of(LinkedList::new, LinkedList::add,
|
|
||||||
(first, second) -> {
|
|
||||||
first.addAll(second);
|
first.addAll(second);
|
||||||
return first;
|
return first;
|
||||||
});
|
});
|
||||||
@ -219,23 +202,20 @@ public class Java8StreamApiUnitTest {
|
|||||||
public void parallelStream_whenWorks_thenCorrect() {
|
public void parallelStream_whenWorks_thenCorrect() {
|
||||||
Stream<Product> streamOfCollection = productList.parallelStream();
|
Stream<Product> streamOfCollection = productList.parallelStream();
|
||||||
boolean isParallel = streamOfCollection.isParallel();
|
boolean isParallel = streamOfCollection.isParallel();
|
||||||
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12)
|
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12).anyMatch(price -> price > 200);
|
||||||
.anyMatch(price -> price > 200);
|
|
||||||
assertTrue(isParallel && haveBigPrice);
|
assertTrue(isParallel && haveBigPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parallel_whenIsParallel_thenCorrect() {
|
public void parallel_whenIsParallel_thenCorrect() {
|
||||||
IntStream intStreamParallel =
|
IntStream intStreamParallel = IntStream.range(1, 150).parallel().map(element -> element * 34);
|
||||||
IntStream.range(1, 150).parallel().map(element -> element * 34);
|
|
||||||
boolean isParallel = intStreamParallel.isParallel();
|
boolean isParallel = intStreamParallel.isParallel();
|
||||||
assertTrue(isParallel);
|
assertTrue(isParallel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parallel_whenIsSequential_thenCorrect() {
|
public void parallel_whenIsSequential_thenCorrect() {
|
||||||
IntStream intStreamParallel =
|
IntStream intStreamParallel = IntStream.range(1, 150).parallel().map(element -> element * 34);
|
||||||
IntStream.range(1, 150).parallel().map(element -> element * 34);
|
|
||||||
IntStream intStreamSequential = intStreamParallel.sequential();
|
IntStream intStreamSequential = intStreamParallel.sequential();
|
||||||
boolean isParallel = intStreamParallel.isParallel();
|
boolean isParallel = intStreamParallel.isParallel();
|
||||||
assertFalse(isParallel);
|
assertFalse(isParallel);
|
||||||
|
@ -47,14 +47,12 @@ public class Java8StreamsUnitTest {
|
|||||||
assertEquals(count, 9);
|
assertEquals(count, 9);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkStreamCount_whenOperationFilter_thanCorrect() {
|
public void checkStreamCount_whenOperationFilter_thanCorrect() {
|
||||||
Stream<String> streamFilter = list.stream().filter(element -> element.isEmpty());
|
Stream<String> streamFilter = list.stream().filter(element -> element.isEmpty());
|
||||||
assertEquals(streamFilter.count(), 2);
|
assertEquals(streamFilter.count(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkStreamCount_whenOperationMap_thanCorrect() {
|
public void checkStreamCount_whenOperationMap_thanCorrect() {
|
||||||
List<String> uris = new ArrayList<>();
|
List<String> uris = new ArrayList<>();
|
||||||
@ -65,12 +63,10 @@ public class Java8StreamsUnitTest {
|
|||||||
List<Detail> details = new ArrayList<>();
|
List<Detail> details = new ArrayList<>();
|
||||||
details.add(new Detail());
|
details.add(new Detail());
|
||||||
details.add(new Detail());
|
details.add(new Detail());
|
||||||
Stream<String> streamFlatMap = details.stream()
|
Stream<String> streamFlatMap = details.stream().flatMap(detail -> detail.getParts().stream());
|
||||||
.flatMap(detail -> detail.getParts().stream());
|
|
||||||
assertEquals(streamFlatMap.count(), 4);
|
assertEquals(streamFlatMap.count(), 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkStreamCount_whenOperationMatch_thenCorrect() {
|
public void checkStreamCount_whenOperationMatch_thenCorrect() {
|
||||||
boolean isValid = list.stream().anyMatch(element -> element.contains("h"));
|
boolean isValid = list.stream().anyMatch(element -> element.contains("h"));
|
||||||
@ -81,7 +77,6 @@ public class Java8StreamsUnitTest {
|
|||||||
assertFalse(isValidTwo);
|
assertFalse(isValidTwo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkStreamReducedValue_whenOperationReduce_thenCorrect() {
|
public void checkStreamReducedValue_whenOperationReduce_thenCorrect() {
|
||||||
List<Integer> integers = new ArrayList<>();
|
List<Integer> integers = new ArrayList<>();
|
||||||
@ -94,14 +89,11 @@ public class Java8StreamsUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkStreamContains_whenOperationCollect_thenCorrect() {
|
public void checkStreamContains_whenOperationCollect_thenCorrect() {
|
||||||
List<String> resultList = list.stream()
|
List<String> resultList = list.stream().map(element -> element.toUpperCase()).collect(Collectors.toList());
|
||||||
.map(element -> element.toUpperCase())
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
assertEquals(resultList.size(), list.size());
|
assertEquals(resultList.size(), list.size());
|
||||||
assertTrue(resultList.contains(""));
|
assertTrue(resultList.contains(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkParallelStream_whenDoWork() {
|
public void checkParallelStream_whenDoWork() {
|
||||||
list.parallelStream().forEach(element -> doWork(element));
|
list.parallelStream().forEach(element -> doWork(element));
|
||||||
|
@ -46,9 +46,7 @@ public class GuavaThreadPoolIntegrationTest {
|
|||||||
ListenableFuture<String> future1 = listeningExecutorService.submit(() -> "Hello");
|
ListenableFuture<String> future1 = listeningExecutorService.submit(() -> "Hello");
|
||||||
ListenableFuture<String> future2 = listeningExecutorService.submit(() -> "World");
|
ListenableFuture<String> future2 = listeningExecutorService.submit(() -> "World");
|
||||||
|
|
||||||
String greeting = Futures.allAsList(future1, future2).get()
|
String greeting = Futures.allAsList(future1, future2).get().stream().collect(Collectors.joining(" "));
|
||||||
.stream()
|
|
||||||
.collect(Collectors.joining(" "));
|
|
||||||
assertEquals("Hello World", greeting);
|
assertEquals("Hello World", greeting);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user