Search in sources :

Example 86 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.

the class JavaBeanMetadata method checkIfInterfaceMethodWasImplemented.

/**
 * To check if current method was implemented on all Java classes or ITds
 * associated to this entity class.
 * If method was implemented, is not necessary to add again.
 *
 * @param methodBuilder
 * @return
 */
private boolean checkIfInterfaceMethodWasImplemented(MethodMetadataBuilder methodBuilder) {
    // ROO-3584: Obtain current declared methods
    List<MethodMetadataBuilder> declaredMethods = builder.getDeclaredMethods();
    for (MethodMetadataBuilder method : declaredMethods) {
        // If current method equals to interface method, return false
        if (method.getMethodName().equals(methodBuilder.getMethodName())) {
            return true;
        }
    }
    // ROO-3587: Obtain ALL declared methods from Java classes and ITDs.
    MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), governorTypeDetails);
    List<MethodMetadata> allMethods = memberDetails.getMethods();
    for (MethodMetadata method : allMethods) {
        // If current method equals to interface method, return false
        if (method.getMethodName().equals(methodBuilder.getMethodName())) {
            return true;
        }
    }
    return false;
}
Also used : MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 87 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.

the class RepositoryJpaIntegrationTestMetadata method getPersistGenerateIdTestMethod.

/**
 * Builds a method to test the generation of id when persisting entities.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getPersistGenerateIdTestMethod() {
    // Check if method exists on governor
    MethodMetadata method = getGovernorMethod(PERSIST_SHOULD_GENERATE_ID_METHOD_NAME);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // Pet pet = dod.getNewRandomTransientPet();
    bodyBuilder.appendFormalLine("%1$s %2$s = %3$s().getNewRandomTransient%1$s();", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(this.dodField).getMethodName());
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(pet).as("Check the Data on demand generated a new non null 'Pet'").isNotNull();
    bodyBuilder.appendFormalLine("%s(%s).as(\"Check the Data on demand generated a new non null '%s'\").isNotNull();", getNameOfJavaType(ASSERT_THAT), this.entityVar, getNameOfJavaType(this.entity));
    // assertThat(pet.getId()).as("Check the Data on demand generated a new 'Pet' whose id is null")
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check the Data on demand generated a new '%s' whose id is null\")", getNameOfJavaType(ASSERT_THAT), this.entityVar, this.identifierAccessorMethodName, getNameOfJavaType(this.entity));
    // .isNull();
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isNull();");
    bodyBuilder.indentRemove();
    // try {
    bodyBuilder.appendFormalLine("try {");
    // pet = repository.saveAndFlush(pet);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("%1$s = %2$s().saveAndFlush(%1$s);", this.entityVar, getAccessorMethod(this.repositoryField).getMethodName());
    // } catch (final ConstraintViolationException e) {
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("} catch (final %s e) {", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION_EXCEPTION));
    // final StringBuilder msg = new StringBuilder();
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("final %1$s msg = new %1$s();", getNameOfJavaType(JdkJavaType.STRING_BUILDER));
    // for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter
    bodyBuilder.appendFormalLine("for (%s<%s<?>> iter = e.getConstraintViolations().iterator(); iter", getNameOfJavaType(JdkJavaType.ITERATOR), getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
    // .hasNext();) {
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".hasNext();) {");
    // final ConstraintViolation<?> cv = iter.next();
    bodyBuilder.appendFormalLine("final %s<?> cv = iter.next();", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
    // msg.append("[").append(cv.getRootBean().getClass().getName()).append(".")
    bodyBuilder.appendFormalLine("msg.append(\"[\").append(cv.getRootBean().getClass().getName()).append(\".\")");
    // .append(cv.getPropertyPath()).append(": ").append(cv.getMessage())
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".append(cv.getPropertyPath()).append(\": \").append(cv.getMessage())");
    // .append(" (invalid value = ").append(cv.getInvalidValue()).append(")").append("]");
    bodyBuilder.appendFormalLine(".append(\" (invalid value = \").append(cv.getInvalidValue()).append(\")\").append(\"]\");");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // throw new IllegalStateException(msg.toString(), e);
    bodyBuilder.appendFormalLine("throw new %s(msg.toString(), e);", getNameOfJavaType(JdkJavaType.ILLEGAL_STATE_EXCEPTION));
    bodyBuilder.indentRemove();
    // }
    bodyBuilder.appendFormalLine("}");
    // assertThat(pet.getId()).as("Check a 'Pet' (%s) id is not null after been persisted", pet)
    bodyBuilder.appendFormalLine("%1$s(%2$s.%3$s()).as(\"Check a '%4$s' (%5$s) id is not null after been persisted\", %2$s)", getNameOfJavaType(ASSERT_THAT), this.entityVar, this.identifierAccessorMethodName, getNameOfJavaType(this.entity), "%s");
    // .isNotNull();
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isNotNull();");
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, PERSIST_SHOULD_GENERATE_ID_METHOD_NAME, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.build();
}
Also used : MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 88 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.

the class RepositoryJpaIntegrationTestMetadata method getDeleteShouldMakeEntityUnavailableTestMethod.

/**
 * Builds a method to test the deletion of entity records.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getDeleteShouldMakeEntityUnavailableTestMethod() {
    JavaSymbolName methodName = new JavaSymbolName(String.format("deleteShouldMake%sUnavailable", getNameOfJavaType(this.entity)));
    // Check if method exists on governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Long id = getRandomPetId();
    bodyBuilder.appendFormalLine("%s id = %s();", getNameOfJavaType(this.identifierType), this.getRandomIdMethodName);
    // Pet pet = repository.findOne(id);
    bodyBuilder.appendFormalLine("%s %s = %s().findOne(id);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(this.repositoryField).getMethodName());
    // Exercise
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Exercise");
    // repository.delete(pet);
    bodyBuilder.appendFormalLine("%s().delete(%s);", getAccessorMethod(this.repositoryField).getMethodName(), this.entityVar);
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(repository.findOne(id))
    bodyBuilder.appendFormalLine("%s(%s().findOne(id))", getNameOfJavaType(ASSERT_THAT), getAccessorMethod(this.repositoryField).getMethodName());
    // .as("Check the deleted 'Pet' %s is no longer available with 'findOne'", pet).isNull();
    bodyBuilder.appendFormalLine(".as(\"Check the deleted '%s' %s is no longer available with 'findOne'\", %s).isNull();", getNameOfJavaType(this.entity), "%s", this.entityVar);
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 89 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.

the class RepositoryJpaIntegrationTestMetadata method getFindAllCustomNotFilteredNotPagedTestMethod.

/**
 * Builds a method to test the find all custom method not filtered and not paged.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getFindAllCustomNotFilteredNotPagedTestMethod() {
    JavaSymbolName methodName = new JavaSymbolName(String.format("findAllCustomNotFilteredNotPagedShouldReturnAll%s", this.entityPlural));
    // Check if method exists on governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // Page<Pet> all = repository.findAll((GlobalSearch) null, new PageRequest(0, dod.getSize()));
    bodyBuilder.appendFormalLine("%s<%s> all = %s().findAll((%s) null, new %s(0, %s().getSize()));", getNameOfJavaType(SpringJavaType.PAGE), getNameOfJavaType(this.defaultReturnType), getAccessorMethod(this.repositoryField).getMethodName(), getNameOfJavaType(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH), getNameOfJavaType(SpringJavaType.PAGE_REQUEST), getAccessorMethod(this.dodField).getMethodName());
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(all.getNumberOfElements())
    bodyBuilder.appendFormalLine("%s(all.getNumberOfElements())", getNameOfJavaType(ASSERT_THAT));
    // .as("Check 'findAll' with null 'GlobalSearch' and no pagination returns all entries")
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".as(\"Check 'findAll' with null '%s' and no pagination returns all entries\")", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH));
    // .isEqualTo(dod.getSize());
    bodyBuilder.appendFormalLine(".isEqualTo(%s().getSize());", getAccessorMethod(this.dodField).getMethodName());
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 90 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata in project spring-roo by spring-projects.

the class RepositoryJpaIntegrationTestMetadata method getFindAllTestMethod.

/**
 * Builds a method to test all one method.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getFindAllTestMethod() {
    // Set method name
    final JavaSymbolName methodName = new JavaSymbolName(String.format("findAllShouldReturnAll%s", this.entityPlural));
    // Check if method exists on governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // assertThat(repository.count())
    bodyBuilder.appendFormalLine("%s(%s().count())", getNameOfJavaType(ASSERT_THAT), getAccessorMethod(this.repositoryField).getMethodName());
    // .as("Check the number of entries is not too big (250 entries). "
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".as(\"Check the number of entries is not too big (250 entries). \"");
    // + "If it is, please review the tests so it doesn't take too long to run them")
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("+ \"If it is, please review the tests so it doesn't take too long to run them\")");
    bodyBuilder.indentRemove();
    // .isLessThan(250);
    bodyBuilder.appendFormalLine(".isLessThan(250);");
    bodyBuilder.indentRemove();
    // Exercise
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Exercise");
    // List<Pet> result = repository.findAll();
    bodyBuilder.appendFormalLine("%s<%s> result = %s().findAll();", getNameOfJavaType(JavaType.LIST), getNameOfJavaType(this.entity), getAccessorMethod(this.repositoryField).getMethodName());
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(result).as("Check 'findAll' returns a not null list of entries").isNotNull();
    bodyBuilder.appendFormalLine("%s(result).as(\"Check 'findAll' returns a not null list of entries\").isNotNull();", getNameOfJavaType(ASSERT_THAT));
    // assertThat(result.size()).as("Check 'findAll' returns a not empty list of entries")
    bodyBuilder.appendFormalLine("%s(result.size()).as(\"Check 'findAll' returns a not empty list of entries\")", getNameOfJavaType(ASSERT_THAT));
    // .isGreaterThan(0);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isGreaterThan(0);");
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15