@Before
  @Override
  public void beforeTest() throws Exception {
    super.beforeTest();
    initializeJavaProject();

    // setup validation
    queueInputLines("", "");
    getShell().execute(VALIDATION_PLUGIN_NAME + " setup");

    // create a class
    assertTrue(getProject().hasFacet(JavaSourceFacet.class));

    final JavaSourceFacet javaSourceFacet = getProject().getFacet(JavaSourceFacet.class);
    assertNotNull(javaSourceFacet);

    final JavaClass fooClass =
        JavaParser.create(JavaClass.class)
            .setPackage(javaSourceFacet.getBasePackage())
            .setName(CLASS_NAME);
    fooClass.addField("private String " + PROPERTY_NAME + ";");
    fooClass.addMethod("public String " + PROPERTY_ACESSOR_NAME + "(){return foo;}");

    JavaResource fooResource = javaSourceFacet.saveJavaSource(fooClass);
    assertNotNull(fooResource);

    // pick-up the created resource
    getShell().execute("pick-up " + fooResource.getFullyQualifiedName());
  }
예제 #2
0
  private void createHashCodeAndEquals(final JavaClass javaClass) {
    javaClass
        .addMethod(
            "public boolean equals(Object that) { "
                + "if (this == that) { return true; } "
                + "if (that == null) { return false; } "
                + "if (getClass() != that.getClass()) { return false; } "
                + "if (id != null) { return id.equals((("
                + javaClass.getName()
                + ") that).id); } "
                + "return super.equals(that); "
                + "}")
        .addAnnotation(Override.class);

    javaClass
        .addMethod(
            "public int hashCode() { "
                + "if (id != null) { return id.hashCode(); } "
                + "return super.hashCode(); }")
        .addAnnotation(Override.class);
  }
  @Command(value = "new-element", help = "Create a new WebElement in the current class")
  public void newElement(
      @Option(required = true, name = "named", help = "the element name") String name,
      @Option(required = true, name = "findby", help = "the locator name") FindByType findBy,
      @Option(required = true, name = "value", help = "the locator value") String value,
      @Option(required = false, name = "fragmentClass", help = "the Page Fragment class")
          Resource<?> fragmentClass,
      final PipeOut out)
      throws Exception {
    final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

    JavaClass javaClass = getJavaClass();

    if (javaClass.hasField(name)) {
      throw new IllegalStateException("Class already has a field named [" + name + "]");
    }

    Field<JavaClass> field = javaClass.addField();
    field.setName(name).setPrivate();
    if (fragmentClass != null) {

      JavaClass javaFragment =
          JavaParser.parse(JavaClass.class, fragmentClass.getResourceInputStream());
      if (javaFragment == null) {
        throw new IllegalStateException(
            "Class notfound in test resources [" + fragmentClass.getFullyQualifiedName() + "]");
      }
      field.setType(javaFragment.getQualifiedName());
    } else {
      field.setType("org.openqa.selenium.WebElement");
    }
    Annotation<JavaClass> annotation =
        field.addAnnotation("org.jboss.arquillian.graphene.enricher.findby.FindBy");
    annotation.setStringValue(findBy.name(), value);
    javaClass
        .addMethod()
        .setReturnType(field.getTypeInspector().toString())
        .setName("get" + Strings.capitalize(name))
        .setPublic()
        .setBody("return this." + name + ";");

    java.saveTestJavaSource(javaClass);

    shell.println("Created element  [" + field.getName() + "]");
  }
예제 #4
0
  @Override
  public Result execute(UIExecutionContext context) throws Exception {
    UIContext uiContext = context.getUIContext();
    JavaResource resource = (JavaResource) uiContext.getInitialSelection().get();
    String name = named.getValue();
    String fieldName = conversationFieldName.getValue();
    String beginName = beginMethodName.getValue();
    String endName = endMethodName.getValue();
    Boolean overwriteValue = overwrite.getValue();
    UIOutput output = uiContext.getProvider().getOutput();
    if (resource.exists()) {
      if (resource.getJavaSource().isClass()) {
        JavaClass javaClass = (JavaClass) resource.getJavaSource();

        if (javaClass.hasField(fieldName)
            && !javaClass.getField(fieldName).isType(Conversation.class)) {
          if (overwriteValue) {
            javaClass.removeField(javaClass.getField(fieldName));
          } else {
            return Results.fail("Field [" + fieldName + "] already exists.");
          }
        }
        if (javaClass.hasMethodSignature(beginName)
            && (javaClass.getMethod(beginName).getParameters().size() == 0)) {
          if (overwriteValue) {
            javaClass.removeMethod(javaClass.getMethod(beginName));
          } else {
            return Results.fail("Method [" + beginName + "] exists.");
          }
        }
        if (javaClass.hasMethodSignature(endName)
            && (javaClass.getMethod(endName).getParameters().size() == 0)) {
          if (overwriteValue) {
            javaClass.removeMethod(javaClass.getMethod(endName));
          } else {
            return Results.fail("Method [" + endName + "] exists.");
          }
        }

        javaClass
            .addField()
            .setPrivate()
            .setName(fieldName)
            .setType(Conversation.class)
            .addAnnotation(Inject.class);

        Method<JavaClass> beginMethod =
            javaClass.addMethod().setName(beginName).setReturnTypeVoid().setPublic();
        if (Strings.isNullOrEmpty(name)) {
          beginMethod.setBody(fieldName + ".begin();");
        } else {
          beginMethod.setBody(fieldName + ".begin(\"" + name + "\");");
        }

        if (timeout.getValue() != null) {
          beginMethod.setBody(
              beginMethod.getBody() + "\n" + fieldName + ".setTimeout(" + timeout + ");");
        }

        javaClass
            .addMethod()
            .setName(endName)
            .setReturnTypeVoid()
            .setPublic()
            .setBody(fieldName + ".end();");

        if (javaClass.hasSyntaxErrors()) {
          output.err().println("Modified Java class contains syntax errors:");
          for (SyntaxError error : javaClass.getSyntaxErrors()) {
            output.err().print(error.getDescription());
          }
        }

        resource.setContents(javaClass);
      } else {
        return Results.fail(
            "Must operate on a Java Class file, not an ["
                + resource.getJavaSource().getSourceType()
                + "]");
      }
    }
    return Results.success("Conversation block created");
  }