Search in sources :

Example 1 with SpoonAPI

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));
}
Also used : Bar(spoon.test.api.testclasses.Bar) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 2 with SpoonAPI

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));
}
Also used : ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) File(java.io.File) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 3 with SpoonAPI

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())));
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) CtRole(spoon.reflect.path.CtRole) ArrayList(java.util.ArrayList) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) SpoonAPI(spoon.SpoonAPI) CtExpression(spoon.reflect.code.CtExpression) CtNewArray(spoon.reflect.code.CtNewArray) Metamodel(spoon.Metamodel) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtField(spoon.reflect.declaration.CtField) CtReference(spoon.reflect.reference.CtReference) MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) PropertySetter(spoon.reflect.annotations.PropertySetter) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldRead(spoon.reflect.code.CtFieldRead) Assert(org.junit.Assert) Collections(java.util.Collections) PropertyGetter(spoon.reflect.annotations.PropertyGetter) CtMethod(spoon.reflect.declaration.CtMethod) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Example 4 with SpoonAPI

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)));
    }
}
Also used : Launcher(spoon.Launcher) CtInterface(spoon.reflect.declaration.CtInterface) CtCodeElement(spoon.reflect.code.CtCodeElement) CtPackage(spoon.reflect.declaration.CtPackage) StringUtils(org.apache.commons.lang3.StringUtils) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CtType(spoon.reflect.declaration.CtType) SpoonAPI(spoon.SpoonAPI) ABSTRACT(spoon.test.metamodel.MMTypeKind.ABSTRACT) Assert.fail(org.junit.Assert.fail) CtConstructor(spoon.reflect.declaration.CtConstructor) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) SpoonMetaModel(spoon.test.metamodel.SpoonMetaModel) CtField(spoon.reflect.declaration.CtField) CtInheritanceScanner(spoon.reflect.visitor.CtInheritanceScanner) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) AbstractManualProcessor(spoon.processing.AbstractManualProcessor) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) AbstractProcessor(spoon.processing.AbstractProcessor) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CtMethod(spoon.reflect.declaration.CtMethod) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) SpoonAPI(spoon.SpoonAPI) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 5 with SpoonAPI

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Test(org.junit.Test) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)21 SpoonAPI (spoon.SpoonAPI)21 Test (org.junit.Test)19 CtType (spoon.reflect.declaration.CtType)9 ArrayList (java.util.ArrayList)8 CtField (spoon.reflect.declaration.CtField)6 CtMethod (spoon.reflect.declaration.CtMethod)6 Factory (spoon.reflect.factory.Factory)6 CtClass (spoon.reflect.declaration.CtClass)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)5 File (java.io.File)4 Collections (java.util.Collections)4 List (java.util.List)4 Set (java.util.Set)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 CtElement (spoon.reflect.declaration.CtElement)4 ModifierKind (spoon.reflect.declaration.ModifierKind)4 Arrays (java.util.Arrays)3 Collectors (java.util.stream.Collectors)3