Search in sources :

Example 6 with MethodMetadataBuilder

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

the class JpaUnitTestMetadata method getRemoveFromRelationMethod.

/**
 * Creates a method to test item removes from a relationship.
 *
 * @param removeMethod the {@link MethodMetadata} to test.
 * @param relationFieldName a {@link String} with the field name
 *            representing the relation on the parent entity.
 * @param childEntity the {@link JavaType} of child entity.
 * @param field the {@link FieldMetadata} of relation field.
 * @return the {@link MethodMetadata}
 */
private MethodMetadata getRemoveFromRelationMethod(RelationInfo relationInfo) {
    // Initialize local variables
    String relationFieldName = relationInfo.fieldName;
    JavaType childEntity = relationInfo.childType;
    FieldMetadata relationField = relationInfo.fieldMetadata;
    // Create test method name using target method name, child entity name and relation field
    JavaSymbolName methodName = new JavaSymbolName(String.format("%sShouldRemoveThe%sFromThe%sRelationship", relationInfo.addMethod.getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Create body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Entity entity = entityFactory.create(0);
    FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(entityFactory).getMethodName());
    // ChildEntity childEntity1 = childEntityFactory.create(0);
    FieldMetadata childEntityFactory = entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
    Validate.notNull(childEntityFactory, "Unable to locate the entity factory of %s in JpaUnitTestMetadata", childEntity.getSimpleTypeName());
    String childEntityVar1 = String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity), childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
    // ChildEntity childEntity2 = childEntityFactory.create(1);
    String childEntityVar2 = String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity), childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
    // entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
    bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, relationInfo.addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
    bodyBuilder.newLine();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // entity.REMOVE_METHOD(Collections.singleton(childEntity1));
    bodyBuilder.appendFormalLine("%s.%s(%s.singleton(%s));", this.entityVar, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(JavaType.COLLECTIONS), childEntityVar1);
    bodyBuilder.newLine();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(childEntity1.PARENT_ENTITY_ACCESSOR()).as("Check 'REMOVE_METHOD' updates the CHILD_ENTITY relationship side")
    JavaSymbolName parentEntityAccessor = BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' updates the %s relationship side\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), childEntityVar1, parentEntityAccessor, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .isNull();
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isNull();");
    bodyBuilder.indentRemove();
    // assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'REMOVE_METHOD' removes a CHILD_ENTITY from the relationship")
    JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' removes a %s from the relationship\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, relationFieldAccessorName, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .doesNotContain(childEntity1).contains(childEntity2);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".doesNotContain(%s).contains(%s);", childEntityVar1, childEntityVar2);
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    // Add throws types
    methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
    return methodBuilder.build();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) 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 7 with MethodMetadataBuilder

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

the class JpaUnitTestMetadata method getAddToRelationMethod.

/**
 * Creates a method to test item additions to a relationship.
 *
 * @param addMethod the {@link MethodMetadata} to test.
 * @param relationFieldName a {@link String} with the field name
 *            representing the relation on the parent entity.
 * @param childEntity the {@link JavaType} of child entity.
 * @param field the {@link FieldMetadata} of relation field.
 * @param relationInfo the {@link RelationInfo} of the relation field.
 * @return the {@link MethodMetadata}
 */
private MethodMetadata getAddToRelationMethod(RelationInfo relationInfo) {
    // Initialize local variables
    MethodMetadata addMethod = relationInfo.addMethod;
    String relationFieldName = relationInfo.fieldName;
    JavaType childEntity = relationInfo.childType;
    FieldMetadata relationField = relationInfo.fieldMetadata;
    // Create test method name using target method name, child entity name and relation field
    JavaSymbolName methodName = new JavaSymbolName(String.format("%sShouldAddThe%sToThe%sRelationship", addMethod.getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Create body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Entity entity = entityFactory.create(0);
    FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(entityFactory).getMethodName());
    // ChildEntity childEntity1 = childEntityFactory.create(0);
    FieldMetadata childEntityFactory = entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
    Validate.notNull(childEntityFactory, "Unable to locate the entity factory of %s in JpaUnitTestMetadata", childEntity.getSimpleTypeName());
    String childEntityVar1 = String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity), childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
    // ChildEntity childEntity2 = childEntityFactory.create(1);
    String childEntityVar2 = String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity), childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
    bodyBuilder.newLine();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
    bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
    bodyBuilder.newLine();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'ADD_METHOD_NAME' adds the FIELDNAME to the relationship")
    JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' adds the %s to the relationship\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, relationFieldAccessorName, addMethod.getMethodName(), relationFieldName);
    // .contains(childEntity1).contains(childEntity2);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".contains(%s).contains(%s);", childEntityVar1, childEntityVar2);
    bodyBuilder.indentRemove();
    // assertThat(entity).as("Check 'ADD_METHOD_NAME' updates the CHILD_ENTITY relationship side")
    bodyBuilder.appendFormalLine("%s(%s).as(\"Check '%s' updates the %s relationship side\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, addMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .isEqualTo(childEntity1.PARENT_ENTITY_ACCESSOR()).isEqualTo(childEntity2.PARENT_ENTITY_ACCESSOR());
    JavaSymbolName parentEntityAccessor = BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isEqualTo(%1$s.%3$s()).isEqualTo(%2$s.%3$s());", childEntityVar1, childEntityVar2, parentEntityAccessor);
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    // Add throws types
    methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
    return methodBuilder.build();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) 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 8 with MethodMetadataBuilder

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

the class JpaDataOnDemandMetadata method getRandomPersistentEntityMethod.

/**
 * @return the "getRandomEntity():Entity" method (never returns null)
 */
private MethodMetadata getRandomPersistentEntityMethod() {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName("getRandom" + this.entity.getSimpleTypeName());
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
        this.randomPersistentEntityMethod = userMethod;
        return userMethod;
    }
    // Create method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // init();
    bodyBuilder.appendFormalLine("init();");
    // return data.get(rnd.nextInt(data.size()));
    bodyBuilder.appendFormalLine("return %1$s().get(%2$s().nextInt(%1$s().size()));", getAccessorMethod(getDataField().build()).getMethodName(), getAccessorMethod(getRndField().build()).getMethodName());
    final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a random index.", this.entity.getSimpleTypeName()), null, String.format("%1$s a random {@link %1$s}", this.entity.getSimpleTypeName()), null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    this.randomPersistentEntityMethod = methodBuilder.build();
    return this.randomPersistentEntityMethod;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 9 with MethodMetadataBuilder

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

the class JpaDataOnDemandMetadata method getNewRandomTransientEntityMethod.

private MethodMetadata getNewRandomTransientEntityMethod() {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName("getNewRandomTransient" + this.entity.getSimpleTypeName());
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
        this.newTransientEntityMethod = userMethod;
        return userMethod;
    }
    // Create method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // int randomIndex = getSize() + rnd.nextInt(Integer.MAX_VALUE - getSize());
    bodyBuilder.appendFormalLine("int randomIndex = %1$s() + %2$s().nextInt(Integer.MAX_VALUE - %1$s());", this.sizeAccesorName, getAccessorMethod(getRndField().build()).getMethodName());
    // return factory.create(randomIndex);
    bodyBuilder.appendFormalLine("return %s().%s(randomIndex);", getAccessorMethod(getEntityFactoryField().build()).getMethodName(), this.entityFactoryMetadata.getCreateFactoryMethodName());
    final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    JavadocComment javadocComment = new JavadocComment(String.format("Creates a new transient %s in a random index out of " + "the initial list of the created entities,".concat(IOUtils.LINE_SEPARATOR).concat("with an index greater than {@link %s#getSize()} - 1."), this.entity.getSimpleTypeName(), this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), null, String.format("%1$s the generated transient {@link %1$s}", this.entity.getSimpleTypeName()), null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    this.newTransientEntityMethod = methodBuilder.build();
    return this.newTransientEntityMethod;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 10 with MethodMetadataBuilder

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

the class JpaDataOnDemandMetadata method getSpecificEntityMethod.

private MethodMetadata getSpecificEntityMethod() {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName(JpaEntityFactoryMetadata.SPECIFIC_METHOD_PREFIX + this.entity.getSimpleTypeName());
    final JavaType parameterType = JavaType.INT_PRIMITIVE;
    final List<JavaSymbolName> parameterNames = Arrays.asList(INDEX_SYMBOL);
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName, parameterType);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
        this.specificEntityMethod = userMethod;
        return userMethod;
    }
    // Create method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("init();");
    bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " < 0) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(INDEX_VAR + " = 0;");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " > (" + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1)) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(INDEX_VAR + " = " + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1;");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine("return %s().get(%s);", getAccessorMethod(getDataField().build()).getMethodName(), INDEX_VAR);
    final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add(String.format("%s the position of the {@link %s} to return", INDEX_VAR, this.entity.getSimpleTypeName()));
    JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a given index.", this.entity.getSimpleTypeName()), paramsInfo, String.format("%1$s the specific {@link %1$s}", this.entity.getSimpleTypeName()), null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    this.specificEntityMethod = methodBuilder.build();
    return this.specificEntityMethod;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Aggregations

MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)155 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)120 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)117 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)115 ArrayList (java.util.ArrayList)110 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)87 JavaType (org.springframework.roo.model.JavaType)65 SpringJavaType (org.springframework.roo.model.SpringJavaType)48 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)43 JdkJavaType (org.springframework.roo.model.JdkJavaType)34 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)30 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)14 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)14 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)12 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)7