Search in sources :

Example 6 with TestCreatorProvider

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

the class TestOperationsImpl method createUnitTest.

@Override
public void createUnitTest(JavaType type) {
    // Check if specified type exists in the project
    String physicalTypeIdentifier = typeLocationService.getPhysicalTypeIdentifier(type);
    if (physicalTypeIdentifier == null) {
        throw new IllegalArgumentException(String.format("The class '%s' doesn't exists in the project. Please, specify an existing class", type));
    }
    // Adding unit test dependencies
    List<TestCreatorProvider> validTestCreators = getValidTestCreatorsForType(type);
    if (!validTestCreators.isEmpty()) {
        addUnitTestDependencies(type.getModule());
    }
    // Creating tests
    if (validTestCreators.isEmpty()) {
        throw new IllegalArgumentException("Unable to find a valid test creator for this type of class. " + "Please, select another type of class to generate the test, such as an entity.");
    } else {
        for (TestCreatorProvider creator : validTestCreators) {
            creator.createUnitTest(type);
        }
    }
}
Also used : TestCreatorProvider(org.springframework.roo.addon.test.providers.TestCreatorProvider)

Example 7 with TestCreatorProvider

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

the class TestCommands method getClassPosibleValues.

@CliOptionAutocompleteIndicator(command = "test unit", help = "Option `--class` must " + "be a non-abstract valid type. Please, use auto-complete feature to select it.", param = "class")
public List<String> getClassPosibleValues(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("class");
    // Create results to return
    List<String> results = new ArrayList<String>();
    // Look for all valid types for all available test creators
    for (TestCreatorProvider creator : getAllTestCreators()) {
        if (creator.isUnitTestCreationAvailable()) {
            for (JavaType annotationType : creator.getValidTypes()) {
                // Look for types with this annotation type
                Set<ClassOrInterfaceTypeDetails> types = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(annotationType);
                for (ClassOrInterfaceTypeDetails typeCid : types) {
                    String name = replaceTopLevelPackageString(typeCid.getType(), currentText);
                    if (!results.contains(name) && !typeCid.isAbstract()) {
                        results.add(name);
                    }
                }
            }
        }
    }
    return results;
}
Also used : JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) TestCreatorProvider(org.springframework.roo.addon.test.providers.TestCreatorProvider) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 8 with TestCreatorProvider

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

the class TestCommands method getIntegrationTestCreationAvailable.

/**
 * Checks all {@link TestCreatorProvider} implementations looking for any
 * available for 'test integration' command.
 *
 * @return `true` if any of the implementations is available or
 *            `false` if none of the implementations are available.
 */
private boolean getIntegrationTestCreationAvailable() {
    // Get all Services implement TestCreatorProvider interface
    if (this.testCreators.isEmpty()) {
        try {
            ServiceReference<?>[] references = this.context.getAllServiceReferences(TestCreatorProvider.class.getName(), null);
            for (ServiceReference<?> ref : references) {
                TestCreatorProvider testCreatorProvider = (TestCreatorProvider) this.context.getService(ref);
                this.testCreators.add(testCreatorProvider);
            }
        } catch (InvalidSyntaxException e) {
            LOGGER.warning("Cannot load TestCreatorProvider on UnitTestCommands.");
            return false;
        }
    }
    for (TestCreatorProvider provider : this.testCreators) {
        if (provider.isIntegrationTestCreationAvailable()) {
            return true;
        }
    }
    return false;
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TestCreatorProvider(org.springframework.roo.addon.test.providers.TestCreatorProvider) ServiceReference(org.osgi.framework.ServiceReference)

Example 9 with TestCreatorProvider

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

the class TestOperationsImpl method getValidTestCreatorsForType.

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

Aggregations

TestCreatorProvider (org.springframework.roo.addon.test.providers.TestCreatorProvider)9 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 ServiceReference (org.osgi.framework.ServiceReference)5 ArrayList (java.util.ArrayList)4 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)2 JavaType (org.springframework.roo.model.JavaType)2 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)2