Skip to content

Commit fc38c05

Browse files
committed
refactor(testing): migrate to SpoonAssertions and deprecate old assertion package
Replaces all uses of spoon.testing.Assert with SpoonAssertions from spoon.testing.assertions. Test model building is migrated to @modeltest, JUnit assertions are replaced with AssertJ/SpoonAssertions. The eight assertion classes in spoon.testing (src/main) are annotated @deprecated(since="11", forRemoval=true) pointing to SpoonAssertions.
1 parent fd30129 commit fc38c05

17 files changed

Lines changed: 173 additions & 94 deletions

src/main/java/spoon/testing/AbstractAssert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
* the self type of this assertion class.
2121
* @param <A>
2222
* the type of the actual value.
23+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
2324
*/
25+
@Deprecated(since = "11", forRemoval = true)
2426
public abstract class AbstractAssert<T extends AbstractAssert<T, A>, A> {
2527
protected final LinkedList<Processor<?>> processors = new LinkedList<>();
2628
protected final A actual;

src/main/java/spoon/testing/AbstractCtElementAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import static spoon.testing.utils.Check.assertIsSame;
1515
import static spoon.testing.utils.ProcessorUtils.process;
1616

17+
/**
18+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
19+
*/
20+
@Deprecated(since = "11", forRemoval = true)
1721
public abstract class AbstractCtElementAssert<T extends AbstractCtElementAssert<T>> extends AbstractAssert<T, CtElement> {
1822
protected AbstractCtElementAssert(CtElement actual, Class<?> selfType) {
1923
super(actual, selfType);

src/main/java/spoon/testing/AbstractCtPackageAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import static spoon.testing.utils.Check.assertNotNull;
2020
import static spoon.testing.utils.ProcessorUtils.process;
2121

22+
/**
23+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
24+
*/
25+
@Deprecated(since = "11", forRemoval = true)
2226
public abstract class AbstractCtPackageAssert<T extends AbstractCtPackageAssert<T>> extends AbstractAssert<T, CtPackage> {
2327
protected AbstractCtPackageAssert(CtPackage actual, Class<?> selfType) {
2428
super(actual, selfType);

src/main/java/spoon/testing/AbstractFileAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import static spoon.testing.utils.ModelUtils.build;
1919
import static spoon.testing.utils.ProcessorUtils.process;
2020

21+
/**
22+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
23+
*/
24+
@Deprecated(since = "11", forRemoval = true)
2125
public abstract class AbstractFileAssert<T extends AbstractFileAssert<T>> extends AbstractAssert<T, File> {
2226
public AbstractFileAssert(File actual, Class<?> selfType) {
2327
super(actual, selfType);

src/main/java/spoon/testing/Assert.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
* Each method in this class is a static factory for the type-specific
2121
* assertion objects. The purpose of this class is to make test code
2222
* more readable.
23+
*
24+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
2325
*/
26+
@Deprecated(since = "11", forRemoval = true)
2427
public class Assert {
2528
private Assert() { }
2629
/**

src/main/java/spoon/testing/CtElementAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import spoon.reflect.declaration.CtElement;
1111

12+
/**
13+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
14+
*/
15+
@Deprecated(since = "11", forRemoval = true)
1216
public class CtElementAssert extends AbstractCtElementAssert<CtElementAssert> {
1317
protected CtElementAssert(CtElement actual) {
1418
super(actual, CtElementAssert.class);

src/main/java/spoon/testing/CtPackageAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import spoon.reflect.declaration.CtPackage;
1111

12+
/**
13+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
14+
*/
15+
@Deprecated(since = "11", forRemoval = true)
1216
public class CtPackageAssert extends AbstractCtPackageAssert<CtPackageAssert> {
1317
public CtPackageAssert(CtPackage actual) {
1418
super(actual, CtPackageAssert.class);

src/main/java/spoon/testing/FileAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import java.io.File;
1111

12+
/**
13+
* @deprecated Use {@link spoon.testing.assertions.SpoonAssertions} instead.
14+
*/
15+
@Deprecated(since = "11", forRemoval = true)
1216
public class FileAssert extends AbstractFileAssert<FileAssert> {
1317
public FileAssert(File actual) {
1418
super(actual, FileAssert.class);

src/test/java/spoon/processing/CtGenerationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import java.util.regex.Pattern;
3838
import java.util.stream.Stream;
3939

40-
import static spoon.testing.Assert.assertThat;
40+
import static spoon.testing.assertions.SpoonAssertions.assertThat;
4141
import static spoon.testing.utils.ModelUtils.build;
4242

4343
public class CtGenerationTest {

src/test/java/spoon/processing/ProcessingTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@
2929
import spoon.test.processing.processors.MyProcessor;
3030
import spoon.test.template.testclasses.AssertToIfAssertedStatementTemplate;
3131

32+
import spoon.reflect.declaration.CtType;
33+
import spoon.reflect.factory.Factory;
34+
import spoon.testing.utils.ModelTest;
35+
3236
import java.io.File;
3337
import java.io.IOException;
3438
import java.nio.file.Files;
3539
import java.nio.file.Path;
40+
import java.util.List;
3641

37-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
42+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3843
import static org.junit.jupiter.api.Assertions.assertFalse;
3944
import static org.junit.jupiter.api.Assertions.assertTrue;
4045
import static org.junit.jupiter.api.Assertions.fail;
41-
import static spoon.testing.Assert.assertThat;
46+
import static spoon.testing.assertions.SpoonAssertions.assertThat;
47+
import static spoon.testing.utils.ModelUtils.build;
4248

4349
public class ProcessingTest {
4450

@@ -130,8 +136,8 @@ public void testNullPointerException() throws IOException {
130136
l.run();
131137
}
132138

133-
@Test
134-
public void testTemplateNotInOutput() throws IOException {
139+
@ModelTest("src/test/resources/spoon/test/template/SimpleIfAsserted.java")
140+
public void testTemplateNotInOutput(Factory expectedFactory) throws IOException {
135141
// https://github.com/INRIA/spoon/issues/2987
136142
class AssertProcessor extends AbstractProcessor<CtAssert<?>> {
137143
public void process(CtAssert<?> element) {
@@ -141,24 +147,29 @@ public void process(CtAssert<?> element) {
141147
);
142148
}
143149
}
144-
150+
145151
String templatePath = "src/test/java/spoon/test/template/testclasses/AssertToIfAssertedStatementTemplate.java";
146152
String resourcePath = "src/test/resources/spoon/test/template/";
147-
153+
148154
final Launcher l = new Launcher();
149155
Path outputPath = Files.createTempDirectory("emptydir");
150-
156+
151157
l.addProcessor(new AssertProcessor());
152158
l.addTemplateResource(new FileSystemFile(templatePath));
153-
159+
154160
l.addInputResource(resourcePath + "SimpleAssert.java");
155161
l.setSourceOutputDirectory(outputPath.toFile());
156162
l.run();
157163

158164
// If template is applied to itself then there will be modified spoon/...Template.java on output
159-
assertArrayEquals(new String[]{"SimpleAssert.java"}, outputPath.toFile().list(), "Template source found in output");
165+
org.assertj.core.api.Assertions.assertThat(outputPath.toFile().list())
166+
.containsExactly("SimpleAssert.java");
160167
// Check that the template worked as intended
161-
assertThat(outputPath.toString() + "/SimpleAssert.java")
162-
.isEqualTo(resourcePath + "SimpleIfAsserted.java");
168+
List<CtType<?>> actualTypes = build(new File(outputPath.toString() + "/SimpleAssert.java")).Type().getAll();
169+
List<CtType<?>> expectedTypes = expectedFactory.Type().getAll();
170+
org.assertj.core.api.Assertions.assertThat(actualTypes).hasSameSizeAs(expectedTypes);
171+
for (int i = 0; i < actualTypes.size(); i++) {
172+
assertThat(actualTypes.get(i)).isEqualTo(expectedTypes.get(i));
173+
}
163174
}
164175
}

0 commit comments

Comments
 (0)