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 org.apache.maven.model.DeploymentRepository;
28  import org.apache.maven.model.DistributionManagement;
29  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.report.projectinfo.stubs.SettingsStub;
33  import org.apache.maven.settings.Settings;
34  import org.mortbay.jetty.Connector;
35  import org.mortbay.jetty.Handler;
36  import org.mortbay.jetty.Server;
37  import org.mortbay.jetty.handler.DefaultHandler;
38  import org.mortbay.jetty.nio.SelectChannelConnector;
39  import org.mortbay.jetty.security.Constraint;
40  import org.mortbay.jetty.security.ConstraintMapping;
41  import org.mortbay.jetty.security.HashUserRealm;
42  import org.mortbay.jetty.security.SecurityHandler;
43  import org.mortbay.jetty.security.SslSocketConnector;
44  import org.mortbay.jetty.webapp.WebAppContext;
45  
46  import static org.apache.maven.report.projectinfo.ProjectInfoReportUtils.getArchiveServer;
47  
48  /**
49   * @author <a href="mailto:vincent.siveton@crim.ca">Vincent Siveton</a>
50   * @version $Id$
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-reports.properties" ).toURI().toURL();
131 
132         String content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
133                                                             "ISO-8859-1" );
134         assertNotNull( content );
135         assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
136 
137         // file
138         url = new File( getBasedir(), "/src/test/resources/iso-8859-5-encoded.txt" ).toURI().toURL();
139 
140         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
141                                                             "ISO-8859-5" );
142         assertNotNull( content );
143         assertTrue( content.contains( "Свобода всем народам!" ) );
144 
145         // http + no auth
146         startJetty( false, false );
147 
148         url = new URL( "http://localhost:" + port + "/project-info-reports.properties" );
149 
150         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
151                                                      "ISO-8859-1" );
152         assertNotNull( content );
153         assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
154 
155         stopJetty();
156 
157         // http + auth
158         startJetty( false, true );
159 
160         url = new URL( "http://localhost:" + port + "/project-info-reports.properties" );
161 
162         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
163                                                      "ISO-8859-1" );
164         assertNotNull( content );
165         assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
166 
167         stopJetty();
168 
169         // https + no auth
170         startJetty( true, false );
171 
172         url = new URL( "https://localhost:" + port + "/project-info-reports.properties" );
173 
174         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub,
175                                                      "ISO-8859-1" );
176         assertNotNull( content );
177         assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
178 
179         stopJetty();
180 
181         // https + auth
182         startJetty( true, true );
183 
184         url = new URL( "https://localhost:" + port + "/project-info-reports.properties" );
185 
186         content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub,
187                                                      "ISO-8859-1" );
188         assertNotNull( content );
189         assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
190 
191         stopJetty();
192 
193         // TODO need to test with a proxy
194     }
195 
196     public void testGetArchiveServer()
197     {
198         assertEquals( "???UNKNOWN???", getArchiveServer( null ) );
199 
200         assertNull( getArchiveServer( "" ) );
201 
202         assertEquals( "mail-archives.apache.org",
203                 getArchiveServer( "http://mail-archives.apache.org/mod_mbox/maven-announce/" ) );
204 
205         assertEquals( "mail-archives.apache.org",
206                 getArchiveServer( "https://mail-archives.apache.org/mod_mbox/maven-announce/" ) );
207 
208         assertEquals( "mail-archives.apache.org",
209                 getArchiveServer( "http://mail-archives.apache.org/mod_mbox/maven-announce" ) );
210 
211         assertEquals( "www.mail-archive.com",
212                 getArchiveServer( "http://www.mail-archive.com/announce@maven.apache.org" ) );
213 
214         assertEquals( "www.nabble.com", getArchiveServer( "http://www.nabble.com/Maven-Announcements-f15617.html" ) );
215 
216         assertEquals( "maven.announce.markmail.org", getArchiveServer( "http://maven.announce.markmail.org/" ) );
217 
218         assertEquals( "maven.announce.markmail.org", getArchiveServer( "http://maven.announce.markmail.org" ) );
219     }
220 
221     private void startJetty( boolean isSSL, boolean withAuth )
222         throws Exception
223     {
224         jettyServer = new Server();
225         jettyServer.setStopAtShutdown( true );
226 
227         Connector connector = ( isSSL ? getSSLConnector() : getDefaultConnector() );
228         jettyServer.setConnectors( new Connector[] { connector } );
229 
230         WebAppContext webapp = new WebAppContext();
231         webapp.setContextPath( "/" );
232         webapp.setResourceBase( getBasedir() + "/target/classes/" );
233 
234         webapp.setServer( jettyServer );
235 
236         if ( withAuth )
237         {
238             Constraint constraint = new Constraint();
239             constraint.setName( Constraint.__BASIC_AUTH );
240             constraint.setRoles( new String[] { "user", "admin" } );
241             constraint.setAuthenticate( true );
242 
243             ConstraintMapping cm = new ConstraintMapping();
244             cm.setConstraint( constraint );
245             cm.setPathSpec( "/*" );
246 
247             SecurityHandler sh = new SecurityHandler();
248             sh.setUserRealm( new HashUserRealm( "MyRealm", getBasedir() + "/src/test/resources/realm.properties" ) );
249             sh.setConstraintMappings( new ConstraintMapping[] { cm } );
250 
251             webapp.addHandler( sh );
252         }
253 
254         DefaultHandler defaultHandler = new DefaultHandler();
255         defaultHandler.setServer( jettyServer );
256 
257         Handler[] handlers = new Handler[2];
258         handlers[0] = webapp;
259         handlers[1] = defaultHandler;
260         jettyServer.setHandlers( handlers );
261 
262         jettyServer.start();
263 
264         port = connector.getLocalPort();
265     }
266 
267     private void stopJetty()
268         throws Exception
269     {
270         if ( jettyServer != null )
271         {
272             jettyServer.stop();
273 
274             jettyServer = null;
275 
276             port = -1;
277         }
278     }
279 
280     private Connector getDefaultConnector()
281     {
282         Connector connector = new SelectChannelConnector();
283         connector.setMaxIdleTime( MAX_IDLE_TIME );
284         return connector;
285     }
286 
287     private Connector getSSLConnector()
288     {
289         SslSocketConnector connector = new SslSocketConnector();
290         connector.setKeystore( getBasedir() + "/target/jetty.jks" );
291         connector.setPassword( "apache" );
292         connector.setKeyPassword( "apache" );
293         connector.setTruststore( getBasedir() + "/target/jetty.jks" );
294         connector.setTrustPassword( "apache" );
295         return connector;
296     }
297 }