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.NetworkConnector;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.handler.AbstractHandler;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import java.io.File;
31  import java.io.FileInputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  import java.io.PrintWriter;
36  import java.util.Properties;
37  
38  /**
39   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-5280">MNG-5280</a>.
40   *
41   * @author Anders Hammar
42   */
43  public class MavenITmng5280SettingsProfilesRepositoriesOrderTest
44      extends AbstractMavenIntegrationTestCase
45  {
46      private File testDir;
47  
48      private Server server;
49  
50      public MavenITmng5280SettingsProfilesRepositoriesOrderTest()
51      {
52          super( "[3.1-A,)" );
53      }
54  
55      @Override
56      protected void setUp()
57          throws Exception
58      {
59          testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5280" );
60          server = new Server( 0 );
61      }
62  
63      @Override
64      protected void tearDown()
65          throws Exception
66      {
67          if ( server != null )
68          {
69              server.stop();
70              server.join();
71          }
72      }
73  
74      /**
75       * Verify that the repositories are used in the reversed order of definition in settings.xml.
76       *
77       * @throws Exception in case of failure
78       */
79      public void testRepositoriesOrder()
80          throws Exception
81      {
82          RepoHandler repoHandler = new RepoHandler();
83          server.setHandler( repoHandler );
84          server.start();
85          if ( server.isFailed() )
86          {
87              fail( "Couldn't bind the server socket to a free port!" );
88          }
89          int httpPort = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
90          System.out.println( "Bound server socket to the port " + httpPort );
91  
92          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
93  
94          verifier.setAutoclean( false );
95          verifier.deleteDirectory( "target" );
96          verifier.deleteArtifacts( "org.apache.maven.its.mng5280" );
97          Properties filterProps = verifier.newDefaultFilterProperties();
98          filterProps.setProperty( "@httpserver.port@", Integer.toString( httpPort ) );
99          verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
100         verifier.addCliOption( "--settings" );
101         verifier.addCliOption( "settings.xml" );
102         verifier.executeGoal( "org.apache.maven.its.plugins:maven-it-plugin-dependency-resolution:2.1-SNAPSHOT:compile" );
103         verifier.verifyErrorFreeLog();
104         verifier.resetStreams();
105 
106         assertTrue( repoHandler.artifactRequestedFromRepo2 );
107         assertTrue( repoHandler.artifactRequestedFromRepo1Last );
108     }
109 
110     /**
111      * Verify that the plugin repositories are used in the reversed order of definition in settings.xml.
112      *
113      * @throws Exception in case of failure
114      */
115     public void testPluginRepositoriesOrder()
116         throws Exception
117     {
118         PluginRepoHandler pluginRepoHandler = new PluginRepoHandler();
119         server.setHandler( pluginRepoHandler );
120         server.start();
121         if ( server.isFailed() )
122         {
123             fail( "Couldn't bind the server socket to a free port!" );
124         }
125         int httpPort = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
126         System.out.println( "Bound server socket to the port " + httpPort );
127 
128         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
129 
130         verifier.setAutoclean( false );
131         verifier.deleteDirectory( "target" );
132         verifier.deleteArtifacts( "org.apache.maven.its.mng5280" );
133         Properties filterProps = verifier.newDefaultFilterProperties();
134         filterProps.setProperty( "@httpserver.port@", Integer.toString( httpPort ) );
135         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
136         verifier.addCliOption( "--settings" );
137         verifier.addCliOption( "settings.xml" );
138         verifier.executeGoal( "org.apache.maven.its.mng5280:fake-maven-plugin:1.0:fake" );
139         verifier.resetStreams();
140 
141         assertTrue( pluginRepoHandler.pluginRequestedFromRepo2 );
142         assertTrue( pluginRepoHandler.pluginRequestedFromRepo1Last );
143     }
144 
145     private static final class RepoHandler
146         extends AbstractHandler
147     {
148         private volatile boolean artifactRequestedFromRepo1Last;
149 
150         private volatile boolean artifactRequestedFromRepo2;
151 
152         public void handle( String target, Request baseRequest, HttpServletRequest request,
153                             HttpServletResponse response )
154             throws IOException
155         {
156             String uri = request.getRequestURI();
157 
158             if ( uri.startsWith( "/repo1/org/apache/maven/its/mng5280/fake-artifact/1.0/" ) )
159             {
160                 PrintWriter writer = response.getWriter();
161 
162                 if ( uri.endsWith( ".pom" ) )
163                 {
164                     writer.println( "<project>" );
165                     writer.println( "  <modelVersion>4.0.0</modelVersion>" );
166                     writer.println( "  <groupId>org.apache.maven.its.mng5280</groupId>" );
167                     writer.println( "  <artifactId>fake-artifact</artifactId>" );
168                     writer.println( "  <version>1.0</version>" );
169                     writer.println( "</project>" );
170 
171                     response.setStatus( HttpServletResponse.SC_OK );
172                 }
173                 else if ( uri.endsWith( ".jar" ) )
174                 {
175                     writer.println( "empty" );
176 
177                     response.setStatus( HttpServletResponse.SC_OK );
178                     artifactRequestedFromRepo1Last = true;
179                 }
180                 else
181                 {
182                     response.setStatus( HttpServletResponse.SC_NOT_FOUND );
183                 }
184             }
185             else if ( uri.startsWith( "/repo2/org/apache/maven/its/mng5280/fake-artifact/1.0/" ) )
186             {
187                 if ( uri.endsWith( ".jar" ) )
188                 {
189                     artifactRequestedFromRepo1Last = false;
190                     artifactRequestedFromRepo2 = true;
191                 }
192                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
193             }
194             else
195             {
196                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
197             }
198 
199             ( (Request) request ).setHandled( true );
200         }
201     }
202 
203     private class PluginRepoHandler
204         extends AbstractHandler
205     {
206         private volatile boolean pluginRequestedFromRepo1Last;
207 
208         private volatile boolean pluginRequestedFromRepo2;
209 
210         public void handle( String target, Request baseRequest, HttpServletRequest request,
211                             HttpServletResponse response )
212             throws IOException
213         {
214             String uri = request.getRequestURI();
215 
216             if ( uri.startsWith( "/pluginRepo1/org/apache/maven/its/mng5280/fake-maven-plugin/1.0/" ) )
217             {
218                 OutputStream outStream = response.getOutputStream();
219 
220                 if ( uri.endsWith( ".pom" ) )
221                 {
222                     File pluginPom = new File( testDir, "fake-maven-plugin/fake-maven-plugin-1.0.pom" );
223                     InputStream inStream = new FileInputStream( pluginPom );
224                     copy( inStream, outStream );
225 
226                     response.setStatus( HttpServletResponse.SC_OK );
227                 }
228                 else if ( uri.endsWith( ".jar" ) )
229                 {
230                     File pluginJar = new File( testDir, "fake-maven-plugin/fake-maven-plugin-1.0.jar" );
231                     InputStream inStream = new FileInputStream( pluginJar );
232                     copy( inStream, outStream );
233 
234                     response.setStatus( HttpServletResponse.SC_OK );
235                     pluginRequestedFromRepo1Last = true;
236                 }
237                 else
238                 {
239                     response.setStatus( HttpServletResponse.SC_NOT_FOUND );
240                 }
241             }
242             else if ( uri.startsWith( "/pluginRepo2/org/apache/maven/its/mng5280/fake-maven-plugin/1.0/" ) )
243             {
244                 if ( uri.endsWith( ".jar" ) )
245                 {
246                     pluginRequestedFromRepo1Last = false;
247                     pluginRequestedFromRepo2 = true;
248                 }
249                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
250             }
251             else
252             {
253                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
254             }
255 
256             ( (Request) request ).setHandled( true );
257         }
258 
259         private long copy( InputStream input, OutputStream output )
260             throws IOException
261         {
262             byte[] buffer = new byte[4 * 1024];
263             long count = 0;
264             int n = 0;
265             while ( -1 != ( n = input.read( buffer ) ) )
266             {
267                 output.write( buffer, 0, n );
268                 count += n;
269             }
270             return count;
271         }
272     }
273 }