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.html 861765 2013-05-12 18:46:29Z 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         assertTrue( ProjectInfoReportUtils.isArtifactUrlValid( "http://my.intern.domain:8080/test" ) );
128 
129         // file
130         URL url = new File( getBasedir(), "/target/classes/project-info-report.properties" ).toURI().toURL();
131 
132         String content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub, null );
133         Assert.assertNotNull( content );
134         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
135 
136         // http + no auth
137         startJetty( false, false );
138 
139         url = new URL( "http://localhost:" + port + "/project-info-report.properties" );
140 
141         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub, null );
142         Assert.assertNotNull( content );
143         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
144 
145         stopJetty();
146 
147         // http + auth
148         startJetty( false, true );
149 
150         url = new URL( "http://localhost:" + port + "/project-info-report.properties" );
151 
152         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub, null );
153         Assert.assertNotNull( content );
154         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
155 
156         stopJetty();
157 
158         // https + no auth
159         startJetty( true, false );
160 
161         url = new URL( "https://localhost:" + port + "/project-info-report.properties" );
162 
163         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub, null );
164         Assert.assertNotNull( content );
165         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
166 
167         stopJetty();
168 
169         // https + auth
170         startJetty( true, true );
171 
172         url = new URL( "https://localhost:" + port + "/project-info-report.properties" );
173 
174         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub, null );
175         Assert.assertNotNull( content );
176         Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
177 
178         stopJetty();
179 
180         // TODO need to test with a proxy
181     }
182 
183     private void startJetty( boolean isSSL, boolean withAuth )
184         throws Exception
185     {
186         jettyServer = new Server();
187         jettyServer.setStopAtShutdown( true );
188 
189         Connector connector = ( isSSL ? getSSLConnector() : getDefaultConnector() );
190         jettyServer.setConnectors( new Connector[] { connector } );
191 
192         WebAppContext webapp = new WebAppContext();
193         webapp.setContextPath( "/" );
194         webapp.setResourceBase( getBasedir() + "/target/classes/" );
195 
196         webapp.setServer( jettyServer );
197 
198         if ( withAuth )
199         {
200             Constraint constraint = new Constraint();
201             constraint.setName( Constraint.__BASIC_AUTH );
202             constraint.setRoles( new String[] { "user", "admin" } );
203             constraint.setAuthenticate( true );
204 
205             ConstraintMapping cm = new ConstraintMapping();
206             cm.setConstraint( constraint );
207             cm.setPathSpec( "/*" );
208 
209             SecurityHandler sh = new SecurityHandler();
210             sh.setUserRealm( new HashUserRealm( "MyRealm", getBasedir() + "/src/test/resources/realm.properties" ) );
211             sh.setConstraintMappings( new ConstraintMapping[] { cm } );
212 
213             webapp.addHandler( sh );
214         }
215 
216         DefaultHandler defaultHandler = new DefaultHandler();
217         defaultHandler.setServer( jettyServer );
218 
219         Handler[] handlers = new Handler[2];
220         handlers[0] = webapp;
221         handlers[1] = defaultHandler;
222         jettyServer.setHandlers( handlers );
223 
224         jettyServer.start();
225 
226         port = connector.getLocalPort();
227     }
228 
229     private void stopJetty()
230         throws Exception
231     {
232         if ( jettyServer != null )
233         {
234             jettyServer.stop();
235 
236             jettyServer = null;
237 
238             port = -1;
239         }
240     }
241 
242     private Connector getDefaultConnector()
243     {
244         Connector connector = new SelectChannelConnector();
245         connector.setMaxIdleTime( MAX_IDLE_TIME );
246         return connector;
247     }
248 
249     private Connector getSSLConnector()
250     {
251         SslSocketConnector connector = new SslSocketConnector();
252         connector.setKeystore( getBasedir() + "/target/jetty.jks" );
253         connector.setPassword( "apache" );
254         connector.setKeyPassword( "apache" );
255         connector.setTruststore( getBasedir() + "/target/jetty.jks" );
256         connector.setTrustPassword( "apache" );
257         return connector;
258     }
259 }