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.io.IOException;
26  import java.io.PrintWriter;
27  import java.util.Deque;
28  import java.util.Properties;
29  import java.util.concurrent.ConcurrentLinkedDeque;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.hamcrest.CoreMatchers;
35  import org.eclipse.jetty.server.Handler;
36  import org.eclipse.jetty.server.NetworkConnector;
37  import org.eclipse.jetty.server.Request;
38  import org.eclipse.jetty.server.Server;
39  import org.eclipse.jetty.server.handler.AbstractHandler;
40  
41  import static org.hamcrest.CoreMatchers.hasItem;
42  import static org.hamcrest.MatcherAssert.assertThat;
43  
44  /**
45   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4343">MNG-4343</a>.
46   *
47   * @author Benjamin Bentmann
48   *
49   */
50  public class MavenITmng4343MissingReleaseUpdatePolicyTest
51      extends AbstractMavenIntegrationTestCase
52  {
53      private Server server;
54  
55      private Deque<String> requestedUris;
56  
57      private volatile boolean blockAccess;
58  
59      private int port;
60  
61      public MavenITmng4343MissingReleaseUpdatePolicyTest()
62      {
63          super( "[3.0-alpha-3,)" );
64      }
65  
66      @Override
67      protected void setUp()
68          throws Exception
69      {
70          Handler repoHandler = new AbstractHandler()
71          {
72              @Override
73              public void handle( String target, Request baseRequest, HttpServletRequest request,
74                                  HttpServletResponse response )
75                  throws IOException
76              {
77                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
78  
79                  if ( request.getRequestURI().startsWith( "/org/apache/maven/its/mng4343" ) )
80                  {
81                      requestedUris.add( request.getRequestURI().substring( 29 ) );
82                  }
83  
84                  if ( blockAccess )
85                  {
86                      response.setStatus( HttpServletResponse.SC_NOT_FOUND );
87                  }
88                  else
89                  {
90                      PrintWriter writer = response.getWriter();
91  
92                      response.setStatus( HttpServletResponse.SC_OK );
93  
94                      if ( request.getRequestURI().endsWith( ".pom" ) )
95                      {
96                          writer.println( "<project>" );
97                          writer.println( "  <modelVersion>4.0.0</modelVersion>" );
98                          writer.println( "  <groupId>org.apache.maven.its.mng4343</groupId>" );
99                          writer.println( "  <artifactId>dep</artifactId>" );
100                         writer.println( "  <version>0.1</version>" );
101                         writer.println( "</project>" );
102                     }
103                     else if ( request.getRequestURI().endsWith( ".jar" ) )
104                     {
105                         writer.println( "empty" );
106                     }
107                     else if ( request.getRequestURI().endsWith( ".md5" ) || request.getRequestURI().endsWith( ".sha1" ) )
108                     {
109                         response.setStatus( HttpServletResponse.SC_NOT_FOUND );
110                     }
111                 }
112 
113                 ( (Request) request ).setHandled( true );
114             }
115         };
116 
117         server = new Server( 0 );
118         server.setHandler( repoHandler );
119         server.start();
120         if ( server.isFailed() )
121         {
122             fail( "Couldn't bind the server socket to a free port!" );
123         }
124         port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
125         System.out.println( "Bound server socket to the port " + port );
126         requestedUris = new ConcurrentLinkedDeque<>();
127     }
128 
129     @Override
130     protected void tearDown()
131         throws Exception
132     {
133         if ( server != null )
134         {
135             server.stop();
136             server.join();
137         }
138     }
139 
140     /**
141      * Verify that checking for *missing* release artifacts respects the update policy that is configured in the
142      * release section for the respective repository, in this case "always".
143      *
144      * @throws Exception in case of failure
145      */
146     public void testitAlways()
147         throws Exception
148     {
149         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4343" );
150 
151         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
152         verifier.setAutoclean( false );
153         verifier.deleteArtifacts( "org.apache.maven.its.mng4343" );
154         verifier.addCliOption( "-s" );
155         verifier.addCliOption( "settings.xml" );
156 
157         Properties filterProps = verifier.newDefaultFilterProperties();
158         filterProps.setProperty( "@updates@", "always" );
159         filterProps.setProperty( "@port@", Integer.toString( port ) );
160         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
161 
162         blockAccess = true;
163 
164         verifier.setLogFileName( "log-always-1.txt" );
165         try
166         {
167             verifier.executeGoal( "validate" );
168             verifier.verifyErrorFreeLog();
169             fail( "Build succeeded despite missing dependency" );
170         }
171         catch ( VerificationException e )
172         {
173             // expected
174         }
175 
176         assertTrue( requestedUris.toString(),
177             requestedUris.contains( "/dep/0.1/dep-0.1.jar" ) || requestedUris.contains( "/dep/0.1/dep-0.1.pom" ) );
178         requestedUris.clear();
179 
180         blockAccess = false;
181 
182         verifier.setLogFileName( "log-always-2.txt" );
183         verifier.executeGoal( "validate" );
184         verifier.verifyErrorFreeLog();
185 
186         assertTrue( requestedUris.toString(), requestedUris.contains( "/dep/0.1/dep-0.1.jar" ) );
187         assertTrue( requestedUris.toString(), requestedUris.contains( "/dep/0.1/dep-0.1.pom" ) );
188         verifier.assertArtifactPresent( "org.apache.maven.its.mng4343", "dep", "0.1", "jar" );
189         verifier.assertArtifactPresent( "org.apache.maven.its.mng4343", "dep", "0.1", "pom" );
190 
191         verifier.resetStreams();
192     }
193 
194     /**
195      * Verify that checking for *missing* release artifacts respects the update policy that is configured in the
196      * release section for the respective repository, in this case "never", unless overriden from the CLI via -U.
197      *
198      * @throws Exception in case of failure
199      */
200     public void testitNever()
201         throws Exception
202     {
203         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4343" );
204 
205         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
206         verifier.setAutoclean( false );
207         verifier.deleteArtifacts( "org.apache.maven.its.mng4343" );
208         verifier.addCliOption( "-s" );
209         verifier.addCliOption( "settings.xml" );
210 
211         Properties filterProps = verifier.newDefaultFilterProperties();
212         filterProps.setProperty( "@updates@", "never" );
213         filterProps.setProperty( "@port@", Integer.toString( port ) );
214         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
215 
216         blockAccess = true;
217 
218         verifier.setLogFileName( "log-never-1.txt" );
219         try
220         {
221             verifier.executeGoal( "validate" );
222             verifier.verifyErrorFreeLog();
223             fail( "Build succeeded despite missing dependency" );
224         }
225         catch ( VerificationException e )
226         {
227             // expected
228         }
229 
230         assertTrue( requestedUris.toString(),
231             requestedUris.contains( "/dep/0.1/dep-0.1.jar" ) || requestedUris.contains( "/dep/0.1/dep-0.1.pom" ) );
232         requestedUris.clear();
233 
234         blockAccess = false;
235 
236         verifier.setLogFileName( "log-never-2.txt" );
237         try
238         {
239             verifier.executeGoal( "validate" );
240             verifier.verifyErrorFreeLog();
241             fail( "Remote repository was accessed despite updatePolicy=never" );
242         }
243         catch ( VerificationException e )
244         {
245             // expected
246         }
247 
248         //noinspection unchecked
249         assertThat( requestedUris, CoreMatchers.<String>hasItems() );
250         verifier.assertArtifactNotPresent( "org.apache.maven.its.mng4343", "dep", "0.1", "jar" );
251         verifier.assertArtifactNotPresent( "org.apache.maven.its.mng4343", "dep", "0.1", "pom" );
252 
253         verifier.setLogFileName( "log-never-3.txt" );
254         verifier.addCliOption( "-U" );
255         verifier.executeGoal( "validate" );
256         verifier.verifyErrorFreeLog();
257 
258         assertThat( requestedUris, hasItem( "/dep/0.1/dep-0.1.jar" ) );
259         assertThat( requestedUris, hasItem( "/dep/0.1/dep-0.1.pom" ) );
260         verifier.assertArtifactPresent( "org.apache.maven.its.mng4343", "dep", "0.1", "jar" );
261         verifier.assertArtifactPresent( "org.apache.maven.its.mng4343", "dep", "0.1", "pom" );
262 
263         requestedUris.clear();
264 
265         verifier.setLogFileName( "log-never-4.txt" );
266         verifier.addCliOption( "-U" );
267         verifier.executeGoal( "validate" );
268         verifier.verifyErrorFreeLog();
269 
270         //noinspection unchecked
271         assertThat( requestedUris, CoreMatchers.<String>hasItems() );
272 
273         verifier.resetStreams();
274     }
275 
276 }