View Javadoc
1   package org.apache.maven.it;
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.maven.it.util.ResourceExtractor;
23  
24  import java.io.File;
25  import java.util.List;
26  import java.util.Properties;
27  
28  /**
29   *
30   * @author Benjamin Bentmann
31   *
32   */
33  public class MavenIT0143TransitiveDependencyScopesTest
34      extends AbstractMavenIntegrationTestCase
35  {
36  
37      /*
38       * NOTE: Class path ordering is another issue (MNG-1412), so we merely check set containment here.
39       */
40  
41      public MavenIT0143TransitiveDependencyScopesTest()
42      {
43          super( ALL_MAVEN_VERSIONS );
44      }
45  
46      /**
47       * Test that the different scopes of transitive dependencies end up on the right class paths when mediated from
48       * a compile-scope dependency.
49       *
50       * @throws Exception in case of failure
51       */
52      public void testitCompileScope()
53          throws Exception
54      {
55          Verifier verifier = run( "compile" );
56          String targetDir = "target-compile";
57  
58          /*
59           * NOTE: Transitive compile dependencies end up in compile scope to support the case of a class in the direct
60           * dependency that extends a class from the transitive dependency, i.e.
61           * project imports A from direct dependency and A extends B from transitive dependency.
62           */
63          List<String> compileArtifacts = verifier.loadLines( targetDir + "/compile-artifacts.txt", "UTF-8" );
64          assertTrue( compileArtifacts.toString(), compileArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
65          assertTrue( compileArtifacts.toString(), compileArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
66          assertEquals( 2, compileArtifacts.size() );
67  
68          List<String> compileClassPath = verifier.loadLines( targetDir + "/compile-cp.txt", "UTF-8" );
69          assertTrue( compileClassPath.toString(), compileClassPath.contains( "classes" ) );
70          assertTrue( compileClassPath.toString(), compileClassPath.contains( "direct-0.1.jar" ) );
71          assertTrue( compileClassPath.toString(), compileClassPath.contains( "compile-0.1.jar" ) );
72          assertEquals( 3, compileClassPath.size() );
73  
74          List<String> runtimeArtifacts = verifier.loadLines( targetDir + "/runtime-artifacts.txt", "UTF-8" );
75          assertTrue( runtimeArtifacts.toString(), runtimeArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
76          assertTrue( runtimeArtifacts.toString(), runtimeArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
77          assertTrue( runtimeArtifacts.toString(), runtimeArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
78          assertEquals( 3, runtimeArtifacts.size() );
79  
80          List<String> runtimeClassPath = verifier.loadLines( targetDir + "/runtime-cp.txt", "UTF-8" );
81          assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "classes" ) );
82          assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "direct-0.1.jar" ) );
83          assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "compile-0.1.jar" ) );
84          assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "runtime-0.1.jar" ) );
85          assertEquals( 4, runtimeClassPath.size() );
86  
87          List<String> testArtifacts = verifier.loadLines( targetDir + "/test-artifacts.txt", "UTF-8" );
88          assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
89          assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
90          assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
91          assertEquals( 3, testArtifacts.size() );
92  
93          List<String> testClassPath = verifier.loadLines( targetDir + "/test-cp.txt", "UTF-8" );
94          assertTrue( testClassPath.toString(), testClassPath.contains( "classes" ) );
95          assertTrue( testClassPath.toString(), testClassPath.contains( "test-classes" ) );
96          assertTrue( testClassPath.toString(), testClassPath.contains( "direct-0.1.jar" ) );
97          assertTrue( testClassPath.toString(), testClassPath.contains( "compile-0.1.jar" ) );
98          assertTrue( testClassPath.toString(), testClassPath.contains( "runtime-0.1.jar" ) );
99          assertEquals( 5, testClassPath.size() );
100     }
101 
102     /**
103      * Test that the different scopes of transitive dependencies end up on the right class paths when mediated from
104      * a provided-scope dependency.
105      *
106      * @throws Exception in case of failure
107      */
108     public void testitProvidedScope()
109         throws Exception
110     {
111         Verifier verifier = run( "provided" );
112         String targetDir = "target-provided";
113 
114         List<String> compileArtifacts = verifier.loadLines( targetDir + "/compile-artifacts.txt", "UTF-8" );
115         assertTrue( compileArtifacts.toString(), compileArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
116         assertTrue( compileArtifacts.toString(), compileArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
117         assertTrue( compileArtifacts.toString(), compileArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
118         assertEquals( 3, compileArtifacts.size() );
119 
120         List<String> compileClassPath = verifier.loadLines( targetDir + "/compile-cp.txt", "UTF-8" );
121         assertTrue( compileClassPath.toString(), compileClassPath.contains( "classes" ) );
122         assertTrue( compileClassPath.toString(), compileClassPath.contains( "direct-0.1.jar" ) );
123         assertTrue( compileClassPath.toString(), compileClassPath.contains( "compile-0.1.jar" ) );
124         assertTrue( compileClassPath.toString(), compileClassPath.contains( "runtime-0.1.jar" ) );
125         assertEquals( 4, compileClassPath.size() );
126 
127         List<String> runtimeArtifacts = verifier.loadLines( targetDir + "/runtime-artifacts.txt", "UTF-8" );
128         assertEquals( 0, runtimeArtifacts.size() );
129 
130         List<String> runtimeClassPath = verifier.loadLines( targetDir + "/runtime-cp.txt", "UTF-8" );
131         assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "classes" ) );
132         assertEquals( 1, runtimeClassPath.size() );
133 
134         List<String> testArtifacts = verifier.loadLines( targetDir + "/test-artifacts.txt", "UTF-8" );
135         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
136         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
137         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
138         assertEquals( 3, testArtifacts.size() );
139 
140         List<String> testClassPath = verifier.loadLines( targetDir + "/test-cp.txt", "UTF-8" );
141         assertTrue( testClassPath.toString(), testClassPath.contains( "classes" ) );
142         assertTrue( testClassPath.toString(), testClassPath.contains( "test-classes" ) );
143         assertTrue( testClassPath.toString(), testClassPath.contains( "direct-0.1.jar" ) );
144         assertTrue( testClassPath.toString(), testClassPath.contains( "compile-0.1.jar" ) );
145         assertTrue( testClassPath.toString(), testClassPath.contains( "runtime-0.1.jar" ) );
146         assertEquals( 5, testClassPath.size() );
147     }
148 
149     /**
150      * Test that the different scopes of transitive dependencies end up on the right class paths when mediated from
151      * a runtime-scope dependency.
152      *
153      * @throws Exception in case of failure
154      */
155     public void testitRuntimeScope()
156         throws Exception
157     {
158         Verifier verifier = run( "runtime" );
159         String targetDir = "target-runtime";
160 
161         List<String> compileArtifacts = verifier.loadLines( targetDir + "/compile-artifacts.txt", "UTF-8" );
162         assertEquals( 0, compileArtifacts.size() );
163 
164         List<String> compileClassPath = verifier.loadLines( targetDir + "/compile-cp.txt", "UTF-8" );
165         assertTrue( compileClassPath.toString(), compileClassPath.contains( "classes" ) );
166         assertEquals( 1, compileClassPath.size() );
167 
168         List<String> runtimeArtifacts = verifier.loadLines( targetDir + "/runtime-artifacts.txt", "UTF-8" );
169         assertTrue( runtimeArtifacts.toString(), runtimeArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
170         assertTrue( runtimeArtifacts.toString(), runtimeArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
171         assertTrue( runtimeArtifacts.toString(), runtimeArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
172         assertEquals( 3, runtimeArtifacts.size() );
173 
174         List<String> runtimeClassPath = verifier.loadLines( targetDir + "/runtime-cp.txt", "UTF-8" );
175         assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "classes" ) );
176         assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "direct-0.1.jar" ) );
177         assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "compile-0.1.jar" ) );
178         assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "runtime-0.1.jar" ) );
179         assertEquals( 4, runtimeClassPath.size() );
180 
181         List<String> testArtifacts = verifier.loadLines( targetDir + "/test-artifacts.txt", "UTF-8" );
182         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
183         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
184         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
185         assertEquals( 3, testArtifacts.size() );
186 
187         List<String> testClassPath = verifier.loadLines( targetDir + "/test-cp.txt", "UTF-8" );
188         assertTrue( testClassPath.toString(), testClassPath.contains( "classes" ) );
189         assertTrue( testClassPath.toString(), testClassPath.contains( "test-classes" ) );
190         assertTrue( testClassPath.toString(), testClassPath.contains( "direct-0.1.jar" ) );
191         assertTrue( testClassPath.toString(), testClassPath.contains( "compile-0.1.jar" ) );
192         assertTrue( testClassPath.toString(), testClassPath.contains( "runtime-0.1.jar" ) );
193         assertEquals( 5, testClassPath.size() );
194     }
195 
196     /**
197      * Test that the different scopes of transitive dependencies end up on the right class paths when mediated from
198      * a test-scope dependency.
199      *
200      * @throws Exception in case of failure
201      */
202     public void testitTestScope()
203         throws Exception
204     {
205         Verifier verifier = run( "test" );
206         String targetDir = "target-test";
207 
208         List<String> compileArtifacts = verifier.loadLines( targetDir + "/compile-artifacts.txt", "UTF-8" );
209         assertEquals( 0, compileArtifacts.size() );
210 
211         List<String> compileClassPath = verifier.loadLines( targetDir + "/compile-cp.txt", "UTF-8" );
212         assertTrue( compileClassPath.toString(), compileClassPath.contains( "classes" ) );
213         assertEquals( 1, compileClassPath.size() );
214 
215         List<String> runtimeArtifacts = verifier.loadLines( targetDir + "/runtime-artifacts.txt", "UTF-8" );
216         assertEquals( 0, runtimeArtifacts.size() );
217 
218         List<String> runtimeClassPath = verifier.loadLines( targetDir + "/runtime-cp.txt", "UTF-8" );
219         assertTrue( runtimeClassPath.toString(), runtimeClassPath.contains( "classes" ) );
220         assertEquals( 1, runtimeClassPath.size() );
221 
222         List<String> testArtifacts = verifier.loadLines( targetDir + "/test-artifacts.txt", "UTF-8" );
223         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:direct:jar:0.1" ) );
224         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:compile:jar:0.1" ) );
225         assertTrue( testArtifacts.toString(), testArtifacts.contains( "org.apache.maven.its.it0143:runtime:jar:0.1" ) );
226         assertEquals( 3, testArtifacts.size() );
227 
228         List<String> testClassPath = verifier.loadLines( targetDir + "/test-cp.txt", "UTF-8" );
229         assertTrue( testClassPath.toString(), testClassPath.contains( "classes" ) );
230         assertTrue( testClassPath.toString(), testClassPath.contains( "test-classes" ) );
231         assertTrue( testClassPath.toString(), testClassPath.contains( "direct-0.1.jar" ) );
232         assertTrue( testClassPath.toString(), testClassPath.contains( "compile-0.1.jar" ) );
233         assertTrue( testClassPath.toString(), testClassPath.contains( "runtime-0.1.jar" ) );
234         assertEquals( 5, testClassPath.size() );
235     }
236 
237     private Verifier run( String scope )
238         throws Exception
239     {
240         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/it0143" );
241 
242         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
243         verifier.setAutoclean( false );
244         verifier.deleteDirectory( "target-" + scope );
245         verifier.deleteArtifacts( "org.apache.maven.its.it0143" );
246         Properties filterProps = verifier.newDefaultFilterProperties();
247         filterProps.setProperty( "@scope@", scope );
248         verifier.filterFile( "pom-template.xml", "pom.xml", "UTF-8", filterProps );
249         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
250         verifier.addCliOption( "--settings" );
251         verifier.addCliOption( "settings.xml" );
252         verifier.setLogFileName( "log-" + scope + ".txt" );
253         verifier.executeGoal( "validate" );
254         verifier.verifyErrorFreeLog();
255         verifier.resetStreams();
256 
257         return verifier;
258     }
259 
260 }