View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.transformer;
20  
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.eclipse.aether.DefaultRepositorySystemSession;
25  import org.eclipse.aether.collection.DependencyGraphTransformationContext;
26  import org.eclipse.aether.collection.DependencyGraphTransformer;
27  import org.eclipse.aether.graph.DependencyNode;
28  import org.eclipse.aether.internal.test.util.DependencyGraphParser;
29  import org.eclipse.aether.internal.test.util.TestUtils;
30  import org.junit.jupiter.api.AfterEach;
31  import org.junit.jupiter.api.BeforeEach;
32  
33  import static org.junit.jupiter.api.Assertions.*;
34  
35  /**
36   */
37  public abstract class AbstractDependencyGraphTransformerTest {
38  
39      protected DependencyGraphTransformer transformer;
40  
41      protected DependencyGraphParser parser;
42  
43      protected DefaultRepositorySystemSession session;
44  
45      protected DependencyGraphTransformationContext context;
46  
47      protected abstract DependencyGraphTransformer newTransformer();
48  
49      protected abstract DependencyGraphParser newParser();
50  
51      protected DependencyNode transform(DependencyNode root) throws Exception {
52          context = TestUtils.newTransformationContext(session);
53          root = transformer.transformGraph(root, context);
54          assertNotNull(root);
55          return root;
56      }
57  
58      protected DependencyNode parseResource(String resource, String... substitutions) throws Exception {
59          parser.setSubstitutions(substitutions);
60          return parser.parseResource(resource);
61      }
62  
63      protected DependencyNode parseLiteral(String literal, String... substitutions) throws Exception {
64          parser.setSubstitutions(substitutions);
65          return parser.parseLiteral(literal);
66      }
67  
68      protected List<DependencyNode> find(DependencyNode node, String id) {
69          LinkedList<DependencyNode> trail = new LinkedList<>();
70          find(trail, node, id);
71          return trail;
72      }
73  
74      private boolean find(LinkedList<DependencyNode> trail, DependencyNode node, String id) {
75          trail.addFirst(node);
76  
77          if (isMatch(node, id)) {
78              return true;
79          }
80  
81          for (DependencyNode child : node.getChildren()) {
82              if (find(trail, child, id)) {
83                  return true;
84              }
85          }
86  
87          trail.removeFirst();
88  
89          return false;
90      }
91  
92      private boolean isMatch(DependencyNode node, String id) {
93          if (node.getDependency() == null) {
94              return false;
95          }
96          return id.equals(node.getDependency().getArtifact().getArtifactId());
97      }
98  
99      @BeforeEach
100     void setUp() {
101         transformer = newTransformer();
102         parser = newParser();
103         session = TestUtils.newSession();
104     }
105 
106     @AfterEach
107     void tearDown() {
108         transformer = null;
109         parser = null;
110         session = null;
111         context = null;
112     }
113 }