]> source.dussan.org Git - archiva.git/blob
0e403ff5b0e262a44738040e875240e6571e1559
[archiva.git] /
1 package org.apache.maven.archiva.plugins.dev.testgen;
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 org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.functors.NotPredicate;
25 import org.apache.maven.archiva.plugins.dev.functors.DependencyNodeToArtifactTransformer;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
28 import org.apache.maven.artifact.resolver.ArtifactCollector;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.shared.dependency.tree.DependencyTree;
31 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
32 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.plexus.util.StringUtils;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.PrintWriter;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.List;
43
44 /**
45  * DependencyGraphTestCreator 
46  *
47  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48  * @version $Id$
49  */
50 public class DependencyGraphTestCreator
51     extends AbstractCreator
52 {
53     private class RootArtifactPredicate
54         implements Predicate
55     {
56         private String rootKey;
57
58         public RootArtifactPredicate( DependencyTree tree )
59         {
60             this.rootKey = toKey( tree.getRootNode().getArtifact() );
61         }
62
63         public boolean evaluate( Object input )
64         {
65             boolean satisfies = false;
66         
67             if ( input instanceof Artifact )
68             {
69                 Artifact nodeArtifact = (Artifact) input;
70                 String key = toKey( nodeArtifact );
71                 
72                 if ( key.equals( rootKey ) )
73                 {
74                     satisfies = true;
75                 }
76             }
77         
78             return satisfies;
79         }
80     }
81
82     private File outputFile;
83
84     private PrintWriter out;
85
86     private DependencyTreeBuilder dependencyTreeBuilder;
87
88     private ArtifactMetadataSource artifactMetadataSource;
89
90     private ArtifactCollector collector;
91
92     public void create( String classPrefix )
93         throws MojoExecutionException
94     {
95         String classname = classPrefix + "DependencyGraphTest";
96
97         getLog().info( "Generating " + classname + ".java ..." );
98
99         outputFile = new File( outputDir, classname + ".java" );
100         try
101         {
102             out = new PrintWriter( outputFile );
103         }
104         catch ( FileNotFoundException e )
105         {
106             throw new MojoExecutionException( "Unable to open file " + outputFile.getName() + " for output: "
107                 + e.getMessage(), e );
108         }
109
110         try
111         {
112             out.println( "package org.apache.maven.archiva.dependency.graph;" );
113             out.println( "" );
114
115             writeLicense( out );
116
117             // Imports
118             out.println( "import org.apache.maven.archiva.dependency.DependencyGraphFactory;" );
119             out.println( "import org.apache.maven.archiva.model.DependencyScope;" );
120             out.println( "import org.apache.maven.archiva.model.VersionedReference;" );
121             out.println( "" );
122             out.println( "import java.util.ArrayList;" );
123             out.println( "import java.util.List;" );
124             out.println( "" );
125
126             String projectKey = toKey( project.getModel() );
127
128             writeJavadoc( classname, projectKey );
129
130             // The class itself.
131             out.println( "public class " + classname );
132             out.println( "   extends AbstractDependencyGraphFactoryTestCase" );
133             out.println( "{" );
134
135             DependencyTree dependencyTree = getDependencyTree();
136
137             writeGraphNodesTest( classPrefix, dependencyTree );
138
139             // TODO: enable in future, when resolution between archiva and maven are equal.
140             // writeDirectCompileDepsTest( classPrefix, dependencyTree );
141             // writeDirectTestDepsTest( classPrefix, dependencyTree );
142             // writeTransitiveCompileDepsTest( classPrefix, dependencyTree );
143             // writeTransitiveTestDepsTest( classPrefix, dependencyTree );
144
145             out.println( "}" );
146         }
147         finally
148         {
149             out.flush();
150             IOUtil.close( out );
151         }
152     }
153
154     public ArtifactMetadataSource getArtifactMetadataSource()
155     {
156         return artifactMetadataSource;
157     }
158
159     public ArtifactCollector getCollector()
160     {
161         return collector;
162     }
163
164     public DependencyTreeBuilder getDependencyTreeBuilder()
165     {
166         return dependencyTreeBuilder;
167     }
168
169     public void setArtifactMetadataSource( ArtifactMetadataSource artifactMetadataSource )
170     {
171         this.artifactMetadataSource = artifactMetadataSource;
172     }
173
174     public void setCollector( ArtifactCollector collector )
175     {
176         this.collector = collector;
177     }
178
179     public void setDependencyTreeBuilder( DependencyTreeBuilder dependencyTreeBuilder )
180     {
181         this.dependencyTreeBuilder = dependencyTreeBuilder;
182     }
183
184     private DependencyTree getDependencyTree()
185         throws MojoExecutionException
186     {
187         try
188         {
189             return dependencyTreeBuilder.buildDependencyTree( project, localRepository, artifactFactory,
190                                                               artifactMetadataSource, collector );
191         }
192         catch ( DependencyTreeBuilderException e )
193         {
194             String emsg = "Unable to build dependency tree.";
195             getLog().error( emsg, e );
196             throw new MojoExecutionException( emsg, e );
197         }
198     }
199
200     private void writeDirectCompileDepsTest( String classPrefix, DependencyTree tree )
201     {
202         out.println( "   public void testResolveOfDirectCompileDeps()" );
203         out.println( "        throws GraphTaskException" );
204         out.println( "   {" );
205         writeTestProlog( classPrefix );
206         writeDirectDependenciesCheck( tree, "compile" );
207         out.println( "   }" );
208         out.println( "" );
209     }
210
211     private void writeDirectDependenciesCheck( DependencyTree dependencyTree, String scope )
212     {
213         out.println( "      // Check for direct dependencies on scope " + scope );
214         out.println( "      expectedNodes.clear();" );
215
216         List directDeps = new ArrayList();
217         directDeps.addAll( dependencyTree.getRootNode().getChildren() );
218         CollectionUtils.transform( directDeps, new DependencyNodeToArtifactTransformer() );
219
220         Collections.sort( directDeps );
221
222         writeExpectedNodesAdd( directDeps, scope );
223
224         out.println( "      assertDirectNodes( graph, expectedNodes, \"" + scope + "\" );" );
225     }
226
227     private void writeDirectTestDepsTest( String classPrefix, DependencyTree tree )
228     {
229         out.println( "   public void testResolveOfDirectTestDeps()" );
230         out.println( "        throws GraphTaskException" );
231         out.println( "   {" );
232         writeTestProlog( classPrefix );
233         writeDirectDependenciesCheck( tree, "test" );
234         out.println( "   }" );
235         out.println( "" );
236     }
237
238     private void writeExpectedNodesAdd( List deps, String scope )
239     {
240         Iterator it = deps.iterator();
241         while ( it.hasNext() )
242         {
243             Artifact artifact = (Artifact) it.next();
244             String depKey = toKey( artifact );
245             if ( StringUtils.equals( scope, artifact.getScope() ) )
246             {
247                 out.println( "      expectedNodes.add( \"" + depKey + "\" );" );
248             }
249         }
250     }
251
252     private void writeJavadoc( String classname, String projectKey )
253     {
254         out.println( "/**" );
255         out.println( " * " + classname );
256         out.println( " * " );
257         out.println( " * DependencyGraphTest for testing <code>" + projectKey + "</code>" );
258         out.println( " *" );
259         out.println( " * Generated by <code>archivadev:generate-dependency-tests</code> plugin" );
260         out.println( " * @version $Id$" );
261         out.println( " */" );
262     }
263
264     private void writeGraphNodesTest( String classPrefix, final DependencyTree tree )
265     {
266         out.println( "   public void testResolvedDepsToNodes()" );
267         out.println( "        throws GraphTaskException" );
268         out.println( "   {" );
269         writeTestProlog( classPrefix );
270
271         String projectKey = toKey( project.getModel() );
272         out.println( "      String expectedRootRef = \"" + projectKey + "\";" );
273         out.println( "      List expectedNodes = new ArrayList();" );
274         out.println( "" );
275         out.println( "      // Check for all nodes, regardless of scope." );
276         out.println( "      expectedNodes.clear();" );
277
278         // Add all deps.
279         List deps = new ArrayList();
280         Predicate notRootNode = NotPredicate.getInstance( new RootArtifactPredicate( tree ) );
281         CollectionUtils.select( tree.getArtifacts(), notRootNode, deps );
282         CollectionUtils.transform( deps, new DependencyNodeToArtifactTransformer() );
283         Collections.sort( deps );
284
285         Iterator it = deps.iterator();
286         while ( it.hasNext() )
287         {
288             Artifact artifact = (Artifact) it.next();
289             String depKey = toKey( artifact );
290             out.println( "      expectedNodes.add( \"" + depKey + "\" );" );
291         }
292
293         out.println( "" );
294         out.println( "      assertGraph( graph, expectedRootRef, expectedNodes );" );
295
296         out.println( "   }" );
297         out.println( "" );
298
299     }
300
301     private void writeTestProlog( String classPrefix )
302     {
303         out.println( "      MemoryRepositoryDependencyGraphBuilder graphBuilder = " );
304         out.println( "                     new MemoryRepositoryDependencyGraphBuilder();" );
305         out.println( "      MemoryRepository repository = new " + classPrefix + "MemoryRepository();" );
306         out.println( "      graphBuilder.setMemoryRepository( repository );" );
307         out.println( "" );
308         out.println( "      // Create the factory, and add the test resolver." );
309         out.println( "      DependencyGraphFactory factory = new DependencyGraphFactory();" );
310         out.println( "      factory.setGraphBuilder( graphBuilder );" );
311         out.println( "      factory.setDesiredScope( DependencyScope.TEST );" );
312         out.println( "" );
313         out.println( "      // Get the model to resolve from" );
314         out.println( "      VersionedReference rootRef = toVersionedReference( \"" + project.getGroupId() + ":"
315             + project.getArtifactId() + ":" + project.getVersion() + "\"); " );
316         out.println( "" );
317         out.println( "      // Perform the resolution." );
318         out.println( "      DependencyGraph graph = factory.getGraph( rootRef );" );
319         out.println( "" );
320         out.println( "      // Test the results." );
321         out.println( "      assertNotNull( \"Graph shouldn't be null.\", graph );" );
322         out.println( "" );
323     }
324
325     private void writeTransientDependenciesCheck( DependencyTree dependencyTree, String scope )
326     {
327         out.println( "      // Check for transient dependencies on scope " + scope );
328         out.println( "      expectedNodes.clear();" );
329
330         // Add all deps.
331         List deps = new ArrayList( dependencyTree.getArtifacts() );
332         // Remove the direct deps.
333         List directDeps = new ArrayList();
334         directDeps.addAll( dependencyTree.getRootNode().getChildren() );
335         CollectionUtils.transform( directDeps, new DependencyNodeToArtifactTransformer() );
336         deps.removeAll( directDeps );
337
338         Collections.sort( deps );
339
340         writeExpectedNodesAdd( deps, scope );
341
342         out.println( "      assertTransientNodes( graph, expectedNodes, \"" + scope + "\" );" );
343     }
344
345     private void writeTransitiveCompileDepsTest( String classPrefix, DependencyTree tree )
346     {
347         out.println( "   public void testResolveOfTransitiveCompileDeps()" );
348         out.println( "        throws GraphTaskException" );
349         out.println( "   {" );
350         writeTestProlog( classPrefix );
351         writeTransientDependenciesCheck( tree, "compile" );
352         out.println( "   }" );
353         out.println( "" );
354     }
355
356     private void writeTransitiveTestDepsTest( String classPrefix, DependencyTree tree )
357     {
358         out.println( "   public void testResolveOfTransitiveTestDeps()" );
359         out.println( "        throws GraphTaskException" );
360         out.println( "   {" );
361         writeTestProlog( classPrefix );
362         writeTransientDependenciesCheck( tree, "test" );
363         out.println( "   }" );
364         out.println( "" );
365     }
366 }