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