use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredPackage.
@Test
public void testGetUnitTypeWorksWithDeclaredPackage() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/pkg/package-info.java");
launcher.buildModel();
CtPackage ctPackage = launcher.getFactory().Package().get("spoon.test.pkg");
CompilationUnit compilationUnit = ctPackage.getPosition().getCompilationUnit();
assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, compilationUnit.getUnitType());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class TestCompilationUnit method testGetUnitTypeWorksWithCreatedObjects.
@Test
public void testGetUnitTypeWorksWithCreatedObjects() {
final Launcher launcher = new Launcher();
CtPackage myPackage = launcher.getFactory().Package().getOrCreate("my.package");
CompilationUnit cu = launcher.getFactory().createCompilationUnit();
assertEquals(CompilationUnit.UNIT_TYPE.UNKNOWN, cu.getUnitType());
cu.setDeclaredPackage(myPackage);
assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, cu.getUnitType());
cu.setDeclaredTypes(Collections.singletonList(launcher.getFactory().createClass()));
assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, cu.getUnitType());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class TestCompilationUnit method testCompilationUnitDeclaredTypes.
@Test
public void testCompilationUnitDeclaredTypes() throws IOException {
// contract: the list of declared types should be unmodifiable
File resource = new File("./src/test/java/spoon/test/model/Foo.java");
final Launcher launcher = new Launcher();
launcher.addInputResource(resource.getPath());
launcher.buildModel();
CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
assertEquals(3, cu.getDeclaredTypes().size());
List<CtType<?>> typeList = cu.getDeclaredTypes();
try {
typeList.remove(0);
fail();
} catch (UnsupportedOperationException e) {
// do nothing
}
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class TestCompilationUnit method testAddDeclaredTypeInCU.
@Test
public void testAddDeclaredTypeInCU() throws IOException {
// contract: when a type is added to a CU, it should also be pretty printed in cu mode
File resource = new File("./src/test/java/spoon/test/model/Foo.java");
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "compilationunits" });
launcher.addInputResource(resource.getPath());
launcher.setSourceOutputDirectory("./target/cu-onemoretype");
launcher.buildModel();
CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
assertEquals(3, cu.getDeclaredTypes().size());
CtType typeBla = launcher.getFactory().Class().create("spoon.test.model.Bla");
cu.addDeclaredType(typeBla);
assertEquals(4, cu.getDeclaredTypes().size());
launcher.prettyprint();
File output = new File("./target/cu-onemoretype/spoon/test/model/Foo.java");
List<String> lines = Files.readAllLines(output.toPath());
String fullContent = StringUtils.join(lines, "\n");
assertTrue(fullContent.contains("public class Foo"));
assertTrue(fullContent.contains("class Bar"));
assertTrue(fullContent.contains("class Baz"));
assertTrue(fullContent.contains("class Bla"));
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class TestCompilationUnit method testIsoEncodingIsSupported.
@Test
public void testIsoEncodingIsSupported() throws Exception {
File resource = new File("./src/test/resources/noclasspath/IsoEncoding.java");
String content = new String(Files.readAllBytes(resource.toPath()), "ISO-8859-1");
Launcher launcher = new Launcher();
launcher.addInputResource(resource.getPath());
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setEncoding(Charset.forName("ISO-8859-1"));
launcher.buildModel();
CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getPath());
assertEquals(content, cu.getOriginalSourceCode());
}
Aggregations