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.security.ConstraintMapping;
24  import org.eclipse.jetty.security.ConstraintSecurityHandler;
25  import org.eclipse.jetty.security.HashLoginService;
26  import org.eclipse.jetty.server.NetworkConnector;
27  import org.eclipse.jetty.server.Server;
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  import org.eclipse.jetty.servlet.ServletContextHandler;
32  import org.eclipse.jetty.util.security.Constraint;
33  import org.eclipse.jetty.util.security.Password;
34  
35  import java.io.File;
36  import java.util.Properties;
37  
38  import static org.eclipse.jetty.servlet.ServletContextHandler.SECURITY;
39  import static org.eclipse.jetty.servlet.ServletContextHandler.SESSIONS;
40  import static org.eclipse.jetty.util.security.Constraint.__BASIC_AUTH;
41  
42  /**
43   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4729">MNG-4729</a>.
44   *
45   * @author Benjamin Bentmann
46   */
47  public class MavenITmng4729MirrorProxyAuthUsedByProjectBuilderTest
48      extends AbstractMavenIntegrationTestCase
49  {
50  
51      public MavenITmng4729MirrorProxyAuthUsedByProjectBuilderTest()
52      {
53          super( "[2.0.3,3.0-alpha-1),[3.0-beta-2,)" );
54      }
55  
56      /**
57       * Test that the 2.x project builder obeys the network settings (mirror, proxy, auth) when building remote POMs
58       * and discovering additional repositories.
59       *
60       * @throws Exception in case of failure
61       */
62      public void testit()
63          throws Exception
64      {
65          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4729" );
66  
67          Constraint constraint = new Constraint();
68          constraint.setName( Constraint.__BASIC_AUTH );
69          constraint.setRoles( new String[] { "user" } );
70          constraint.setAuthenticate( true );
71  
72          ConstraintMapping constraintMapping = new ConstraintMapping();
73          constraintMapping.setConstraint( constraint );
74          constraintMapping.setPathSpec( "/*" );
75  
76          HashLoginService userRealm = new HashLoginService( "TestRealm" );
77          userRealm.putUser( "testuser", new Password( "testtest" ), new String[] { "user" } );
78  
79          Server server = new Server( 0 );
80          ServletContextHandler ctx = new ServletContextHandler( server, "/", SESSIONS | SECURITY );
81          ConstraintSecurityHandler securityHandler = (ConstraintSecurityHandler) ctx.getSecurityHandler();
82          securityHandler.setLoginService( userRealm );
83          securityHandler.setAuthMethod( __BASIC_AUTH );
84          securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
85  
86          ResourceHandler repoHandler = new ResourceHandler();
87          repoHandler.setResourceBase( testDir.getAbsolutePath() );
88  
89          HandlerList handlerList = new HandlerList();
90          handlerList.addHandler( securityHandler );
91          handlerList.addHandler( repoHandler );
92          handlerList.addHandler( new DefaultHandler() );
93  
94          server.setHandler( handlerList );
95          server.start();
96  
97          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
98          try
99          {
100             if ( server.isFailed() )
101             {
102                 fail( "Couldn't bind the server socket to a free port!" );
103             }
104             int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
105             System.out.println( "Bound server socket to the port " + port );
106             verifier.setAutoclean( false );
107             verifier.deleteDirectory( "target" );
108             verifier.deleteArtifacts( "org.apache.maven.its.mng4729" );
109             Properties filterProps = verifier.newDefaultFilterProperties();
110             filterProps.setProperty( "@port@", Integer.toString( port ) );
111             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
112             verifier.addCliOption( "-s" );
113             verifier.addCliOption( "settings.xml" );
114             verifier.executeGoal( "validate" );
115             verifier.verifyErrorFreeLog();
116             verifier.resetStreams();
117 
118             Properties props = verifier.loadProperties( "target/pom.properties" );
119             assertEquals( "PASSED", props.get( "org.apache.maven.its.mng4729:a:jar:0.1.project.name" ) );
120         }
121         finally
122         {
123             server.stop();
124             server.join();
125         }
126     }
127 }