@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());
  }
Example #2
0
 @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());
 }
 @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);
 }
Example #4
0
  @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());
  }
Example #5
0
  @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());
  }