From 7ad63c95f8983d1925ccc1a52605c515e551ea7b Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Tue, 7 Mar 2023 20:49:17 +0400 Subject: [PATCH] Add sealed class testCases --- .../SealedClassTestsFormatted.test | 45 ++++++++++++++++++ .../SealedClassTestsUnformatted.test | 46 +++++++++++++++++++ ...portsStep_withCleanthatJavaparserTest.java | 4 +- ...dImportsStep_withGoogleJavaFormatTest.java | 8 ++-- 4 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 testlib/src/main/resources/java/removeunusedimports/SealedClassTestsFormatted.test create mode 100644 testlib/src/main/resources/java/removeunusedimports/SealedClassTestsUnformatted.test diff --git a/testlib/src/main/resources/java/removeunusedimports/SealedClassTestsFormatted.test b/testlib/src/main/resources/java/removeunusedimports/SealedClassTestsFormatted.test new file mode 100644 index 0000000000..2ef96e6c72 --- /dev/null +++ b/testlib/src/main/resources/java/removeunusedimports/SealedClassTestsFormatted.test @@ -0,0 +1,45 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package java.removeunusedimports; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.Test; + +class SealedClassTests extends AbstractJupiterTestEngineTests { + + @Test + void sealedTestClassesAreTestClasses() { + executeTestsForClass(TestCase.class).testEvents() // + .assertStatistics(stats -> stats.finished(2).succeeded(1).failed(1)); + } + + sealed + abstract static class AbstractTestCase + permits TestCase + { + + @Test + void succeedingTest() { + assertTrue(true); + } + + @Test + void failingTest() { + fail("always fails"); + } + } + + static final class TestCase extends AbstractTestCase { + } + +} diff --git a/testlib/src/main/resources/java/removeunusedimports/SealedClassTestsUnformatted.test b/testlib/src/main/resources/java/removeunusedimports/SealedClassTestsUnformatted.test new file mode 100644 index 0000000000..942aa4d235 --- /dev/null +++ b/testlib/src/main/resources/java/removeunusedimports/SealedClassTestsUnformatted.test @@ -0,0 +1,46 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package java.removeunusedimports; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.Test; +import useless.project.UnusedClass; + +class SealedClassTests extends AbstractJupiterTestEngineTests { + + @Test + void sealedTestClassesAreTestClasses() { + executeTestsForClass(TestCase.class).testEvents() // + .assertStatistics(stats -> stats.finished(2).succeeded(1).failed(1)); + } + + sealed + abstract static class AbstractTestCase + permits TestCase + { + + @Test + void succeedingTest() { + assertTrue(true); + } + + @Test + void failingTest() { + fail("always fails"); + } + } + + static final class TestCase extends AbstractTestCase { + } + +} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withCleanthatJavaparserTest.java b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withCleanthatJavaparserTest.java index d34096de87..11e31aeae2 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withCleanthatJavaparserTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withCleanthatJavaparserTest.java @@ -32,7 +32,9 @@ void behavior() throws Exception { .testResource("java/removeunusedimports/JavaCodeWithLicensePackageUnformatted.test", "java/removeunusedimports/JavaCodeWithLicensePackageFormatted.test") .testResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test", "java/removeunusedimports/JavaCodeWithPackageFormatted.test") .testResource("java/removeunusedimports/Jdk17TextBlockUnformatted.test", "java/removeunusedimports/Jdk17TextBlockFormatted.test") - .testResource("java/removeunusedimports/RevelcUnformatted.test", "java/removeunusedimports/RevelcFormatted.test"); + .testResource("java/removeunusedimports/RevelcUnformatted.test", "java/removeunusedimports/RevelcFormatted.test") + // Sealed classes are introduced with JDK15: This syntax is not supported by javaParser: such files are not trimmed from unused imports (for now) + .testResource("java/removeunusedimports/SealedClassTestsUnformatted.test", "java/removeunusedimports/SealedClassTestsUnformatted.test"); } @Test diff --git a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java index ba7dfef4a3..44699a8215 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java @@ -31,9 +31,11 @@ void behavior() throws Exception { .testResource("java/removeunusedimports/JavaCodeWithLicenseUnformatted.test", "java/removeunusedimports/JavaCodeWithLicenseFormatted.test") .testResource("java/removeunusedimports/JavaCodeWithLicensePackageUnformatted.test", "java/removeunusedimports/JavaCodeWithLicensePackageFormatted.test") .testResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test", "java/removeunusedimports/JavaCodeWithPackageFormatted.test") - // GoogleFormat requires running over a JDK17 to handle JDK17 features - // .testResource("java/removeunusedimports/Jdk17TextBlockUnformatted.test", "java/removeunusedimports/Jdk17TextBlockFormatted.test") - .testResource("java/removeunusedimports/RevelcUnformatted.test", "java/removeunusedimports/RevelcFormatted.test"); + .testResource("java/removeunusedimports/RevelcUnformatted.test", "java/removeunusedimports/RevelcFormatted.test") + // GoogleFormat requires running over a JDK17 to handle JDK17 features + // .testResource("java/removeunusedimports/Jdk17TextBlockUnformatted.test", "java/removeunusedimports/Jdk17TextBlockFormatted.test") + //.testResource("java/removeunusedimports/SealedClassTestsUnformatted.test", "java/removeunusedimports/SealedClassTestsFormatted.test") + ; } @Test