Search in sources :

Example 96 with DependencyNode

use of org.sonatype.aether.graph.DependencyNode in project sonatype-aether by sonatype.

the class DependencyGraphParser method parse.

private DependencyNode parse(BufferedReader in) throws IOException {
    if (substitutions != null) {
        substitutionIterator = substitutions.iterator();
    }
    String line = null;
    DependencyNode root = null;
    DependencyNode node = null;
    int prevLevel = 0;
    LinkedList<DependencyNode> stack = new LinkedList<DependencyNode>();
    boolean isRootNode = true;
    while ((line = in.readLine()) != null) {
        line = cutComment(line);
        if (isEmpty(line)) {
            // skip empty line
            continue;
        }
        if (isEOFMarker(line)) {
            // stop parsing
            break;
        }
        while (line.contains("%s")) {
            if (!substitutionIterator.hasNext()) {
                throw new IllegalArgumentException("not enough substitutions to fill placeholders");
            }
            line = line.replaceFirst("%s", substitutionIterator.next());
        }
        LineContext ctx = createContext(line);
        if (prevLevel < ctx.getLevel()) {
            // previous node is new parent
            stack.add(node);
        }
        // get to real parent
        while (prevLevel > ctx.getLevel()) {
            stack.removeLast();
            prevLevel -= 1;
        }
        prevLevel = ctx.getLevel();
        if (ctx.getDefinition() != null && ctx.getDefinition().isReference()) {
            DependencyNode child = reference(ctx.getDefinition().getReference());
            node.getChildren().add(child);
            node = child;
        } else {
            node = build(isRootNode ? null : stack.getLast(), ctx, isRootNode);
            if (isRootNode) {
                root = node;
                isRootNode = false;
            }
            if (ctx.getDefinition() != null && ctx.getDefinition().hasId()) {
                this.nodes.put(ctx.getDefinition().getId(), node);
            }
        }
    }
    this.nodes.clear();
    return root;
}
Also used : DependencyNode(org.sonatype.aether.graph.DependencyNode) LinkedList(java.util.LinkedList)

Example 97 with DependencyNode

use of org.sonatype.aether.graph.DependencyNode in project sonatype-aether by sonatype.

the class DependencyGraphParser method parseMultiple.

/**
     * Parse multiple graphs in one resource, divided by "---".
     */
public List<DependencyNode> parseMultiple(String resource) throws IOException {
    URL res = this.getClass().getClassLoader().getResource(prefix + resource);
    if (res == null) {
        throw new IOException("Could not find classpath resource " + prefix + resource);
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(res.openStream(), "UTF-8"));
    List<DependencyNode> ret = new ArrayList<DependencyNode>();
    DependencyNode root = null;
    while ((root = parse(reader)) != null) {
        ret.add(root);
    }
    return ret;
}
Also used : InputStreamReader(java.io.InputStreamReader) DependencyNode(org.sonatype.aether.graph.DependencyNode) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL)

Example 98 with DependencyNode

use of org.sonatype.aether.graph.DependencyNode in project sonatype-aether by sonatype.

the class DependencyGraphParserTest method testResourceLoadingWithPrefix.

@Test
public void testResourceLoadingWithPrefix() throws UnsupportedEncodingException, IOException {
    String prefix = "org/sonatype/aether/test/util/";
    parser = new DependencyGraphParser(prefix);
    String name = "testResourceLoading.txt";
    DependencyNode node = parser.parse(name);
    assertEquals(0, node.getChildren().size());
    assertNodeProperties(node, "");
}
Also used : DependencyNode(org.sonatype.aether.graph.DependencyNode) Test(org.junit.Test)

Example 99 with DependencyNode

use of org.sonatype.aether.graph.DependencyNode in project sonatype-aether by sonatype.

the class DefaultDependencyCollectorTest method assertEqualSubtree.

private static void assertEqualSubtree(DependencyNode expected, DependencyNode actual, LinkedList<DependencyNode> parents) {
    assertEquals("path: " + parents, expected.getDependency(), actual.getDependency());
    if (actual.getDependency() != null) {
        Artifact artifact = actual.getDependency().getArtifact();
        for (DependencyNode parent : parents) {
            if (parent.getDependency() != null && artifact.equals(parent.getDependency().getArtifact())) {
                return;
            }
        }
    }
    parents.addLast(expected);
    assertEquals("path: " + parents + ", expected: " + expected.getChildren() + ", actual: " + actual.getChildren(), expected.getChildren().size(), actual.getChildren().size());
    Iterator<DependencyNode> iterator1 = expected.getChildren().iterator();
    Iterator<DependencyNode> iterator2 = actual.getChildren().iterator();
    while (iterator1.hasNext()) {
        assertEqualSubtree(iterator1.next(), iterator2.next(), parents);
    }
    parents.removeLast();
}
Also used : DependencyNode(org.sonatype.aether.graph.DependencyNode) Artifact(org.sonatype.aether.artifact.Artifact)

Example 100 with DependencyNode

use of org.sonatype.aether.graph.DependencyNode in project sonatype-aether by sonatype.

the class DefaultDependencyCollectorTest method testSimpleCollection.

@Test
public void testSimpleCollection() throws IOException, DependencyCollectionException {
    DependencyNode root = parser.parseLiteral("gid:aid:ext:ver");
    Dependency dependency = root.getDependency();
    CollectRequest request = new CollectRequest(dependency, Arrays.asList(repository));
    CollectResult result = collector.collectDependencies(session, request);
    assertEquals(0, result.getExceptions().size());
    DependencyNode newRoot = result.getRoot();
    Dependency newDependency = newRoot.getDependency();
    assertEquals(dependency, newDependency);
    assertEquals(dependency.getArtifact(), newDependency.getArtifact());
    assertEquals(1, newRoot.getChildren().size());
    DependencyNode expect = parser.parseLiteral("gid:aid2:ext:ver:compile");
    assertEquals(expect.getDependency(), newRoot.getChildren().get(0).getDependency());
}
Also used : CollectResult(org.sonatype.aether.collection.CollectResult) DependencyNode(org.sonatype.aether.graph.DependencyNode) Dependency(org.sonatype.aether.graph.Dependency) CollectRequest(org.sonatype.aether.collection.CollectRequest) Test(org.junit.Test)

Aggregations

DependencyNode (org.sonatype.aether.graph.DependencyNode)114 Test (org.junit.Test)82 NodeBuilder (org.sonatype.aether.test.util.NodeBuilder)20 Dependency (org.sonatype.aether.graph.Dependency)14 IdentityHashMap (java.util.IdentityHashMap)13 CollectRequest (org.sonatype.aether.collection.CollectRequest)12 CollectResult (org.sonatype.aether.collection.CollectResult)11 List (java.util.List)8 Map (java.util.Map)8 LinkedList (java.util.LinkedList)7 Artifact (org.sonatype.aether.artifact.Artifact)6 DependencyFilter (org.sonatype.aether.graph.DependencyFilter)5 ArrayList (java.util.ArrayList)4 DependencyGraphTransformationContext (org.sonatype.aether.collection.DependencyGraphTransformationContext)4 ConflictMarker (org.sonatype.aether.util.graph.transformer.ConflictMarker)4 HashMap (java.util.HashMap)3 ArtifactDescriptorException (org.sonatype.aether.resolution.ArtifactDescriptorException)3 BufferedReader (java.io.BufferedReader)2 HashSet (java.util.HashSet)2 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)2