formatting work

This commit is contained in:
eugenp 2016-11-03 18:04:09 +02:00
parent 455df4e499
commit 8ce7e1f588
13 changed files with 106 additions and 144 deletions

View File

@ -32,7 +32,6 @@ public class EncoderDecoderUnitTest {
return encoded;
}
private String decode(String value) {
String decoded = null;
try {
@ -59,9 +58,7 @@ public class EncoderDecoderUnitTest {
requestParams.put("key2", "value@!$2");
requestParams.put("key3", "value%3");
String encodedURL = requestParams.keySet().stream()
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
.collect(joining("&", "http://www.baeldung.com?", ""));
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", ""));
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
}
@ -72,12 +69,9 @@ public class EncoderDecoderUnitTest {
String query = url.getQuery();
String decodedQuery = Arrays.stream(query.split("&"))
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
.collect(joining("&"));
String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&"));
Assert.assertEquals(
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
Assert.assertEquals("http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
}
}

View File

@ -1,6 +1,5 @@
package com.baeldung.enums;
import org.junit.Test;
import java.util.ArrayList;

View File

@ -1,6 +1,5 @@
package com.baeldung.java.networking.udp;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@ -27,18 +27,14 @@ public class Java8StreamApiUnitTest {
@Before
public void init() {
productList = Arrays.asList(
new Product(23, "potatoes"), new Product(14, "orange"),
new Product(13, "lemon"), new Product(23, "bread"),
new Product(13, "sugar"));
productList = Arrays.asList(new Product(23, "potatoes"), new Product(14, "orange"), new Product(13, "lemon"), new Product(23, "bread"), new Product(13, "sugar"));
}
@Test
public void checkPipeline_whenStreamOneElementShorter_thenCorrect() {
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
long size = list.stream().skip(1)
.map(element -> element.substring(0, 3)).count();
long size = list.stream().skip(1).map(element -> element.substring(0, 3)).count();
assertEquals(list.size() - 1, size);
}
@ -48,8 +44,7 @@ public class Java8StreamApiUnitTest {
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
counter = 0;
long sizeFirst = list.stream()
.skip(2).map(element -> {
long sizeFirst = list.stream().skip(2).map(element -> {
wasCalled();
return element.substring(0, 3);
}).count();
@ -126,8 +121,7 @@ public class Java8StreamApiUnitTest {
public void runStreamPipeline_whenOrderIsRight_thenCorrect() {
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
Optional<String> stream = list.stream()
.filter(element -> {
Optional<String> stream = list.stream().filter(element -> {
log.info("filter() was called");
return element.contains("2");
}).map(element -> {
@ -145,15 +139,13 @@ public class Java8StreamApiUnitTest {
int reducedTwoParams = IntStream.range(1, 4).reduce(10, (a, b) -> a + b);
assertEquals(16, reducedTwoParams);
int reducedThreeParams = Stream.of(1, 2, 3)
.reduce(10, (a, b) -> a + b, (a, b) -> {
int reducedThreeParams = Stream.of(1, 2, 3).reduce(10, (a, b) -> a + b, (a, b) -> {
log.info("combiner was called");
return a + b;
});
assertEquals(16, reducedThreeParams);
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream()
.reduce(10, (a, b) -> a + b, (a, b) -> {
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream().reduce(10, (a, b) -> a + b, (a, b) -> {
log.info("combiner was called");
return a + b;
});
@ -163,14 +155,12 @@ public class Java8StreamApiUnitTest {
@Test
public void collecting_whenAsExpected_thenCorrect() {
List<String> collectorCollection = productList.stream()
.map(Product::getName).collect(Collectors.toList());
List<String> collectorCollection = productList.stream().map(Product::getName).collect(Collectors.toList());
assertTrue(collectorCollection instanceof List);
assertEquals(5, collectorCollection.size());
String listToString = productList.stream().map(Product::getName)
.collect(Collectors.joining(", ", "[", "]"));
String listToString = productList.stream().map(Product::getName).collect(Collectors.joining(", ", "[", "]"));
assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]"));
@ -180,33 +170,26 @@ public class Java8StreamApiUnitTest {
int summingPrice = productList.stream().collect(Collectors.summingInt(Product::getPrice));
assertEquals(86, summingPrice);
IntSummaryStatistics statistics = productList.stream()
.collect(Collectors.summarizingInt(Product::getPrice));
IntSummaryStatistics statistics = productList.stream().collect(Collectors.summarizingInt(Product::getPrice));
assertEquals(23, statistics.getMax());
Map<Integer, List<Product>> collectorMapOfLists = productList.stream()
.collect(Collectors.groupingBy(Product::getPrice));
Map<Integer, List<Product>> collectorMapOfLists = productList.stream().collect(Collectors.groupingBy(Product::getPrice));
assertEquals(3, collectorMapOfLists.keySet().size());
Map<Boolean, List<Product>> mapPartioned = productList.stream()
.collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
Map<Boolean, List<Product>> mapPartioned = productList.stream().collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
assertEquals(2, mapPartioned.keySet().size());
}
@Test(expected = UnsupportedOperationException.class)
public void collect_whenThrows_thenCorrect() {
Set<Product> unmodifiableSet = productList.stream()
.collect(Collectors.collectingAndThen(Collectors.toSet(),
Collections::unmodifiableSet));
Set<Product> unmodifiableSet = productList.stream().collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet));
unmodifiableSet.add(new Product(4, "tea"));
}
@Test
public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() {
Collector<Product, ?, LinkedList<Product>> toLinkedList =
Collector.of(LinkedList::new, LinkedList::add,
(first, second) -> {
Collector<Product, ?, LinkedList<Product>> toLinkedList = Collector.of(LinkedList::new, LinkedList::add, (first, second) -> {
first.addAll(second);
return first;
});
@ -219,23 +202,20 @@ public class Java8StreamApiUnitTest {
public void parallelStream_whenWorks_thenCorrect() {
Stream<Product> streamOfCollection = productList.parallelStream();
boolean isParallel = streamOfCollection.isParallel();
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12)
.anyMatch(price -> price > 200);
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12).anyMatch(price -> price > 200);
assertTrue(isParallel && haveBigPrice);
}
@Test
public void parallel_whenIsParallel_thenCorrect() {
IntStream intStreamParallel =
IntStream.range(1, 150).parallel().map(element -> element * 34);
IntStream intStreamParallel = IntStream.range(1, 150).parallel().map(element -> element * 34);
boolean isParallel = intStreamParallel.isParallel();
assertTrue(isParallel);
}
@Test
public void parallel_whenIsSequential_thenCorrect() {
IntStream intStreamParallel =
IntStream.range(1, 150).parallel().map(element -> element * 34);
IntStream intStreamParallel = IntStream.range(1, 150).parallel().map(element -> element * 34);
IntStream intStreamSequential = intStreamParallel.sequential();
boolean isParallel = intStreamParallel.isParallel();
assertFalse(isParallel);

View File

@ -47,14 +47,12 @@ public class Java8StreamsUnitTest {
assertEquals(count, 9);
}
@Test
public void checkStreamCount_whenOperationFilter_thanCorrect() {
Stream<String> streamFilter = list.stream().filter(element -> element.isEmpty());
assertEquals(streamFilter.count(), 2);
}
@Test
public void checkStreamCount_whenOperationMap_thanCorrect() {
List<String> uris = new ArrayList<>();
@ -65,12 +63,10 @@ public class Java8StreamsUnitTest {
List<Detail> details = new ArrayList<>();
details.add(new Detail());
details.add(new Detail());
Stream<String> streamFlatMap = details.stream()
.flatMap(detail -> detail.getParts().stream());
Stream<String> streamFlatMap = details.stream().flatMap(detail -> detail.getParts().stream());
assertEquals(streamFlatMap.count(), 4);
}
@Test
public void checkStreamCount_whenOperationMatch_thenCorrect() {
boolean isValid = list.stream().anyMatch(element -> element.contains("h"));
@ -81,7 +77,6 @@ public class Java8StreamsUnitTest {
assertFalse(isValidTwo);
}
@Test
public void checkStreamReducedValue_whenOperationReduce_thenCorrect() {
List<Integer> integers = new ArrayList<>();
@ -94,14 +89,11 @@ public class Java8StreamsUnitTest {
@Test
public void checkStreamContains_whenOperationCollect_thenCorrect() {
List<String> resultList = list.stream()
.map(element -> element.toUpperCase())
.collect(Collectors.toList());
List<String> resultList = list.stream().map(element -> element.toUpperCase()).collect(Collectors.toList());
assertEquals(resultList.size(), list.size());
assertTrue(resultList.contains(""));
}
@Test
public void checkParallelStream_whenDoWork() {
list.parallelStream().forEach(element -> doWork(element));

View File

@ -46,9 +46,7 @@ public class GuavaThreadPoolIntegrationTest {
ListenableFuture<String> future1 = listeningExecutorService.submit(() -> "Hello");
ListenableFuture<String> future2 = listeningExecutorService.submit(() -> "World");
String greeting = Futures.allAsList(future1, future2).get()
.stream()
.collect(Collectors.joining(" "));
String greeting = Futures.allAsList(future1, future2).get().stream().collect(Collectors.joining(" "));
assertEquals("Hello World", greeting);
}