diff options
author | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-07-28 10:55:02 +0700 |
---|---|---|
committer | Alexander Kriegisch <Alexander@Kriegisch.name> | 2021-09-07 08:44:33 +0200 |
commit | 620add3a3c18dc3e126a03254b6482ad9ab7ef36 (patch) | |
tree | 5a43d8a1db333183ac9e335772088e3c922e35de /tests | |
parent | c80551f1d36e288b4b3be658e70c0560b7e04c9e (diff) | |
download | aspectj-620add3a3c18dc3e126a03254b6482ad9ab7ef36.tar.gz aspectj-620add3a3c18dc3e126a03254b6482ad9ab7ef36.zip |
Add + activate some Java 17 tests
- Fix one fault sanity test configuration
- Deactivate Java 16 preview tests (no longer supported by Java 17
compiler)
- Test sealed classes as final on Java 17 (no longer preview)
- Add tests for JEP 406, pattern matching for switch (preview). At
present, the beta 17 branch of JDT Core does not handle the tested
features and expected compile errors correctly yet, so I had to
temporarily deactivate test execution, only printing TODO messages.
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Diffstat (limited to 'tests')
7 files changed, 239 insertions, 62 deletions
diff --git a/tests/features198/java17/SwitchPatternAspect.aj b/tests/features198/java17/SwitchPatternAspect.aj new file mode 100644 index 000000000..5ca2b9611 --- /dev/null +++ b/tests/features198/java17/SwitchPatternAspect.aj @@ -0,0 +1,65 @@ +aspect SwitchPatternAspect { + Object around(Object o) : execution(* doSomethingWithObject(*)) && args(o) { + System.out.println(switch (o) { + case null -> "null"; + case Integer i -> String.format("int %d", i); + case Long l -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> o.toString(); + }); + return proceed(o); + } + + before(Shape s) : execution(* doSomethingWithShape(*)) && args(s) { + System.out.println(switch (s) { + case Circle c && (c.calculateArea() > 100) -> "Large circle"; + case Circle c -> "Small circle"; + default -> "Non-circle"; + }); + } + + after(S s) : execution(* doSomethingWithSealedClass(*)) && args(s) { + System.out.println(switch (s) { + case A a -> "Sealed sub-class A"; + case B b -> "Sealed sub-class B"; + case C c -> "Sealed sub-record C"; + }); + } +} + +class Shape {} +class Rectangle extends Shape {} +class Circle extends Shape { + private final double radius; + public Circle(double radius) { this.radius = radius; } + double calculateArea() { return Math.PI * radius * radius; } +} + +sealed interface S permits A, B, C {} +final class A implements S {} +final class B implements S {} +record C(int i) implements S {} // Implicitly final + +public class Application { + public static void main(String[] args) { + doSomethingWithObject(null); + doSomethingWithObject(123); + doSomethingWithObject(999L); + doSomethingWithObject(12.34); + doSomethingWithObject("foo"); + doSomethingWithObject(List.of(123, "foo", 999L, 12.34)); + + doSomethingWithShape(new Rectangle()); + doSomethingWithShape(new Circle(5)); + doSomethingWithShape(new Circle(6)); + + doSomethingWithSealedClass(new A())); + doSomethingWithSealedClass(new B())); + doSomethingWithSealedClass(new C(5))); + } + + public Object doSomethingWithObject(Object o) { return o; } + public void doSomethingWithSealedClass(S s) {} + public void doSomethingWithShape(Shape s) {} +} diff --git a/tests/features198/java17/SwitchPatternError.java b/tests/features198/java17/SwitchPatternError.java new file mode 100644 index 000000000..745fd375c --- /dev/null +++ b/tests/features198/java17/SwitchPatternError.java @@ -0,0 +1,16 @@ +/** + * Inspired by examples in https://openjdk.java.net/jeps/406 + */ +public class SwitchPatternError { + static void error(Object o) { + switch(o) { + case CharSequence cs -> + System.out.println("A sequence of length " + cs.length()); + case String s -> // Error - pattern is dominated by previous pattern + System.out.println("A string: " + s); + default -> { + break; + } + } + } +} diff --git a/tests/features198/java17/SwitchPatternOK.java b/tests/features198/java17/SwitchPatternOK.java new file mode 100644 index 000000000..26c1cf755 --- /dev/null +++ b/tests/features198/java17/SwitchPatternOK.java @@ -0,0 +1,63 @@ +import java.util.List; + +/** + * Inspired by examples in https://openjdk.java.net/jeps/406 + */ +public class SwitchPatternOK { + public static void main(String[] args) { + System.out.println(formatterPatternSwitch(null)); + System.out.println(formatterPatternSwitch(123)); + System.out.println(formatterPatternSwitch(999L)); + System.out.println(formatterPatternSwitch(12.34)); + System.out.println(formatterPatternSwitch("foo")); + System.out.println(formatterPatternSwitch(List.of(123, "foo", 999L, 12.34))); + + System.out.println(testCircle(new Rectangle())); + System.out.println(testCircle(new Circle(5))); + System.out.println(testCircle(new Circle(6))); + + System.out.println(testSealedCoverage(new A())); + System.out.println(testSealedCoverage(new B())); + System.out.println(testSealedCoverage(new C(5))); + } + + static String formatterPatternSwitch(Object o) { + return switch (o) { + case null -> "null"; + case Integer i -> String.format("int %d", i); + case Long l -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> o.toString(); + }; + } + + static class Shape {} + static class Rectangle extends Shape {} + static class Circle extends Shape { + private final double radius; + public Circle(double radius) { this.radius = radius; } + double calculateArea() { return Math.PI * radius * radius; } + } + + static String testCircle(Shape s) { + return switch (s) { + case Circle c && (c.calculateArea() > 100) -> "Large circle"; + case Circle c -> "Small circle"; + default -> "Non-circle"; + }; + } + + sealed interface S permits A, B, C {} + final static class A implements S {} + final static class B implements S {} + static record C(int i) implements S {} // Implicitly final + + static String testSealedCoverage(S s) { + return switch (s) { + case A a -> "Sealed sub-class A"; + case B b -> "Sealed sub-class B"; + case C c -> "Sealed sub-record C"; + }; + } +} diff --git a/tests/src/test/java/org/aspectj/systemtest/ajc198/Ajc198TestsJava.java b/tests/src/test/java/org/aspectj/systemtest/ajc198/Ajc198TestsJava.java index 172d6e14e..8fa9110f2 100644 --- a/tests/src/test/java/org/aspectj/systemtest/ajc198/Ajc198TestsJava.java +++ b/tests/src/test/java/org/aspectj/systemtest/ajc198/Ajc198TestsJava.java @@ -17,40 +17,24 @@ import org.aspectj.testing.XMLBasedAjcTestCaseForJava17OrLater; */ public class Ajc198TestsJava extends XMLBasedAjcTestCaseForJava17OrLater { - public void testHiddenClass() { - runTest("hidden class"); - checkVersion("HiddenClassDemo", Constants.MAJOR_17, Constants.MINOR_17); - } - - public void testTextBlock1() { - runTest("textblock 1"); - checkVersion("Code", Constants.MAJOR_17, Constants.MINOR_17); - } - - public void testTextBlock2() { - runTest("textblock 2"); - checkVersion("Code2", Constants.MAJOR_17, Constants.MINOR_17); - } - - public void testRecords() { - runTest("simple record"); - checkVersion("Person", Constants.MAJOR_17, Constants.MINOR_17); - } - - public void testRecords2() { - runTest("using a record"); - checkVersion("UsingPersonRecord", Constants.MAJOR_17, Constants.MINOR_17); - } - - public void testAdvisingRecords() { - runTest("advising records"); - checkVersion("TraceRecordComponents", Constants.MAJOR_17, Constants.MINOR_17); - } - - public void testInstanceofPatterns() { - runTest("instanceof patterns"); - checkVersion("Jep305", Constants.MAJOR_17, Constants.MINOR_17); - } + public void testSealedClassWithLegalSubclasses() { + runTest("sealed class with legal subclasses"); + // TODO: replace 0 by Constants.PREVIEW_MINOR_VERSION after no longer using EA build, but final JDK version + checkVersion("Employee", Constants.MAJOR_17, 0 /*Constants.PREVIEW_MINOR_VERSION*/); + checkVersion("Manager", Constants.MAJOR_17, 0 /*Constants.PREVIEW_MINOR_VERSION*/); + } + + public void testSealedClassWithIllegalSubclass() { + runTest("sealed class with illegal subclass"); + // TODO: replace 0 by Constants.PREVIEW_MINOR_VERSION after no longer using EA build, but final JDK version + checkVersion("Person", Constants.MAJOR_17, 0 /*Constants.PREVIEW_MINOR_VERSION*/); + } + + public void testWeaveSealedClass() { + runTest("weave sealed class"); + // TODO: replace 0 by Constants.PREVIEW_MINOR_VERSION after no longer using EA build, but final JDK version + checkVersion("PersonAspect", Constants.MAJOR_17, 0 /*Constants.PREVIEW_MINOR_VERSION*/); + } public static Test suite() { return XMLBasedAjcTestCase.loadSuite(Ajc198TestsJava.class); diff --git a/tests/src/test/java/org/aspectj/systemtest/ajc198/Java17PreviewFeaturesTests.java b/tests/src/test/java/org/aspectj/systemtest/ajc198/Java17PreviewFeaturesTests.java index fab787724..e17ae26d6 100644 --- a/tests/src/test/java/org/aspectj/systemtest/ajc198/Java17PreviewFeaturesTests.java +++ b/tests/src/test/java/org/aspectj/systemtest/ajc198/Java17PreviewFeaturesTests.java @@ -17,23 +17,28 @@ import org.aspectj.testing.XMLBasedAjcTestCaseForJava17Only; */ public class Java17PreviewFeaturesTests extends XMLBasedAjcTestCaseForJava17Only { -/* - public void testSealedClassWithLegalSubclasses() { - runTest("sealed class with legal subclasses"); - checkVersion("Employee", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION); - checkVersion("Manager", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION); + public void testSwitchPatternMatchingCaseLabelDominatedByPrecedingError() { + // TODO: JDT Core does not support detecting type domination detection in the development version yet -> activate when available + System.out.println("TODO: JDT Core does not support detecting type domination detection in the development version yet -> activate when available"); +// runTest("switch pattern matching error"); } - public void testSealedClassWithIllegalSubclass() { - runTest("sealed class with illegal subclass"); - checkVersion("Person", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION); + public void testSwitchPatternMatchingJava() { + // TODO: JDT Core does not support sealed class coverage in the development version yet -> activate when available + System.out.println("TODO: JDT Core does not support sealed class coverage in the development version yet -> activate when available"); +// runTest("switch pattern matching java"); +// checkVersion("SwitchPatternOK", Constants.MAJOR_17, Constants.PREVIEW_MINOR_VERSION); } - public void testWeaveSealedClass() { - runTest("weave sealed class"); - checkVersion("PersonAspect", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION); + public void testSwitchPatternMatchingAspect() { + // TODO: JDT Core does not support sealed class coverage in the development version yet -> activate when available + System.out.println("TODO: JDT Core does not support sealed class coverage in the development version yet -> activate when available"); +// runTest("switch pattern matching aspect"); +// checkVersion("SwitchPatternAspect", Constants.MAJOR_17, Constants.PREVIEW_MINOR_VERSION); +// checkVersion("Application", Constants.MAJOR_17, Constants.PREVIEW_MINOR_VERSION); +// checkVersion("Shape", Constants.MAJOR_17, Constants.PREVIEW_MINOR_VERSION); +// checkVersion("S", Constants.MAJOR_17, Constants.PREVIEW_MINOR_VERSION); } -*/ public static Test suite() { return XMLBasedAjcTestCase.loadSuite(Java17PreviewFeaturesTests.class); diff --git a/tests/src/test/resources/org/aspectj/systemtest/ajc198/ajc198.xml b/tests/src/test/resources/org/aspectj/systemtest/ajc198/ajc198.xml index 06ca7d048..a0c26d098 100644 --- a/tests/src/test/resources/org/aspectj/systemtest/ajc198/ajc198.xml +++ b/tests/src/test/resources/org/aspectj/systemtest/ajc198/ajc198.xml @@ -3,34 +3,78 @@ <suite> - <!-- Java ?? final, Java 16, 15 preview --> -<!-- + <!-- Java 17 final, Java 16, 15 preview --> <ajc-test dir="features197/java15" vm="17" title="sealed class with legal subclasses"> - <compile files="Person.java Employee.java Manager.java" options="--enable-preview -16" /> + <compile files="Person.java Employee.java Manager.java" options="-17" /> </ajc-test> ---> - <!-- Java ?? final, Java 16, 15 preview --> -<!-- + <!-- Java 17 final, Java 16, 15 preview --> <ajc-test dir="features197/java15" vm="17" title="sealed class with illegal subclass"> - <compile files="Person.java Employee.java Manager.java PersonaNonGrata.java" options="--enable-preview -16"> + <compile files="Person.java Employee.java Manager.java PersonaNonGrata.java" options="-17"> <message kind="error" file="PersonaNonGrata.java" text="should be a permitted subtype of Person"/> </compile> </ajc-test> ---> - <!-- Java ?? final, Java 16, 15 preview --> -<!-- + <!-- Java 17 final, Java 16, 15 preview --> <ajc-test dir="features197/java15" vm="17" title="weave sealed class"> - <compile files="Person.java Employee.java Manager.java TopManager.java PersonAspect.aj" options="--enable-preview -16" /> - <run class="TopManager" vmargs="--enable-preview"> + <compile files="Person.java Employee.java Manager.java TopManager.java PersonAspect.aj" options="-17" /> + <run class="TopManager"> <stdout> <line text="Hello Sir John" /> <line text="CEO" /> </stdout> </run> </ajc-test> ---> + + <!-- Java ?? final, Java 17 preview --> + <ajc-test dir="features198/java17" vm="17" title="switch pattern matching error"> + <compile files="SwitchPatternError.java" options="--enable-preview -17"> + <!-- TODO: Add correct compiler error message, as soon as JDT Core supports it --> + <message kind="error" file="SwitchPatternError.java" text="XXX"/> + </compile> + </ajc-test> + + <!-- Java ?? final, Java 17 preview --> + <ajc-test dir="features198/java17" vm="17" title="switch pattern matching java"> + <compile files="SwitchPatternOK.java" options="--enable-preview -17" /> + <run class="SwitchPatternOK" vmargs="--enable-preview"> + <stdout> + <line text="null" /> + <line text="int 123" /> + <line text="long 999" /> + <line text="double 12,340000" /> + <line text="String foo" /> + <line text="[123, foo, 999, 12.34]" /> + <line text="Non-circle" /> + <line text="Small circle" /> + <line text="Large circle" /> + <line text="Sealed sub-class A" /> + <line text="Sealed sub-class B" /> + <line text="Sealed sub-record C" /> + </stdout> + </run> + </ajc-test> + + <!-- Java ?? final, Java 17 preview --> + <ajc-test dir="features198/java17" vm="17" title="switch pattern matching aspect"> + <compile files="SwitchPatternAspect.aj" options="--enable-preview -17" /> + <run class="Application" vmargs="--enable-preview"> + <stdout> + <line text="null" /> + <line text="int 123" /> + <line text="long 999" /> + <line text="double 12,340000" /> + <line text="String foo" /> + <line text="[123, foo, 999, 12.34]" /> + <line text="Non-circle" /> + <line text="Small circle" /> + <line text="Large circle" /> + <line text="Sealed sub-class A" /> + <line text="Sealed sub-class B" /> + <line text="Sealed sub-record C" /> + </stdout> + </run> + </ajc-test> <!-- Javac/ECJ 9+ compiler option, but belated implementation in AJC 1.9.7 --> <ajc-test dir="features198/compiler_release" vm="9" title="compile to older JDK release"> diff --git a/tests/src/test/resources/org/aspectj/systemtest/ajc198/sanity-tests-17.xml b/tests/src/test/resources/org/aspectj/systemtest/ajc198/sanity-tests-17.xml index 70f52fafa..adb339a41 100644 --- a/tests/src/test/resources/org/aspectj/systemtest/ajc198/sanity-tests-17.xml +++ b/tests/src/test/resources/org/aspectj/systemtest/ajc198/sanity-tests-17.xml @@ -48,14 +48,14 @@ <compile files="SimpleI.java" options="-17"/> </ajc-test> - <!-- check class file version is 60.0 (Java 16) --> + <!-- check class file version is 61.0 (Java 17) --> <ajc-test dir="bugs160/simplejava" title="simple - j"> <compile files="SimpleJ.java" options="-17"/> </ajc-test> - <!-- check class file version is 60.0 (Java 16) --> + <!-- check class file version is 61.0 (Java 17) --> <ajc-test dir="bugs160/simplejava" title="simple - k"> - <compile files="SimpleJ.java" options="-source 16"/> + <compile files="SimpleJ.java" options="-source 17"/> </ajc-test> <!-- check class file version is 49.0 --> |