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.Verifier;
23  import org.apache.maven.it.util.ResourceExtractor;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.PrintWriter;
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.Properties;
32  
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.mortbay.jetty.Handler;
38  import org.mortbay.jetty.Request;
39  import org.mortbay.jetty.Server;
40  import org.mortbay.jetty.handler.AbstractHandler;
41  
42  /**
43   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-768">MNG-768</a>.
44   * 
45   * @author John Casey
46   * @version $Id: MavenITmng0768OfflineModeTest.java 981712 2010-08-03 00:36:19Z bentmann $
47   */
48  public class MavenITmng0768OfflineModeTest
49      extends AbstractMavenIntegrationTestCase
50  {
51  
52      public MavenITmng0768OfflineModeTest()
53      {
54          super( ALL_MAVEN_VERSIONS );
55      }
56  
57      /**
58       * Test offline mode.
59       */
60      public void testitMNG768()
61          throws Exception
62      {
63          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-0768" );
64  
65          final List requestedUris = Collections.synchronizedList( new ArrayList() );
66  
67          Handler repoHandler = new AbstractHandler()
68          {
69              public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
70                  throws IOException, ServletException
71              {
72                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
73  
74                  requestedUris.add( request.getRequestURI() );
75  
76                  PrintWriter writer = response.getWriter();
77  
78                  response.setStatus( HttpServletResponse.SC_OK );
79  
80                  if ( request.getRequestURI().endsWith( ".pom" ) )
81                  {
82                      writer.println( "<project>" );
83                      writer.println( "  <modelVersion>4.0.0</modelVersion>" );
84                      writer.println( "  <groupId>org.apache.maven.its.mng0768</groupId>" );
85                      writer.println( "  <artifactId>dep</artifactId>" );
86                      writer.println( "  <version>0.1</version>" );
87                      writer.println( "</project>" );
88                  }
89                  else if ( request.getRequestURI().endsWith( ".jar" ) )
90                  {
91                      writer.println( "empty" );
92                  }
93                  else if ( request.getRequestURI().endsWith( ".md5" ) || request.getRequestURI().endsWith( ".sha1" ) )
94                  {
95                      response.setStatus( HttpServletResponse.SC_NOT_FOUND );
96                  }
97  
98                  ( (Request) request ).setHandled( true );
99              }
100         };
101 
102         Server server = new Server( 0 );
103         server.setHandler( repoHandler );
104         server.start();
105         int port = server.getConnectors()[0].getLocalPort();
106 
107         try
108         {
109             {
110                 // phase 1: run build in online mode to fill local repo
111                 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
112                 verifier.setAutoclean( false );
113                 verifier.deleteDirectory( "target" );
114                 verifier.deleteArtifacts( "org.apache.maven.its.mng0768" );
115                 verifier.setLogFileName( "log1.txt" );
116                 Properties props = new Properties();
117                 props.put( "@port@", Integer.toString( port ) );
118                 verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", props );
119                 verifier.getCliOptions().add( "--settings" );
120                 verifier.getCliOptions().add( "settings.xml" );
121                 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
122                 verifier.assertFilePresent( "target/compile.txt" );
123                 verifier.verifyErrorFreeLog();
124                 verifier.resetStreams();
125             }
126 
127             requestedUris.clear();
128 
129             {
130                 // phase 2: run build in offline mode to check it still passes, without network accesses
131                 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
132                 verifier.setAutoclean( false );
133                 verifier.deleteDirectory( "target" );
134                 verifier.getCliOptions().add( "-o" );
135                 verifier.getCliOptions().add( "--settings" );
136                 verifier.getCliOptions().add( "settings.xml" );
137                 verifier.setLogFileName( "log2.txt" );
138                 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
139                 verifier.assertFilePresent( "target/compile.txt" );
140                 verifier.verifyErrorFreeLog();
141                 verifier.resetStreams();
142             }
143 
144             assertTrue( requestedUris.toString(), requestedUris.isEmpty() );
145 
146             {
147                 // phase 3: delete test artifact and run build in offline mode to check it fails now
148                 // NOTE: Adding the settings again to offer Maven the bad choice of using the remote repo
149                 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
150                 verifier.setAutoclean( false );
151                 verifier.deleteDirectory( "target" );
152                 verifier.deleteArtifacts( "org.apache.maven.its.mng0768" );
153                 verifier.getCliOptions().add( "-o" );
154                 verifier.getCliOptions().add( "--settings" );
155                 verifier.getCliOptions().add( "settings.xml" );
156                 verifier.setLogFileName( "log3.txt" );
157                 try
158                 {
159                     verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
160                     verifier.verifyErrorFreeLog();
161                     fail( "Build did not fail to resolve missing dependency although Maven ought to work offline!" );
162                 }
163                 catch( VerificationException e )
164                 {
165                     // expected, should fail
166                 }
167                 verifier.resetStreams();
168             }
169 
170             assertTrue( requestedUris.toString(), requestedUris.isEmpty() );
171         }
172         finally
173         {
174             server.stop();
175         }
176     }
177 
178 }