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  import org.eclipse.jetty.server.Handler;
24  import org.eclipse.jetty.server.NetworkConnector;
25  import org.eclipse.jetty.server.Request;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.server.handler.AbstractHandler;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.PrintWriter;
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.List;
37  import java.util.Properties;
38  
39  /**
40   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-768">MNG-768</a>.
41   *
42   * @author John Casey
43   *
44   */
45  public class MavenITmng0768OfflineModeTest
46      extends AbstractMavenIntegrationTestCase
47  {
48  
49      public MavenITmng0768OfflineModeTest()
50      {
51          super( ALL_MAVEN_VERSIONS );
52      }
53  
54      /**
55       * Test offline mode.
56       *
57       * @throws Exception in case of failure
58       */
59      public void testitMNG768()
60          throws Exception
61      {
62          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-0768" );
63  
64          final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
65  
66          Handler repoHandler = new AbstractHandler()
67          {
68              public void handle( String target, Request baseRequest, HttpServletRequest request,
69                                  HttpServletResponse response )
70                  throws IOException
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 
105         try
106         {
107             server.start();
108             if ( server.isFailed() )
109             {
110                 fail( "Couldn't bind the server socket to a free port!" );
111             }
112 
113             int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
114 
115             {
116                 // phase 1: run build in online mode to fill local repo
117                 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
118                 verifier.setAutoclean( false );
119                 verifier.deleteDirectory( "target" );
120                 verifier.deleteArtifacts( "org.apache.maven.its.mng0768" );
121                 verifier.setLogFileName( "log1.txt" );
122                 Properties props = new Properties();
123                 props.put( "@port@", Integer.toString( port ) );
124                 verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", props );
125                 verifier.addCliOption( "--settings" );
126                 verifier.addCliOption( "settings.xml" );
127                 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
128                 verifier.assertFilePresent( "target/compile.txt" );
129                 verifier.verifyErrorFreeLog();
130                 verifier.resetStreams();
131             }
132 
133             requestedUris.clear();
134 
135             {
136                 // phase 2: run build in offline mode to check it still passes, without network accesses
137                 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
138                 verifier.setAutoclean( false );
139                 verifier.deleteDirectory( "target" );
140                 verifier.addCliOption( "-o" );
141                 verifier.addCliOption( "--settings" );
142                 verifier.addCliOption( "settings.xml" );
143                 verifier.setLogFileName( "log2.txt" );
144                 verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
145                 verifier.assertFilePresent( "target/compile.txt" );
146                 verifier.verifyErrorFreeLog();
147                 verifier.resetStreams();
148             }
149 
150             assertTrue( requestedUris.toString(), requestedUris.isEmpty() );
151 
152             {
153                 // phase 3: delete test artifact and run build in offline mode to check it fails now
154                 // NOTE: Adding the settings again to offer Maven the bad choice of using the remote repo
155                 Verifier verifier = newVerifier( testDir.getAbsolutePath() );
156                 verifier.setAutoclean( false );
157                 verifier.deleteDirectory( "target" );
158                 verifier.deleteArtifacts( "org.apache.maven.its.mng0768" );
159                 verifier.addCliOption( "-o" );
160                 verifier.addCliOption( "--settings" );
161                 verifier.addCliOption( "settings.xml" );
162                 verifier.setLogFileName( "log3.txt" );
163                 try
164                 {
165                     verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
166                     verifier.verifyErrorFreeLog();
167                     fail( "Build did not fail to resolve missing dependency although Maven ought to work offline!" );
168                 }
169                 catch( VerificationException e )
170                 {
171                     // expected, should fail
172                 }
173                 verifier.resetStreams();
174             }
175 
176             System.out.println( "Bound server socket to the port " + port );
177 
178             assertTrue( requestedUris.toString(), requestedUris.isEmpty() );
179         }
180         finally
181         {
182             server.stop();
183             server.join();
184         }
185     }
186 }