View Javadoc
1   /*
2    * Copyright 2014 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.maven.it;
17  
18  import java.io.File;
19  import java.util.List;
20  import java.util.regex.Pattern;
21  import org.apache.maven.it.util.ResourceExtractor;
22  
23  public class MavenITmng2199ParentVersionRangeTest
24      extends AbstractMavenIntegrationTestCase
25  {
26  
27      public MavenITmng2199ParentVersionRangeTest()
28      {
29          super( "[3.2.2,)" );
30      }
31  
32      public void testValidParentVersionRangeWithInclusiveUpperBound()
33          throws Exception
34      {
35          failingMavenVersions( "(3.2.2,3.5.0-alpha-0)" );
36          Verifier verifier = null;
37          File testDir =
38              ResourceExtractor.simpleExtractResources( getClass(),
39                                                        "/mng-2199-parent-version-range/valid-inclusive-upper-bound" );
40  
41          try
42          {
43              verifier = newVerifier( testDir.getAbsolutePath(), "remote" );
44              verifier.addCliOption( "-U" );
45              verifier.setAutoclean( false );
46  
47              verifier.executeGoal( "verify" );
48              verifier.verifyErrorFreeLog();
49  
50              // All Maven versions not supporting remote parent version ranges will log a warning message whenever
51              // building a parent fails. The build succeeds without any parent. If that warning message appears in the
52              // log, parent resolution failed.
53              final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
54              assertFalse( "Unxpected error message found.",
55                           indexOf( lines, ".*Failed to build parent project.*" ) >= 0 );
56  
57          }
58          finally
59          {
60              if ( verifier != null )
61              {
62                  verifier.resetStreams();
63              }
64          }
65      }
66  
67      public void testValidParentVersionRangeWithExclusiveUpperBound()
68          throws Exception
69      {
70          failingMavenVersions( "(3.2.2,3.5.0-alpha-0)" );
71          Verifier verifier = null;
72          File testDir = ResourceExtractor.simpleExtractResources(
73              getClass(), "/mng-2199-parent-version-range/valid-exclusive-upper-bound" );
74  
75          try
76          {
77              verifier = newVerifier( testDir.getAbsolutePath(), "remote" );
78              verifier.addCliOption( "-U" );
79              verifier.setAutoclean( false );
80  
81              verifier.executeGoal( "verify" );
82              verifier.verifyErrorFreeLog();
83  
84              // All Maven versions not supporting remote parent version ranges will log a warning message whenever
85              // building a parent fails. The build succeeds without any parent. If that warning message appears in the
86              // log, parent resolution failed.
87              final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
88              assertFalse( "Unxpected error message found.",
89                           indexOf( lines, ".*Failed to build parent project.*" ) >= 0 );
90  
91          }
92          finally
93          {
94              if ( verifier != null )
95              {
96                  verifier.resetStreams();
97              }
98          }
99      }
100 
101     public void testInvalidParentVersionRangeWithoutUpperBound()
102         throws Exception
103     {
104         Verifier verifier = null;
105         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2199-parent-version-range/invalid" );
106 
107         try
108         {
109             verifier = newVerifier( testDir.getAbsolutePath(), "remote" );
110             verifier.setAutoclean( false );
111             verifier.addCliOption( "-U" );
112             verifier.executeGoal( "verify" );
113             fail( "Expected 'VerificationException' not thrown." );
114         }
115         catch ( final VerificationException e )
116         {
117             assertNotNull( verifier );
118             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
119             assertTrue( "Expected error message not found.",
120                         indexOf( lines, ".*(parent)? version range.*does not specify an upper bound.*" ) >= 0 );
121 
122         }
123         finally
124         {
125             if ( verifier != null )
126             {
127                 verifier.resetStreams();
128             }
129         }
130     }
131 
132     public void testValidParentVersionRangeInvalidVersionExpression()
133         throws Exception
134     {
135         Verifier verifier = null;
136         File testDir = ResourceExtractor.simpleExtractResources(
137             getClass(), "/mng-2199-parent-version-range/expression" );
138 
139         try
140         {
141             verifier = newVerifier( testDir.getAbsolutePath(), "remote" );
142             verifier.setAutoclean( false );
143             verifier.addCliOption( "-U" );
144             verifier.executeGoal( "verify" );
145             fail( "Expected 'VerificationException' not thrown." );
146         }
147         catch ( final VerificationException e )
148         {
149             assertNotNull( verifier );
150             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
151             int msg = indexOf( lines, ".*Version must be a constant.*org.apache.maven.its.mng2199:expression.*" );
152             assertTrue( "Expected error message not found.", msg >= 0 );
153         }
154         finally
155         {
156             if ( verifier != null )
157             {
158                 verifier.resetStreams();
159             }
160         }
161     }
162 
163     public void testValidParentVersionRangeInvalidVersionInheritance()
164         throws Exception
165     {
166         Verifier verifier = null;
167         File testDir = ResourceExtractor.simpleExtractResources(
168             getClass(), "/mng-2199-parent-version-range/inherited" );
169 
170         try
171         {
172             verifier = newVerifier( testDir.getAbsolutePath(), "remote" );
173             verifier.setAutoclean( false );
174             verifier.addCliOption( "-U" );
175             verifier.executeGoal( "verify" );
176             fail( "Expected 'VerificationException' not thrown." );
177         }
178         catch ( final VerificationException e )
179         {
180             assertNotNull( verifier );
181             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
182             int msg = indexOf( lines, ".*Version must be a constant.*org.apache.maven.its.mng2199:inherited.*" );
183             assertTrue( "Expected error message not found.", msg >= 0 );
184         }
185         finally
186         {
187             if ( verifier != null )
188             {
189                 verifier.resetStreams();
190             }
191         }
192     }
193 
194     public void testValidLocalParentVersionRange()
195         throws Exception
196     {
197         failingMavenVersions( "(,3.3.0),(3.3.9,3.5.0-alpha-0)" );
198         Verifier verifier = null;
199         File testDir = ResourceExtractor.simpleExtractResources(
200             getClass(), "/mng-2199-parent-version-range/valid-local/child" );
201 
202         try
203         {
204             verifier = newVerifier( testDir.getAbsolutePath() );
205             verifier.executeGoal( "verify" );
206             verifier.verifyErrorFreeLog();
207 
208             // All Maven versions not supporting remote parent version ranges will log a warning message whenever
209             // building a parent fails. The build succeeds without any parent. If that warning message appears in the
210             // log, parent resolution failed. For this test, this really just tests the project on disk getting tested
211             // is not corrupt. It's expected to find the local parent and not fall back to remote resolution. If it
212             // falls back to remote resolution, this just catches the test project to be broken.
213             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
214             assertFalse( "Unxpected error message found.",
215                          indexOf( lines, ".*Failed to build parent project.*" ) >= 0 );
216 
217         }
218         finally
219         {
220             if ( verifier != null )
221             {
222                 verifier.resetStreams();
223             }
224         }
225     }
226 
227     public void testInvalidLocalParentVersionRange()
228         throws Exception
229     {
230         failingMavenVersions( "[3.3.0,3.3.9)" );
231         // Fallback to remote resolution not tested here. Remote parent expected to not be available anywhere.
232         Verifier verifier = null;
233         File testDir = ResourceExtractor.simpleExtractResources(
234             getClass(), "/mng-2199-parent-version-range/invalid-local/child" );
235 
236         try
237         {
238             verifier = newVerifier( testDir.getAbsolutePath() );
239             verifier.executeGoal( "verify" );
240             fail( "Expected 'VerificationException' not thrown." );
241         }
242         catch ( final VerificationException e )
243         {
244             assertNotNull( verifier );
245             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
246             int msg = indexOf( lines,
247                                ".*Non-resolvable parent POM org.apache.maven.its.mng2199:local-parent:\\[2,3\\].*" );
248             assertTrue( "Expected error message not found.", msg >= 0 );
249         }
250         finally
251         {
252             if ( verifier != null )
253             {
254                 verifier.resetStreams();
255             }
256         }
257     }
258 
259     public void testInvalidLocalParentVersionRangeFallingBackToRemote()
260         throws Exception
261     {
262         failingMavenVersions( "[3.3.9]" );
263         Verifier verifier = null;
264         File testDir = ResourceExtractor.simpleExtractResources(
265             getClass(), "/mng-2199-parent-version-range/local-fallback-to-remote/child" );
266 
267         try
268         {
269             verifier = newVerifier( testDir.getAbsolutePath(), "remote" );
270             verifier.executeGoal( "verify" );
271             verifier.verifyErrorFreeLog();
272 
273             // All Maven versions not supporting remote parent version ranges will log a warning message whenever
274             // building a parent fails. The build succeeds without any parent. If that warning message appears in the
275             // log, parent resolution failed. For this test, local parent resolution falls back to remote parent
276             // resolution with a version range in use. If the warning message is in the logs, that remote parent
277             // resolution failed unexpectedly.
278             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
279             assertFalse( "Unxpected error message found.",
280                          indexOf( lines, ".*Failed to build parent project.*" ) >= 0 );
281 
282         }
283         finally
284         {
285             if ( verifier != null )
286             {
287                 verifier.resetStreams();
288             }
289         }
290     }
291 
292     public void testValidLocalParentVersionRangeInvalidVersionExpression()
293         throws Exception
294     {
295         failingMavenVersions( "(,3.5.0-alpha-0)" );
296         Verifier verifier = null;
297         File testDir = ResourceExtractor.simpleExtractResources(
298             getClass(), "/mng-2199-parent-version-range/expression-local/child" );
299 
300         try
301         {
302             verifier = newVerifier( testDir.getAbsolutePath() );
303             verifier.executeGoal( "verify" );
304             fail( "Expected 'VerificationException' not thrown." );
305         }
306         catch ( final VerificationException e )
307         {
308             assertNotNull( verifier );
309             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
310             int msg = indexOf( lines, ".*Version must be a constant.*org.apache.maven.its.mng2199:expression.*" );
311             assertTrue( "Expected error message not found.", msg >= 0 );
312         }
313         finally
314         {
315             if ( verifier != null )
316             {
317                 verifier.resetStreams();
318             }
319         }
320     }
321 
322     public void testValidLocalParentVersionRangeInvalidVersionInheritance()
323         throws Exception
324     {
325         failingMavenVersions( "(,3.5.0-alpha-0)" );
326         Verifier verifier = null;
327         File testDir = ResourceExtractor.simpleExtractResources(
328             getClass(), "/mng-2199-parent-version-range/inherited-local/child" );
329 
330         try
331         {
332             verifier = newVerifier( testDir.getAbsolutePath() );
333             verifier.executeGoal( "verify" );
334             fail( "Expected 'VerificationException' not thrown." );
335         }
336         catch ( final VerificationException e )
337         {
338             assertNotNull( verifier );
339             final List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
340             int msg = indexOf( lines, ".*Version must be a constant.*org.apache.maven.its.mng2199:inherited.*" );
341             assertTrue( "Expected error message not found.", msg >= 0 );
342         }
343         finally
344         {
345             if ( verifier != null )
346             {
347                 verifier.resetStreams();
348             }
349         }
350     }
351 
352     private static int indexOf( final List<String> logLines, final String regex )
353     {
354         final Pattern pattern = Pattern.compile( regex );
355 
356         for ( int i = 0, l0 = logLines.size(); i < l0; i++ )
357         {
358             if ( pattern.matcher( logLines.get( i ) ).matches() )
359             {
360                 return i;
361             }
362         }
363 
364         return -1;
365     }
366 
367 }