1 package org.apache.maven.report.projectinfo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52 public class ProjectInfoReportUtilsTest
53 extends AbstractMojoTestCase
54 {
55 private static final int MAX_IDLE_TIME = 30000;
56
57 private MavenProject projectStub;
58
59 private Settings settingsStub;
60
61 private MavenProject projectStubSec;
62
63 private Server jettyServer;
64
65 protected void setUp()
66 throws Exception
67 {
68 super.setUp();
69
70 final List<org.apache.maven.settings.Server> servers = new ArrayList<org.apache.maven.settings.Server>();
71 org.apache.maven.settings.Server server = new org.apache.maven.settings.Server();
72 server.setId( "localhost" );
73 server.setUsername( "admin" );
74 server.setPassword( "admin" );
75 servers.add( server );
76 settingsStub = new SettingsStub()
77 {
78 private static final long serialVersionUID = 1L;
79
80 @Override
81 public org.apache.maven.settings.Server getServer( String serverId )
82 {
83 for ( org.apache.maven.settings.Server server : getServers() )
84 {
85 if ( server.getId().equals( serverId ) )
86 {
87 return server;
88 }
89 }
90 return null;
91 }
92
93 @Override
94 public List<org.apache.maven.settings.Server> getServers()
95 {
96 return servers;
97 }
98 };
99
100 final DistributionManagement distributionManagement = new DistributionManagement();
101 DeploymentRepository repository = new DeploymentRepository();
102 repository.setId( "localhost" );
103 repository.setUrl( "http://localhost:8080" );
104 distributionManagement.setRepository( repository );
105 distributionManagement.setSnapshotRepository( repository );
106 projectStub = new MavenProjectStub()
107 {
108 @Override
109 public DistributionManagement getDistributionManagement()
110 {
111 return distributionManagement;
112 }
113 };
114
115 final DistributionManagement distributionManagementSec = new DistributionManagement();
116 DeploymentRepository repositorySec = new DeploymentRepository();
117 repositorySec.setId( "localhost" );
118 repositorySec.setUrl( "https://localhost:8443" );
119 distributionManagementSec.setRepository( repositorySec );
120 distributionManagementSec.setSnapshotRepository( repositorySec );
121 projectStubSec = new MavenProjectStub()
122 {
123 @Override
124 public DistributionManagement getDistributionManagement()
125 {
126 return distributionManagementSec;
127 }
128 };
129 }
130
131 protected void tearDown()
132 throws Exception
133 {
134 super.tearDown();
135 }
136
137 public void testGetInputStreamURL()
138 throws Exception
139 {
140
141 URL url = new File( getBasedir(), "/target/classes/project-info-report.properties" ).toURI().toURL();
142
143 String content = ProjectInfoReportUtils.getContent( url, projectStub, settingsStub, null );
144 Assert.assertNotNull( content );
145 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
146
147
148 startJetty( false, false );
149
150 url = new URL( "http://localhost:8080/project-info-report.properties" );
151
152 content = ProjectInfoReportUtils.getContent( url, projectStub, settingsStub, null );
153 Assert.assertNotNull( content );
154 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
155
156 stopJetty();
157
158
159 startJetty( false, true );
160
161 url = new URL( "http://localhost:8080/project-info-report.properties" );
162
163 content = ProjectInfoReportUtils.getContent( url, projectStub, settingsStub, null );
164 Assert.assertNotNull( content );
165 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
166
167 stopJetty();
168
169
170 startJetty( true, false );
171
172 url = new URL( "https://localhost:8443/project-info-report.properties" );
173
174 content = ProjectInfoReportUtils.getContent( url, projectStub, settingsStub, null );
175 Assert.assertNotNull( content );
176 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
177
178 stopJetty();
179
180
181 startJetty( true, true );
182
183 url = new URL( "https://localhost:8443/project-info-report.properties" );
184
185 content = ProjectInfoReportUtils.getContent( url, projectStubSec, settingsStub, null );
186 Assert.assertNotNull( content );
187 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
188
189 stopJetty();
190
191
192 }
193
194 private void startJetty( boolean isSSL, boolean withAuth )
195 throws Exception
196 {
197 jettyServer = new Server();
198 jettyServer.setStopAtShutdown( true );
199
200 Connector connector = ( isSSL ? getSSLConnector() : getDefaultConnector() );
201 jettyServer.setConnectors( new Connector[] { connector } );
202
203 WebAppContext webapp = new WebAppContext();
204 webapp.setContextPath( "/" );
205 webapp.setResourceBase( getBasedir() + "/target/classes/" );
206
207 webapp.setServer( jettyServer );
208
209 if ( withAuth )
210 {
211 Constraint constraint = new Constraint();
212 constraint.setName( Constraint.__BASIC_AUTH );
213 constraint.setRoles( new String[] { "user", "admin" } );
214 constraint.setAuthenticate( true );
215
216 ConstraintMapping cm = new ConstraintMapping();
217 cm.setConstraint( constraint );
218 cm.setPathSpec( "/*" );
219
220 SecurityHandler sh = new SecurityHandler();
221 sh.setUserRealm( new HashUserRealm( "MyRealm", getBasedir() + "/src/test/resources/realm.properties" ) );
222 sh.setConstraintMappings( new ConstraintMapping[] { cm } );
223
224 webapp.addHandler( sh );
225 }
226
227 DefaultHandler defaultHandler = new DefaultHandler();
228 defaultHandler.setServer( jettyServer );
229
230 Handler[] handlers = new Handler[2];
231 handlers[0] = webapp;
232 handlers[1] = defaultHandler;
233 jettyServer.setHandlers( handlers );
234
235 jettyServer.start();
236 }
237
238 private void stopJetty()
239 throws Exception
240 {
241 if ( jettyServer != null )
242 {
243 jettyServer.stop();
244
245 jettyServer = null;
246 }
247 }
248
249 private Connector getDefaultConnector()
250 {
251 Connector connector = new SelectChannelConnector();
252 connector.setPort( 8080 );
253 connector.setMaxIdleTime( MAX_IDLE_TIME );
254 return connector;
255 }
256
257 private Connector getSSLConnector()
258 {
259 SslSocketConnector connector = new SslSocketConnector();
260 connector.setPort( 8443 );
261 connector.setKeystore( getBasedir() + "/target/jetty.jks" );
262 connector.setPassword( "apache" );
263 connector.setKeyPassword( "apache" );
264 connector.setTruststore( getBasedir() + "/target/jetty.jks" );
265 connector.setTrustPassword( "apache" );
266 return connector;
267 }
268 }