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.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /**
30   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-2883">MNG-2883</a>.
31   *
32   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
33   */
34  public class MavenITmng2883LegacyRepoOfflineTest
35      extends AbstractMavenIntegrationTestCase
36  {
37  
38      public MavenITmng2883LegacyRepoOfflineTest()
39      {
40          super( "(2.0.9,2.1.0-M1),(2.1.0-M1,3.0-alpha-1)" );
41      }
42  
43      public void testParentUnresolvable()
44          throws Exception
45      {
46          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2883" );
47          testDir = new File( testDir, "parent" );
48  
49          Verifier verifier;
50  
51          verifier = newVerifier( testDir.getAbsolutePath() );
52          verifier.setAutoclean( false );
53          verifier.deleteDirectory( "target" );
54          verifier.deleteArtifacts( "org.apache.maven.its.mng2883" );
55  
56          File settings = verifier.filterFile( "../settings-template.xml", "settings.xml", "UTF-8",
57                                               verifier.newDefaultFilterProperties() );
58  
59          // used to inject the remote repository
60          verifier.addCliOption( "-s" );
61          verifier.addCliOption( settings.getName() );
62  
63          // execute once just to make sure this test works at all!
64          try
65          {
66              verifier.setLogFileName( "log-parent-a.txt" );
67              // this will ensure that all relevant plugins are present.
68              verifier.executeGoal( "validate" );
69          }
70          catch ( VerificationException e )
71          {
72              throw new VerificationException( "Build should succeed the first time through when NOT in offline mode!",
73                                               e );
74          }
75  
76          // the centerpiece of these tests!
77          verifier.addCliOption( "-o" );
78  
79          // re-run in offline mode, should still succeed by using local repo
80          verifier.setLogFileName( "log-parent-b.txt" );
81          verifier.executeGoal( "validate" );
82  
83          // clear out the parent POM if it's in the local repository.
84          verifier.deleteArtifacts( "org.apache.maven.its.mng2883" );
85  
86          try
87          {
88              verifier.setLogFileName( "log-parent-c.txt" );
89              verifier.executeGoal( "validate" );
90  
91              fail( "Build should fail with unresolvable parent POM." );
92          }
93          catch ( VerificationException e )
94          {
95              // expected
96          }
97  
98          List<String> missingMessages = new ArrayList<>();
99          missingMessages.add( " is offline" );
100         missingMessages.add( "org.apache.maven.its.mng2883:parent:pom:1.0-SNAPSHOT" );
101 
102         List<String> lines = verifier.loadFile( new File( testDir, verifier.getLogFileName() ), false );
103 
104         for ( String line : lines )
105         {
106             for ( Iterator<String> messageIt = missingMessages.iterator(); messageIt.hasNext(); )
107             {
108                 String message = messageIt.next();
109 
110                 if ( line.contains( message ) )
111                 {
112                     messageIt.remove();
113                 }
114             }
115         }
116 
117         if ( !missingMessages.isEmpty() )
118         {
119             StringBuilder buffer = new StringBuilder();
120 
121             buffer.append( "The following key messages were missing from build output:\n\n" );
122 
123             for ( String message : missingMessages )
124             {
125                 if ( buffer.length() < 1 )
126                 {
127                     buffer.append( "\n" );
128                 }
129                 buffer.append( '\'' ).append( message ).append( '\'' );
130             }
131 
132             fail( buffer.toString() );
133         }
134     }
135 
136     public void testDependencyUnresolvable()
137         throws Exception
138     {
139         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2883" );
140         testDir = new File( testDir, "dependency" );
141 
142         Verifier verifier;
143 
144         verifier = newVerifier( testDir.getAbsolutePath() );
145         verifier.setAutoclean( false );
146         verifier.deleteDirectory( "target" );
147         verifier.deleteArtifacts( "org.apache.maven.its.mng2883" );
148 
149         File settings = verifier.filterFile( "../settings-template.xml", "settings.xml", "UTF-8",
150                                              verifier.newDefaultFilterProperties() );
151 
152         // used to inject the remote repository
153         verifier.addCliOption( "-s" );
154         verifier.addCliOption( settings.getName() );
155 
156         // execute once just to make sure this test works at all!
157         try
158         {
159             verifier.setLogFileName( "log-dep-a.txt" );
160             // this will ensure that all relevant plugins are present.
161             verifier.executeGoal( "validate" );
162         }
163         catch ( VerificationException e )
164         {
165             throw new VerificationException( "Build should succeed the first time through when NOT in offline mode!",
166                                              e );
167         }
168 
169         // the centerpiece of these tests!
170         verifier.addCliOption( "-o" );
171 
172         // re-run in offline mode, should still succeed by using local repo
173         verifier.setLogFileName( "log-dep-b.txt" );
174         verifier.executeGoal( "validate" );
175 
176         // clear out the dependency if it's in the local repository.
177         verifier.deleteArtifacts( "org.apache.maven.its.mng2883" );
178 
179         try
180         {
181             verifier.setLogFileName( "log-dep-c.txt" );
182             verifier.executeGoal( "validate" );
183 
184             fail( "Build should fail with unresolvable dependency artifact." );
185         }
186         catch ( VerificationException e )
187         {
188             // expected
189         }
190 
191         List<String> missingMessages = new ArrayList<>();
192 
193         // FIXME: We need a more prominent diagnosis including system being in offline mode for 2.0.x.
194         missingMessages.add( "offline mode." );
195         missingMessages.add( "org.apache.maven.its.mng2883:dep:jar:1.0-SNAPSHOT" );
196 
197         List<String> lines = verifier.loadFile( new File( testDir, verifier.getLogFileName() ), false );
198 
199         for ( String line : lines )
200         {
201             for ( Iterator<String> messageIt = missingMessages.iterator(); messageIt.hasNext(); )
202             {
203                 String message = (String) messageIt.next();
204 
205                 if ( line.contains( message ) )
206                 {
207                     messageIt.remove();
208                 }
209             }
210         }
211 
212         if ( !missingMessages.isEmpty() )
213         {
214             StringBuilder buffer = new StringBuilder();
215 
216             buffer.append( "The following key messages were missing from build output:\n\n" );
217 
218             for ( String message : missingMessages )
219             {
220                 if ( buffer.length() < 1 )
221                 {
222                     buffer.append( "\n" );
223                 }
224                 buffer.append( '\'' ).append( message ).append( '\'' );
225             }
226 
227             fail( buffer.toString() );
228         }
229     }
230 
231     public void testPluginUnresolvable()
232         throws Exception
233     {
234         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2883" );
235         testDir = new File( testDir, "plugin" );
236 
237         Verifier verifier;
238 
239         verifier = newVerifier( testDir.getAbsolutePath() );
240         verifier.setAutoclean( false );
241         verifier.deleteDirectory( "target" );
242         verifier.deleteArtifacts( "org.apache.maven.its.mng2883" );
243 
244         File settings = verifier.filterFile( "../settings-template.xml", "settings.xml", "UTF-8",
245                                              verifier.newDefaultFilterProperties() );
246 
247         // used to inject the remote repository
248         verifier.addCliOption( "-s" );
249         verifier.addCliOption( settings.getName() );
250 
251         verifier.setLogFileName( "log-plugin-a.txt" );
252         verifier.executeGoal( "org.apache.maven.its.mng2883:plugin:1.0-SNAPSHOT:run" );
253 
254         // the centerpiece of these tests!
255         verifier.addCliOption( "-o" );
256 
257         // re-run in offline mode, should still succeed by using local repo
258         verifier.setLogFileName( "log-plugin-b.txt" );
259         verifier.executeGoal( "org.apache.maven.its.mng2883:plugin:1.0-SNAPSHOT:run" );
260 
261         // clear out the dependency if it's in the local repository.
262         verifier.deleteArtifacts( "org.apache.maven.its.mng2883" );
263 
264         try
265         {
266             verifier.setLogFileName( "log-plugin-c.txt" );
267             verifier.executeGoal( "org.apache.maven.its.mng2883:plugin:1.0-SNAPSHOT:run" );
268 
269             fail( "Build should fail with unresolvable plugin artifact." );
270         }
271         catch ( VerificationException e )
272         {
273             // expected
274         }
275 
276         List<String> missingMessages = new ArrayList<>();
277         missingMessages.add( " is offline" );
278         missingMessages.add( "org.apache.maven.its.mng2883:plugin" );
279 
280         List<String> lines = verifier.loadFile( new File( testDir, verifier.getLogFileName() ), false );
281 
282         for ( String line : lines )
283         {
284             for ( Iterator<String> messageIt = missingMessages.iterator(); messageIt.hasNext(); )
285             {
286                 String message = messageIt.next();
287 
288                 if ( line.contains( message ) )
289                 {
290                     messageIt.remove();
291                 }
292             }
293         }
294 
295         if ( !missingMessages.isEmpty() )
296         {
297             StringBuilder buffer = new StringBuilder();
298 
299             buffer.append( "The following key messages were missing from build output:\n\n" );
300 
301             for ( String message : missingMessages )
302             {
303                 if ( buffer.length() < 1 )
304                 {
305                     buffer.append( "\n" );
306                 }
307                 buffer.append( '\'' ).append( message ).append( '\'' );
308             }
309 
310             fail( buffer.toString() );
311         }
312     }
313 
314 }