@Command(value = "new-page", help = "Create a new Graphene page element class")
  public void createTest(
      @Option(
              required = false,
              help = "the package in which to build this page class",
              description = "source package",
              type = PromptType.JAVA_PACKAGE,
              name = "package")
          final String packageName,
      @Option(required = true, name = "named", help = "the page class name") String name,
      @Option(
              required = false,
              name = "type",
              help = "Page Object or Page Fragment",
              defaultValue = "page")
          PageType type,
      final PipeOut out)
      throws Exception {
    if (PageType.page.equals(type) && !name.endsWith("Page")) {
      name = name + "Page";
    }
    if (PageType.fragment.equals(type) && !name.endsWith("Fragment")) {
      name = name + "Fragment";
    }
    final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

    String pagePackage;

    if ((packageName != null) && !"".equals(packageName)) {
      pagePackage = packageName;
    } else if (getPackagePortionOfCurrentDirectory() != null) {
      pagePackage = getPackagePortionOfCurrentDirectory();
    } else {
      pagePackage =
          shell.promptCommon(
              "In which package you'd like to create this Page, or enter for default",
              PromptType.JAVA_PACKAGE,
              java.getBasePackage() + ".pages");
    }

    JavaClass javaClass =
        JavaParser.create(JavaClass.class).setPackage(pagePackage).setName(name).setPublic();

    if (PageType.fragment.equals(type)) {
      Field<JavaClass> field = javaClass.addField();
      field
          .setName("root")
          .setPrivate()
          .setType("org.openqa.selenium.WebElement")
          .addAnnotation("org.jboss.arquillian.graphene.spi.annotations.Root");
    }
    JavaResource javaFileLocation = java.saveTestJavaSource(javaClass);

    shell.println("Created " + type + " [" + javaClass.getQualifiedName() + "]");
    pickup.fire(new PickupResource(javaFileLocation));
  }
  @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() + "]");
  }
  public List<String> searchForAllResourcesFQN(String sourcePackage, boolean recursive) {
    try {
      System.out.println("recursive ?: " + recursive);
      List<String> resourcesFQNs = new ArrayList<String>();
      // get resource Files
      List<File> resources =
          recursive == true
              ? this.searchForAllResourcesRecursive(sourcePackage)
              : this.searchForAllResources(sourcePackage);
      System.out.println("resourcesFQN: " + resources);

      // make sure all resources exist and are Java Types
      for (File file : resources) {
        JavaClass js = JavaParser.parse(JavaClass.class, file);
        if (js != null) {
          resourcesFQNs.add(js.getQualifiedName());
        }
      }
      return resourcesFQNs;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
示例#4
0
  @SuppressWarnings("unchecked")
  @DefaultCommand(help = "Create a JPA @Entity")
  public void newEntity(
      @Option(required = true, name = "named", description = "The @Entity name")
          final String entityName,
      @Option(
              required = false,
              name = "package",
              type = PromptType.JAVA_PACKAGE,
              description = "The package name")
          final String packageName)
      throws Throwable {
    final PersistenceFacet jpa = project.getFacet(PersistenceFacet.class);
    final JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

    String entityPackage;

    if ((packageName != null) && !"".equals(packageName)) {
      entityPackage = packageName;
    } else if (getPackagePortionOfCurrentDirectory() != null) {
      entityPackage = getPackagePortionOfCurrentDirectory();
    } else {
      entityPackage =
          shell.promptCommon(
              "In which package you'd like to create this @Entity, or enter for default",
              PromptType.JAVA_PACKAGE,
              jpa.getEntityPackage());
    }

    JavaClass javaClass =
        JavaParser.create(JavaClass.class)
            .setPackage(entityPackage)
            .setName(entityName)
            .setPublic()
            .addAnnotation(Entity.class)
            .getOrigin()
            .addInterface(Serializable.class);

    Field<JavaClass> id = javaClass.addField("private Long id = null;");
    id.addAnnotation(Id.class);
    id.addAnnotation(GeneratedValue.class).setEnumValue("strategy", GenerationType.AUTO);
    id.addAnnotation(Column.class)
        .setStringValue("name", "id")
        .setLiteralValue("updatable", "false")
        .setLiteralValue("nullable", "false");

    Field<JavaClass> version = javaClass.addField("private int version = 0;");
    version.addAnnotation(Version.class);
    version.addAnnotation(Column.class).setStringValue("name", "version");

    Refactory.createGetterAndSetter(javaClass, id);
    Refactory.createGetterAndSetter(javaClass, version);
    Refactory.createToStringFromFields(javaClass, id);
    createHashCodeAndEquals(javaClass);

    JavaResource javaFileLocation = java.saveJavaSource(javaClass);

    shell.println("Created @Entity [" + javaClass.getQualifiedName() + "]");

    /** Pick up the generated resource. */
    shell.execute("pick-up " + javaFileLocation.getFullyQualifiedName());
  }