Beispiel #1
0
  @Test
  public void testRemoveAnnotation() throws Exception {
    CompilerAssert<File, File> helper = compiler("metamodel.tag");
    helper.assertCompile();

    //
    JavaFile file = helper.assertSource("metamodel", "tag", "package-info.java");
    PackageDeclaration a = file.assertPackage();
    NormalAnnotationExpr applicationDecl = (NormalAnnotationExpr) a.getAnnotations().get(0);
    applicationDecl.getPairs().clear();
    file.assertSave();

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

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    unserialize = Tools.unserialize(MetaModelState.class, ser);
    mm = (ModuleMetaModel) unserialize.metaModel;
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(2, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(TemplateMetaModel.class, events.get(0).getObject());
    assertEquals(
        Path.parse("/metamodel/tag/tags/foo.gtmpl"),
        ((TemplateMetaModel) events.get(0).getObject()).getPath());
    assertEquals(MetaModelEvent.UPDATED, events.get(1).getType());
    assertInstanceOf(ApplicationMetaModel.class, events.get(1).getObject());
  }
  @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());
  }
Beispiel #3
0
  @Test
  public void testUpdateAnnotation() throws Exception {
    CompilerAssert<File, File> helper = compiler("metamodel.tag");
    helper.assertCompile();

    //
    JavaFile file = helper.assertSource("metamodel", "tag", "package-info.java");
    PackageDeclaration a = file.assertPackage();
    NormalAnnotationExpr applicationDecl = (NormalAnnotationExpr) a.getAnnotations().get(0);
    NormalAnnotationExpr tagDecl =
        (NormalAnnotationExpr) applicationDecl.getPairs().get(0).getValue();
    boolean changed = false;
    for (MemberValuePair pair : tagDecl.getPairs()) {
      if (pair.getName().equals("path")) {
        pair.setValue(new StringLiteralExpr("bar.gtmpl"));
        changed = true;
      }
    }
    assertTrue(changed);
    file.assertSave();

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

    //
    helper.addClassPath(helper.getClassOutput()).assertCompile();
    unserialize = Tools.unserialize(MetaModelState.class, ser);
    mm = (ModuleMetaModel) unserialize.metaModel;
    List<MetaModelEvent> events = mm.getQueue().clear();
    assertEquals(3, events.size());
    assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType());
    assertInstanceOf(TemplateMetaModel.class, events.get(0).getObject());
    assertEquals(
        Path.parse("/metamodel/tag/tags/foo.gtmpl"),
        ((TemplateMetaModel) events.get(0).getObject()).getPath());
    assertEquals(MetaModelEvent.AFTER_ADD, events.get(1).getType());
    assertInstanceOf(TemplateMetaModel.class, events.get(1).getObject());
    assertEquals(
        Path.parse("/metamodel/tag/tags/bar.gtmpl"),
        ((TemplateMetaModel) events.get(1).getObject()).getPath());
    assertEquals(MetaModelEvent.UPDATED, events.get(2).getType());
    assertInstanceOf(ApplicationMetaModel.class, events.get(2).getObject());
  }
  @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());
  }
  @Test
  public void testRender() throws Exception {

    //
    driver.get(getURL().toString());
    assertEquals(RunMode.LIVE, RUN_MODE);
    WebElement elt = driver.findElement(By.id("trigger"));
    URL url = new URL(elt.getAttribute("href"));
    driver.get(url.toString());
    assertEquals("ok", driver.findElement(By.tagName("body")).getText());
    assertFalse(SAME_CL_1);
    assertFalse(SAME_CL_2);
    assertNull(SAME_CL_3);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    assertEquals(getErrorStatus(), conn.getResponseCode());
    driver.get(url.toString());
    assertEquals(
        "java.lang.RuntimeException: throwed",
        driver.findElement(By.cssSelector("div.juzu > section > p")).getText().trim());

    // Make a change
    JavaFile pkgFile = getCompiler().assertJavaSource("bridge.runmode.live.controller.A");
    pkgFile.assertSave(pkgFile.assertContent().replace("\"ok\"", "\"OK\""));
    driver.get(applicationURL().toString());
    elt = driver.findElement(By.id("trigger"));
    elt.click();
    assertEquals("OK", driver.findElement(By.tagName("body")).getText());
    assertTrue(SAME_CL_1);
    assertFalse(SAME_CL_2);
    assertNull(SAME_CL_3);

    // Now make fail with compilation error
    pkgFile.assertSave(pkgFile.assertContent().replace("public", "_public_"));

    //
    conn = (HttpURLConnection) applicationURL().openConnection();
    assertEquals(getErrorStatus(), conn.getResponseCode());
    driver.get(applicationURL().toString());
    assertNotNull(driver.findElement(By.cssSelector("div.juzu")));
    assertNotNull(elt);

    //
    pkgFile.assertSave(pkgFile.assertContent().replace("_public_", "public"));

    //
    driver.get(applicationURL().toString());
    elt = driver.findElement(By.id("trigger"));
    elt.click();
    assertEquals("OK", driver.findElement(By.tagName("body")).getText());

    // Add a new class
    JavaFile abc = getCompiler().assertAddJavaSource("bridge.runmode.live.controller.C");
    driver.get(applicationURL().toString());
    assertTrue(SAME_CL_1);
    assertFalse(SAME_CL_2);
    assertTrue(SAME_CL_3);
  }
  @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());
  }
  @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());
  }
  @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());
  }