/** * Builds a method annotated with `@Before` which checks if data-on-demand * field has initialized correctly. * * @return {@link MethodMetadata} */ private MethodMetadata getBeforeMethod() { MethodMetadata method = getGovernorMethod(BEFORE_METHOD_NAME); if (method != null) { return method; } InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); // assertThat(dod.getRandomPet()) bodyBuilder.appendFormalLine("%s(%s().%s())", getNameOfJavaType(ASSERT_THAT, true), getAccessorMethod(this.dodField).getMethodName(), this.getRandomMethodName); // .as("Check data on demand for 'Pet' initializes correctly by getting a random Pet") bodyBuilder.indent(); bodyBuilder.appendFormalLine( ".as(\"Check data on demand for '%1$s' initializes correctly by getting a random %1$s\")", getNameOfJavaType(this.entity)); // .isNotNull(); bodyBuilder.appendFormalLine(".isNotNull();"); bodyBuilder.reset(); MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, BEFORE_METHOD_NAME, JavaType.VOID_PRIMITIVE, bodyBuilder); // Add @Before methodBuilder.addAnnotation(new AnnotationMetadataBuilder(BEFORE)); return methodBuilder.build(); }
/** * Build remove method body for OneToOne relation * * @param field * @param mappedBy * @param builder */ private void buildRemoveOneToOneBody(final FieldMetadata field, final String mappedBy, final InvocableMemberBodyBuilder builder) { final String fieldName = field.getFieldName().getSymbolName(); // Build toString method body /* * if (this.{prop} != null) { * {prop}.set{mappedBy}(null); * } * this.{prop} = null; */ // if (this.{prop} != null) { builder.appendFormalLine(String.format("if (this.%s != null) {", fieldName)); // {prop}.set{mappedBy}(null); builder.indent(); builder.appendFormalLine(String.format("%s.set%s(null);", fieldName, StringUtils.capitalize(mappedBy))); // } builder.indentRemove(); builder.appendFormalLine("}"); // this.{prop} = null; builder.appendFormalLine(String.format("this.%s = null;", fieldName)); }
bodyBuilder.indent(); bodyBuilder.appendFormalLine( ".as(\"Check 'findAll' with null '%s' and no pagination returns all entries\")",
private InvocableMemberBodyBuilder getSingularEntityMutator(final FieldMetadata field, final JavaSymbolName hiddenIdFieldName) { final String entityName = field.getFieldName().getSymbolName(); final String entityIdName = hiddenIdFieldName.getSymbolName(); final String identifierMethodName = getIdentifierMethodName(field).getSymbolName(); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("if (" + entityName + " != null) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine("if (" + entityName + "." + identifierMethodName + " () == null) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(entityName + ".persist();"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine(THIS_DOT + entityIdName + " = " + entityName + "." + identifierMethodName + "();"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("} else {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(THIS_DOT + entityIdName + " = null;"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine(THIS_DOT + entityName + " = " + entityName + ";"); return bodyBuilder; }
private InvocableMemberBodyBuilder getSingularEntityAccessor(final FieldMetadata field, final JavaSymbolName hiddenIdFieldName) { final String entityName = field.getFieldName().getSymbolName(); final String entityIdName = hiddenIdFieldName.getSymbolName(); final String simpleFieldTypeName = field.getFieldType().getSimpleTypeName(); final String identifierMethodName = getIdentifierMethodName(field).getSymbolName(); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("if (this." + entityIdName + " != null) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine("if (this." + entityName + " == null || this." + entityName + "." + identifierMethodName + "() != this." + entityIdName + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(THIS_DOT + entityName + " = " + simpleFieldTypeName + ".find" + simpleFieldTypeName + "(this." + entityIdName + ");"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("} else {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(THIS_DOT + entityName + " = null;"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("return this." + entityName + ";"); return bodyBuilder; }
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("if (!(" + OBJECT_NAME + " instanceof " + typeName + ")) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine("return false;"); bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); bodyBuilder.appendFormalLine("if (this == " + OBJECT_NAME + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine("return true;"); bodyBuilder.indentRemove();
builder.indent(); builder.appendFormalLine(String.format("this.%s.add(item);", fieldName));
builder.indent(); builder.appendFormalLine(String.format("this.%s.remove(item);", fieldName));
builder.indent(); builder.appendFormalLine(String.format("this.%s.add(item);", fieldName));
builder.indent(); builder.appendFormalLine(String.format("this.%s.remove(item);", fieldName));
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");"); bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue.doubleValue() + suffix + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = " + maxValue.doubleValue() + suffix + ";");
private String getDigitsBody(final FieldMetadata field, final AnnotationMetadata digitsAnnotation, final String suffix) { final String fieldName = field.getFieldName().getSymbolName(); final JavaType fieldType = field.getFieldType(); final Integer integerValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("integer")).getValue(); final Integer fractionValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("fraction")).getValue(); final BigDecimal maxValue = new BigDecimal(StringUtils.rightPad("9", integerValue, '9') + "." + StringUtils.rightPad("9", fractionValue, '9')); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); if (fieldType.equals(BIG_DECIMAL)) { bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");"); } else { bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue.doubleValue() + suffix + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = " + maxValue.doubleValue() + suffix + ";"); } bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); return bodyBuilder.getOutput(); }
bodyBuilder.indent(); bodyBuilder.appendFormalLine("query.where(searchCondition);");
+ instantiableCollection + "();"); bodyBuilder.appendFormalLine("for (Key key : " + entityIdsName + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(simpleCollectionElementTypeName + " entity = " + simpleCollectionElementTypeName + ".find" + simpleCollectionElementTypeName + "(key.getId());"); bodyBuilder.appendFormalLine("if (entity != null) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(localEnitiesName + ".add(entity);"); bodyBuilder.indentRemove();
bodyBuilder.indent(); bodyBuilder .appendFormalLine(".as(\"Check result number is not greater than the page size\").isLessThanOrEqualTo(3);");