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 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
47
48
49
50 public class ProjectInfoReportUtilsTest
51 extends AbstractMojoTestCase
52 {
53 private static final int MAX_IDLE_TIME = 30000;
54
55 private int port = -1;
56
57 private Settings settingsStub;
58
59 private Server jettyServer;
60
61 protected void setUp()
62 throws Exception
63 {
64 super.setUp();
65
66 final List<org.apache.maven.settings.Server> servers = new ArrayList<org.apache.maven.settings.Server>();
67 org.apache.maven.settings.Server server = new org.apache.maven.settings.Server();
68 server.setId( "localhost" );
69 server.setUsername( "admin" );
70 server.setPassword( "admin" );
71 servers.add( server );
72 settingsStub = new SettingsStub()
73 {
74 private static final long serialVersionUID = 1L;
75
76 @Override
77 public org.apache.maven.settings.Server getServer( String serverId )
78 {
79 for ( org.apache.maven.settings.Server server : getServers() )
80 {
81 if ( server.getId().equals( serverId ) )
82 {
83 return server;
84 }
85 }
86 return null;
87 }
88
89 @Override
90 public List<org.apache.maven.settings.Server> getServers()
91 {
92 return servers;
93 }
94 };
95
96 }
97
98 private MavenProject getMavenProjectStub( boolean https )
99 {
100 final DistributionManagement distributionManagement = new DistributionManagement();
101 DeploymentRepository repository = new DeploymentRepository();
102 repository.setId( "localhost" );
103 repository.setUrl( ( https ? "https" : "http" ) + "://localhost:" + port );
104 distributionManagement.setRepository( repository );
105 distributionManagement.setSnapshotRepository( repository );
106 return new MavenProjectStub()
107 {
108 @Override
109 public DistributionManagement getDistributionManagement()
110 {
111 return distributionManagement;
112 }
113 };
114 }
115
116 protected void tearDown()
117 throws Exception
118 {
119 super.tearDown();
120 }
121
122 public void testGetInputStreamURL()
123 throws Exception
124 {
125 assertTrue( ProjectInfoReportUtils.isArtifactUrlValid( "http://my.intern.domain:8080/test" ) );
126
127
128 URL url = new File( getBasedir(), "/target/classes/project-info-reports.properties" ).toURI().toURL();
129
130 String content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
131 "ISO-8859-1" );
132 assertNotNull( content );
133 assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
134
135
136 url = new File( getBasedir(), "/src/test/resources/iso-8859-5-encoded.txt" ).toURI().toURL();
137
138 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
139 "ISO-8859-5" );
140 assertNotNull( content );
141 assertTrue( content.contains( "Свобода всем народам!" ) );
142
143
144 startJetty( false, false );
145
146 url = new URL( "http://localhost:" + port + "/project-info-reports.properties" );
147
148 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
149 "ISO-8859-1" );
150 assertNotNull( content );
151 assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
152
153 stopJetty();
154
155
156 startJetty( false, true );
157
158 url = new URL( "http://localhost:" + port + "/project-info-reports.properties" );
159
160 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( false ), settingsStub,
161 "ISO-8859-1" );
162 assertNotNull( content );
163 assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
164
165 stopJetty();
166
167
168 startJetty( true, false );
169
170 url = new URL( "https://localhost:" + port + "/project-info-reports.properties" );
171
172 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub,
173 "ISO-8859-1" );
174 assertNotNull( content );
175 assertTrue( content.contains( "Licensed to the Apache Software Foundation" ) );
176
177 stopJetty();
178
179
180 startJetty( true, true );
181
182 url = new URL( "https://localhost:" + port + "/project-info-reports.properties" );
183
184 content = ProjectInfoReportUtils.getContent( url, getMavenProjectStub( true ), settingsStub,
185 "ISO-8859-1" );
186 assertNotNull( content );
187 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 port = connector.getLocalPort();
238 }
239
240 private void stopJetty()
241 throws Exception
242 {
243 if ( jettyServer != null )
244 {
245 jettyServer.stop();
246
247 jettyServer = null;
248
249 port = -1;
250 }
251 }
252
253 private Connector getDefaultConnector()
254 {
255 Connector connector = new SelectChannelConnector();
256 connector.setMaxIdleTime( MAX_IDLE_TIME );
257 return connector;
258 }
259
260 private Connector getSSLConnector()
261 {
262 SslSocketConnector connector = new SslSocketConnector();
263 connector.setKeystore( getBasedir() + "/target/jetty.jks" );
264 connector.setPassword( "apache" );
265 connector.setKeyPassword( "apache" );
266 connector.setTruststore( getBasedir() + "/target/jetty.jks" );
267 connector.setTrustPassword( "apache" );
268 return connector;
269 }
270 }