Search in sources :

Example 1 with AbstractResourcePath

use of org.eclipse.elk.alg.test.framework.io.AbstractResourcePath in project elk by eclipse.

the class GraphFromFile method fromTestClass.

/**
 * Loads graphs from files as specified by configuration methods in the test class.
 *
 * @param testClass
 *            the test class.
 * @param test
 *            instance of the test class to call methods.
 * @param errors
 *            list of error conditions encountered.
 * @return a list of graphs.
 */
public static List<GraphFromFile> fromTestClass(final TestClass testClass, final Object test, final List<Throwable> errors) {
    List<AbstractResourcePath> resourcePaths = new ArrayList<>();
    for (FrameworkMethod method : testClass.getAnnotatedMethods(GraphResourceProvider.class)) {
        TestUtil.ensurePublic(method, errors);
        TestUtil.ensureReturnsType(method, errors, List.class);
        TestUtil.ensureParameters(method, errors);
        // Invoke the method to obtain the resource paths
        try {
            List<?> result = (List<?>) method.invokeExplosively(test);
            // Check that these are all resource paths
            if (!result.stream().allMatch(o -> o instanceof AbstractResourcePath)) {
                errors.add(new Exception("Method " + method.getName() + " must return List<AbstractResourcePath"));
            } else {
                result.stream().map(o -> (AbstractResourcePath) o).forEach(path -> resourcePaths.add(path));
            }
        } catch (Throwable e) {
            errors.add(e);
        }
    }
    // Set a default graph file filter in case no explicit filter has been specified
    resourcePaths.stream().filter(path -> path.getFilter() == null).forEach(path -> path.setFilter(GRAPH_FILE_FILTER));
    // Turn the abstract paths into absolute paths
    return resourcePaths.stream().flatMap(path -> path.listResources().stream()).map(path -> new GraphFromFile(path)).collect(Collectors.toList());
}
Also used : FrameworkMethod(org.junit.runners.model.FrameworkMethod) URI(org.eclipse.emf.common.util.URI) EObject(org.eclipse.emf.ecore.EObject) ElkNode(org.eclipse.elk.graph.ElkNode) Collectors(java.util.stream.Collectors) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) TestUtil(org.eclipse.elk.alg.test.framework.util.TestUtil) List(java.util.List) AbsoluteResourcePath(org.eclipse.elk.alg.test.framework.io.AbsoluteResourcePath) AbstractResourcePath(org.eclipse.elk.alg.test.framework.io.AbstractResourcePath) FileFilter(java.io.FileFilter) TestClass(org.junit.runners.model.TestClass) FileExtensionFilter(org.eclipse.elk.alg.test.framework.io.FileExtensionFilter) Resource(org.eclipse.emf.ecore.resource.Resource) Collections(java.util.Collections) GraphResourceProvider(org.eclipse.elk.alg.test.framework.annotations.GraphResourceProvider) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AbstractResourcePath(org.eclipse.elk.alg.test.framework.io.AbstractResourcePath) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 2 with AbstractResourcePath

use of org.eclipse.elk.alg.test.framework.io.AbstractResourcePath in project elk by eclipse.

the class RandomGraphFromFile method fromTestClass.

/**
 * Loads random graphs from files as specified by configuration methods in the test class.
 *
 * @param testClass
 *            the test class.
 * @param test
 *            instance of the test class to call methods.
 * @param errors
 *            list of error conditions encountered.
 * @return a list of graphs.
 */
public static List<RandomGraphFromFile> fromTestClass(final TestClass testClass, final Object test, final List<Throwable> errors) {
    List<AbstractResourcePath> resourcePaths = new ArrayList<>();
    for (FrameworkMethod method : testClass.getAnnotatedMethods(RandomGeneratorFile.class)) {
        TestUtil.ensurePublic(method, errors);
        TestUtil.ensureReturnsType(method, errors, AbstractResourcePath.class);
        TestUtil.ensureParameters(method, errors);
        // Invoke the method to obtain the resource paths
        try {
            resourcePaths.add((AbstractResourcePath) method.invokeExplosively(test));
        } catch (Throwable e) {
            errors.add(e);
        }
    }
    // Set proper file filters and load graphs
    List<RandomGraphFromFile> graphs = new ArrayList<>();
    for (AbstractResourcePath abstractPath : resourcePaths) {
        abstractPath.setFilter(RANDOM_GRAPH_FILE_FILTER);
        for (AbsoluteResourcePath absolutePath : abstractPath.listResources()) {
            try {
                for (ElkNode graph : randomGraphs(absolutePath)) {
                    graphs.add(new RandomGraphFromFile(absolutePath, graph));
                }
            } catch (Throwable e) {
                errors.add(new Exception("Unable to load random graphs from " + absolutePath));
            }
        }
    }
    return graphs;
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ArrayList(java.util.ArrayList) AbsoluteResourcePath(org.eclipse.elk.alg.test.framework.io.AbsoluteResourcePath) AbstractResourcePath(org.eclipse.elk.alg.test.framework.io.AbstractResourcePath) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Aggregations

ArrayList (java.util.ArrayList)2 AbsoluteResourcePath (org.eclipse.elk.alg.test.framework.io.AbsoluteResourcePath)2 AbstractResourcePath (org.eclipse.elk.alg.test.framework.io.AbstractResourcePath)2 ElkNode (org.eclipse.elk.graph.ElkNode)2 FrameworkMethod (org.junit.runners.model.FrameworkMethod)2 FileFilter (java.io.FileFilter)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 GraphResourceProvider (org.eclipse.elk.alg.test.framework.annotations.GraphResourceProvider)1 FileExtensionFilter (org.eclipse.elk.alg.test.framework.io.FileExtensionFilter)1 TestUtil (org.eclipse.elk.alg.test.framework.util.TestUtil)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 TestClass (org.junit.runners.model.TestClass)1