Search in sources :

Example 1 with DataOnDemandCreatorProvider

use of org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider in project spring-roo by spring-projects.

the class JsonControllerTestCreator method getValidDataOnDemandCreatorsForType.

/**
 * Gets all the valid implementations of DataOnDemandCreatorProvider for a JavaType.
 *
 * @param type the JavaType to get the valid implementations.
 * @return a `List` with the {@link DataOnDemandCreatorProvider} valid
 *            implementations. Never `null`.
 */
private List<DataOnDemandCreatorProvider> getValidDataOnDemandCreatorsForType(JavaType type) {
    // Get all Services implement DataOnDemandCreatorProvider interface
    if (this.dodCreators.isEmpty()) {
        try {
            ServiceReference<?>[] references = this.context.getAllServiceReferences(DataOnDemandCreatorProvider.class.getName(), null);
            for (ServiceReference<?> ref : references) {
                DataOnDemandCreatorProvider dodCreatorProvider = (DataOnDemandCreatorProvider) this.context.getService(ref);
                this.dodCreators.add(dodCreatorProvider);
            }
        } catch (InvalidSyntaxException e) {
            LOGGER.warning("Cannot load DataOnDemandCreatorProvider on JsonControllerTestCreator.");
            return null;
        }
    }
    List<DataOnDemandCreatorProvider> validDoDCreators = new ArrayList<DataOnDemandCreatorProvider>();
    for (DataOnDemandCreatorProvider provider : this.dodCreators) {
        if (provider.isValid(type)) {
            validDoDCreators.add(provider);
        }
    }
    return validDoDCreators;
}
Also used : ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with DataOnDemandCreatorProvider

use of org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider in project spring-roo by spring-projects.

the class JsonControllerTestCreator 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 JSON Controller
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_CONTROLLER), "Type must be a Roo controller.");
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JSON), "Type must be a Roo JSON controller.");
    // Add springlets-boot-starter-test dependency
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    projectOperations.addDependency(module.getModuleName(), SPRINGLETS_BOOT_STARTER_TEST_DEPENDENCY);
    // Get the controller managed entity
    ControllerAnnotationValues controllerAnnotationValues = new ControllerAnnotationValues(cid);
    JavaType managedEntity = controllerAnnotationValues.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 JSON controllers.");
    Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for JSON controllers. %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 @RooJsonControllerIntegrationTest to source file
    AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JSON_CONTROLLER_INTEGRATION_TEST);
    rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
    // 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 : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) 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)

Example 3 with DataOnDemandCreatorProvider

use of org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider in project spring-roo by spring-projects.

the class JpaTestCreator method createUnitTest.

@Override
public void createUnitTest(final JavaType entity) {
    Validate.notNull(entity, "Class to produce an unit test class for is required");
    // Check if provided JavaType is a Repository
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(entity);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Type must be a Roo JPA Entity type.");
    // Create JPA DataOnDemand artifacts
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(entity);
    for (DataOnDemandCreatorProvider dodCreator : dodCreators) {
        dodCreator.createDataOnDemand(entity);
    }
    final JavaType name = new JavaType(entity + "Test", entity.getModule());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(entity.getModule()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooUnitTest to source file
    AnnotationMetadataBuilder rooUnitTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_UNIT_TEST);
    rooUnitTestAnnotation.addClassAttribute("targetClass", entity);
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooUnitTestAnnotation);
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : 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) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 4 with DataOnDemandCreatorProvider

use of org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider in project spring-roo by spring-projects.

the class JpaTestCreator method getValidDataOnDemandCreatorsForType.

/**
 * Gets all the valid implementations of DataOnDemandCreatorProvider for a JavaType.
 *
 * @param type the JavaType to get the valid implementations.
 * @return a `List` with the {@link DataOnDemandCreatorProvider} valid
 *            implementations. Never `null`.
 */
private List<DataOnDemandCreatorProvider> getValidDataOnDemandCreatorsForType(JavaType type) {
    // Get all Services implement DataOnDemandCreatorProvider interface
    if (this.dodCreators.isEmpty()) {
        try {
            ServiceReference<?>[] references = this.context.getAllServiceReferences(DataOnDemandCreatorProvider.class.getName(), null);
            for (ServiceReference<?> ref : references) {
                DataOnDemandCreatorProvider dodCreatorProvider = (DataOnDemandCreatorProvider) this.context.getService(ref);
                this.dodCreators.add(dodCreatorProvider);
            }
        } catch (InvalidSyntaxException e) {
            LOGGER.warning("Cannot load DataOnDemandCreatorProvider on TestOperationsImpl.");
            return null;
        }
    }
    List<DataOnDemandCreatorProvider> validDoDCreators = new ArrayList<DataOnDemandCreatorProvider>();
    for (DataOnDemandCreatorProvider provider : this.dodCreators) {
        if (provider.isValid(type)) {
            validDoDCreators.add(provider);
        }
    }
    return validDoDCreators;
}
Also used : ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with DataOnDemandCreatorProvider

use of org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider in project spring-roo by spring-projects.

the class ThymeleafControllerTestCreator method getValidDataOnDemandCreatorsForType.

/**
 * Gets all the valid implementations of DataOnDemandCreatorProvider for a JavaType.
 *
 * @param type the JavaType to get the valid implementations.
 * @return a `List` with the {@link DataOnDemandCreatorProvider} valid
 *            implementations. Never `null`.
 */
private List<DataOnDemandCreatorProvider> getValidDataOnDemandCreatorsForType(JavaType type) {
    // Get all Services implement DataOnDemandCreatorProvider interface
    if (this.dodCreators.isEmpty()) {
        try {
            ServiceReference<?>[] references = this.context.getAllServiceReferences(DataOnDemandCreatorProvider.class.getName(), null);
            for (ServiceReference<?> ref : references) {
                DataOnDemandCreatorProvider dodCreatorProvider = (DataOnDemandCreatorProvider) this.context.getService(ref);
                this.dodCreators.add(dodCreatorProvider);
            }
        } catch (InvalidSyntaxException e) {
            LOGGER.warning("Cannot load DataOnDemandCreatorProvider on ThymeleafControllerTestCreator.");
            return null;
        }
    }
    List<DataOnDemandCreatorProvider> validDoDCreators = new ArrayList<DataOnDemandCreatorProvider>();
    for (DataOnDemandCreatorProvider provider : this.dodCreators) {
        if (provider.isValid(type)) {
            validDoDCreators.add(provider);
        }
    }
    return validDoDCreators;
}
Also used : ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

DataOnDemandCreatorProvider (org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider)8 ArrayList (java.util.ArrayList)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ServiceReference (org.osgi.framework.ServiceReference)4 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)4 JavaType (org.springframework.roo.model.JavaType)4 RooJavaType (org.springframework.roo.model.RooJavaType)4 Dependency (org.springframework.roo.project.Dependency)3 Pom (org.springframework.roo.project.maven.Pom)3 ControllerAnnotationValues (org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues)2 RepositoryJpaAnnotationValues (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaAnnotationValues)1