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         // FIXME! assertEquals( "downloads.html", tool.getRelativePath( to, from ) );
159 
160         // MSITE-600, MSHARED-203
161         to = "file:///tmp/bloop";
162         from = "scp://localhost:/tmp/blop";
163         // FIXME! assertEquals( tool.getRelativePath( to, from ), to );
164 
165         // note: 'tmp' is the host here which is probably not the intention, but at least the result is correct
166         to = "file://tmp/bloop";
167         from = "scp://localhost:/tmp/blop";
168         assertEquals( to, tool.getRelativePath( to, from ) );
169 
170         // Tests between files as described in MIDEA-102
171         to = "C:/dev/voca/gateway/parser/gateway-parser.iml";
172         from = "C:/dev/voca/gateway/";
173         assertEquals( "Child file using Windows drive letter",
174                       "parser" + File.separator + "gateway-parser.iml", tool.getRelativePath( to, from ) );
175         to = "C:/foo/child";
176         from = "C:/foo/master";
177         assertEquals( "Sibling directory using Windows drive letter",
178                       ".." + File.separator + "child", tool.getRelativePath( to, from ) );
179         to = "/myproject/myproject-module1";
180         from = "/myproject/myproject";
181         assertEquals( "Sibling directory with similar name",
182                       ".." + File.separator + "myproject-module1", tool.getRelativePath( to, from ) );
183 
184         // Normalized paths as described in MSITE-284
185         assertEquals( ".." + File.separator + "project-module-1" + File.separator + "src" + File.separator + "site",
186                       tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\src\\site",
187                                             "Z:\\dir\\project\\project-module-1\\..\\project-parent" ) );
188         assertEquals( ".." + File.separator + ".." + File.separator + ".." + File.separator + "project-parent",
189                       tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\..\\project-parent",
190                                             "Z:\\dir\\project\\project-module-1\\src\\site" ) );
191 
192         assertEquals( ".." + File.separator + "foo", tool.getRelativePath( "../../foo/foo", "../../foo/bar" ) );
193     }
194 
195     /**
196      * @throws Exception
197      */
198     public void testGetSiteDescriptorFromBasedir()
199         throws Exception
200     {
201         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
202         assertNotNull( tool );
203 
204         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
205         assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), null ).toString(),
206             project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
207         assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), "src/site" ), Locale.ENGLISH ).toString(),
208             project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
209         String siteDir = "src/blabla";
210         assertEquals( tool.getSiteDescriptor( new File( project.getBasedir(), siteDir ), null ).toString(),
211             project.getBasedir() + File.separator + "src" + File.separator + "blabla" + File.separator + "site.xml" );
212     }
213 
214     /**
215      * @throws Exception
216      */
217     public void testGetSiteDescriptorFromRepository()
218         throws Exception
219     {
220         DefaultSiteTool tool = (DefaultSiteTool) lookup( SiteTool.ROLE );
221         assertNotNull( tool );
222 
223         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
224         project.setGroupId( "org.apache.maven" );
225         project.setArtifactId( "maven-site" );
226         project.setVersion( "1.0" );
227         String result = getLocalRepoDir() + File.separator + "org" + File.separator + "apache" + File.separator
228             + "maven" + File.separator + "maven-site" + File.separator + "1.0" + File.separator
229             + "maven-site-1.0-site.xml";
230 
231         assertEquals( tool.getSiteDescriptorFromRepository( project, getLocalRepo(),
232                                                             project.getRemoteArtifactRepositories(), Locale.ENGLISH )
233             .toString(), result );
234     }
235 
236     /**
237      * @throws Exception
238      */
239     public void testGetDecorationModel()
240         throws Exception
241     {
242         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
243         assertNotNull( tool );
244 
245         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
246         List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
247 
248         // model from current local build
249         DecorationModel model =
250             tool.getDecorationModel( new File( project.getBasedir(), "src/site" ), Locale.getDefault(), project,
251                                      reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories() );
252         assertNotNull( model );
253         assertNotNull( model.getBannerLeft() );
254         assertEquals( "Maven Site", model.getBannerLeft().getName() );
255         assertEquals( "http://maven.apache.org/images/apache-maven-project.png", model.getBannerLeft().getSrc() );
256         assertEquals( "http://maven.apache.org/", model.getBannerLeft().getHref() );
257         assertNotNull( model.getBannerRight() );
258         assertNull( model.getBannerRight().getName() );
259         assertEquals( "http://maven.apache.org/images/maven-small.gif", model.getBannerRight().getSrc() );
260         assertNull( model.getBannerRight().getHref() );
261 
262         // model from repo: https://repo1.maven.org/maven2/org/apache/maven/maven-site/1.0/maven-site-1.0-site.xml
263         // TODO Enable this test as soon as we haven a site.xml with head content as string
264         /*project.setBasedir( null );
265         project.setGroupId( "org.apache.maven" );
266         project.setArtifactId( "maven-site" );
267         project.setVersion( "1.0" );
268         DecorationModel modelFromRepo =
269             tool.getDecorationModel( null, Locale.getDefault(), project, reactorProjects, getLocalRepo(),
270                                      project.getRemoteArtifactRepositories() );
271         assertNotNull( modelFromRepo );
272         assertNotNull( modelFromRepo.getBannerLeft() );
273         assertEquals( "Maven", modelFromRepo.getBannerLeft().getName() );
274         assertEquals( "images/apache-maven-project-2.png", modelFromRepo.getBannerLeft().getSrc() );
275         assertEquals( "http://maven.apache.org/", modelFromRepo.getBannerLeft().getHref() );
276         assertNotNull( modelFromRepo.getBannerRight() );
277         assertNull( modelFromRepo.getBannerRight().getName() );
278         assertEquals( "images/maven-logo-2.gif", modelFromRepo.getBannerRight().getSrc() );
279         assertNull( modelFromRepo.getBannerRight().getHref() );*/
280     }
281 
282     /**
283      * @throws Exception
284      */
285     public void testGetDefaultDecorationModel()
286         throws Exception
287     {
288         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
289         assertNotNull( tool );
290 
291         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "no-site-test" );
292         String siteDirectory = "src/site";
293         List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
294 
295         DecorationModel model =
296             tool.getDecorationModel( new File( project.getBasedir(), siteDirectory ), Locale.getDefault(), project,
297                                      reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories() );
298         assertNotNull( model );
299     }
300 
301     public void testGetAvailableLocales()
302                     throws Exception
303     {
304         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
305 
306         assertEquals( Arrays.asList( new Locale[] { SiteTool.DEFAULT_LOCALE } ), tool.getSiteLocales( "en" ) );
307 
308         assertEquals( Arrays.asList( new Locale[] { SiteTool.DEFAULT_LOCALE, Locale.FRENCH, Locale.ITALIAN } ),
309                       tool.getSiteLocales( "en,fr,it" ) );
310 
311         // by default, only DEFAULT_LOCALE
312         assertEquals( Arrays.asList( new Locale[] { SiteTool.DEFAULT_LOCALE } ), tool.getSiteLocales( "" ) );
313     }
314 
315     public void testGetInterpolatedSiteDescriptorContent()
316         throws Exception
317     {
318         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
319         assertNotNull( tool );
320 
321         File pomXmlFile = getTestFile( "src/test/resources/unit/interpolated-site/pom.xml" );
322         assertNotNull( pomXmlFile );
323         assertTrue( pomXmlFile.exists() );
324 
325         File descriptorFile = getTestFile( "src/test/resources/unit/interpolated-site/src/site/site.xml" );
326         assertNotNull( descriptorFile );
327         assertTrue( descriptorFile.exists() );
328 
329         String siteDescriptorContent = FileUtils.fileRead( descriptorFile );
330         assertNotNull( siteDescriptorContent );
331         assertTrue( siteDescriptorContent.contains( "${project.name}" ) );
332         assertFalse( siteDescriptorContent.contains( "Interpolatesite" ) );
333 
334         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "interpolated-site" );
335 
336         SiteTool siteTool = (SiteTool) lookup( SiteTool.ROLE );
337         siteDescriptorContent =
338             siteTool.getInterpolatedSiteDescriptorContent( new HashMap<String, String>(), project,
339                                                            siteDescriptorContent );
340         assertNotNull( siteDescriptorContent );
341         assertFalse( siteDescriptorContent.contains( "${project.name}" ) );
342         assertTrue( siteDescriptorContent.contains( "Interpolatesite" ) );
343     }
344 
345     // MSHARED-217 -> DOXIATOOLS-34 -> DOXIASITETOOLS-118
346     public void testDecorationModelInheritanceAndInterpolation()
347         throws Exception
348     {
349         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
350         assertNotNull( tool );
351 
352         SiteToolMavenProjectStub parentProject = new SiteToolMavenProjectStub( "interpolation-parent-test" );
353         parentProject.setDistgributionManagementSiteUrl( "dav:https://davs.codehaus.org/site" );
354 
355         SiteToolMavenProjectStub childProject = new SiteToolMavenProjectStub( "interpolation-child-test" );
356         childProject.setParent( parentProject );
357         childProject.setDistgributionManagementSiteUrl( "dav:https://davs.codehaus.org/site/child" );
358 
359         List<MavenProject> reactorProjects = Collections.<MavenProject>singletonList( parentProject );
360 
361         DecorationModel model = tool.getDecorationModel( new File( childProject.getBasedir(), "src/site" ),
362                                                          Locale.getDefault(), childProject, reactorProjects,
363                                                          getLocalRepo(), childProject.getRemoteArtifactRepositories() );
364         assertNotNull( model );
365 
366         writeModel( model, "unit/interpolation-child-test/effective-site.xml" );
367 
368         assertEquals( "MSHARED-217 Child", model.getName() );
369         // late (classical) interpolation
370         assertEquals( "project.artifactId = mshared-217-child", model.getBannerLeft().getName() );
371         // early interpolation: DOXIASITETOOLS-158
372         assertEquals( "this.artifactId = mshared-217-parent", model.getBannerRight().getName() );
373         // href rebase
374         assertEquals( "../../index.html", model.getBody().getBreadcrumbs().iterator().next().getHref() );
375         Iterator<LinkItem> links = model.getBody().getLinks().iterator();
376         // late interpolation of pom content (which happens first: properties can't override)
377         assertEquals( "project.name = MSHARED-217 Child", links.next().getName() );
378         assertEquals( "name = MSHARED-217 Child", links.next().getName() );
379         // early interpolation: DOXIASITETOOLS-158
380         assertEquals( "this.name = MSHARED-217 Parent", links.next().getName() );
381 
382         // late interpolation of project properties
383         assertEquals( "my_property = from child pom.xml", links.next().getName() );
384         // early interpolation of project properties: DOXIASITETOOLS-158
385         assertEquals( "this.my_property = from parent pom.xml", links.next().getName() );
386 
387         // Env Var interpolation
388         String envPath = links.next().getName();
389         assertTrue( envPath.startsWith( "env.PATH = " ) );
390         assertFalse( envPath.contains( "${" ) );
391         assertNotSame( "env.PATH = PATH property from pom", envPath );
392 
393         // property overrides env
394         assertEquals( "PATH = PATH property from pom", links.next().getName() );
395     }
396 
397     private void writeModel( DecorationModel model, String to )
398         throws Exception
399     {
400         Writer writer = WriterFactory.newXmlWriter( getTestFile( "target/test-classes/" + to ) );
401         try
402         {
403             new DecorationXpp3Writer().write( writer, model );
404         }
405         finally
406         {
407             IOUtil.close( writer );
408         }
409     }
410 }