maven fix and small formatting work
This commit is contained in:
@@ -5,9 +5,9 @@ import org.junit.Test;
|
||||
|
||||
public class SimulatedAnnealingTest {
|
||||
|
||||
@Test
|
||||
public void testSimulateAnnealing() {
|
||||
Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
|
||||
}
|
||||
@Test
|
||||
public void testSimulateAnnealing() {
|
||||
Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ public class EncoderDecoderUnitTest {
|
||||
private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
|
||||
private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253";
|
||||
|
||||
|
||||
private String encodeValue(String value) {
|
||||
String encoded = 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, is(encodedURL));
|
||||
}
|
||||
@@ -103,12 +100,9 @@ public class EncoderDecoderUnitTest {
|
||||
|
||||
String path = "path+1";
|
||||
|
||||
String encodedURL = requestParams.keySet().stream()
|
||||
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
|
||||
.collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
|
||||
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
|
||||
|
||||
Assert.assertThat(testUrlWithPath, is(encodedURL));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@ public class Java8CollectionCleanupUnitTest {
|
||||
@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());
|
||||
final List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
@@ -27,9 +25,7 @@ public class Java8CollectionCleanupUnitTest {
|
||||
@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());
|
||||
final List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
@@ -45,9 +41,7 @@ public class Java8CollectionCleanupUnitTest {
|
||||
@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());
|
||||
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream().distinct().collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutDuplicates, hasSize(3));
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ public class JavaFileSizeUnitTest {
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
filePath = String.join(separator, new String[] {"src", "test", "resources", "testFolder", "sample_file_1.in"});
|
||||
filePath = String.join(separator, new String[] { "src", "test", "resources", "testFolder", "sample_file_1.in" });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSize_thenCorrect() {
|
||||
final File file = new File(filePath);
|
||||
|
||||
|
||||
final long size = getFileSize(file);
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
|
||||
@@ -34,16 +34,16 @@ public class JavaFileSizeUnitTest {
|
||||
public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException {
|
||||
final Path path = Paths.get(this.filePath);
|
||||
final FileChannel fileChannel = FileChannel.open(path);
|
||||
|
||||
|
||||
final long fileSize = fileChannel.size();
|
||||
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, fileSize);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
|
||||
final File file = new File(filePath);
|
||||
|
||||
|
||||
final long size = FileUtils.sizeOf(file);
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
|
||||
@@ -52,9 +52,9 @@ public class JavaFileSizeUnitTest {
|
||||
@Test
|
||||
public void whenGetReadableFileSize_thenCorrect() {
|
||||
final File file = new File(filePath);
|
||||
|
||||
|
||||
final long size = getFileSize(file);
|
||||
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES + " bytes", FileUtils.byteCountToDisplaySize(size));
|
||||
}
|
||||
|
||||
|
||||
@@ -93,6 +93,7 @@ public class OptionalTest {
|
||||
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
|
||||
assertFalse(is2017);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFiltersWithoutOptional_thenCorrect() {
|
||||
assertTrue(priceIsInRange1(new Modem(10.0)));
|
||||
@@ -121,12 +122,9 @@ public class OptionalTest {
|
||||
}
|
||||
|
||||
public boolean priceIsInRange2(Modem modem2) {
|
||||
return Optional.ofNullable(modem2)
|
||||
.map(Modem::getPrice)
|
||||
.filter(p -> p >= 10)
|
||||
.filter(p -> p <= 15)
|
||||
.isPresent();
|
||||
return Optional.ofNullable(modem2).map(Modem::getPrice).filter(p -> p >= 10).filter(p -> p <= 15).isPresent();
|
||||
}
|
||||
|
||||
// Transforming Value With map()
|
||||
@Test
|
||||
public void givenOptional_whenMapWorks_thenCorrect() {
|
||||
|
||||
@@ -13,46 +13,43 @@ import static org.unix4j.unix.Grep.*;
|
||||
import static org.unix4j.unix.cut.CutOption.*;
|
||||
|
||||
public class GrepWithUnix4JTest {
|
||||
|
||||
private File fileToGrep;
|
||||
|
||||
private File fileToGrep;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
final String filePath = String.join(separator, new String[] {"src", "test", "resources", "dictionary.in"});
|
||||
final String filePath = String.join(separator, new String[] { "src", "test", "resources", "dictionary.in" });
|
||||
fileToGrep = new File(filePath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGrepWithSimpleString_thenCorrect() {
|
||||
int expectedLineCount = 4;
|
||||
|
||||
//grep "NINETEEN" dictionary.txt
|
||||
List<Line> lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList();
|
||||
|
||||
assertEquals(expectedLineCount, lines.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInverseGrepWithSimpleString_thenCorrect() {
|
||||
int expectedLineCount = 178687;
|
||||
|
||||
//grep -v "NINETEEN" dictionary.txt
|
||||
List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep).
|
||||
toLineList();
|
||||
|
||||
assertEquals(expectedLineCount, lines.size());
|
||||
}
|
||||
@Test
|
||||
public void whenGrepWithSimpleString_thenCorrect() {
|
||||
int expectedLineCount = 4;
|
||||
|
||||
// grep "NINETEEN" dictionary.txt
|
||||
List<Line> lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList();
|
||||
|
||||
@Test
|
||||
public void whenGrepWithRegex_thenCorrect() {
|
||||
int expectedLineCount = 151;
|
||||
|
||||
//grep -c ".*?NINE.*?" dictionary.txt
|
||||
String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).
|
||||
cut(fields, ":", 1).toStringResult();
|
||||
|
||||
assertEquals(expectedLineCount, Integer.parseInt(patternCount));
|
||||
}
|
||||
assertEquals(expectedLineCount, lines.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInverseGrepWithSimpleString_thenCorrect() {
|
||||
int expectedLineCount = 178687;
|
||||
|
||||
// grep -v "NINETEEN" dictionary.txt
|
||||
List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep).toLineList();
|
||||
|
||||
assertEquals(expectedLineCount, lines.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGrepWithRegex_thenCorrect() {
|
||||
int expectedLineCount = 151;
|
||||
|
||||
// grep -c ".*?NINE.*?" dictionary.txt
|
||||
String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).cut(fields, ":", 1).toStringResult();
|
||||
|
||||
assertEquals(expectedLineCount, Integer.parseInt(patternCount));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
public void whenJoiningTwoArrays_thenJoined() {
|
||||
String[] animals1 = new String[] { "Dog", "Cat" };
|
||||
String[] animals2 = new String[] { "Bird", "Cow" };
|
||||
String[] result = Stream.concat(
|
||||
Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
|
||||
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
|
||||
|
||||
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
|
||||
}
|
||||
@@ -25,9 +24,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
public void whenJoiningTwoCollections_thenJoined() {
|
||||
Collection<String> collection1 = Arrays.asList("Dog", "Cat");
|
||||
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
|
||||
Collection<String> result = Stream.concat(
|
||||
collection1.stream(), collection2.stream())
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose")));
|
||||
}
|
||||
@@ -36,10 +33,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
||||
Collection<String> collection1 = Arrays.asList("Dog", "Cat");
|
||||
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
|
||||
Collection<String> result = Stream.concat(
|
||||
collection1.stream(), collection2.stream())
|
||||
.filter(e -> e.length() == 3)
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Stream.concat(collection1.stream(), collection2.stream()).filter(e -> e.length() == 3).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow")));
|
||||
}
|
||||
@@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
animals.put(2, "Cat");
|
||||
animals.put(3, "Cow");
|
||||
|
||||
String result = animals.entrySet().stream()
|
||||
.map(entry -> entry.getKey() + " = " + entry.getValue())
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = animals.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow");
|
||||
}
|
||||
@@ -80,10 +72,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
nested.add(Arrays.asList("Dog", "Cat"));
|
||||
nested.add(Arrays.asList("Cow", "Pig"));
|
||||
|
||||
String result = nested.stream().map(
|
||||
nextList -> nextList.stream()
|
||||
.collect(Collectors.joining("-")))
|
||||
.collect(Collectors.joining("; "));
|
||||
String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; "));
|
||||
|
||||
assertEquals(result, "Dog-Cat; Cow-Pig");
|
||||
}
|
||||
@@ -91,17 +80,14 @@ public class JoinSplitCollectionsUnitTest {
|
||||
@Test
|
||||
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
||||
Collection<String> animals = Arrays.asList("Dog", "Cat", null, "Moose");
|
||||
String result = animals.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = animals.stream().filter(Objects::nonNull).collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "Dog, Cat, Moose");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSplitCollectionHalf_thenConverted() {
|
||||
Collection<String> animals = Arrays.asList(
|
||||
"Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
|
||||
Collection<String> animals = Arrays.asList("Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
|
||||
Collection<String> result1 = new ArrayList<>();
|
||||
Collection<String> result2 = new ArrayList<>();
|
||||
AtomicInteger count = new AtomicInteger();
|
||||
@@ -122,9 +108,8 @@ public class JoinSplitCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSplitArrayByWordLength_thenConverted() {
|
||||
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose"};
|
||||
Map<Integer, List<String>> result = Arrays.stream(animals)
|
||||
.collect(Collectors.groupingBy(String::length));
|
||||
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose" };
|
||||
Map<Integer, List<String>> result = Arrays.stream(animals).collect(Collectors.groupingBy(String::length));
|
||||
|
||||
assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig")));
|
||||
assertTrue(result.get(4).equals(Arrays.asList("Bird")));
|
||||
@@ -151,9 +136,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
public void whenConvertStringToMap_thenConverted() {
|
||||
String animals = "1 = Dog, 2 = Cat, 3 = Bird";
|
||||
|
||||
Map<Integer, String> result = Arrays.stream(
|
||||
animals.split(", ")).map(next -> next.split(" = "))
|
||||
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||
Map<Integer, String> result = Arrays.stream(animals.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||
|
||||
assertEquals(result.get(1), "Dog");
|
||||
assertEquals(result.get(2), "Cat");
|
||||
@@ -164,10 +147,7 @@ public class JoinSplitCollectionsUnitTest {
|
||||
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
||||
String animals = "Dog. , Cat, Bird. Cow";
|
||||
|
||||
Collection<String> result = Arrays.stream(animals.split("[,|.]"))
|
||||
.map(String::trim)
|
||||
.filter(next -> !next.isEmpty())
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Arrays.stream(animals.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user