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