Skip to content

Commit 5ab7c4a

Browse files
authored
Merge pull request eugenp#10337 from MajewskiKrzysztof/BAEL-4750
BAEL-4750 Java 12 New Features
2 parents b4bc0c8 + c6cbec1 commit 5ab7c4a

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package java.com.baeldung.newfeatures;
2+
3+
import org.junit.Test;
4+
5+
import java.text.NumberFormat;
6+
import java.util.Locale;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class CompactNumbersUnitTest {
11+
12+
@Test
13+
public void givenNumber_thenCompactValues() {
14+
NumberFormat likesShort = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT);
15+
likesShort.setMaximumFractionDigits(2);
16+
assertEquals("2.59K", likesShort.format(2592));
17+
NumberFormat likesLong = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.LONG);
18+
likesLong.setMaximumFractionDigits(2);
19+
assertEquals("2.59 thousand", likesShort.format(2592));
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package java.com.baeldung.newfeatures;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class FileMismatchUnitTest {
12+
13+
@Test
14+
public void givenIdenticalFiles_thenShouldNotFindMismatch() throws IOException {
15+
Path filePath1 = Files.createTempFile("file1", ".txt");
16+
Path filePath2 = Files.createTempFile("file2", ".txt");
17+
Files.writeString(filePath1, "Java 12 Article");
18+
Files.writeString(filePath2, "Java 12 Article");
19+
long mismatch = Files.mismatch(filePath1, filePath2);
20+
assertEquals(-1, mismatch);
21+
}
22+
23+
@Test
24+
public void givenDifferentFiles_thenShouldFindMismatch() throws IOException {
25+
Path filePath3 = Files.createTempFile("file3", ".txt");
26+
Path filePath4 = Files.createTempFile("file4", ".txt");
27+
Files.writeString(filePath3, "Java 12 Article");
28+
Files.writeString(filePath4, "Java 12 Tutorial");
29+
long mismatch = Files.mismatch(filePath3, filePath4);
30+
assertEquals(8, mismatch);
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package java.com.baeldung.newfeatures;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class StringUnitTest {
8+
9+
@Test
10+
public void givenString_thenRevertValue() {
11+
String text = "Baeldung";
12+
String transformed = text.transform(value -> new StringBuilder(value).reverse().toString());
13+
assertEquals("gnudleaB", transformed);
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package java.com.baeldung.newfeatures;
2+
3+
import org.junit.Test;
4+
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class TeeingCollectorUnitTest {
11+
12+
@Test
13+
public void givenSetOfNumbers_thenCalculateAverage() {
14+
double mean = Stream.of(1, 2, 3, 4, 5)
15+
.collect(Collectors.teeing(Collectors.summingDouble(i -> i), Collectors.counting(), (sum, count) -> sum / count));
16+
assertEquals(3.0, mean);
17+
}
18+
}

0 commit comments

Comments
 (0)