Skip to content

Commit

Permalink
[#148] added tests for sealed classes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Apr 11, 2024
1 parent e731e07 commit ee1fd3a
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: Checkout Code
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v4
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/dropStaging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ jobs:
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v4

# Setup JDK and Maven
- name: Set up JDK 9
uses: actions/setup-java@v1
uses: actions/setup-java@v4
with:
java-version: 9
distribution: 'zulu'
java-version: 17
cache: 'maven'
server-id: sonatype-nexus-staging
server-username: OSS_CENTRAL_USERNAME # env variable for Maven Central
server-password: OSS_CENTRAL_PASSWORD # env variable for Maven Central
Expand Down
17 changes: 6 additions & 11 deletions .github/workflows/jacoco.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Set up JDK 16
uses: actions/setup-java@v1
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 16
distribution: 'zulu'
java-version: 17
cache: 'maven'

- name: Cache .m2
uses: actions/cache@v1
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven

- name: Prepare mvnw
run: chmod +x ./mvnw
Expand Down
14 changes: 4 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ jobs:

steps:
- name: Checkout Code
uses: actions/checkout@v1

- name: Cache .m2
uses: actions/cache@v1
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven
uses: actions/checkout@v4

# Get GPG private key into GPG
- name: Import GPG Owner Trust
Expand All @@ -29,9 +21,11 @@ jobs:

# Setup JDK and Maven
- name: Set up JDK 17
uses: actions/setup-java@v1
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 17
cache: 'maven'
server-id: sonatype-nexus-staging
server-username: OSS_CENTRAL_USERNAME # env variable for Maven Central
server-password: OSS_CENTRAL_PASSWORD # env variable for Maven Central
Expand Down
2 changes: 1 addition & 1 deletion integrationtest/java16/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<version>[3.0.4,)</version>
</requireMavenVersion>
<requireJavaVersion>
<version>9</version>
<version>16</version>
</requireJavaVersion>
<bannedDependencies>
<searchTransitive>false</searchTransitive>
Expand Down
6 changes: 3 additions & 3 deletions integrationtest/java17/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<version>[3.0.4,)</version>
</requireMavenVersion>
<requireJavaVersion>
<version>9</version>
<version>17</version>
</requireJavaVersion>
<bannedDependencies>
<searchTransitive>false</searchTransitive>
Expand All @@ -64,8 +64,8 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class Java17Tests {

@Test
public void test_sealedClassesFeature_unsealed() {
Cute.unitTest().when().passInElement().<TypeElement>fromClass(Java17Tests.class).intoUnitTest((processingEnvironment, element) -> {
Cute.unitTest().when((processingEnvironment) -> {
try {
ToolingProvider.setTooling(processingEnvironment);

TypeElementWrapper elementWrapper = TypeElementWrapper.wrap(element);
TypeElementWrapper elementWrapper = TypeElementWrapper.getByClass(Java17Tests.class).get();

MatcherAssert.assertThat(elementWrapper.getPermittedSubclasses(), Matchers.empty());
MatcherAssert.assertThat(element.getPermittedSubclasses(), Matchers.empty());
MatcherAssert.assertThat(elementWrapper.unwrap().getPermittedSubclasses(), Matchers.empty());

} finally {
ToolingProvider.clearTooling();
Expand All @@ -33,18 +33,16 @@ public void test_sealedClassesFeature_unsealed() {
}



static final class AllowedClass extends SealedClass {

}

@PassIn
static sealed class SealedClass permits AllowedClass{
static sealed class SealedClass permits AllowedClass {

}



@Test
public void test_sealedClassesFeature_sealed() {
Cute.unitTest().when().passInElement().<TypeElement>fromClass(SealedClass.class).intoUnitTest((processingEnvironment, element) -> {
Expand All @@ -53,8 +51,8 @@ public void test_sealedClassesFeature_sealed() {

TypeElementWrapper elementWrapper = TypeElementWrapper.wrap(element);

MatcherAssert.assertThat(elementWrapper.getPermittedSubclasses().stream().map(e -> e.getQualifiedName()).collect(Collectors.toSet()), Matchers.contains(SealedClass.class.getCanonicalName()));
MatcherAssert.assertThat(element.getPermittedSubclasses().stream().map(e -> e.toString()).collect(Collectors.toSet()), , Matchers.contains(SealedClass.class.getCanonicalName()));
MatcherAssert.assertThat(elementWrapper.getPermittedSubclasses().stream().map(e -> e.getQualifiedName()).collect(Collectors.toSet()), Matchers.contains(AllowedClass.class.getCanonicalName()));
MatcherAssert.assertThat(element.getPermittedSubclasses().stream().map(e -> e.toString()).collect(Collectors.toSet()), Matchers.contains(AllowedClass.class.getCanonicalName()));

} finally {
ToolingProvider.clearTooling();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public List<RecordComponentElementWrapper> getRecordComponents() {
public List<TypeMirrorWrapper> getPermittedSubclasses() {

// must make sure that method exists, otherwise return the default value
if (hasMethod(TYPE_ELEMENT_CLASS_NAME, "getPermittedSubclasses")) {
if (!hasMethod(TYPE_ELEMENT_CLASS_NAME, "getPermittedSubclasses")) {
// TODO MUST CHECK WHAT SHOULD BE RETURNED FOR NON SEALED CLASSES!
return Collections.EMPTY_LIST;
}
Expand Down

0 comments on commit ee1fd3a

Please sign in to comment.