use of spoon.SpoonAPI in project spoon by INRIA.
the class APITest method testAddProcessorMethodInSpoonAPI.
@Test
public void testAddProcessorMethodInSpoonAPI() throws Exception {
final SpoonAPI launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/api/testclasses");
launcher.setSourceOutputDirectory("./target/spooned");
final AwesomeProcessor processor = new AwesomeProcessor();
launcher.addProcessor(processor);
launcher.run();
assertEquals(1, processor.getElements().size());
final CtClass<Bar> actual = processor.getElements().get(0);
assertEquals(2, actual.getMethods().size());
assertNotNull(actual.getMethodsByName("prepareMojito").get(0));
assertNotNull(actual.getMethodsByName("makeMojito").get(0));
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class APITest method testPrintNotAllSourcesWithNames.
@Test
public void testPrintNotAllSourcesWithNames() throws Exception {
// contract: setOutputFilter can take a list of fully-qualified classes to be pretty-printed
final File target = new File("./target/print-not-all/array");
final SpoonAPI launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/main/java/spoon/template/");
launcher.setSourceOutputDirectory(target);
launcher.setOutputFilter("spoon.template.Parameter", "spoon.template.AbstractTemplate");
launcher.run();
List<File> list = new ArrayList<>(FileUtils.listFiles(target, new String[] { "java" }, true));
final List<String> filesName = list.stream().map(File::getName).sorted().collect(Collectors.<String>toList());
assertEquals(2, filesName.size());
assertEquals("AbstractTemplate.java", filesName.get(0));
assertEquals("Parameter.java", filesName.get(1));
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class MetamodelTest method testGetAllMetamodelInterfacess.
@Test
public void testGetAllMetamodelInterfacess() {
// contract: Spoon supports runtime introspection on the metamodel
SpoonAPI interfaces = new Launcher();
interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
interfaces.addInputResource("src/main/java/spoon/reflect/code");
interfaces.addInputResource("src/main/java/spoon/reflect/reference");
interfaces.buildModel();
assertThat(Metamodel.getAllMetamodelInterfaces().stream().map(x -> x.getQualifiedName()).collect(Collectors.toSet()), equalTo(interfaces.getModel().getAllTypes().stream().map(x -> x.getQualifiedName()).collect(Collectors.toSet())));
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testStaticClasses.
@Test
public void testStaticClasses() throws Exception {
// contract: helper classes only have static methods and a private constructor
// spoon.compiler.SpoonResourceHelper
// spoon.reflect.visitor.Query
// spoon.support.compiler.jdt.JDTTreeBuilderQuery
// spoon.support.compiler.SnippetCompilationHelper
// spoon.support.util.ByteSerialization
// spoon.support.util.RtHelper
// spoon.support.visitor.equals.CloneHelper
// spoon.template.Substitution
// spoon.testing.utils.Check
// spoon.testing.utils.ProcessorUtils
// spoon.testing.Assert
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/main/java/");
spoon.buildModel();
for (CtClass<?> klass : spoon.getModel().getElements(new TypeFilter<CtClass>(CtClass.class) {
@Override
public boolean matches(CtClass element) {
return element.getSuperclass() == null && super.matches(element) && element.getMethods().size() > 0 && element.getElements(new TypeFilter<>(CtMethod.class)).stream().allMatch(x -> x.hasModifier(ModifierKind.STATIC));
}
})) {
assertTrue(klass.getElements(new TypeFilter<>(CtConstructor.class)).stream().allMatch(x -> x.hasModifier(ModifierKind.PRIVATE)));
}
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testGoodTestClassNames.
@Test
public void testGoodTestClassNames() throws Exception {
// contract: to be run by Maven surefire, all test classes must be called Test* or *Test
// reference: "By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:"
// "**/Test*.java" and "**/*Test.java"
// http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/");
spoon.buildModel();
for (CtMethod<?> meth : spoon.getModel().getElements(new TypeFilter<CtMethod>(CtMethod.class) {
@Override
public boolean matches(CtMethod element) {
return super.matches(element) && element.getAnnotation(Test.class) != null;
}
})) {
assertTrue("naming contract violated for " + meth.getParent(CtClass.class).getSimpleName(), meth.getParent(CtClass.class).getSimpleName().startsWith("Test") || meth.getParent(CtClass.class).getSimpleName().endsWith("Test"));
}
// contract: the Spoon test suite does not depend on Junit 3 classes and methods
// otherwise, intellij automatically selects the junit3 runner, finds nothing
// and crashes with a dirty exception
assertEquals(0, spoon.getModel().getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference element) {
CtMethod parent = element.getParent(CtMethod.class);
return "junit.framework.TestCase".equals(element.getQualifiedName());
}
}).size());
}
Aggregations