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.util.Properties;
34  
35  /**
36   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-3461">MNG-3461</a>.
37   *
38   * @author Benjamin Bentmann
39   *
40   */
41  public class MavenITmng3461MirrorMatchingTest
42      extends AbstractMavenIntegrationTestCase
43  {
44  
45      public MavenITmng3461MirrorMatchingTest()
46      {
47          super( "(2.0.8,)" );
48      }
49  
50      /**
51       * Test that mirror definitions are properly evaluated. In particular, an exact match by id should always
52       * win over wildcard matches.
53       *
54       * @throws Exception in case of failure
55       */
56      public void testitExactMatchDominatesWildcard()
57          throws Exception
58      {
59          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3461/test-1" );
60  
61          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
62          verifier.setAutoclean( false );
63          verifier.deleteArtifacts( "org.apache.maven.its.mng3461" );
64          Properties filterProps = verifier.newDefaultFilterProperties();
65          verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
66          verifier.addCliOption( "--settings" );
67          verifier.addCliOption( "settings.xml" );
68          verifier.executeGoal( "validate" );
69          verifier.verifyErrorFreeLog();
70          verifier.resetStreams();
71  
72          verifier.assertArtifactPresent( "org.apache.maven.its.mng3461", "a", "0.1", "jar" );
73      }
74  
75      /**
76       * Test that mirror definitions are properly evaluated. In particular, the wildcard external:* should not
77       * match file:// and localhost repos but only external repos.
78       *
79       * @throws Exception in case of failure
80       */
81      public void testitExternalWildcard()
82          throws Exception
83      {
84          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3461/test-2" );
85  
86          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
87  
88          Handler repoHandler = new AbstractHandler()
89          {
90              @Override
91              public void handle( String target, Request baseRequest, HttpServletRequest request,
92                                  HttpServletResponse response )
93                  throws IOException
94              {
95                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
96  
97                  if ( request.getRequestURI().endsWith( "/b-0.1.jar" ) )
98                  {
99                      response.setStatus( HttpServletResponse.SC_OK );
100                     response.getWriter().println( request.getRequestURI() );
101                 }
102                 else if ( request.getRequestURI().endsWith( "/b-0.1.pom" ) )
103                 {
104                     response.setStatus( HttpServletResponse.SC_OK );
105                     response.getWriter().println( "<project>" );
106                     response.getWriter().println( "  <modelVersion>4.0.0</modelVersion>" );
107                     response.getWriter().println( "  <groupId>org.apache.maven.its.mng3461</groupId>" );
108                     response.getWriter().println( "  <artifactId>b</artifactId>" );
109                     response.getWriter().println( "  <version>0.1</version>" );
110                     response.getWriter().println( "</project>" );
111                 }
112                 else
113                 {
114                     response.setStatus( HttpServletResponse.SC_NOT_FOUND );
115                 }
116 
117                 ( (Request) request ).setHandled( true );
118             }
119         };
120 
121         Server server = new Server( 0 );
122         server.setHandler( repoHandler );
123 
124         try
125         {
126             server.start();
127             if ( server.isFailed() )
128             {
129                 fail( "Couldn't bind the server socket to a free port!" );
130             }
131 
132             int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
133             System.out.println( "Bound server socket to the port " + port );
134 
135             verifier.setAutoclean( false );
136             verifier.deleteArtifacts( "org.apache.maven.its.mng3461" );
137             Properties filterProps = verifier.newDefaultFilterProperties();
138             filterProps.setProperty( "@test.port@", Integer.toString( port ) );
139             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
140             verifier.addCliOption( "--settings" );
141             verifier.addCliOption( "settings.xml" );
142             verifier.executeGoal( "validate" );
143             verifier.verifyErrorFreeLog();
144             verifier.resetStreams();
145         }
146         finally
147         {
148             server.stop();
149             server.join();
150         }
151 
152         verifier.assertArtifactPresent( "org.apache.maven.its.mng3461", "a", "0.1", "jar" );
153         verifier.assertArtifactPresent( "org.apache.maven.its.mng3461", "b", "0.1", "jar" );
154         verifier.assertArtifactPresent( "org.apache.maven.its.mng3461", "c", "0.1", "jar" );
155     }
156 
157     /**
158      * Test that mirror definitions are properly evaluated. In particular, the wildcards within a single mirrorOf
159      * spec should not be greedy.
160      *
161      * @throws Exception in case of failure
162      */
163     public void testitNonGreedyWildcard()
164         throws Exception
165     {
166         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3461/test-3" );
167 
168         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
169         verifier.setAutoclean( false );
170         verifier.deleteArtifacts( "org.apache.maven.its.mng3461" );
171         Properties filterProps = verifier.newDefaultFilterProperties();
172         verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
173         verifier.addCliOption( "--settings" );
174         verifier.addCliOption( "settings.xml" );
175         verifier.executeGoal( "validate" );
176         verifier.verifyErrorFreeLog();
177         verifier.resetStreams();
178 
179         verifier.assertArtifactPresent( "org.apache.maven.its.mng3461", "a", "0.1", "jar" );
180     }
181 }