Search in sources :

Example 1 with RepositoryJpaAnnotationValues

use of org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaAnnotationValues in project spring-roo by spring-projects.

the class RepositoryJpaTestCreator method createIntegrationTest.

@Override
public void createIntegrationTest(JavaType type, Pom module) {
    Validate.notNull(type, "Class to produce an integration test class for is required");
    // Check if provided JavaType is a Repository
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA), "Type must be a Roo JPA Repository type.");
    // Get the repository managed entity
    RepositoryJpaAnnotationValues repositoryAnnotationValues = new RepositoryJpaAnnotationValues(cid);
    JavaType managedEntity = repositoryAnnotationValues.getEntity();
    // Workaround to get a JavaType with not null module when recovering it
    // from a ClassAttributeValue
    managedEntity = new JavaType(managedEntity.getFullyQualifiedTypeName(), managedEntity.getArray(), managedEntity.getDataType(), managedEntity.getArgName(), managedEntity.getParameters(), typeLocationService.getTypeDetails(managedEntity).getType().getModule());
    // Create Data On Demand artifacts for managed entity
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(managedEntity);
    Validate.isTrue(!dodCreators.isEmpty(), "Couldn't find any 'DataOnDemandCreatorProvider' for JPA repositories.");
    Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for JPA repositories. %s can't decide which one to use.", this.getClass().getName());
    DataOnDemandCreatorProvider creator = dodCreators.get(0);
    creator.createDataOnDemand(managedEntity);
    // Add module dependency with test-jar dependency
    if (projectOperations.isMultimoduleProject()) {
        String managedEntityModuleName = managedEntity.getModule();
        Pom managedEntityModule = projectOperations.getPomFromModuleName(managedEntityModuleName);
        projectOperations.addDependency(module.getModuleName(), new Dependency(managedEntityModule.getGroupId(), managedEntityModule.getArtifactId(), "${project.version}", DependencyType.valueOfTypeCode("test-jar"), DependencyScope.TEST), true, true);
    }
    // Create integration test class
    final JavaType name = new JavaType(type + "IT", module.getModuleName());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(module.getModuleName()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooRepositoryJpaIntegrationTest to source file
    AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_REPOSITORY_JPA_INTEGRATION_TEST);
    rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
    rooIntegrationTestAnnotation.addClassAttribute("dodConfigurationClass", creator.getDataOnDemandConfiguration());
    rooIntegrationTestAnnotation.addClassAttribute("dodClass", creator.getDataOnDemand(managedEntity));
    // Create integration test class
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooIntegrationTestAnnotation);
    // Write changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RepositoryJpaAnnotationValues(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaAnnotationValues) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) Dependency(org.springframework.roo.project.Dependency) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Aggregations

RepositoryJpaAnnotationValues (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaAnnotationValues)1 DataOnDemandCreatorProvider (org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)1 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)1 JavaType (org.springframework.roo.model.JavaType)1 RooJavaType (org.springframework.roo.model.RooJavaType)1 Dependency (org.springframework.roo.project.Dependency)1 Pom (org.springframework.roo.project.maven.Pom)1