Merge pull request #10337 from MajewskiKrzysztof/BAEL-4750

BAEL-4750 Java 12 New Features
This commit is contained in:
Loredana Crusoveanu 2021-01-11 12:00:01 +02:00 committed by GitHub
commit 6564d4265a
4 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package java.com.baeldung.newfeatures;
import org.junit.Test;
import java.text.NumberFormat;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
public class CompactNumbersUnitTest {
@Test
public void givenNumber_thenCompactValues() {
NumberFormat likesShort = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT);
likesShort.setMaximumFractionDigits(2);
assertEquals("2.59K", likesShort.format(2592));
NumberFormat likesLong = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.LONG);
likesLong.setMaximumFractionDigits(2);
assertEquals("2.59 thousand", likesShort.format(2592));
}
}

View File

@ -0,0 +1,32 @@
package java.com.baeldung.newfeatures;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.assertEquals;
public class FileMismatchUnitTest {
@Test
public void givenIdenticalFiles_thenShouldNotFindMismatch() throws IOException {
Path filePath1 = Files.createTempFile("file1", ".txt");
Path filePath2 = Files.createTempFile("file2", ".txt");
Files.writeString(filePath1, "Java 12 Article");
Files.writeString(filePath2, "Java 12 Article");
long mismatch = Files.mismatch(filePath1, filePath2);
assertEquals(-1, mismatch);
}
@Test
public void givenDifferentFiles_thenShouldFindMismatch() throws IOException {
Path filePath3 = Files.createTempFile("file3", ".txt");
Path filePath4 = Files.createTempFile("file4", ".txt");
Files.writeString(filePath3, "Java 12 Article");
Files.writeString(filePath4, "Java 12 Tutorial");
long mismatch = Files.mismatch(filePath3, filePath4);
assertEquals(8, mismatch);
}
}

View File

@ -0,0 +1,15 @@
package java.com.baeldung.newfeatures;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringUnitTest {
@Test
public void givenString_thenRevertValue() {
String text = "Baeldung";
String transformed = text.transform(value -> new StringBuilder(value).reverse().toString());
assertEquals("gnudleaB", transformed);
}
}

View File

@ -0,0 +1,18 @@
package java.com.baeldung.newfeatures;
import org.junit.Test;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class TeeingCollectorUnitTest {
@Test
public void givenSetOfNumbers_thenCalculateAverage() {
double mean = Stream.of(1, 2, 3, 4, 5)
.collect(Collectors.teeing(Collectors.summingDouble(i -> i), Collectors.counting(), (sum, count) -> sum / count));
assertEquals(3.0, mean);
}
}