Skip to content

Commit 7e87310

Browse files
committed
Update Java test classes ...
- refactoring, - extend sealed test, - amend smart casting
1 parent 06d2c7c commit 7e87310

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/main/java/pl/mperor/lab/java/sealed/Sealed.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pl.mperor.lab.java.sealed;
22

3-
public sealed interface Sealed permits AlsoSealed, NonSealed, Final {
3+
public sealed interface Sealed permits AlsoSealed, Final, NonSealed {
44

55
default String sealedMethod() {
66
return "sealed";

src/test/java/pl/mperor/lab/java/Java12.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ private String getTypeOfDayByNumber(Integer dayOfWeek) {
3535
}
3636

3737
@Test
38-
public void testPatternMatchingInstanceOf() {
38+
public void testPatternMatchingInstanceOfAkaSmartCasting() {
3939
Object o = "Hello String!";
4040
if (o instanceof String s) {
4141
Assertions.assertNotNull(s);
4242
Assertions.assertInstanceOf(String.class, s);
4343
}
44+
Assertions.assertTrue(nonEmptyString("Hello World!"));
45+
Assertions.assertFalse(nonEmptyString(null));
46+
}
47+
48+
private static boolean nonEmptyString(Object obj) {
49+
return (obj instanceof String str) && !str.isEmpty();
4450
}
4551

4652
@Test

src/test/java/pl/mperor/lab/java/Java15.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class Java15 {
1111

1212
@Test
13-
public void testSealedClasses() {
13+
public void testSealedClassesAndInterfaces() {
1414
Assertions.assertTrue(Sealed.class.isSealed());
1515
Assertions.assertFalse(NonSealed.class.isSealed());
1616
Assertions.assertFalse(Final.class.isSealed());
@@ -20,14 +20,35 @@ public void testSealedClasses() {
2020
Assertions.assertEquals("final", switchSealed(new Final()));
2121
Assertions.assertEquals("non-sealed", switchSealed(new NonSealed()));
2222
Assertions.assertEquals("sealed", switchSealed(new AlsoSealed()));
23+
24+
SealedClient client = new SealedClient(new NonSealedChild());
25+
Assertions.assertEquals("non-sealed", client.call());
2326
}
2427

25-
private String switchSealed(Sealed sealed) {
28+
private static String switchSealed(Sealed sealed) {
2629
return switch (sealed) {
2730
case AlsoSealed a -> a.alsoSealedMethod();
2831
case Final f -> f.finalMethod();
2932
case NonSealed ns -> ns.nonSealedMethod();
3033
};
3134
}
3235

36+
private static class NonSealedChild extends NonSealed {
37+
}
38+
39+
// not permitted
40+
// private static class SealedChild implements Sealed {
41+
// }
42+
public static class SealedClient {
43+
private Sealed s;
44+
45+
public SealedClient(Sealed s) {
46+
this.s = s;
47+
}
48+
49+
public String call() {
50+
return switchSealed(s);
51+
}
52+
}
53+
3354
}

src/test/java/pl/mperor/lab/java/Java16.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Disabled;
55
import org.junit.jupiter.api.Test;
6-
import org.junit.jupiter.api.condition.DisabledIf;
7-
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
86
import pl.mperor.lab.TestUtils;
97

108
import java.io.IOException;

0 commit comments

Comments
 (0)