java-streams-8-api - adding slf4j fixing indentation
This commit is contained in:
parent
f5ecf03890
commit
a5bde90d40
|
@ -41,6 +41,12 @@
|
||||||
<version>3.3.2</version>
|
<version>3.3.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -2,44 +2,35 @@ package com.baeldung.java8;
|
||||||
|
|
||||||
import com.baeldung.streamApi.Product;
|
import com.baeldung.streamApi.Product;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mock;
|
import org.slf4j.Logger;
|
||||||
import org.mockito.Mockito;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.Matchers.anyString;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Alex Vengr
|
|
||||||
*/
|
|
||||||
public class Java8StreamApiTest {
|
public class Java8StreamApiTest {
|
||||||
|
|
||||||
private long counter;
|
private long counter;
|
||||||
|
|
||||||
private static Logger log = Logger.getAnonymousLogger();
|
private static Logger log = LoggerFactory.getLogger(Java8StreamApiTest.class);
|
||||||
|
|
||||||
private List<Product> productList;
|
private List<Product> productList;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
productList = Arrays.asList(new Product(23, "potatoes"),
|
productList = Arrays.asList(
|
||||||
new Product(14, "orange"), new Product(13, "lemon"),
|
new Product(23, "potatoes"), new Product(14, "orange"),
|
||||||
new Product(23, "bread"), new Product(13, "sugar"));
|
new Product(13, "lemon"), new Product(23, "bread"),
|
||||||
|
new Product(13, "sugar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -62,14 +53,14 @@ public class Java8StreamApiTest {
|
||||||
wasCalled();
|
wasCalled();
|
||||||
return element.substring(0, 3);
|
return element.substring(0, 3);
|
||||||
}).count();
|
}).count();
|
||||||
assertEquals( 1, counter);
|
assertEquals(1, counter);
|
||||||
|
|
||||||
counter = 0;
|
counter = 0;
|
||||||
long sizeSecond = list.stream().map(element -> {
|
long sizeSecond = list.stream().map(element -> {
|
||||||
wasCalled();
|
wasCalled();
|
||||||
return element.substring(0, 3);
|
return element.substring(0, 3);
|
||||||
}).skip(2).count();
|
}).skip(2).count();
|
||||||
assertEquals( 3, counter);
|
assertEquals(3, counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -117,7 +108,7 @@ public class Java8StreamApiTest {
|
||||||
try {
|
try {
|
||||||
streamOfStrings = Files.lines(path, Charset.forName("UTF-8"));
|
streamOfStrings = Files.lines(path, Charset.forName("UTF-8"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("Error creating streams from paths {}", path, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
assertEquals("a", streamOfStrings.findFirst().get());
|
assertEquals("a", streamOfStrings.findFirst().get());
|
||||||
|
|
||||||
|
@ -215,7 +206,10 @@ public class Java8StreamApiTest {
|
||||||
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,
|
Collector.of(LinkedList::new, LinkedList::add,
|
||||||
(first, second) -> { first.addAll(second); return first; });
|
(first, second) -> {
|
||||||
|
first.addAll(second);
|
||||||
|
return first;
|
||||||
|
});
|
||||||
|
|
||||||
LinkedList<Product> linkedListOfPersons = productList.stream().collect(toLinkedList);
|
LinkedList<Product> linkedListOfPersons = productList.stream().collect(toLinkedList);
|
||||||
assertTrue(linkedListOfPersons.containsAll(productList));
|
assertTrue(linkedListOfPersons.containsAll(productList));
|
||||||
|
@ -252,13 +246,13 @@ public class Java8StreamApiTest {
|
||||||
try {
|
try {
|
||||||
path = Files.createTempFile(null, ".txt");
|
path = Files.createTempFile(null, ".txt");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
|
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
|
||||||
writer.write("a\nb\nc");
|
writer.write("a\nb\nc");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue