1 package org.apache.maven.doxia.tools;
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.io.Writer;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Locale;
31
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
34 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
35 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
36 import org.apache.maven.doxia.site.decoration.DecorationModel;
37 import org.apache.maven.doxia.site.decoration.LinkItem;
38 import org.apache.maven.doxia.site.decoration.Skin;
39 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
40 import org.apache.maven.doxia.tools.stubs.SiteToolMavenProjectStub;
41 import org.apache.maven.project.MavenProject;
42
43 import org.codehaus.plexus.PlexusTestCase;
44 import org.codehaus.plexus.util.FileUtils;
45 import org.codehaus.plexus.util.IOUtil;
46 import org.codehaus.plexus.util.WriterFactory;
47
48
49
50
51 public class SiteToolTest
52 extends PlexusTestCase
53 {
54
55
56
57
58
59 protected ArtifactRepository getLocalRepo()
60 throws Exception
61 {
62 String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
63 String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
64 ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy( true, updatePolicyFlag,
65 checksumPolicyFlag );
66 ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy( true, updatePolicyFlag,
67 checksumPolicyFlag );
68 ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
69 ArtifactRepositoryLayout defaultArtifactRepositoryLayout = (ArtifactRepositoryLayout) lookup(
70 ArtifactRepositoryLayout.ROLE,
71 "default" );
72 return artifactRepositoryFactory.createArtifactRepository( "local", getTestFile( "target/local-repo" ).toURI().toURL()
73 .toString(), defaultArtifactRepositoryLayout, snapshotsPolicy, releasesPolicy );
74 }
75
76
77
78
79
80
81 protected File getLocalRepoDir()
82 throws Exception
83 {
84 return new File( getLocalRepo().getBasedir() );
85 }
86
87
88
89
90 public void testGetDefaultSkinArtifact()
91 throws Exception
92 {
93 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
94 assertNotNull( tool );
95
96 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
97 assertNotNull( tool.getDefaultSkinArtifact( getLocalRepo(), project.getRemoteArtifactRepositories() ) );
98 }
99
100
101
102
103 public void testGetSkinArtifactFromRepository()
104 throws Exception
105 {
106 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
107 assertNotNull( tool );
108
109 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
110 DecorationModel decorationModel = new DecorationModel();
111 Skin skin = new Skin();
112 skin.setGroupId( "org.apache.maven.skins" );
113 skin.setArtifactId( "maven-stylus-skin" );
114 decorationModel.setSkin( skin );
115 assertNotNull( tool.getSkinArtifactFromRepository( getLocalRepo(), project.getRemoteArtifactRepositories(),
116 decorationModel ) );
117 }
118
119 private void checkGetRelativePathDirectory( SiteTool tool, String relative, String to, String from )
120 {
121 assertEquals( relative, tool.getRelativePath( to, from ) );
122 assertEquals( relative, tool.getRelativePath( to + '/', from ) );
123 assertEquals( relative, tool.getRelativePath( to, from + '/' ) );
124 assertEquals( relative, tool.getRelativePath( to + '/', from + '/' ) );
125 }
126
127
128
129
130 public void testGetRelativePath()
131 throws Exception
132 {
133 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
134 assertNotNull( tool );
135
136 checkGetRelativePathDirectory( tool, "", "http://maven.apache.org", "http://maven.apache.org" );
137
138 checkGetRelativePathDirectory( tool, ".." + File.separator + "..", "http://maven.apache.org",
139 "http://maven.apache.org/plugins/maven-site-plugin" );
140
141 checkGetRelativePathDirectory( tool, "plugins" + File.separator + "maven-site-plugin",
142 "http://maven.apache.org/plugins/maven-site-plugin", "http://maven.apache.org" );
143
144 checkGetRelativePathDirectory( tool, "", "dav:https://maven.apache.org", "dav:https://maven.apache.org" );
145
146 checkGetRelativePathDirectory( tool, "plugins" + File.separator + "maven-site-plugin",
147 "dav:http://maven.apache.org/plugins/maven-site-plugin",
148 "dav:http://maven.apache.org" );
149
150 checkGetRelativePathDirectory( tool, "", "scm:svn:https://maven.apache.org", "scm:svn:https://maven.apache.org" );
151
152 checkGetRelativePathDirectory( tool, "plugins" + File.separator + "maven-site-plugin",
153 "scm:svn:https://maven.apache.org/plugins/maven-site-plugin",
154 "scm:svn:https://maven.apache.org" );
155
156 String to = "http://maven.apache.org/downloads.html";
157 String from = "http://maven.apache.org/index.html";
158
159
160 to = "file:///tmp/bloop";
161 from = "scp://localhost:/tmp/blop";
162 assertEquals( tool.getRelativePath( to, from ), to );
163
164
165 to = "file://tmp/bloop";
166 from = "scp://localhost:/tmp/blop";
167 assertEquals( to, tool.getRelativePath( to, from ) );
168
169
170 to = "C:/dev/voca/gateway/parser/gateway-parser.iml";
171 from = "C:/dev/voca/gateway/";
172 assertEquals( "Child file using Windows drive letter",
173 "parser" + File.separator + "gateway-parser.iml", tool.getRelativePath( to, from ) );
174 to = "C:/foo/child";
175 from = "C:/foo/master";
176 assertEquals( "Sibling directory using Windows drive letter",
177 ".." + File.separator + "child", tool.getRelativePath( to, from ) );
178 to = "/myproject/myproject-module1";
179 from = "/myproject/myproject";
180 assertEquals( "Sibling directory with similar name",
181 ".." + File.separator + "myproject-module1", tool.getRelativePath( to, from ) );
182
183
184 assertEquals( ".." + File.separator + "project-module-1" + File.separator + "src" + File.separator + "site",
185 tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\src\\site",
186 "Z:\\dir\\project\\project-module-1\\..\\project-parent" ) );
187 assertEquals( ".." + File.separator + ".." + File.separator + ".." + File.separator + "project-parent",
188 tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\..\\project-parent",
189 "Z:\\dir\\project\\project-module-1\\src\\site" ) );
190
191 assertEquals( ".." + File.separator + "foo", tool.getRelativePath( "../../foo/foo", "../../foo/bar" ) );
192 }
193
194
195
196
197 public void testGetSiteDescriptorFromBasedir()
198 throws Exception
199 {
200 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
201 assertNotNull( tool );
202
203 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
204 assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), null ).toString(),
205 project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
206 assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), Locale.ENGLISH ).toString(),
207 project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
208 String siteDir = "src/blabla";
209 assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), siteDir ), null ).toString(),
210 project.getBasedir() + File.separator + "src" + File.separator + "blabla" + File.separator + "site.xml" );
211 }
212
213
214
215
216 public void testGetSiteDescriptorFromRepository()
217 throws Exception
218 {
219 DefaultSiteTool tool = (DefaultSiteTool) lookup( SiteTool.ROLE );
220 assertNotNull( tool );
221
222 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
223 project.setGroupId( "org.apache.maven" );
224 project.setArtifactId( "maven-site" );
225 project.setVersion( "1.0" );
226 String result = getLocalRepoDir() + File.separator + "org" + File.separator + "apache" + File.separator
227 + "maven" + File.separator + "maven-site" + File.separator + "1.0" + File.separator
228 + "maven-site-1.0-site.xml";
229
230 assertEquals( tool.getSiteDescriptorFromRepository( project, getLocalRepo(),
231 project.getRemoteArtifactRepositories(), Locale.ENGLISH )
232 .toString(), result );
233 }
234
235
236
237
238 public void testGetDecorationModel()
239 throws Exception
240 {
241 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
242 assertNotNull( tool );
243
244 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
245 List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
246
247
248 DecorationModel model =
249 tool.getDecorationModel( new File( project.getBasedir(), "src/site" ), Locale.getDefault(), project,
250 reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories() );
251 assertNotNull( model );
252 assertNotNull( model.getBannerLeft() );
253 assertEquals( "Maven Site", model.getBannerLeft().getName() );
254 assertEquals( "http://maven.apache.org/images/apache-maven-project.png", model.getBannerLeft().getSrc() );
255 assertEquals( "http://maven.apache.org/", model.getBannerLeft().getHref() );
256 assertNotNull( model.getBannerRight() );
257 assertNull( model.getBannerRight().getName() );
258 assertEquals( "http://maven.apache.org/images/maven-small.gif", model.getBannerRight().getSrc() );
259 assertNull( model.getBannerRight().getHref() );
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 }
280
281
282
283
284 public void testGetDefaultDecorationModel()
285 throws Exception
286 {
287 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
288 assertNotNull( tool );
289
290 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "no-site-test" );
291 String siteDirectory = "src/site";
292 List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
293
294 DecorationModel model =
295 tool.getDecorationModel( new File( project.getBasedir(), siteDirectory ), Locale.getDefault(), project,
296 reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories() );
297 assertNotNull( model );
298 }
299
300 public void testGetAvailableLocales()
301 throws Exception
302 {
303 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
304
305 assertEquals( Arrays.asList( new Locale[] { SiteTool.DEFAULT_LOCALE } ), tool.getSiteLocales( "en" ) );
306
307 assertEquals( Arrays.asList( new Locale[] { SiteTool.DEFAULT_LOCALE, Locale.FRENCH, Locale.ITALIAN } ),
308 tool.getSiteLocales( "en,fr,it" ) );
309
310
311 assertEquals( Arrays.asList( new Locale[] { SiteTool.DEFAULT_LOCALE } ), tool.getSiteLocales( "" ) );
312 }
313
314 public void testGetInterpolatedSiteDescriptorContent()
315 throws Exception
316 {
317 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
318 assertNotNull( tool );
319
320 File pomXmlFile = getTestFile( "src/test/resources/unit/interpolated-site/pom.xml" );
321 assertNotNull( pomXmlFile );
322 assertTrue( pomXmlFile.exists() );
323
324 File descriptorFile = getTestFile( "src/test/resources/unit/interpolated-site/src/site/site.xml" );
325 assertNotNull( descriptorFile );
326 assertTrue( descriptorFile.exists() );
327
328 String siteDescriptorContent = FileUtils.fileRead( descriptorFile );
329 assertNotNull( siteDescriptorContent );
330 assertTrue( siteDescriptorContent.contains( "${project.name}" ) );
331 assertFalse( siteDescriptorContent.contains( "Interpolatesite" ) );
332
333 SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "interpolated-site" );
334
335 SiteTool siteTool = (SiteTool) lookup( SiteTool.ROLE );
336 siteDescriptorContent =
337 siteTool.getInterpolatedSiteDescriptorContent( new HashMap<String, String>(), project,
338 siteDescriptorContent );
339 assertNotNull( siteDescriptorContent );
340 assertFalse( siteDescriptorContent.contains( "${project.name}" ) );
341 assertTrue( siteDescriptorContent.contains( "Interpolatesite" ) );
342 }
343
344
345 public void testDecorationModelInheritanceAndInterpolation()
346 throws Exception
347 {
348 SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
349 assertNotNull( tool );
350
351 SiteToolMavenProjectStub parentProject = new SiteToolMavenProjectStub( "interpolation-parent-test" );
352 parentProject.setDistgributionManagementSiteUrl( "dav:https://davs.codehaus.org/site" );
353
354 SiteToolMavenProjectStub childProject = new SiteToolMavenProjectStub( "interpolation-child-test" );
355 childProject.setParent( parentProject );
356 childProject.setDistgributionManagementSiteUrl( "dav:https://davs.codehaus.org/site/child" );
357
358 List<MavenProject> reactorProjects = Collections.<MavenProject>singletonList( parentProject );
359
360 DecorationModel model = tool.getDecorationModel( new File( childProject.getBasedir(), "src/site" ),
361 Locale.getDefault(), childProject, reactorProjects,
362 getLocalRepo(), childProject.getRemoteArtifactRepositories() );
363 assertNotNull( model );
364
365 writeModel( model, "unit/interpolation-child-test/effective-site.xml" );
366
367 assertEquals( "MSHARED-217 Child", model.getName() );
368
369 assertEquals( "project.artifactId = mshared-217-child", model.getBannerLeft().getName() );
370
371 assertEquals( "this.artifactId = mshared-217-parent", model.getBannerRight().getName() );
372
373 assertEquals( "../../index.html", model.getBody().getBreadcrumbs().iterator().next().getHref() );
374 Iterator<LinkItem> links = model.getBody().getLinks().iterator();
375
376 assertEquals( "project.name = MSHARED-217 Child", links.next().getName() );
377 assertEquals( "name = MSHARED-217 Child", links.next().getName() );
378
379 assertEquals( "this.name = MSHARED-217 Parent", links.next().getName() );
380
381
382 assertEquals( "my_property = from child pom.xml", links.next().getName() );
383
384 assertEquals( "this.my_property = from parent pom.xml", links.next().getName() );
385
386
387 String envPath = links.next().getName();
388 assertTrue( envPath.startsWith( "env.PATH = " ) );
389 assertFalse( envPath.contains( "${" ) );
390 assertNotSame( "env.PATH = PATH property from pom", envPath );
391
392
393 assertEquals( "PATH = PATH property from pom", links.next().getName() );
394 }
395
396 private void writeModel( DecorationModel model, String to )
397 throws Exception
398 {
399 Writer writer = WriterFactory.newXmlWriter( getTestFile( "target/test-classes/" + to ) );
400 try
401 {
402 new DecorationXpp3Writer().write( writer, model );
403 }
404 finally
405 {
406 IOUtil.close( writer );
407 }
408 }
409 }