1 package org.apache.maven.archiva.plugins.dev.testgen;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
45 * DependencyGraphTestCreator
47 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 public class DependencyGraphTestCreator
51 extends AbstractCreator
53 private class RootArtifactPredicate
56 private String rootKey;
58 public RootArtifactPredicate( DependencyTree tree )
60 this.rootKey = toKey( tree.getRootNode().getArtifact() );
63 public boolean evaluate( Object input )
65 boolean satisfies = false;
67 if ( input instanceof Artifact )
69 Artifact nodeArtifact = (Artifact) input;
70 String key = toKey( nodeArtifact );
72 if ( key.equals( rootKey ) )
82 private File outputFile;
84 private PrintWriter out;
86 private DependencyTreeBuilder dependencyTreeBuilder;
88 private ArtifactMetadataSource artifactMetadataSource;
90 private ArtifactCollector collector;
92 public void create( String classPrefix )
93 throws MojoExecutionException
95 String classname = classPrefix + "DependencyGraphTest";
97 getLog().info( "Generating " + classname + ".java ..." );
99 outputFile = new File( outputDir, classname + ".java" );
102 out = new PrintWriter( outputFile );
104 catch ( FileNotFoundException e )
106 throw new MojoExecutionException( "Unable to open file " + outputFile.getName() + " for output: "
107 + e.getMessage(), e );
112 out.println( "package org.apache.maven.archiva.dependency.graph;" );
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;" );
122 out.println( "import java.util.ArrayList;" );
123 out.println( "import java.util.List;" );
126 String projectKey = toKey( project.getModel() );
128 writeJavadoc( classname, projectKey );
131 out.println( "public class " + classname );
132 out.println( " extends AbstractDependencyGraphFactoryTestCase" );
135 DependencyTree dependencyTree = getDependencyTree();
137 writeGraphNodesTest( classPrefix, dependencyTree );
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 );
154 public ArtifactMetadataSource getArtifactMetadataSource()
156 return artifactMetadataSource;
159 public ArtifactCollector getCollector()
164 public DependencyTreeBuilder getDependencyTreeBuilder()
166 return dependencyTreeBuilder;
169 public void setArtifactMetadataSource( ArtifactMetadataSource artifactMetadataSource )
171 this.artifactMetadataSource = artifactMetadataSource;
174 public void setCollector( ArtifactCollector collector )
176 this.collector = collector;
179 public void setDependencyTreeBuilder( DependencyTreeBuilder dependencyTreeBuilder )
181 this.dependencyTreeBuilder = dependencyTreeBuilder;
184 private DependencyTree getDependencyTree()
185 throws MojoExecutionException
189 return dependencyTreeBuilder.buildDependencyTree( project, localRepository, artifactFactory,
190 artifactMetadataSource, collector );
192 catch ( DependencyTreeBuilderException e )
194 String emsg = "Unable to build dependency tree.";
195 getLog().error( emsg, e );
196 throw new MojoExecutionException( emsg, e );
200 private void writeDirectCompileDepsTest( String classPrefix, DependencyTree tree )
202 out.println( " public void testResolveOfDirectCompileDeps()" );
203 out.println( " throws GraphTaskException" );
205 writeTestProlog( classPrefix );
206 writeDirectDependenciesCheck( tree, "compile" );
211 private void writeDirectDependenciesCheck( DependencyTree dependencyTree, String scope )
213 out.println( " // Check for direct dependencies on scope " + scope );
214 out.println( " expectedNodes.clear();" );
216 List directDeps = new ArrayList();
217 directDeps.addAll( dependencyTree.getRootNode().getChildren() );
218 CollectionUtils.transform( directDeps, new DependencyNodeToArtifactTransformer() );
220 Collections.sort( directDeps );
222 writeExpectedNodesAdd( directDeps, scope );
224 out.println( " assertDirectNodes( graph, expectedNodes, \"" + scope + "\" );" );
227 private void writeDirectTestDepsTest( String classPrefix, DependencyTree tree )
229 out.println( " public void testResolveOfDirectTestDeps()" );
230 out.println( " throws GraphTaskException" );
232 writeTestProlog( classPrefix );
233 writeDirectDependenciesCheck( tree, "test" );
238 private void writeExpectedNodesAdd( List deps, String scope )
240 Iterator it = deps.iterator();
241 while ( it.hasNext() )
243 Artifact artifact = (Artifact) it.next();
244 String depKey = toKey( artifact );
245 if ( StringUtils.equals( scope, artifact.getScope() ) )
247 out.println( " expectedNodes.add( \"" + depKey + "\" );" );
252 private void writeJavadoc( String classname, String projectKey )
254 out.println( "/**" );
255 out.println( " * " + classname );
256 out.println( " * " );
257 out.println( " * DependencyGraphTest for testing <code>" + projectKey + "</code>" );
259 out.println( " * Generated by <code>archivadev:generate-dependency-tests</code> plugin" );
260 out.println( " * @version $Id$" );
261 out.println( " */" );
264 private void writeGraphNodesTest( String classPrefix, final DependencyTree tree )
266 out.println( " public void testResolvedDepsToNodes()" );
267 out.println( " throws GraphTaskException" );
269 writeTestProlog( classPrefix );
271 String projectKey = toKey( project.getModel() );
272 out.println( " String expectedRootRef = \"" + projectKey + "\";" );
273 out.println( " List expectedNodes = new ArrayList();" );
275 out.println( " // Check for all nodes, regardless of scope." );
276 out.println( " expectedNodes.clear();" );
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 );
285 Iterator it = deps.iterator();
286 while ( it.hasNext() )
288 Artifact artifact = (Artifact) it.next();
289 String depKey = toKey( artifact );
290 out.println( " expectedNodes.add( \"" + depKey + "\" );" );
294 out.println( " assertGraph( graph, expectedRootRef, expectedNodes );" );
301 private void writeTestProlog( String classPrefix )
303 out.println( " MemoryRepositoryDependencyGraphBuilder graphBuilder = " );
304 out.println( " new MemoryRepositoryDependencyGraphBuilder();" );
305 out.println( " MemoryRepository repository = new " + classPrefix + "MemoryRepository();" );
306 out.println( " graphBuilder.setMemoryRepository( repository );" );
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 );" );
313 out.println( " // Get the model to resolve from" );
314 out.println( " VersionedReference rootRef = toVersionedReference( \"" + project.getGroupId() + ":"
315 + project.getArtifactId() + ":" + project.getVersion() + "\"); " );
317 out.println( " // Perform the resolution." );
318 out.println( " DependencyGraph graph = factory.getGraph( rootRef );" );
320 out.println( " // Test the results." );
321 out.println( " assertNotNull( \"Graph shouldn't be null.\", graph );" );
325 private void writeTransientDependenciesCheck( DependencyTree dependencyTree, String scope )
327 out.println( " // Check for transient dependencies on scope " + scope );
328 out.println( " expectedNodes.clear();" );
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 );
338 Collections.sort( deps );
340 writeExpectedNodesAdd( deps, scope );
342 out.println( " assertTransientNodes( graph, expectedNodes, \"" + scope + "\" );" );
345 private void writeTransitiveCompileDepsTest( String classPrefix, DependencyTree tree )
347 out.println( " public void testResolveOfTransitiveCompileDeps()" );
348 out.println( " throws GraphTaskException" );
350 writeTestProlog( classPrefix );
351 writeTransientDependenciesCheck( tree, "compile" );
356 private void writeTransitiveTestDepsTest( String classPrefix, DependencyTree tree )
358 out.println( " public void testResolveOfTransitiveTestDeps()" );
359 out.println( " throws GraphTaskException" );
361 writeTestProlog( classPrefix );
362 writeTransientDependenciesCheck( tree, "test" );