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