-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJava11Features.java
More file actions
134 lines (114 loc) · 4.16 KB
/
Copy pathJava11Features.java
File metadata and controls
134 lines (114 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package java11;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestMethodOrder;
/**
* isBlank, String Lines, String repeat, String strip, Files API Changes, Optional Type : isEmpty() changes comes with JAVA 11.
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Java11Features {
@BeforeEach
void setup(TestInfo testInfo) {
System.out.println(testInfo.getDisplayName());
}
@AfterEach
void teardown() {
System.out.println();
}
@Test
@Order(1)
public void isBlankTest() {
String emptyText = "";
String emptyTextWithSpace = " ";
System.out.println("emptyText isEmpty result: " + emptyText.isEmpty()); //true
System.out.println("emptyText isBlank result: " + emptyText.isBlank()); //true
System.out.println("emptyTextWithSpace isEmpty result: " + emptyTextWithSpace.isEmpty()); //false (because it contains spaces.)
System.out.println("emptyTextWithSpace isBlank result: " + emptyTextWithSpace.isBlank()); //true
}
@Test
@Order(2)
public void stringLinesTest() {
String text = "Hello\nSW Test Academy\nIt is a great site!\nGo and check!";
System.out.println(text);
var textList = text.lines() //lines() method creates a stream.
.collect(Collectors.toList());
System.out.println(textList);
Assertions.assertEquals(4, textList.size());
}
@Test
@Order(3)
public void stringRepeatTest() {
String text = "Let's repeat!";
System.out.println(text.repeat(3));
}
@Test
@Order(4)
public void stringStripTest() {
char c = '\u2002';
String text = c + " SW TEST ACADEMY ";
System.out.println(text.trim()); //trim cannot remove unicode character.
System.out.println(text.strip()); //strip can remove unicode character.
}
@SneakyThrows
@Test
@Order(5)
public void filesAPITest() {
Path path = Paths.get(ClassLoader.getSystemResource("onur.txt").getPath());
Files.writeString(path, "SW Test Academy", StandardOpenOption.WRITE, StandardOpenOption.SYNC);
System.out.println("The Content: " + Files.readString(path) );
System.out.println(Files.readString(path).contains("SW Test Academy"));
}
@Test
@Order(6)
public void optionalEmptyTest1() {
var numbers = List.of(1, 2, 3, 4, 5, 6, 7);
Optional<Integer> optionalNumber = numbers.stream()
.filter(number -> number > 4)
.findFirst();
//isPresent version
if (optionalNumber.isPresent()) {
System.out.println("The number: " + optionalNumber.get());
} else {
System.out.println("Number is not available!");
}
//isEmpty version
if (optionalNumber.isEmpty()) {
System.out.println("Number is not available!");
} else {
System.out.println("The number: " + optionalNumber.get());
}
}
@Test
@Order(7)
public void optionalEmptyTest2() {
var numbers = List.of(1, 2, 3, 4, 5, 6, 7);
Optional<Integer> numbersGreaterThanSeven = numbers.stream()
.filter(number -> number > 7)
.findFirst();
//isPresent version
if (numbersGreaterThanSeven.isPresent()) {
System.out.println("The number: " + numbersGreaterThanSeven.get());
} else {
System.out.println("Number is not available!");
}
//isEmpty version
if (numbersGreaterThanSeven.isEmpty()) {
System.out.println("Number is not available!");
} else {
System.out.println("The number: " + numbersGreaterThanSeven.get());
}
}
}