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   * @version $Id: SiteToolTest.html 1018749 2017-09-26 18:39:07Z rfscholte $
51   */
52  public class SiteToolTest
53      extends PlexusTestCase
54  {
55      /**
56       * @return the repo.
57       *
58       * @throws Exception
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       * @return the local repo directory.
79       *
80       * @throws Exception
81       */
82      protected File getLocalRepoDir()
83          throws Exception
84      {
85          return new File( getLocalRepo().getBasedir() );
86      }
87  
88      /**
89       * @throws Exception
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      * @throws Exception
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      * @throws Exception
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         // FIXME! assertEquals( "downloads.html", tool.getRelativePath( to, from ) );
160 
161         // MSITE-600, MSHARED-203
162         to = "file:///tmp/bloop";
163         from = "scp://localhost:/tmp/blop";
164         // FIXME! assertEquals( tool.getRelativePath( to, from ), to );
165 
166         // note: 'tmp' is the host here which is probably not the intention, but at least the result is correct
167         to = "file://tmp/bloop";
168         from = "scp://localhost:/tmp/blop";
169         assertEquals( to, tool.getRelativePath( to, from ) );
170 
171         // Tests between files as described in MIDEA-102
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         // Normalized paths as described in MSITE-284
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      * @throws Exception
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      * @throws Exception
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      * @throws Exception
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         // model from current local build
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         // model from repo: http://repo1.maven.org/maven2/org/apache/maven/maven-site/1.0/maven-site-1.0-site.xml
264         // TODO Enable this test as soon as we haven a site.xml with head content as string
265         /*project.setBasedir( null );
266         project.setGroupId( "org.apache.maven" );
267         project.setArtifactId( "maven-site" );
268         project.setVersion( "1.0" );
269         DecorationModel modelFromRepo =
270             tool.getDecorationModel( null, Locale.getDefault(), project, reactorProjects, getLocalRepo(),
271                                      project.getRemoteArtifactRepositories() );
272         assertNotNull( modelFromRepo );
273         assertNotNull( modelFromRepo.getBannerLeft() );
274         assertEquals( "Maven", modelFromRepo.getBannerLeft().getName() );
275         assertEquals( "images/apache-maven-project-2.png", modelFromRepo.getBannerLeft().getSrc() );
276         assertEquals( "http://maven.apache.org/", modelFromRepo.getBannerLeft().getHref() );
277         assertNotNull( modelFromRepo.getBannerRight() );
278         assertNull( modelFromRepo.getBannerRight().getName() );
279         assertEquals( "images/maven-logo-2.gif", modelFromRepo.getBannerRight().getSrc() );
280         assertNull( modelFromRepo.getBannerRight().getHref() );*/
281     }
282 
283     /**
284      * @throws Exception
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         // by default, only DEFAULT_LOCALE
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     // MSHARED-217 -> DOXIATOOLS-34 -> DOXIASITETOOLS-118
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         // late (classical) interpolation
371         assertEquals( "project.artifactId = mshared-217-child", model.getBannerLeft().getName() );
372         // early interpolation: DOXIASITETOOLS-158
373         assertEquals( "this.artifactId = mshared-217-parent", model.getBannerRight().getName() );
374         // href rebase
375         assertEquals( "../../index.html", model.getBody().getBreadcrumbs().iterator().next().getHref() );
376         Iterator<LinkItem> links = model.getBody().getLinks().iterator();
377         // late interpolation of pom content (which happens first: properties can't override)
378         assertEquals( "project.name = MSHARED-217 Child", links.next().getName() );
379         assertEquals( "name = MSHARED-217 Child", links.next().getName() );
380         // early interpolation: DOXIASITETOOLS-158
381         assertEquals( "this.name = MSHARED-217 Parent", links.next().getName() );
382 
383         // late interpolation of project properties
384         assertEquals( "my_property = from child pom.xml", links.next().getName() );
385         // early interpolation of project properties: DOXIASITETOOLS-158
386         assertEquals( "this.my_property = from parent pom.xml", links.next().getName() );
387 
388         // Env Var interpolation
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         // property overrides env
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 }