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  
27  /**
28   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3043">MNG-3043</a>.
29   *
30   * @author Benjamin Bentmann
31   */
32  public class MavenITmng3043BestEffortReactorResolutionTest
33      extends AbstractMavenIntegrationTestCase
34  {
35  
36      public MavenITmng3043BestEffortReactorResolutionTest()
37      {
38          super( "[3.0-alpha-3,)" );
39      }
40  
41      /**
42       * Test that dependencies on attached artifacts like a test JAR or an EJB client JAR which have not been built
43       * yet, i.e. in build phases prior to "package" like "test", are satisfied from the output directories of the
44       * projects in the reactor. This is meant as a best effort to provide a class path for compilation or testing.
45       *
46       * @throws Exception in case of failure
47       */
48      public void testitTestPhase()
49          throws Exception
50      {
51          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3043" );
52  
53          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
54          verifier.setAutoclean( false );
55          verifier.deleteDirectory( "consumer-a/target" );
56          verifier.deleteDirectory( "consumer-b/target" );
57          verifier.deleteDirectory( "consumer-c/target" );
58          verifier.deleteArtifacts( "org.apache.maven.its.mng3043" );
59          verifier.setLogFileName( "log-test.txt" );
60          verifier.executeGoal( "test" );
61          verifier.verifyErrorFreeLog();
62          verifier.resetStreams();
63  
64          List<String> classpath;
65  
66          classpath = verifier.loadLines( "consumer-a/target/compile.txt", "UTF-8" );
67          assertContains( classpath, new String[] { "classes-test" } );
68          assertNotContains( classpath, new String[] { "classes-main" } );
69          classpath = verifier.loadLines( "consumer-a/target/runtime.txt", "UTF-8" );
70          assertContains( classpath, new String[] { "classes-test" } );
71          assertNotContains( classpath, new String[] { "classes-main" } );
72          classpath = verifier.loadLines( "consumer-a/target/test.txt", "UTF-8" );
73          assertContains( classpath, new String[] { "classes-test" } );
74          assertNotContains( classpath, new String[] { "classes-main" } );
75  
76          classpath = verifier.loadLines( "consumer-b/target/compile.txt", "UTF-8" );
77          assertContains( classpath, new String[] { "classes-main" } );
78          assertNotContains( classpath, new String[] { "classes-test" } );
79          classpath = verifier.loadLines( "consumer-b/target/runtime.txt", "UTF-8" );
80          assertContains( classpath, new String[] { "classes-main" } );
81          assertNotContains( classpath, new String[] { "classes-test" } );
82          classpath = verifier.loadLines( "consumer-b/target/test.txt", "UTF-8" );
83          assertContains( classpath, new String[] { "classes-main" } );
84          assertNotContains( classpath, new String[] { "classes-test" } );
85  
86          classpath = verifier.loadLines( "consumer-c/target/compile.txt", "UTF-8" );
87          assertContains( classpath, new String[] { "classes-main" } );
88          assertContains( classpath, new String[] { "classes-test" } );
89          classpath = verifier.loadLines( "consumer-c/target/runtime.txt", "UTF-8" );
90          assertContains( classpath, new String[] { "classes-main" } );
91          assertContains( classpath, new String[] { "classes-test" } );
92          classpath = verifier.loadLines( "consumer-c/target/test.txt", "UTF-8" );
93          assertContains( classpath, new String[] { "classes-main" } );
94          assertContains( classpath, new String[] { "classes-test" } );
95      }
96  
97      /**
98       * Test that dependency resolution still uses the actual artifact files once these have been
99       * assembled/attached in the "package" phase. This ensures the class path is accurate and not locked to
100      * the output directories of the best effort model from above.
101      *
102      * @throws Exception in case of failure
103      */
104     public void testitPackagePhase()
105         throws Exception
106     {
107         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3043" );
108 
109         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
110         verifier.setAutoclean( false );
111         verifier.deleteDirectory( "consumer-a/target" );
112         verifier.deleteDirectory( "consumer-b/target" );
113         verifier.deleteDirectory( "consumer-c/target" );
114         verifier.deleteArtifacts( "org.apache.maven.its.mng3043" );
115         verifier.setLogFileName( "log-package.txt" );
116         verifier.executeGoal( "package" );
117         verifier.verifyErrorFreeLog();
118         verifier.resetStreams();
119 
120         List<String> classpath;
121 
122         classpath = verifier.loadLines( "consumer-a/target/compile.txt", "UTF-8" );
123         assertContains( classpath, new String[] { "tests.jar" } );
124         assertNotContains( classpath, new String[] { "client.jar" } );
125         classpath = verifier.loadLines( "consumer-a/target/runtime.txt", "UTF-8" );
126         assertContains( classpath, new String[] { "tests.jar" } );
127         assertNotContains( classpath, new String[] { "client.jar" } );
128         classpath = verifier.loadLines( "consumer-a/target/test.txt", "UTF-8" );
129         assertContains( classpath, new String[] { "tests.jar" } );
130         assertNotContains( classpath, new String[] { "client.jar" } );
131 
132         classpath = verifier.loadLines( "consumer-b/target/compile.txt", "UTF-8" );
133         assertContains( classpath, new String[] { "client.jar" } );
134         assertNotContains( classpath, new String[] { "tests.jar" } );
135         classpath = verifier.loadLines( "consumer-b/target/runtime.txt", "UTF-8" );
136         assertContains( classpath, new String[] { "client.jar" } );
137         assertNotContains( classpath, new String[] { "tests.jar" } );
138         classpath = verifier.loadLines( "consumer-b/target/test.txt", "UTF-8" );
139         assertContains( classpath, new String[] { "client.jar" } );
140         assertNotContains( classpath, new String[] { "tests.jar" } );
141 
142         classpath = verifier.loadLines( "consumer-c/target/compile.txt", "UTF-8" );
143         assertContains( classpath, new String[] { "client.jar" } );
144         assertContains( classpath, new String[] { "tests.jar" } );
145         classpath = verifier.loadLines( "consumer-c/target/runtime.txt", "UTF-8" );
146         assertContains( classpath, new String[] { "client.jar" } );
147         assertContains( classpath, new String[] { "tests.jar" } );
148         classpath = verifier.loadLines( "consumer-c/target/test.txt", "UTF-8" );
149         assertContains( classpath, new String[] { "client.jar" } );
150         assertContains( classpath, new String[] { "tests.jar" } );
151     }
152 
153     private void assertContains( List<String> collection, String[] items )
154     {
155         for ( String item : items )
156         {
157             assertContains( collection, item );
158         }
159     }
160 
161     private void assertContains( List<String> collection, String item )
162     {
163         assertTrue( item + " missing in " + collection, collection.contains( item ) );
164     }
165 
166     private void assertNotContains( List<String> collection, String[] items )
167     {
168         for ( String item : items )
169         {
170             assertNotContains( collection, item );
171         }
172     }
173 
174     private void assertNotContains( List<String> collection, String item )
175     {
176         assertFalse( item + " present in " + collection, collection.contains( item ) );
177     }
178 
179 }