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 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
130 URL url = new File( getBasedir(), "/target/classes/project-info-report.properties" ).toURI().toURL();
131
132 String content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
133 "ISO-8859-1" );
134 Assert.assertNotNull( content );
135 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
136
137
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 Assert.assertNotNull( content );
143 Assert.assertTrue( content.contains( "Свобода всем народам!" ) );
144
145
146 startJetty( false, false );
147
148 url = new URL( "http://localhost:" + port + "/project-info-report.properties" );
149
150 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
151 "ISO-8859-1" );
152 Assert.assertNotNull( content );
153 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
154
155 stopJetty();
156
157
158 startJetty( false, true );
159
160 url = new URL( "http://localhost:" + port + "/project-info-report.properties" );
161
162 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
163 "ISO-8859-1" );
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:" + port + "/project-info-report.properties" );
173
174 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub,
175 "ISO-8859-1" );
176 Assert.assertNotNull( content );
177 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
178
179 stopJetty();
180
181
182 startJetty( true, true );
183
184 url = new URL( "https://localhost:" + port + "/project-info-report.properties" );
185
186 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub,
187 "ISO-8859-1" );
188 Assert.assertNotNull( content );
189 Assert.assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
190
191 stopJetty();
192
193
194 }
195
196 private void startJetty( boolean isSSL, boolean withAuth )
197 throws Exception
198 {
199 jettyServer = new Server();
200 jettyServer.setStopAtShutdown( true );
201
202 Connector connector = ( isSSL ? getSSLConnector() : getDefaultConnector() );
203 jettyServer.setConnectors( new Connector[] { connector } );
204
205 WebAppContext webapp = new WebAppContext();
206 webapp.setContextPath( "/" );
207 webapp.setResourceBase( getBasedir() + "/target/classes/" );
208
209 webapp.setServer( jettyServer );
210
211 if ( withAuth )
212 {
213 Constraint constraint = new Constraint();
214 constraint.setName( Constraint.__BASIC_AUTH );
215 constraint.setRoles( new String[] { "user", "admin" } );
216 constraint.setAuthenticate( true );
217
218 ConstraintMapping cm = new ConstraintMapping();
219 cm.setConstraint( constraint );
220 cm.setPathSpec( "/*" );
221
222 SecurityHandler sh = new SecurityHandler();
223 sh.setUserRealm( new HashUserRealm( "MyRealm", getBasedir() + "/src/test/resources/realm.properties" ) );
224 sh.setConstraintMappings( new ConstraintMapping[] { cm } );
225
226 webapp.addHandler( sh );
227 }
228
229 DefaultHandler defaultHandler = new DefaultHandler();
230 defaultHandler.setServer( jettyServer );
231
232 Handler[] handlers = new Handler[2];
233 handlers[0] = webapp;
234 handlers[1] = defaultHandler;
235 jettyServer.setHandlers( handlers );
236
237 jettyServer.start();
238
239 port = connector.getLocalPort();
240 }
241
242 private void stopJetty()
243 throws Exception
244 {
245 if ( jettyServer != null )
246 {
247 jettyServer.stop();
248
249 jettyServer = null;
250
251 port = -1;
252 }
253 }
254
255 private Connector getDefaultConnector()
256 {
257 Connector connector = new SelectChannelConnector();
258 connector.setMaxIdleTime( MAX_IDLE_TIME );
259 return connector;
260 }
261
262 private Connector getSSLConnector()
263 {
264 SslSocketConnector connector = new SslSocketConnector();
265 connector.setKeystore( getBasedir() + "/target/jetty.jks" );
266 connector.setPassword( "apache" );
267 connector.setKeyPassword( "apache" );
268 connector.setTruststore( getBasedir() + "/target/jetty.jks" );
269 connector.setTrustPassword( "apache" );
270 return connector;
271 }
272 }