Search in sources :

Example 51 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class PackageTest method testPrintPackageInfoWhenNothingInPackage.

@Test
public void testPrintPackageInfoWhenNothingInPackage() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/pkg/testclasses/internal");
    launcher.setSourceOutputDirectory("./target/spooned/package");
    launcher.getEnvironment().setCommentEnabled(true);
    launcher.run();
    final CtPackage aPackage = launcher.getFactory().Package().get("spoon.test.pkg.testclasses.internal");
    assertEquals(1, aPackage.getAnnotations().size());
    assertEquals(3, aPackage.getComments().size());
    assertEquals(CtComment.CommentType.JAVADOC, aPackage.getComments().get(0).getCommentType());
    assertEquals(CtComment.CommentType.BLOCK, aPackage.getComments().get(1).getCommentType());
    assertEquals(CtComment.CommentType.INLINE, aPackage.getComments().get(2).getCommentType());
    assertThat(aPackage).isEqualTo(ModelUtils.build(new File("./target/spooned/package/spoon/test/pkg/testclasses/internal")).Package().get("spoon.test.pkg.testclasses.internal"));
}
Also used : Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) Test(org.junit.Test)

Example 52 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class PackageTest method testRenameRootPackageWithNullOrEmpty.

@Test
public void testRenameRootPackageWithNullOrEmpty() throws Exception {
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/noclasspath/app/Test.java");
    spoon.getEnvironment().setNoClasspath(true);
    spoon.buildModel();
    CtPackage rootPackage = spoon.getFactory().Package().getRootPackage();
    String rootPackageName = rootPackage.getSimpleName();
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, rootPackageName);
    rootPackage.setSimpleName("");
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, rootPackageName);
    rootPackage.setSimpleName(null);
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, rootPackageName);
}
Also used : Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 53 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class PackageTest method testPackage.

@Test
public void testPackage() throws Exception {
    final String classFilePath = "./src/test/java/spoon/test/pkg/name/PackageTestClass.java";
    final String packageInfoFilePath = "./src/test/java/spoon/test/pkg/package-info.java";
    final File packageInfoFile = new File(packageInfoFilePath);
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    factory.getEnvironment().setCommentEnabled(true);
    spoon.createCompiler(factory, SpoonResourceHelper.resources(classFilePath, packageInfoFilePath)).build();
    CtClass<?> clazz = factory.Class().get(PackageTestClass.class);
    Assert.assertEquals(PackageTestClass.class, clazz.getActualClass());
    CtPackage ctPackage = clazz.getPackage();
    Assert.assertEquals("spoon.test.pkg.name", ctPackage.getQualifiedName());
    Assert.assertEquals("", ctPackage.getDocComment());
    assertTrue(CtPackage.class.isAssignableFrom(ctPackage.getParent().getClass()));
    ctPackage = (CtPackage) ctPackage.getParent();
    Assert.assertEquals("spoon.test.pkg", ctPackage.getQualifiedName());
    Assert.assertNotNull(ctPackage.getPosition());
    Assert.assertEquals(packageInfoFile.getCanonicalPath(), ctPackage.getPosition().getFile().getCanonicalPath());
    Assert.assertEquals(1, ctPackage.getPosition().getLine());
    Assert.assertEquals(1, ctPackage.getAnnotations().size());
    Assert.assertEquals("This is test\nJavaDoc.", ctPackage.getComments().get(0).getContent());
    CtAnnotation<?> annotation = ctPackage.getAnnotations().get(0);
    Assert.assertEquals(Deprecated.class, annotation.getAnnotationType().getActualClass());
    Assert.assertEquals(packageInfoFile.getCanonicalPath(), annotation.getPosition().getFile().getCanonicalPath());
    Assert.assertEquals(5, annotation.getPosition().getLine());
    assertTrue(CtPackage.class.isAssignableFrom(ctPackage.getParent().getClass()));
    ctPackage = (CtPackage) ctPackage.getParent();
    Assert.assertEquals("spoon.test", ctPackage.getQualifiedName());
    Assert.assertEquals("", ctPackage.getDocComment());
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) Test(org.junit.Test)

Aggregations

CtPackage (spoon.reflect.declaration.CtPackage)53 Test (org.junit.Test)29 Launcher (spoon.Launcher)21 Factory (spoon.reflect.factory.Factory)13 CtType (spoon.reflect.declaration.CtType)12 File (java.io.File)8 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 ArrayList (java.util.ArrayList)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtModule (spoon.reflect.declaration.CtModule)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 SpoonAPI (spoon.SpoonAPI)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 Annotation (java.lang.annotation.Annotation)2