Przeglądaj źródła

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>
tags/V1_9_8_RC1
Alexander Kriegisch 2 lat temu
rodzic
commit
620add3a3c

+ 3
- 2
testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCaseForJava16Only.java Wyświetl plik

@@ -20,13 +20,13 @@ public abstract class XMLBasedAjcTestCaseForJava16Only extends XMLBasedAjcTestCa
@Override
public void setUp() throws Exception {
// Activate this block after upgrading to JDT Core Java 17
/*
throw new IllegalStateException(
"These tests need a Java 16 level AspectJ compiler " +
"(e.g. because they use version-specific preview features). " +
"This compiler does not support preview features of a previous version anymore."
);
*/
// Activate this block before upgrading to JDT Core Java 17
/*
if (!LangUtil.is16VMOrGreater() || LangUtil.is17VMOrGreater()) {
throw new IllegalStateException(
"These tests should be run on Java 16 only " +
@@ -34,6 +34,7 @@ public abstract class XMLBasedAjcTestCaseForJava16Only extends XMLBasedAjcTestCa
);
}
super.setUp();
*/
}

}

+ 1
- 0
testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCaseForJava17Only.java Wyświetl plik

@@ -27,6 +27,7 @@ public abstract class XMLBasedAjcTestCaseForJava17Only extends XMLBasedAjcTestCa
"This compiler does not support preview features of a previous version anymore."
);
*/
// Activate this block before upgrading to JDT Core Java 18
if (!LangUtil.is17VMOrGreater() || LangUtil.is18VMOrGreater()) {
throw new IllegalStateException(
"These tests should be run on Java 17 only " +

+ 65
- 0
tests/features198/java17/SwitchPatternAspect.aj Wyświetl plik

@@ -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) {}
}

+ 16
- 0
tests/features198/java17/SwitchPatternError.java Wyświetl plik

@@ -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;
}
}
}
}

+ 63
- 0
tests/features198/java17/SwitchPatternOK.java Wyświetl plik

@@ -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";
};
}
}

+ 18
- 34
tests/src/test/java/org/aspectj/systemtest/ajc198/Ajc198TestsJava.java Wyświetl plik

@@ -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);

+ 17
- 12
tests/src/test/java/org/aspectj/systemtest/ajc198/Java17PreviewFeaturesTests.java Wyświetl plik

@@ -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);

+ 57
- 13
tests/src/test/resources/org/aspectj/systemtest/ajc198/ajc198.xml Wyświetl plik

@@ -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="&#45;&#45;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="&#45;&#45;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="&#45;&#45;enable-preview -16" />
<run class="TopManager" vmargs="&#45;&#45;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">

+ 3
- 3
tests/src/test/resources/org/aspectj/systemtest/ajc198/sanity-tests-17.xml Wyświetl plik

@@ -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 -->

Ładowanie…
Anuluj
Zapisz