/** @return a test for the flush method, if available and requested (may return null) */ private MethodMetadataBuilder getFlushMethodTest( final MethodMetadata versionAccessorMethod, final MethodMetadata identifierAccessorMethod, final MemberTypeAdditions flushMethod, final MemberTypeAdditions findMethod) { if (!annotationValues.isFlush() || versionAccessorMethod == null || identifierAccessorMethod == null || flushMethod == null || findMethod == null) { // User does not want this method, or core dependencies are missing return null; } // Prepare method signature JavaSymbolName methodName = new JavaSymbolName("test" + StringUtils.capitalize(flushMethod.getMethodName())); if (governorHasMethod(methodName)) { return null; } JavaType versionType = versionAccessorMethod.getReturnType(); builder .getImportRegistrationResolver() .addImports(identifierAccessorMethod.getReturnType(), versionType); List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>(); annotations.add(new AnnotationMetadataBuilder(TEST)); final String entityName = annotationValues.getEntity().getSimpleTypeName(); InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine( entityName + " obj = dod." + dataOnDemandMetadata.getRandomPersistentEntityMethod().getMethodName().getSymbolName() + "();"); bodyBuilder.appendFormalLine( "Assert.assertNotNull(\"Data on demand for '" + entityName + "' failed to initialize correctly\", obj);"); bodyBuilder.appendFormalLine( identifierAccessorMethod.getReturnType().getSimpleTypeName() + " id = obj." + identifierAccessorMethod.getMethodName().getSymbolName() + "();"); bodyBuilder.appendFormalLine( "Assert.assertNotNull(\"Data on demand for '" + entityName + "' failed to provide an identifier\", id);"); bodyBuilder.appendFormalLine("obj = " + findMethod.getMethodCall() + ";"); bodyBuilder.appendFormalLine( "Assert.assertNotNull(\"Find method for '" + entityName + "' illegally returned null for id '\" + id + \"'\", obj);"); bodyBuilder.appendFormalLine( "boolean modified = dod." + dataOnDemandMetadata.getModifyMethod().getMethodName().getSymbolName() + "(obj);"); bodyBuilder.appendFormalLine( versionAccessorMethod.getReturnType().getSimpleTypeName() + " currentVersion = obj." + versionAccessorMethod.getMethodName().getSymbolName() + "();"); bodyBuilder.appendFormalLine(flushMethod.getMethodCall() + ";"); if (JdkJavaType.isDateField(versionType)) { bodyBuilder.appendFormalLine( "Assert.assertTrue(\"Version for '" + entityName + "' failed to increment on flush directive\", (currentVersion != null && obj." + versionAccessorMethod.getMethodName().getSymbolName() + "().after(currentVersion)) || !modified);"); } else { bodyBuilder.appendFormalLine( "Assert.assertTrue(\"Version for '" + entityName + "' failed to increment on flush directive\", (currentVersion != null && obj." + versionAccessorMethod.getMethodName().getSymbolName() + "() > currentVersion) || !modified);"); } flushMethod.copyAdditionsTo(builder, governorTypeDetails); findMethod.copyAdditionsTo(builder, governorTypeDetails); MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder( getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder); methodBuilder.setAnnotations(annotations); return methodBuilder; }