View Javadoc
1   package org.apache.maven.doxia.tools;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
50   */
51  public class SiteToolTest
52      extends PlexusTestCase
53  {
54      /**
55       * @return the repo.
56       *
57       * @throws Exception
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       * @return the local repo directory.
78       *
79       * @throws Exception
80       */
81      protected File getLocalRepoDir()
82          throws Exception
83      {
84          return new File( getLocalRepo().getBasedir() );
85      }
86  
87      /**
88       * @throws Exception
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      * @throws Exception
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      * @throws Exception
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         // MSITE-600, MSHARED-203
160         to = "file:///tmp/bloop";
161         from = "scp://localhost:/tmp/blop";
162         assertEquals( tool.getRelativePath( to, from ), to );
163 
164         // note: 'tmp' is the host here which is probably not the intention, but at least the result is correct
165         to = "file://tmp/bloop";
166         from = "scp://localhost:/tmp/blop";
167         assertEquals( to, tool.getRelativePath( to, from ) );
168 
169         // Tests between files as described in MIDEA-102
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         // Normalized paths as described in MSITE-284
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      * @throws Exception
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      * @throws Exception
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      * @throws Exception
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         // model from current local build
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         // model from repo: https://repo1.maven.org/maven2/org/apache/maven/maven-site/1.0/maven-site-1.0-site.xml
262         // TODO Enable this test as soon as we haven a site.xml with head content as string
263         /*project.setBasedir( null );
264         project.setGroupId( "org.apache.maven" );
265         project.setArtifactId( "maven-site" );
266         project.setVersion( "1.0" );
267         DecorationModel modelFromRepo =
268             tool.getDecorationModel( null, Locale.getDefault(), project, reactorProjects, getLocalRepo(),
269                                      project.getRemoteArtifactRepositories() );
270         assertNotNull( modelFromRepo );
271         assertNotNull( modelFromRepo.getBannerLeft() );
272         assertEquals( "Maven", modelFromRepo.getBannerLeft().getName() );
273         assertEquals( "images/apache-maven-project-2.png", modelFromRepo.getBannerLeft().getSrc() );
274         assertEquals( "http://maven.apache.org/", modelFromRepo.getBannerLeft().getHref() );
275         assertNotNull( modelFromRepo.getBannerRight() );
276         assertNull( modelFromRepo.getBannerRight().getName() );
277         assertEquals( "images/maven-logo-2.gif", modelFromRepo.getBannerRight().getSrc() );
278         assertNull( modelFromRepo.getBannerRight().getHref() );*/
279     }
280 
281     /**
282      * @throws Exception
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         // by default, only DEFAULT_LOCALE
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     // MSHARED-217 -> DOXIATOOLS-34 -> DOXIASITETOOLS-118
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         // late (classical) interpolation
369         assertEquals( "project.artifactId = mshared-217-child", model.getBannerLeft().getName() );
370         // early interpolation: DOXIASITETOOLS-158
371         assertEquals( "this.artifactId = mshared-217-parent", model.getBannerRight().getName() );
372         // href rebase
373         assertEquals( "../../index.html", model.getBody().getBreadcrumbs().iterator().next().getHref() );
374         Iterator<LinkItem> links = model.getBody().getLinks().iterator();
375         // late interpolation of pom content
376         assertEquals( "project.name = MSHARED-217 Child", links.next().getName() );
377         assertEquals( "name = name property", links.next().getName() );
378         // early interpolation: DOXIASITETOOLS-158
379         assertEquals( "this.name = MSHARED-217 Parent", links.next().getName() );
380 
381         // late interpolation of project properties
382         assertEquals( "my_property = from child pom.xml", links.next().getName() );
383         // early interpolation of project properties: DOXIASITETOOLS-158
384         assertEquals( "this.my_property = from parent pom.xml", links.next().getName() );
385 
386         // Env Var interpolation
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         // property overrides env
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 }