1   package org.apache.maven.shared.dependency.analyzer;
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 java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.DefaultArtifact;
31  import org.apache.maven.artifact.handler.ArtifactHandler;
32  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
33  import org.apache.maven.artifact.versioning.VersionRange;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.shared.invoker.InvocationResult;
36  import org.apache.maven.shared.test.plugin.BuildTool;
37  import org.apache.maven.shared.test.plugin.ProjectTool;
38  import org.apache.maven.shared.test.plugin.TestToolsException;
39  import org.codehaus.plexus.PlexusTestCase;
40  
41  /**
42   * Tests <code>DefaultProjectDependencyAnalyzer</code>.
43   * 
44   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
45   * @version $Id: DefaultProjectDependencyAnalyzerTest.java 661727 2008-05-30 14:21:49Z bentmann $
46   * @see DefaultProjectDependencyAnalyzer
47   */
48  public class DefaultProjectDependencyAnalyzerTest
49      extends PlexusTestCase
50  {
51      // fields -----------------------------------------------------------------
52  
53      private BuildTool buildTool;
54  
55      private ProjectTool projectTool;
56  
57      private ProjectDependencyAnalyzer analyzer;
58  
59      // TestCase methods -------------------------------------------------------
60  
61      /*
62       * @see org.codehaus.plexus.PlexusTestCase#setUp()
63       */
64      protected void setUp()
65          throws Exception
66      {
67          super.setUp();
68  
69          buildTool = (BuildTool) lookup( BuildTool.ROLE );
70  
71          projectTool = (ProjectTool) lookup( ProjectTool.ROLE );
72  
73          analyzer = (ProjectDependencyAnalyzer) lookup( ProjectDependencyAnalyzer.ROLE );
74      }
75  
76      // tests ------------------------------------------------------------------
77  
78      public void testPom()
79          throws TestToolsException, ProjectDependencyAnalyzerException
80      {
81          compileProject( "pom/pom.xml" );
82  
83          MavenProject project = getProject( "pom/pom.xml" );
84  
85          ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( project );
86  
87          ProjectDependencyAnalysis expectedAnalysis = new ProjectDependencyAnalysis();
88  
89          assertEquals( expectedAnalysis, actualAnalysis );
90      }
91  
92      public void testJarWithNoDependencies()
93          throws TestToolsException, ProjectDependencyAnalyzerException
94      {
95          compileProject( "jarWithNoDependencies/pom.xml" );
96  
97          MavenProject project = getProject( "jarWithNoDependencies/pom.xml" );
98  
99          ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( project );
100 
101         ProjectDependencyAnalysis expectedAnalysis = new ProjectDependencyAnalysis();
102 
103         assertEquals( expectedAnalysis, actualAnalysis );
104     }
105 
106     public void testJarWithCompileDependency()
107         throws TestToolsException, ProjectDependencyAnalyzerException
108     {
109         compileProject( "jarWithCompileDependency/pom.xml" );
110 
111         MavenProject project2 = getProject( "jarWithCompileDependency/project2/pom.xml" );
112 
113         ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( project2 );
114 
115         Artifact project1 =
116             createArtifact( "org.apache.maven.shared.dependency-analyzer.tests", "jarWithCompileDependency1", "jar",
117                             "1.0", "compile" );
118         Set usedDeclaredArtifacts = Collections.singleton( project1 );
119         ProjectDependencyAnalysis expectedAnalysis = new ProjectDependencyAnalysis( usedDeclaredArtifacts, null, null );
120 
121         assertEquals( expectedAnalysis, actualAnalysis );
122     }
123 
124     public void testJarWithTestDependency()
125         throws TestToolsException, ProjectDependencyAnalyzerException
126     {
127         compileProject( "jarWithTestDependency/pom.xml" );
128 
129         MavenProject project2 = getProject( "jarWithTestDependency/project2/pom.xml" );
130 
131         ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( project2 );
132 
133         Artifact project1 =
134             createArtifact( "org.apache.maven.shared.dependency-analyzer.tests", "jarWithTestDependency1", "jar",
135                             "1.0", "test" );
136         Set usedDeclaredArtifacts = Collections.singleton( project1 );
137 
138         // TODO: remove workaround for SUREFIRE-300 when 2.3.1 released
139         Artifact junit = createArtifact( "junit", "junit", "jar", "3.8.1", "test" );
140         Set unusedDeclaredArtifacts = Collections.singleton( junit );
141 
142         ProjectDependencyAnalysis expectedAnalysis =
143             new ProjectDependencyAnalysis( usedDeclaredArtifacts, null, unusedDeclaredArtifacts );
144 
145         assertEquals( expectedAnalysis, actualAnalysis );
146     }
147 
148     // private methods --------------------------------------------------------
149 
150     private void compileProject( String pomPath )
151         throws TestToolsException
152     {
153         File pom = getTestFile( "target/test-classes/", pomPath );
154         Properties properties = new Properties();
155         List goals = Arrays.asList( new String[] { "clean", "install" } );
156         File log = new File( pom.getParentFile(), "build.log" );
157 
158         // TODO: don't install test artifacts to local repository
159         InvocationResult result = buildTool.executeMaven( pom, properties, goals, log );
160 
161         assertNull( "Error compiling test project", result.getExecutionException() );
162         assertEquals( "Error compiling test project", 0, result.getExitCode() );
163     }
164 
165     private MavenProject getProject( String pomPath )
166         throws TestToolsException
167     {
168         File pom = getTestFile( "target/test-classes/", pomPath );
169 
170         return projectTool.readProjectWithDependencies( pom );
171     }
172 
173     private Artifact createArtifact( String groupId, String artifactId, String type, String version, String scope )
174     {
175         VersionRange versionRange = VersionRange.createFromVersion( version );
176         ArtifactHandler handler = new DefaultArtifactHandler();
177 
178         return new DefaultArtifact( groupId, artifactId, versionRange, scope, type, null, handler );
179     }
180 }