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 java.io.File;
23  import java.io.IOException;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.maven.it.Verifier;
30  import org.apache.maven.it.util.ResourceExtractor;
31  import org.mortbay.jetty.Handler;
32  import org.mortbay.jetty.Request;
33  import org.mortbay.jetty.Server;
34  import org.mortbay.jetty.handler.AbstractHandler;
35  import org.mortbay.jetty.handler.HandlerList;
36  import org.mortbay.jetty.security.Constraint;
37  import org.mortbay.jetty.security.ConstraintMapping;
38  import org.mortbay.jetty.security.HashUserRealm;
39  import org.mortbay.jetty.security.SecurityHandler;
40  
41  /**
42   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-3953">MNG-3953</a>.
43   * 
44   * @author Benjamin Bentmann
45   * @version $Id: MavenITmng3953AuthenticatedDeploymentTest.java 989809 2010-08-26 16:30:20Z bentmann $
46   */
47  public class MavenITmng3953AuthenticatedDeploymentTest
48      extends AbstractMavenIntegrationTestCase
49  {
50  
51      private Server server;
52  
53      private int port;
54  
55      private volatile boolean deployed;
56  
57      public MavenITmng3953AuthenticatedDeploymentTest()
58      {
59          super( "(2.0.1,)" );
60      }
61  
62      public void setUp()
63          throws Exception
64      {
65          super.setUp();
66  
67          Handler repoHandler = new AbstractHandler()
68          {
69              public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
70                  throws IOException, ServletException
71              {
72                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
73  
74                  if ( "PUT".equalsIgnoreCase( request.getMethod() ) )
75                  {
76                      response.setStatus( HttpServletResponse.SC_OK );
77                      deployed = true;
78                  }
79                  else
80                  {
81                      response.setStatus( HttpServletResponse.SC_NOT_FOUND );
82                  }
83  
84                  ( (Request) request ).setHandled( true );
85              }
86          };
87  
88          Constraint constraint = new Constraint();
89          constraint.setName( Constraint.__BASIC_AUTH );
90          constraint.setRoles( new String[] { "deployer" } );
91          constraint.setAuthenticate( true );
92  
93          ConstraintMapping constraintMapping = new ConstraintMapping();
94          constraintMapping.setConstraint( constraint );
95          constraintMapping.setPathSpec( "/*" );
96  
97          HashUserRealm userRealm = new HashUserRealm( "TestRealm" );
98          userRealm.put( "testuser", "testtest" );
99          userRealm.addUserToRole( "testuser", "deployer" );
100 
101         SecurityHandler securityHandler = new SecurityHandler();
102         securityHandler.setUserRealm( userRealm );
103         securityHandler.setConstraintMappings( new ConstraintMapping[] { constraintMapping } );
104 
105         HandlerList handlerList = new HandlerList();
106         handlerList.addHandler( securityHandler );
107         handlerList.addHandler( repoHandler );
108 
109         server = new Server( 0 );
110         server.setHandler( handlerList );
111         server.start();
112 
113         port = server.getConnectors()[0].getLocalPort();
114 
115         deployed = false;
116     }
117 
118     protected void tearDown()
119         throws Exception
120     {
121         if ( server != null )
122         {
123             server.stop();
124             server = null;
125         }
126 
127         super.tearDown();
128     }
129 
130     /**
131      * Test that deployment (of a release) to a repository that requires authentification works.
132      */
133     public void testitRelease()
134         throws Exception
135     {
136         testitMNG3953( "release" );
137     }
138 
139     /**
140      * Test that deployment (of a snapshot) to a repository that requires authentification works.
141      */
142     public void testitSnapshot()
143         throws Exception
144     {
145         testitMNG3953( "snapshot" );
146     }
147 
148     private void testitMNG3953( String project )
149         throws Exception
150     {
151         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-3953/" + project );
152 
153         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
154         verifier.setAutoclean( false );
155         verifier.getCliOptions().add( "--settings" );
156         verifier.getCliOptions().add( "settings.xml" );
157         verifier.getCliOptions().add( "-DdeploymentPort=" + port );
158         verifier.executeGoal( "validate" );
159         verifier.verifyErrorFreeLog();
160         verifier.resetStreams();
161 
162         assertTrue( deployed );
163     }
164 
165 }