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   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-1895">MNG-1895</a>.
30   *
31   * @author Benjamin Bentmann
32   */
33  public class MavenITmng1895ScopeConflictResolutionTest
34      extends AbstractMavenIntegrationTestCase
35  {
36  
37      public MavenITmng1895ScopeConflictResolutionTest()
38      {
39          super( "[2.0.3,)" );
40      }
41  
42      /**
43       * Verify that for a dependency being referenced in two different scopes, the scope given directly in the POM
44       * always wins, even if weaker than the scope of the transitive dependency.
45       *
46       * @throws Exception in case of failure
47       */
48      public void testitDirectVsIndirect()
49          throws Exception
50      {
51          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-1895/direct-vs-indirect" );
52  
53          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
54          verifier.setAutoclean( false );
55          verifier.deleteArtifacts( "org.apache.maven.its.mng1895" );
56          verifier.deleteDirectory( "target" );
57          verifier.addCliOption( "-s" );
58          verifier.addCliOption( "settings.xml" );
59          verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", verifier.newDefaultFilterProperties() );
60          verifier.executeGoal( "validate" );
61          verifier.verifyErrorFreeLog();
62          verifier.resetStreams();
63  
64          List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
65          assertTrue( compile.toString(), compile.contains( "a-0.1.jar" ) );
66          assertFalse( compile.toString(), compile.contains( "b-0.1.jar" ) );
67          assertFalse( compile.toString(), compile.contains( "c-0.1.jar" ) );
68          assertTrue( compile.toString(), compile.contains( "d-0.1.jar" ) );
69  
70          List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
71          assertFalse( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
72          assertTrue( runtime.toString(), runtime.contains( "b-0.1.jar" ) );
73          assertFalse( runtime.toString(), runtime.contains( "c-0.1.jar" ) );
74          assertTrue( runtime.toString(), runtime.contains( "d-0.1.jar" ) );
75  
76          List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
77          assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
78          assertTrue( test.toString(), test.contains( "b-0.1.jar" ) );
79          assertTrue( test.toString(), test.contains( "c-0.1.jar" ) );
80          assertTrue( test.toString(), test.contains( "d-0.1.jar" ) );
81      }
82  
83      /**
84       * Verify that for a dependency being referenced in compile and in runtime scope, compile scope wins.
85       *
86       * @throws Exception in case of failure
87       */
88      public void testitCompileVsRuntime()
89          throws Exception
90      {
91          Verifier verifier = run( "compile", "runtime" );
92  
93          List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
94          assertTrue( compile.toString(), compile.contains( "x-0.1.jar" ) );
95          assertFalse( compile.toString(), compile.contains( "a-0.1.jar" ) );
96  
97          List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
98          assertTrue( runtime.toString(), runtime.contains( "x-0.1.jar" ) );
99          assertTrue( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
100 
101         List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
102         assertTrue( test.toString(), test.contains( "x-0.1.jar" ) );
103         assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
104     }
105 
106     /**
107      * Verify that for a dependency being referenced in compile and in test scope, compile scope wins.
108      *
109      * @throws Exception in case of failure
110      */
111     public void testitCompileVsTest()
112         throws Exception
113     {
114         Verifier verifier = run( "compile", "test" );
115 
116         List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
117         assertTrue( compile.toString(), compile.contains( "x-0.1.jar" ) );
118         assertFalse( compile.toString(), compile.contains( "a-0.1.jar" ) );
119 
120         List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
121         assertTrue( runtime.toString(), runtime.contains( "x-0.1.jar" ) );
122         assertFalse( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
123 
124         List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
125         assertTrue( test.toString(), test.contains( "x-0.1.jar" ) );
126         assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
127     }
128 
129     /**
130      * Verify that for a dependency being referenced in compile and in provided scope, compile scope wins.
131      *
132      * @throws Exception in case of failure
133      */
134     public void testitCompileVsProvided()
135         throws Exception
136     {
137         Verifier verifier = run( "compile", "provided" );
138 
139         List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
140         assertTrue( compile.toString(), compile.contains( "x-0.1.jar" ) );
141         assertTrue( compile.toString(), compile.contains( "a-0.1.jar" ) );
142 
143         List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
144         assertTrue( runtime.toString(), runtime.contains( "x-0.1.jar" ) );
145         assertFalse( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
146 
147         List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
148         assertTrue( test.toString(), test.contains( "x-0.1.jar" ) );
149         assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
150     }
151 
152     /**
153      * Verify that for a dependency being referenced in runtime and in test scope, runtime scope wins.
154      *
155      * @throws Exception in case of failure
156      */
157     public void testitRuntimeVsTest()
158         throws Exception
159     {
160         Verifier verifier = run( "runtime", "test" );
161 
162         List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
163         assertFalse( compile.toString(), compile.contains( "x-0.1.jar" ) );
164         assertFalse( compile.toString(), compile.contains( "a-0.1.jar" ) );
165 
166         List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
167         assertTrue( runtime.toString(), runtime.contains( "x-0.1.jar" ) );
168         assertFalse( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
169 
170         List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
171         assertTrue( test.toString(), test.contains( "x-0.1.jar" ) );
172         assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
173     }
174 
175     /**
176      * Verify that for a dependency being referenced in runtime and in provided scope, runtime scope wins.
177      *
178      * @throws Exception in case of failure
179      */
180     public void testitRuntimeVsProvided()
181         throws Exception
182     {
183         Verifier verifier = run( "runtime", "provided" );
184 
185         List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
186         assertFalse( compile.toString(), compile.contains( "x-0.1.jar" ) );
187         assertTrue( compile.toString(), compile.contains( "a-0.1.jar" ) );
188 
189         List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
190         assertTrue( runtime.toString(), runtime.contains( "x-0.1.jar" ) );
191         assertFalse( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
192 
193         List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
194         assertTrue( test.toString(), test.contains( "x-0.1.jar" ) );
195         assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
196     }
197 
198     /**
199      * Verify that for a dependency being referenced in provided and in test scope, provided scope wins.
200      *
201      * @throws Exception in case of failure
202      */
203     public void testitProvidedVsTest()
204         throws Exception
205     {
206         requiresMavenVersion( "[3.0-beta-3,)" ); // MNG-2686
207 
208         Verifier verifier = run( "provided", "test" );
209 
210         List<String> compile = verifier.loadLines( "target/compile.txt", "UTF-8" );
211         assertTrue( compile.toString(), compile.contains( "x-0.1.jar" ) );
212         assertFalse( compile.toString(), compile.contains( "a-0.1.jar" ) );
213 
214         List<String> runtime = verifier.loadLines( "target/runtime.txt", "UTF-8" );
215         assertFalse( runtime.toString(), runtime.contains( "x-0.1.jar" ) );
216         assertFalse( runtime.toString(), runtime.contains( "a-0.1.jar" ) );
217 
218         List<String> test = verifier.loadLines( "target/test.txt", "UTF-8" );
219         assertTrue( test.toString(), test.contains( "x-0.1.jar" ) );
220         assertTrue( test.toString(), test.contains( "a-0.1.jar" ) );
221     }
222 
223     private Verifier run( String scopeB, String scopeA )
224         throws Exception
225     {
226         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-1895/strong-vs-weak" );
227 
228         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
229         verifier.setAutoclean( false );
230         verifier.deleteArtifacts( "org.apache.maven.its.mng1895" );
231         verifier.deleteDirectory( "target" );
232         verifier.addCliOption( "-s" );
233         verifier.addCliOption( "settings.xml" );
234         Properties props = verifier.newDefaultFilterProperties();
235         props.setProperty( "@scope.a@", scopeA );
236         props.setProperty( "@scope.b@", scopeB );
237         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", props );
238         verifier.filterFile( "pom-template.xml", "pom.xml", "UTF-8", props );
239         verifier.setLogFileName( "log-" + scopeB + "-vs-" + scopeA + ".txt" );
240         verifier.executeGoal( "validate" );
241         verifier.verifyErrorFreeLog();
242         verifier.resetStreams();
243 
244         return verifier;
245     }
246 
247 }