View Javadoc

1   package org.apache.maven.report.projectinfo;
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.net.URL;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import junit.framework.Assert;
28  
29  import org.apache.maven.model.DeploymentRepository;
30  import org.apache.maven.model.DistributionManagement;
31  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
32  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.report.projectinfo.stubs.SettingsStub;
35  import org.apache.maven.settings.Settings;
36  import org.mortbay.jetty.Connector;
37  import org.mortbay.jetty.Handler;
38  import org.mortbay.jetty.Server;
39  import org.mortbay.jetty.handler.DefaultHandler;
40  import org.mortbay.jetty.nio.SelectChannelConnector;
41  import org.mortbay.jetty.security.Constraint;
42  import org.mortbay.jetty.security.ConstraintMapping;
43  import org.mortbay.jetty.security.HashUserRealm;
44  import org.mortbay.jetty.security.SecurityHandler;
45  import org.mortbay.jetty.security.SslSocketConnector;
46  import org.mortbay.jetty.webapp.WebAppContext;
47  
48  /**
49   * @author <a href="mailto:vincent.siveton@crim.ca">Vincent Siveton</a>
50   * @version $Id: ProjectInfoReportUtilsTest.java 1152625 2011-07-31 20:11:31Z hboutemy $
51   */
52  public class ProjectInfoReportUtilsTest
53      extends AbstractMojoTestCase
54  {
55      private static final int MAX_IDLE_TIME = 30000;
56  
57      private int port = -1;
58  
59      private Settings settingsStub;
60  
61      private Server jettyServer;
62  
63      protected void setUp()
64          throws Exception
65      {
66          super.setUp();
67  
68          final List<org.apache.maven.settings.Server> servers = new ArrayList<org.apache.maven.settings.Server>();
69          org.apache.maven.settings.Server server = new org.apache.maven.settings.Server();
70          server.setId( "localhost" );
71          server.setUsername( "admin" );
72          server.setPassword( "admin" );
73          servers.add( server );
74          settingsStub = new SettingsStub()
75          {
76              private static final long serialVersionUID = 1L;
77  
78              @Override
79              public org.apache.maven.settings.Server getServer( String serverId )
80              {
81                  for ( org.apache.maven.settings.Server server : getServers() )
82                  {
83                      if ( server.getId().equals( serverId ) )
84                      {
85                          return server;
86                      }
87                  }
88                  return null;
89              }
90  
91              @Override
92              public List<org.apache.maven.settings.Server> getServers()
93              {
94                  return servers;
95              }
96          };
97  
98      }
99  
100     private MavenProject getMavenProjectStub( boolean https )
101     {
102         final DistributionManagement distributionManagement = new DistributionManagement();
103         DeploymentRepository repository = new DeploymentRepository();
104         repository.setId( "localhost" );
105         repository.setUrl( ( https ? "https" : "http" ) + "://localhost:" + port );
106         distributionManagement.setRepository( repository );
107         distributionManagement.setSnapshotRepository( repository );
108         return new MavenProjectStub()
109         {
110             @Override
111             public DistributionManagement getDistributionManagement()
112             {
113                 return distributionManagement;
114             }
115         };
116     }
117 
118     protected void tearDown()
119         throws Exception
120     {
121         super.tearDown();
122     }
123 
124     public void testGetInputStreamURL()
125         throws Exception
126     {
127         // file
128         URL url = new File( getBasedir(), "/target/classes/project-info-report.properties" ).toURI().toURL();
129 
130         String content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub, null );
131         Assert.assertNotNull( content );
132         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
133 
134         // http + no auth
135         startJetty( false, false );
136 
137         url = new URL( "http://localhost:" + port + "/project-info-report.properties" );
138 
139         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub, null );
140         Assert.assertNotNull( content );
141         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
142 
143         stopJetty();
144 
145         // http + auth
146         startJetty( false, true );
147 
148         url = new URL( "http://localhost:" + port + "/project-info-report.properties" );
149 
150         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub, null );
151         Assert.assertNotNull( content );
152         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
153 
154         stopJetty();
155 
156         // https + no auth
157         startJetty( true, false );
158 
159         url = new URL( "https://localhost:" + port + "/project-info-report.properties" );
160 
161         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub, null );
162         Assert.assertNotNull( content );
163         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
164 
165         stopJetty();
166 
167         // https + auth
168         startJetty( true, true );
169 
170         url = new URL( "https://localhost:" + port + "/project-info-report.properties" );
171 
172         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub, null );
173         Assert.assertNotNull( content );
174         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
175 
176         stopJetty();
177 
178         // TODO need to test with a proxy
179     }
180 
181     private void startJetty( boolean isSSL, boolean withAuth )
182         throws Exception
183     {
184         jettyServer = new Server();
185         jettyServer.setStopAtShutdown( true );
186 
187         Connector connector = ( isSSL ? getSSLConnector() : getDefaultConnector() );
188         jettyServer.setConnectors( new Connector[] { connector } );
189 
190         WebAppContext webapp = new WebAppContext();
191         webapp.setContextPath( "/" );
192         webapp.setResourceBase( getBasedir() + "/target/classes/" );
193 
194         webapp.setServer( jettyServer );
195 
196         if ( withAuth )
197         {
198             Constraint constraint = new Constraint();
199             constraint.setName( Constraint.__BASIC_AUTH );
200             constraint.setRoles( new String[] { "user", "admin" } );
201             constraint.setAuthenticate( true );
202 
203             ConstraintMapping cm = new ConstraintMapping();
204             cm.setConstraint( constraint );
205             cm.setPathSpec( "/*" );
206 
207             SecurityHandler sh = new SecurityHandler();
208             sh.setUserRealm( new HashUserRealm( "MyRealm", getBasedir() + "/src/test/resources/realm.properties" ) );
209             sh.setConstraintMappings( new ConstraintMapping[] { cm } );
210 
211             webapp.addHandler( sh );
212         }
213 
214         DefaultHandler defaultHandler = new DefaultHandler();
215         defaultHandler.setServer( jettyServer );
216 
217         Handler[] handlers = new Handler[2];
218         handlers[0] = webapp;
219         handlers[1] = defaultHandler;
220         jettyServer.setHandlers( handlers );
221 
222         jettyServer.start();
223 
224         port = connector.getLocalPort();
225     }
226 
227     private void stopJetty()
228         throws Exception
229     {
230         if ( jettyServer != null )
231         {
232             jettyServer.stop();
233 
234             jettyServer = null;
235 
236             port = -1;
237         }
238     }
239 
240     private Connector getDefaultConnector()
241     {
242         Connector connector = new SelectChannelConnector();
243         connector.setMaxIdleTime( MAX_IDLE_TIME );
244         return connector;
245     }
246 
247     private Connector getSSLConnector()
248     {
249         SslSocketConnector connector = new SslSocketConnector();
250         connector.setKeystore( getBasedir() + "/target/jetty.jks" );
251         connector.setPassword( "apache" );
252         connector.setKeyPassword( "apache" );
253         connector.setTruststore( getBasedir() + "/target/jetty.jks" );
254         connector.setTrustPassword( "apache" );
255         return connector;
256     }
257 }