@Test public void testOverrideOutputWriter() throws Exception { // this test that we can correctly set the Java output processor final List<Object> l = new ArrayList<Object>(); Launcher spoon = new Launcher() { @Override public JavaOutputProcessor createOutputWriter( File sourceOutputDir, Environment environment) { return new JavaOutputProcessor() { @Override public void process(CtType<?> e) { l.add(e); } @Override public void init() { // we do nothing } }; } }; spoon.run( new String[] { "-i", "src/test/resources/spoon/test/api/", "-o", "target/spooned/apitest" }); Assert.assertEquals(2, l.size()); }
@Test public void testPrintAMethodWithGeneric() throws Exception { final Launcher launcher = new Launcher(); final Factory factory = launcher.getFactory(); factory.getEnvironment().setAutoImports(true); final SpoonCompiler compiler = launcher.createCompiler(); compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/")); compiler.build(); final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class); final String expected = "public List<? extends ArrayList> aMethodWithGeneric() {" + System.lineSeparator() + " return new ArrayList<>();" + System.lineSeparator() + "}"; assertEquals(expected, aClass.getMethodsByName("aMethodWithGeneric").get(0).toString()); final CtConstructorCall<?> constructorCall = aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0); final CtTypeReference<?> ctTypeReference = constructorCall.getType().getActualTypeArguments().get(0); assertTrue(ctTypeReference instanceof CtImplicitTypeReference); assertEquals("Object", ctTypeReference.getSimpleName()); }
@Test public void testGetAllMethods() throws Exception { /* getAllMethods must not throw Exception in no classpath mode */ Launcher l = new Launcher(); l.getEnvironment().setNoClasspath(true); l.addInputResource("src/test/resources/noclasspath/A3.java"); l.buildModel(); Set<CtMethod<?>> methods = l.getFactory().Class().get("A3").getAllMethods(); assertEquals( 1, methods.stream().filter(method -> "foo".equals(method.getSimpleName())).count()); }
@Before public void setUp() throws Exception { final File testDirectory = new File("./src/test/java/spoon/test/interfaces/testclasses/"); final Launcher launcher = new Launcher(); this.factory = launcher.createFactory(); factory.getEnvironment().setComplianceLevel(8); SpoonCompiler compiler = launcher.createCompiler(this.factory); compiler.addInputSource(testDirectory); compiler.build(); }
@Test public void testDuplicateEntry() throws Exception { // it's possible to pass twice the same file as parameter // 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())); Launcher.main( new String[] { "-i", // note the nasty ./ duplicateEntry + File.pathSeparator + "./" + duplicateEntry, "-o", "target/spooned/apitest" }); } catch (IllegalArgumentException e) // from JDT { fail(); } }
// example usage (please do not use directly, use instead the spoon.Launcher // API to create the factory) public static void main(String[] args) throws Exception { Launcher main = new Launcher(); JDTBasedSpoonCompiler comp = new JDTBasedSpoonCompiler(main.createFactory()); comp.createBatchCompiler().printUsage(); SpoonFile file = new FileSystemFile(new File("./src/main/java/spoon/support/compiler/JDTCompiler.java")); comp.addInputSource(file); try { comp.build(); final Set<CtType<?>> types = comp.getFactory().Package().get("spoon.support.compiler").getTypes(); for (CtType<?> type : types) { main.getEnvironment().debugMessage(type.toString()); } } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testFieldWriteDeclaredInTheSuperclass() throws Exception { final Launcher launcher = new Launcher(); launcher.run( new String[] { "-i", "./src/test/resources/spoon/test/variable/Tacos.java", "-o", "target/spooned/variable", "--noclasspath", "--compliance", "8", "--level", "OFF" }); MainTest.checkAssignmentContracts(launcher.getFactory().Package().getRootPackage()); }
@Test public void instrumentSynchronizedTest() throws Exception { Launcher l = new Launcher(); l.addInputResource("src/test/java"); l.buildModel(); CtClass<?> sample = ((CtClass<?>) (l.getFactory() .Package() .getRootPackage() .getElements( new spoon.reflect.visitor.filter.NameFilter< spoon.reflect.declaration.CtNamedElement>("SynchronizedSample")) .get(0))); Integer nbSynchronizedBlock = 2; Integer nbStatementToInsert = 2; Insertion insertionStrategy = new SynchronizedInsert(); CtStatement statementToInsert = l.getFactory().Code().createCodeSnippetStatement("TO BE INSERT"); Assertions.assertThat( sample .getElements( new spoon.reflect.visitor.filter.TypeFilter< spoon.reflect.declaration.CtElement>(CtElement.class)) .stream() .filter(insertionStrategy::match) .collect(java.util.stream.Collectors.toList())) .hasSize(nbSynchronizedBlock); sample .getElements( new spoon.reflect.visitor.filter.TypeFilter<spoon.reflect.declaration.CtElement>( CtElement.class)) .stream() .filter(insertionStrategy::match) .forEach((CtElement element) -> insertionStrategy.apply(element, statementToInsert)); System.out.println(sample); Assertions.assertThat( sample.getElements( new fil.iagl.opl.rendu.two.tools.ContainsSameElementFilter(statementToInsert))) .hasSize(nbStatementToInsert); }
public static void main(java.lang.String[] args) { java.lang.String[] spoonArgs = new java.lang.String[] { "-i", "C:/workspace/idlScrum/src/main/java", "-p", "fil.iagl.idl.processor.AnnotationProcessor", "-x" }; try { spoon.Launcher.main(spoonArgs); } catch (java.lang.Exception e) { e.printStackTrace(); } }
@Test public void testDestinationOfSpoon() throws Exception { final File binaryOutput = new File("./target/spoon/test/binary/"); final Launcher launcher = new Launcher(); launcher.getEnvironment().setShouldCompile(true); launcher.addInputResource("./src/test/java/spoon/test/api/testclasses"); launcher.setSourceOutputDirectory("./target/spooned"); launcher.setBinaryOutputDirectory(binaryOutput); launcher.run(); assertTrue(binaryOutput.exists()); }
@Test public void testDuplicateFilePlusFolder() throws Exception { // more complex case: a file is given, together with the enclosing folder try { Launcher.main( new String[] { "-i", "src/test/resources/spoon/test/api/" + File.pathSeparator + "src/test/resources/spoon/test/api/Foo.java", "-o", "target/spooned/apitest" }); } catch (IllegalArgumentException e) // from JDT { fail(); } }
@Test public void testDuplicateFolder() throws Exception { // it's possible to pass twice the same folder as parameter // the virtual folder removes the duplicate before passing to JDT try { String duplicateEntry = "src/test/resources/spoon/test/api/"; Launcher.main( new String[] { "-i", duplicateEntry + File.pathSeparator + "./" + duplicateEntry, "-o", "target/spooned/apitest" }); } catch (IllegalArgumentException e) // from JDT { fail(); } }
@Test public void testVariableAccessInNoClasspath() throws Exception { final Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.addInputResource("./src/test/resources/variable"); launcher.setSourceOutputDirectory("./target/variable/"); launcher.run(); final CtClass<Object> aClass = launcher.getFactory().Class().get("org.argouml.uml.ui.behavior.use_cases.PropPanelUseCase"); final List<CtFieldRead> elements = aClass.getElements(new TypeFilter<>(CtFieldRead.class)); for (CtFieldRead element : elements) { assertNotNull(element.getVariable()); } assertEquals( "java.lang.Class mclass = ((java.lang.Class) (org.argouml.model.ModelFacade.USE_CASE))", elements.get(0).getParent().toString()); assertEquals( "new org.argouml.uml.ui.PropPanelButton(org.argouml.uml.ui.behavior.use_cases.PropPanelUseCase.this, buttonPanel, _navUpIcon, org.argouml.i18n.Translator.localize(\"UMLMenu\", \"button.go-up\"), \"navigateNamespace\", null)", elements.get(2).getParent().toString()); }
@Test(expected = Exception.class) public void testNotValidInput() throws Exception { String invalidEntry = "does/not/exists//Foo.java"; Launcher.main(new String[] {"-i", invalidEntry, "-o", "target/spooned/apitest"}); }