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.apache.maven.shared.utils.io.FileUtils;
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  import org.eclipse.jetty.server.handler.DefaultHandler;
29  import org.eclipse.jetty.server.handler.HandlerList;
30  import org.eclipse.jetty.server.handler.ResourceHandler;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.io.File;
35  import java.io.IOException;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.List;
39  import java.util.Properties;
40  
41  /**
42   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4554">MNG-4554</a>.
43   *
44   * @author Benjamin Bentmann
45   */
46  public class MavenITmng4554PluginPrefixMappingUpdateTest
47      extends AbstractMavenIntegrationTestCase
48  {
49  
50      public MavenITmng4554PluginPrefixMappingUpdateTest()
51      {
52          super( "[2.0.3,3.0-alpha-1),[3.0-alpha-7,)" );
53      }
54  
55      /**
56       * Test that the metadata holding the plugin prefix mapping is cached and not redownloaded upon each
57       * Maven invocation.
58       *
59       * @throws Exception in case of failure
60       */
61      public void testitCached()
62          throws Exception
63      {
64          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4554" );
65  
66          String metadataUri = "/repo-1/org/apache/maven/its/mng4554/maven-metadata.xml";
67  
68          final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
69  
70          AbstractHandler logHandler = new AbstractHandler()
71          {
72              @Override
73              public void handle( String target, Request baseRequest, HttpServletRequest request,
74                                  HttpServletResponse response )
75              {
76                  requestedUris.add( request.getRequestURI() );
77              }
78          };
79  
80          ResourceHandler repoHandler = new ResourceHandler();
81          repoHandler.setResourceBase( testDir.getAbsolutePath() );
82  
83          HandlerList handlerList = new HandlerList();
84          handlerList.addHandler( logHandler );
85          handlerList.addHandler( repoHandler );
86          handlerList.addHandler( new DefaultHandler() );
87  
88          Server server = new Server( 0 );
89          server.setHandler( handlerList );
90          server.start();
91  
92          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
93          try
94          {
95              if ( server.isFailed() )
96              {
97                  fail( "Couldn't bind the server socket to a free port!" );
98              }
99              verifier.setAutoclean( false );
100             verifier.deleteDirectory( "target" );
101             try
102             {
103                 verifier.deleteArtifacts( "org.apache.maven.its.mng4554" );
104             }
105             catch ( IOException e )
106             {
107                 // expected when running test on Windows using embedded Maven (JAR files locked by plugin class realm)
108                 assertFalse( new File( verifier.getArtifactMetadataPath( "org.apache.maven.its.mng4554", null, null, "maven-metadata-mng4554.xml" ) ).exists() );
109             }
110             Properties filterProps = verifier.newDefaultFilterProperties();
111             NetworkConnector connector = (NetworkConnector) server.getConnectors()[0];
112             filterProps.setProperty( "@port@", Integer.toString( connector.getLocalPort() ) );
113             filterProps.setProperty( "@repo@", "repo-1" );
114             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
115             verifier.addCliOption( "-s" );
116             verifier.addCliOption( "settings.xml" );
117 
118             verifier.setLogFileName( "log-cached-1.txt" );
119             verifier.executeGoal( "a:touch" );
120             verifier.verifyErrorFreeLog();
121 
122             verifier.assertFilePresent( "target/touch.txt" );
123             assertTrue( requestedUris.toString(), requestedUris.contains( metadataUri ) );
124 
125             requestedUris.clear();
126 
127             verifier.setLogFileName( "log-cached-2.txt" );
128             verifier.executeGoal( "a:touch" );
129             verifier.verifyErrorFreeLog();
130 
131             assertFalse( requestedUris.toString(), requestedUris.contains( metadataUri ) );
132         }
133         finally
134         {
135             verifier.resetStreams();
136             server.stop();
137             server.join();
138         }
139     }
140 
141     /**
142      * Test that the local metadata holding the plugin prefix mapping can be forcefully updated via the command
143      * line flag -U.
144      *
145      * @throws Exception in case of failure
146      */
147     public void testitForcedUpdate()
148         throws Exception
149     {
150         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4554" );
151 
152         String metadataUri = "/repo-1/org/apache/maven/its/mng4554/maven-metadata.xml";
153 
154         final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
155 
156         AbstractHandler logHandler = new AbstractHandler()
157         {
158             @Override
159             public void handle( String target, Request baseRequest, HttpServletRequest request,
160                                 HttpServletResponse response )
161             {
162                 requestedUris.add( request.getRequestURI() );
163             }
164         };
165 
166         ResourceHandler repoHandler = new ResourceHandler();
167         repoHandler.setResourceBase( testDir.getAbsolutePath() );
168 
169         HandlerList handlerList = new HandlerList();
170         handlerList.addHandler( logHandler );
171         handlerList.addHandler( repoHandler );
172         handlerList.addHandler( new DefaultHandler() );
173 
174         Server server = new Server( 0 );
175         server.setHandler( handlerList );
176         server.start();
177 
178         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
179         try
180         {
181             if ( server.isFailed() )
182             {
183                 fail( "Couldn't bind the server socket to a free port!" );
184             }
185             verifier.setAutoclean( false );
186             verifier.deleteDirectory( "target" );
187             try
188             {
189                 verifier.deleteArtifacts( "org.apache.maven.its.mng4554" );
190             }
191             catch ( IOException e )
192             {
193                 // expected when running test on Windows using embedded Maven (JAR files locked by plugin class realm)
194                 assertFalse( new File( verifier.getArtifactMetadataPath( "org.apache.maven.its.mng4554", null, null, "maven-metadata-mng4554.xml" ) ).exists() );
195             }
196             Properties filterProps = verifier.newDefaultFilterProperties();
197             NetworkConnector connector = (NetworkConnector) server.getConnectors()[0];
198             filterProps.setProperty( "@port@", Integer.toString( connector.getLocalPort() ) );
199             filterProps.setProperty( "@repo@", "repo-1" );
200             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
201             verifier.addCliOption( "-U" );
202             verifier.addCliOption( "-s" );
203             verifier.addCliOption( "settings.xml" );
204 
205             verifier.setLogFileName( "log-forced-1.txt" );
206             verifier.executeGoal( "a:touch" );
207             verifier.verifyErrorFreeLog();
208 
209             verifier.assertFilePresent( "target/touch.txt" );
210             assertTrue( requestedUris.toString(), requestedUris.contains( metadataUri ) );
211 
212             requestedUris.clear();
213 
214             verifier.setLogFileName( "log-forced-2.txt" );
215             verifier.executeGoal( "a:touch" );
216             verifier.verifyErrorFreeLog();
217 
218             assertTrue( requestedUris.toString(), requestedUris.contains( metadataUri ) );
219         }
220         finally
221         {
222             verifier.resetStreams();
223             server.stop();
224             server.join();
225         }
226     }
227 
228     /**
229      * Test that the local metadata holding the plugin prefix mapping is automatically refetched from the remote
230      * repositories if the local metadata fails to resolve a new/other plugin prefix.
231      *
232      * @throws Exception in case of failure
233      */
234     public void testitRefetched()
235         throws Exception
236     {
237         requiresMavenVersion( "[3.0-alpha-3,)" );
238 
239         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4554" );
240 
241         String metadataUri = "/repo-it/org/apache/maven/its/mng4554/maven-metadata.xml";
242 
243         final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
244 
245         AbstractHandler logHandler = new AbstractHandler()
246         {
247             @Override
248             public void handle( String target, Request baseRequest, HttpServletRequest request,
249                                 HttpServletResponse response )
250             {
251                 requestedUris.add( request.getRequestURI() );
252             }
253         };
254 
255         ResourceHandler repoHandler = new ResourceHandler();
256         repoHandler.setResourceBase( testDir.getAbsolutePath() );
257 
258         HandlerList handlerList = new HandlerList();
259         handlerList.addHandler( logHandler );
260         handlerList.addHandler( repoHandler );
261         handlerList.addHandler( new DefaultHandler() );
262 
263         Server server = new Server( 0 );
264         server.setHandler( handlerList );
265         server.start();
266 
267         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
268         try
269         {
270             if ( server.isFailed() )
271             {
272                 fail( "Couldn't bind the server socket to a free port!" );
273             }
274             verifier.setAutoclean( false );
275             verifier.deleteDirectory( "target" );
276             try
277             {
278                 verifier.deleteArtifacts( "org.apache.maven.its.mng4554" );
279             }
280             catch ( IOException e )
281             {
282                 // expected when running test on Windows using embedded Maven (JAR files locked by plugin class realm)
283                 assertFalse( new File( verifier.getArtifactMetadataPath( "org.apache.maven.its.mng4554", null, null, "maven-metadata-mng4554.xml" ) ).exists() );
284             }
285             Properties filterProps = verifier.newDefaultFilterProperties();
286             NetworkConnector connector = (NetworkConnector) server.getConnectors()[0];
287             filterProps.setProperty( "@port@", Integer.toString( connector.getLocalPort() ) );
288             filterProps.setProperty( "@repo@", "repo-it" );
289             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
290             verifier.addCliOption( "-s" );
291             verifier.addCliOption( "settings.xml" );
292 
293             FileUtils.copyDirectoryStructure( new File( testDir, "repo-1" ), new File( testDir, "repo-it" ) );
294 
295             verifier.setLogFileName( "log-refetched-1.txt" );
296             verifier.executeGoal( "a:touch" );
297             verifier.verifyErrorFreeLog();
298 
299             verifier.assertFilePresent( "target/touch.txt" );
300             assertTrue( requestedUris.toString(), requestedUris.contains( metadataUri ) );
301 
302             requestedUris.clear();
303 
304             // simulate deployment of new plugin which updates the prefix mapping in the remote repo
305             FileUtils.copyDirectoryStructure( new File( testDir, "repo-2" ), new File( testDir, "repo-it" ) );
306 
307             verifier.setLogFileName( "log-refetched-2.txt" );
308             verifier.executeGoal( "b:touch" );
309             verifier.verifyErrorFreeLog();
310 
311             assertTrue( requestedUris.toString(), requestedUris.contains( metadataUri ) );
312         }
313         finally
314         {
315             verifier.resetStreams();
316             server.stop();
317             server.join();
318         }
319     }
320 }