Search in sources :

Example 56 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class JavaParserTypeParsingServiceTest method testGetTypeFromStringWhenFileContainsNoTypes.

@Test
public void testGetTypeFromStringWhenFileContainsNoTypes() {
    // Set up
    final JavaType mockTargetType = mock(JavaType.class);
    // Invoke
    final ClassOrInterfaceTypeDetails locatedType = typeParsingService.getTypeFromString(EMPTY_FILE, DECLARED_BY_MID, mockTargetType);
    // Check
    assertNull(locatedType);
}
Also used : JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 57 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class NewUpdateCompilationUnitTest method testSimpleClassAddAnnotation.

@Test
public void testSimpleClassAddAnnotation() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(new JavaType("org.springframework.roo.addon.javabean.addon.RooToString"));
    final ClassOrInterfaceTypeDetails newSimpleInterfaceDetails = addAnnotation(simpleInterfaceDetails, annotationBuilder.build());
    // Invoke
    final String result = typeParsingService.updateAndGetCompilationUnitContents(file.getCanonicalPath(), newSimpleInterfaceDetails);
    saveResult(file, result, "-addedAnnotation");
    checkSimpleClass(result);
    assertTrue(result.contains("import org.springframework.roo.addon.javabean.addon.RooToString;"));
    assertTrue(result.contains("@RooToString"));
    // Invoke again
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final String result2 = typeParsingService.updateAndGetCompilationUnitContents(file.getCanonicalPath(), simpleInterfaceDetails2);
    saveResult(file, result2, "-addedAnnotation2");
    checkSimpleClass(result2);
    assertTrue(result2.contains("import org.springframework.roo.addon.javabean.addon.RooToString;"));
    assertTrue(result2.contains("@RooToString"));
}
Also used : JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Test(org.junit.Test)

Example 58 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class NewUpdateCompilationUnitTest method testSimpleClassAddField.

@Test
public void testSimpleClassAddField() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(SIMPLE_CLASS_DECLARED_BY_MID, Modifier.PRIVATE, new JavaSymbolName("newFieldAddedByCode"), new JavaType(String.class), "\"Create by code\"");
    final ClassOrInterfaceTypeDetails newSimpleInterfaceDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
    // Invoke
    final String result = typeParsingService.updateAndGetCompilationUnitContents(file.getCanonicalPath(), newSimpleInterfaceDetails);
    saveResult(file, result, "-addedField");
    checkSimpleClass(result);
    assertTrue(result.contains("private String newFieldAddedByCode = \"Create by code\";"));
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 59 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class SeleniumOperationsImpl method generateTest.

/**
 * Creates a new Selenium testcase
 *
 * @param controller the JavaType of the controller under test (required)
 * @param name the name of the test case (optional)
 * @param serverURL the URL of the Selenium server (optional)
 */
@Override
public void generateTest(final JavaType controller, String name, String serverURL) {
    Validate.notNull(controller, "Controller type required");
    final ClassOrInterfaceTypeDetails controllerTypeDetails = typeLocationService.getTypeDetails(controller);
    Validate.notNull(controllerTypeDetails, "Class or interface type details for type '%s' could not be resolved", controller);
    final LogicalPath path = PhysicalTypeIdentifier.getPath(controllerTypeDetails.getDeclaredByMetadataId());
    final String webScaffoldMetadataIdentifier = WebScaffoldMetadata.createIdentifier(controller, path);
    final WebScaffoldMetadata webScaffoldMetadata = (WebScaffoldMetadata) metadataService.get(webScaffoldMetadataIdentifier);
    Validate.notNull(webScaffoldMetadata, "Web controller '%s' does not appear to be an automatic, scaffolded controller", controller.getFullyQualifiedTypeName());
    // allow the creation of new instances for the form backing object
    if (!webScaffoldMetadata.getAnnotationValues().isCreate()) {
        LOGGER.warning("The controller you specified does not allow the creation of new instances of the form backing object. No Selenium tests created.");
        return;
    }
    if (!serverURL.endsWith("/")) {
        serverURL = serverURL + "/";
    }
    final JavaType formBackingType = webScaffoldMetadata.getAnnotationValues().getFormBackingObject();
    final String relativeTestFilePath = "selenium/test-" + formBackingType.getSimpleTypeName().toLowerCase() + ".xhtml";
    final String seleniumPath = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_WEBAPP, relativeTestFilePath);
    final InputStream templateInputStream = FileUtils.getInputStream(getClass(), "selenium-template.xhtml");
    Validate.notNull(templateInputStream, "Could not acquire selenium.xhtml template");
    final Document document = XmlUtils.readXml(templateInputStream);
    final Element root = (Element) document.getLastChild();
    if (root == null || !"html".equals(root.getNodeName())) {
        throw new IllegalArgumentException("Could not parse selenium test case template file!");
    }
    name = name != null ? name : "Selenium test for " + controller.getSimpleTypeName();
    XmlUtils.findRequiredElement("/html/head/title", root).setTextContent(name);
    XmlUtils.findRequiredElement("/html/body/table/thead/tr/td", root).setTextContent(name);
    final Element tbody = XmlUtils.findRequiredElement("/html/body/table/tbody", root);
    tbody.appendChild(openCommand(document, serverURL + projectOperations.getProjectName(projectOperations.getFocusedModuleName()) + "/" + webScaffoldMetadata.getAnnotationValues().getPath() + "?form"));
    final ClassOrInterfaceTypeDetails formBackingTypeDetails = typeLocationService.getTypeDetails(formBackingType);
    Validate.notNull(formBackingType, "Class or interface type details for type '%s' could not be resolved", formBackingType);
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), formBackingTypeDetails);
    // Add composite PK identifier fields if needed
    for (final FieldMetadata field : persistenceMemberLocator.getEmbeddedIdentifierFields(formBackingType)) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(field);
            final String fieldName = field.getFieldName().getSymbolName();
            fieldBuilder.setFieldName(new JavaSymbolName(fieldName + "." + fieldName));
            tbody.appendChild(typeCommand(document, fieldBuilder.build()));
        }
    }
    // Add all other fields
    final List<FieldMetadata> fields = webMetadataService.getScaffoldEligibleFieldMetadata(formBackingType, memberDetails, null);
    for (final FieldMetadata field : fields) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            tbody.appendChild(typeCommand(document, field));
        }
    }
    tbody.appendChild(clickAndWaitCommand(document, "//input[@id = 'proceed']"));
    // Add verifications for all other fields
    for (final FieldMetadata field : fields) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            tbody.appendChild(verifyTextCommand(document, formBackingType, field));
        }
    }
    fileManager.createOrUpdateTextFileIfRequired(seleniumPath, XmlUtils.nodeToString(document), false);
    manageTestSuite(relativeTestFilePath, name, serverURL);
    // Install selenium-maven-plugin
    installMavenPlugin();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) LogicalPath(org.springframework.roo.project.LogicalPath) Document(org.w3c.dom.Document) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) WebScaffoldMetadata(org.springframework.roo.addon.web.mvc.controller.addon.scaffold.WebScaffoldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 60 with JavaType

use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.

the class WebFlowCommands method getClassPossibleValues.

@CliOptionAutocompleteIndicator(command = "web flow", param = "class", help = "You should specify an existing and serializable class for option " + "'--class'.", validate = false)
public List<String> getClassPossibleValues(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("class");
    List<String> allPossibleValues = new ArrayList<String>();
    // Getting all existing entities
    Set<ClassOrInterfaceTypeDetails> domainClassesInProject = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY, RooJavaType.ROO_DTO);
    for (ClassOrInterfaceTypeDetails classDetails : domainClassesInProject) {
        // Check if class implements serializable (needed for WebFlow)
        boolean isSerializable = false;
        // First, chech for @RooSerializable
        if (classDetails.getAnnotation(RooJavaType.ROO_SERIALIZABLE) != null) {
            isSerializable = true;
        }
        // Check for the explicit 'implements Serializable'
        if (!isSerializable) {
            List<JavaType> implementsTypes = classDetails.getImplementsTypes();
            for (JavaType type : implementsTypes) {
                if (type.equals(JdkJavaType.SERIALIZABLE)) {
                    isSerializable = true;
                    break;
                }
            }
        }
        if (isSerializable) {
            // Add to possible values
            String name = replaceTopLevelPackageString(classDetails, currentText);
            if (!allPossibleValues.contains(name)) {
                allPossibleValues.add(name);
            }
        }
    }
    if (allPossibleValues.isEmpty()) {
        // Any entity or DTO in project is serializable
        LOGGER.info("Any auto-complete value offered because the project hasn't any entity " + "or DTO which implement Serializable");
    }
    return allPossibleValues;
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Aggregations

JavaType (org.springframework.roo.model.JavaType)411 RooJavaType (org.springframework.roo.model.RooJavaType)212 ArrayList (java.util.ArrayList)142 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)133 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)129 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)114 JdkJavaType (org.springframework.roo.model.JdkJavaType)110 SpringJavaType (org.springframework.roo.model.SpringJavaType)101 JpaJavaType (org.springframework.roo.model.JpaJavaType)83 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)78 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)76 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)72 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)70 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)65 LogicalPath (org.springframework.roo.project.LogicalPath)62 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)60 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)59 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)38 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)35 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)30