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