Example #1
0
  @Test
  public void testRefactorPackageName() throws Exception {
    CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller");
    helper.assertCompile();

    //
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModel mm = Tools.unserialize(MetaModel.class, ser);
    mm.getQueue().clear();
    Tools.serialize(mm, ser);

    //
    File file = helper.getSourcePath().getPath("model", "meta", "controller", "A.java");
    File sub = new File(file.getParentFile(), "sub");
    assertTrue(sub.mkdir());
    File tmp = new File(sub, file.getName());
    assertTrue(file.renameTo(tmp));
    JavaFile javaFile = helper.assertJavaFile("model", "meta", "controller", "sub", "A.java");
    javaFile
        .assertCompilationUnit()
        .getPackage()
        .setName(ASTHelper.createNameExpr("model.meta.controller.sub"));
    javaFile.assertSave();

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    mm = Tools.unserialize(MetaModel.class, ser);

    //
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(5, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertEquals(
        ElementHandle.Method.create(
            new FQN("model.meta.controller.A"), "index", Collections.<String>emptyList()),
        ((MethodMetaModel) events.get(0).getObject()).getHandle());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(1).getType());
    assertEquals(
        ElementHandle.Class.create(new FQN("model.meta.controller.A")),
        ((ControllerMetaModel) events.get(1).getObject()).getHandle());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(2).getType());
    assertEquals(
        ElementHandle.Class.create(new FQN("model.meta.controller.sub.A")),
        ((ControllerMetaModel) events.get(2).getObject()).getHandle());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(3).getType());
    assertEquals(
        ElementHandle.Method.create(
            new FQN("model.meta.controller.sub.A"), "index", Collections.<String>emptyList()),
        ((MethodMetaModel) events.get(3).getObject()).getHandle());
    assertEquals(MetaModelEvent.UPDATED, events.get(4).getType());
    assertEquals(
        ElementHandle.Class.create(new FQN("model.meta.controller.sub.A")),
        ((ControllerMetaModel) events.get(4).getObject()).getHandle());
  }
Example #2
0
  @Test
  public void testRemoveAnnotation() throws Exception {
    CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller");
    helper.assertCompile();
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModel mm = Tools.unserialize(MetaModel.class, ser);
    mm.getQueue().clear();
    Tools.serialize(mm, ser);

    //
    JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java");
    ClassOrInterfaceDeclaration a = file.assertDeclaration();
    MethodDeclaration decl = (MethodDeclaration) a.getMembers().get(0);
    decl.getAnnotations().clear();
    file.assertSave();

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    mm = Tools.unserialize(MetaModel.class, ser);

    //
    JSON expected =
        json()
            .set(
                "applications",
                json()
                    .list(
                        "values",
                        json()
                            .list("controllers")
                            .set("fqn", "model.meta.controller.ControllerApplication")
                            .set("handle", "ElementHandle.Package[qn=model.meta.controller]")
                            .list("templates")));
    assertEquals(expected, mm.toJSON());

    //
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(2, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(MethodMetaModel.class, events.get(0).getObject());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(1).getType());
    assertInstanceOf(ControllerMetaModel.class, events.get(1).getObject());
  }
Example #3
0
  @Test
  public void testRemoveOverloadedMethod() throws Exception {
    CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller");
    JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java");
    ClassOrInterfaceDeclaration a = file.assertDeclaration();
    MethodDeclaration index = new MethodDeclaration(Modifier.PUBLIC, ASTHelper.VOID_TYPE, "index");
    ASTHelper.addParameter(
        index, ASTHelper.createParameter(new ClassOrInterfaceType(String.class.getName()), "s"));
    index.setAnnotations(
        Collections.<AnnotationExpr>singletonList(
            new NormalAnnotationExpr(
                ASTHelper.createNameExpr(View.class.getName()),
                Collections.<MemberValuePair>emptyList())));
    index.setBody(new BlockStmt());
    a.getMembers().add(index);
    file.assertSave();
    helper.assertCompile();

    //
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModel mm = Tools.unserialize(MetaModel.class, ser);
    mm.getQueue().clear();
    Tools.serialize(mm, ser);

    //
    assertTrue(a.getMembers().remove(index));
    file.assertSave();

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    mm = Tools.unserialize(MetaModel.class, ser);

    //
    JSON expected =
        json()
            .set(
                "applications",
                json()
                    .list(
                        "values",
                        json()
                            .list(
                                "controllers",
                                json()
                                    .set(
                                        "handle",
                                        "ElementHandle.Class[fqn=model.meta.controller.A]")
                                    .list(
                                        "methods",
                                        json()
                                            .set(
                                                "handle",
                                                "ElementHandle.Method[fqn=model.meta.controller.A,name=index,parameterTypes[]]")
                                            .set("id", null)
                                            .set("name", "index")
                                            .list("parameters")
                                            .set("phase", "RENDER")))
                            .set("fqn", "model.meta.controller.ControllerApplication")
                            .set("handle", "ElementHandle.Package[qn=model.meta.controller]")
                            .list("templates")));
    assertEquals(expected, mm.toJSON());

    //
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(1, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(MethodMetaModel.class, events.get(0).getObject());
  }
Example #4
0
  @Test
  public void testAddMethod() throws Exception {
    CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller");
    JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java");
    ClassOrInterfaceDeclaration a = file.assertDeclaration();
    MethodDeclaration decl = (MethodDeclaration) a.getMembers().get(0);
    assertTrue(a.getMembers().remove(decl));
    file.assertSave();
    helper.assertCompile();

    //
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModel mm = Tools.unserialize(MetaModel.class, ser);
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(1, events.size());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(0).getType());
    assertTrue(events.get(0).getObject() instanceof ApplicationMetaModel);
    Tools.serialize(mm, ser);

    //
    a.getMembers().add(decl);
    file.assertSave();
    helper.addClassPath(helper.getClassOutput()).assertCompile();

    //
    mm = Tools.unserialize(MetaModel.class, ser);

    //
    JSON expected =
        json()
            .set(
                "applications",
                json()
                    .list(
                        "values",
                        json()
                            .list(
                                "controllers",
                                json()
                                    .set(
                                        "handle",
                                        "ElementHandle.Class[fqn=model.meta.controller.A]")
                                    .list(
                                        "methods",
                                        json()
                                            .set(
                                                "handle",
                                                "ElementHandle.Method[fqn=model.meta.controller.A,name=index,parameterTypes[]]")
                                            .set("id", null)
                                            .set("name", "index")
                                            .list("parameters")
                                            .set("phase", "RENDER")))
                            .set("fqn", "model.meta.controller.ControllerApplication")
                            .set("handle", "ElementHandle.Package[qn=model.meta.controller]")
                            .list("templates")));
    assertEquals(expected, mm.toJSON());

    //
    events = mm.getQueue().clear();
    assertEquals(3, events.size());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(0).getType());
    assertInstanceOf(ControllerMetaModel.class, events.get(0).getObject());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(1).getType());
    assertInstanceOf(MethodMetaModel.class, events.get(1).getObject());
    assertEquals(MetaModelEvent.UPDATED, events.get(2).getType());
    assertInstanceOf(ControllerMetaModel.class, events.get(2).getObject());
  }
Example #5
0
  @Test
  public void testChangeAnnotation() throws Exception {
    CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller");
    helper.assertCompile();
    File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser");
    MetaModel mm = Tools.unserialize(MetaModel.class, ser);
    mm.getQueue().clear();
    Tools.serialize(mm, ser);

    //
    JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java");
    ClassOrInterfaceDeclaration a = file.assertDeclaration();
    MethodDeclaration decl = (MethodDeclaration) a.getMembers().get(0);
    decl.getAnnotations().get(0).setName(ASTHelper.createNameExpr(Action.class.getName()));
    file.assertSave();

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    mm = Tools.unserialize(MetaModel.class, ser);

    //
    JSON expected =
        json()
            .set(
                "applications",
                json()
                    .list(
                        "values",
                        json()
                            .list(
                                "controllers",
                                json()
                                    .set(
                                        "handle",
                                        "ElementHandle.Class[fqn=model.meta.controller.A]")
                                    .list(
                                        "methods",
                                        json()
                                            .set(
                                                "handle",
                                                "ElementHandle.Method[fqn=model.meta.controller.A,name=index,parameterTypes[]]")
                                            .set("id", null)
                                            .set("name", "index")
                                            .list("parameters")
                                            .set("phase", "ACTION")))
                            .set("fqn", "model.meta.controller.ControllerApplication")
                            .set("handle", "ElementHandle.Package[qn=model.meta.controller]")
                            .list("templates")));
    assertEquals(expected, mm.toJSON());

    //
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(5, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(MethodMetaModel.class, events.get(0).getObject());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(1).getType());
    assertInstanceOf(ControllerMetaModel.class, events.get(1).getObject());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(2).getType());
    assertInstanceOf(ControllerMetaModel.class, events.get(2).getObject());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(3).getType());
    assertInstanceOf(MethodMetaModel.class, events.get(3).getObject());
    assertEquals(MetaModelEvent.UPDATED, events.get(4).getType());
    assertInstanceOf(ControllerMetaModel.class, events.get(4).getObject());
  }