Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate APITest to Junit 5 #4420

Merged
merged 1 commit into from
Jan 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions src/test/java/spoon/test/api/APITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,24 @@
*/
package spoon.test.api;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.OutputType;
import spoon.SpoonAPI;
Expand Down Expand Up @@ -55,26 +70,13 @@
import spoon.test.api.processors.AwesomeProcessor;
import spoon.test.api.testclasses.Bar;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class APITest {

Expand Down Expand Up @@ -129,10 +131,9 @@ public void testDuplicateEntry() throws Exception {
// the virtual folder removes the duplicate before passing to JDT
try {
String duplicateEntry = "src/test/resources/spoon/test/api/Foo.java";

// check on the JDK API
// this is later use by FileSystemFile
assertTrue(new File(duplicateEntry).getCanonicalFile().equals(new File("./" + duplicateEntry).getCanonicalFile()));
assertEquals(new File(duplicateEntry).getCanonicalFile(), new File("./" + duplicateEntry).getCanonicalFile());

Launcher.main(new String[] {
"-i",
Expand Down Expand Up @@ -173,14 +174,13 @@ public void testDuplicateFilePlusFolder() {
}
}

@Test(expected = Exception.class)
@Test
public void testNotValidInput() {
String invalidEntry = "does/not/exists//Foo.java";
Launcher.main(new String[] { "-i",
invalidEntry,
"-o",
"target/spooned/apitest" });
}
assertThrows(Exception.class, () -> {
String invalidEntry = "does/not/exists//Foo.java";
Launcher.main(new String[]{ "-i", invalidEntry, "-o", "target/spooned/apitest" });
});
}

@Test
public void testAddProcessorMethodInSpoonAPI() {
Expand Down Expand Up @@ -424,16 +424,16 @@ public void accept(CtVisitor visitor) {
CtIf templateRoot = matcherCtClass.getMethod("matcher").getBody().getStatement(0);

final List<CtMethod<?>> setters = Query.getElements(launcher.getFactory(), new SetterMethodWithoutCollectionsFilter(launcher.getFactory()));
assertTrue("Number of setters found null", !setters.isEmpty());
assertTrue(!setters.isEmpty(), "Number of setters found null");

for (CtStatement statement : setters.stream().map((Function<CtMethod<?>, CtStatement>) ctMethod -> ctMethod.getBody().getStatement(0)).collect(Collectors.toList())) {

// First statement should be a condition to protect the setter of the parent.
assertTrue("Check the method " + statement.getParent(CtMethod.class).getSignature() + " in the declaring class " + statement.getParent(CtType.class).getQualifiedName(), statement instanceof CtIf);
assertTrue(statement instanceof CtIf, "Check the method " + statement.getParent(CtMethod.class).getSignature() + " in the declaring class " + statement.getParent(CtType.class).getQualifiedName());
CtIf ifCondition = (CtIf) statement;
TemplateMatcher matcher = new TemplateMatcher(templateRoot);

assertEquals("Check the number of if in method " + statement.getParent(CtMethod.class).getSignature() + " in the declaring class " + statement.getParent(CtType.class).getQualifiedName(), 1, matcher.find(ifCondition).size());
assertEquals(1, matcher.find(ifCondition).size(), "Check the number of if in method " + statement.getParent(CtMethod.class).getSignature() + " in the declaring class " + statement.getParent(CtType.class).getQualifiedName());
}
}

Expand Down Expand Up @@ -566,9 +566,9 @@ public File getDefaultOutputDirectory() {
Set<String> units = launcher.getFactory().CompilationUnit().getMap().keySet();
assertEquals(3, units.size());

assertTrue("Module file not contained (" + moduleFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(moduleFile.getCanonicalPath()));
assertTrue("Package file not contained (" + packageFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(packageFile.getCanonicalPath()));
assertTrue("Class file not contained (" + classFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(classFile.getCanonicalPath()));
assertTrue(units.contains(moduleFile.getCanonicalPath()), "Module file not contained (" + moduleFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"));
assertTrue(units.contains(packageFile.getCanonicalPath()), "Package file not contained (" + packageFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"));
assertTrue(units.contains(classFile.getCanonicalPath()), "Class file not contained (" + classFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"));
}

@Test
Expand All @@ -584,7 +584,7 @@ public void testOutputWithNoOutputProduceNoFolder() {
launcher.run();
File outputDir = new File(destPath);
System.out.println(destPath);
assertFalse("Output dir should not exist: " + outputDir.getAbsolutePath(), outputDir.exists());
assertFalse(outputDir.exists(), "Output dir should not exist: " + outputDir.getAbsolutePath());
}

@Test
Expand Down