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.List;
26  import java.util.Locale;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
30  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
31  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
32  import org.apache.maven.doxia.site.decoration.DecorationModel;
33  import org.apache.maven.doxia.site.decoration.Skin;
34  import org.apache.maven.doxia.tools.stubs.SiteToolMavenProjectStub;
35  import org.apache.maven.project.MavenProject;
36  
37  import org.codehaus.plexus.PlexusTestCase;
38  
39  /**
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   * @version $Id: SiteToolTest.html 914485 2014-06-30 19:54:45Z hboutemy $
42   */
43  public class SiteToolTest
44      extends PlexusTestCase
45  {
46      /**
47       * @return the repo.
48       *
49       * @throws Exception
50       */
51      protected ArtifactRepository getLocalRepo()
52          throws Exception
53      {
54          String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
55          String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
56          ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy( true, updatePolicyFlag,
57                                                                                   checksumPolicyFlag );
58          ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy( true, updatePolicyFlag,
59                                                                                  checksumPolicyFlag );
60          ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
61          ArtifactRepositoryLayout defaultArtifactRepositoryLayout = (ArtifactRepositoryLayout) lookup(
62                                                                                                        ArtifactRepositoryLayout.ROLE,
63                                                                                                        "default" );
64          return artifactRepositoryFactory.createArtifactRepository( "local", getTestFile( "target/local-repo" ).toURI().toURL()
65              .toString(), defaultArtifactRepositoryLayout, snapshotsPolicy, releasesPolicy );
66      }
67  
68      /**
69       * @return the local repo directory.
70       *
71       * @throws Exception
72       */
73      protected File getLocalRepoDir()
74          throws Exception
75      {
76          return new File( getLocalRepo().getBasedir() );
77      }
78  
79      /**
80       * @throws Exception
81       */
82      public void testGetDefaultSkinArtifact()
83          throws Exception
84      {
85          SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
86          assertNotNull( tool );
87  
88          SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
89          assertNotNull( tool.getDefaultSkinArtifact( getLocalRepo(), project.getRemoteArtifactRepositories() ) );
90      }
91  
92      /**
93       * @throws Exception
94       */
95      public void testGetSkinArtifactFromRepository()
96          throws Exception
97      {
98          SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
99          assertNotNull( tool );
100 
101         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
102         DecorationModel decorationModel = new DecorationModel();
103         Skin skin = new Skin();
104         skin.setGroupId( "org.apache.maven.skins" );
105         skin.setArtifactId( "maven-stylus-skin" );
106         decorationModel.setSkin( skin );
107         assertNotNull( tool.getSkinArtifactFromRepository( getLocalRepo(), project.getRemoteArtifactRepositories(),
108                                                            decorationModel ) );
109     }
110 
111     /**
112      * @throws Exception
113      */
114     public void testGetRelativePath()
115         throws Exception
116     {
117         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
118         assertNotNull( tool );
119 
120         String to = "http://maven.apache.org";
121         String from = "http://maven.apache.org";
122         assertEquals( tool.getRelativePath( to, from ), "" );
123 
124         to = "http://maven.apache.org";
125         from = "http://maven.apache.org/";
126         assertEquals( tool.getRelativePath( to, from ), "" );
127 
128         to = "http://maven.apache.org/";
129         from = "http://maven.apache.org";
130         assertEquals( tool.getRelativePath( to, from ), "" );
131 
132         to = "http://maven.apache.org/";
133         from = "http://maven.apache.org/";
134         assertEquals( tool.getRelativePath( to, from ), "" );
135 
136         to = "http://maven.apache.org/";
137         from = "http://maven.apache.org/plugins/maven-site-plugin";
138         assertEquals( tool.getRelativePath( to, from ), ".." + File.separator + ".." );
139         to = "http://maven.apache.org";
140         from = "http://maven.apache.org/plugins/maven-site-plugin/";
141         assertEquals( tool.getRelativePath( to, from ), ".." + File.separator + ".." );
142         to = "http://maven.apache.org/";
143         from = "http://maven.apache.org/plugins/maven-site-plugin/";
144         assertEquals( tool.getRelativePath( to, from ), ".." + File.separator + ".." );
145         to = "http://maven.apache.org";
146         from = "http://maven.apache.org/plugins/maven-site-plugin";
147         assertEquals( tool.getRelativePath( to, from ), ".." + File.separator + ".." );
148 
149         to = "http://maven.apache.org/downloads.html";
150         from = "http://maven.apache.org/index.html";
151         // FIXME! assertEquals( "downloads.html", tool.getRelativePath( to, from ) );
152 
153         // MSITE-600, MSHARED-203
154         to = "file:///tmp/bloop";
155         from = "scp://localhost:/tmp/blop";
156         // FIXME! assertEquals( tool.getRelativePath( to, from ), to );
157 
158         // note: 'tmp' is the host here which is probably not the intention, but at least the result is correct
159         to = "file://tmp/bloop";
160         from = "scp://localhost:/tmp/blop";
161         assertEquals( tool.getRelativePath( to, from ), to );
162 
163         to = "http://maven.apache.org/plugins/maven-site-plugin/";
164         from = "http://maven.apache.org";
165         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
166         to = "http://maven.apache.org/plugins/maven-site-plugin/";
167         from = "http://maven.apache.org/";
168         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
169         to = "http://maven.apache.org/plugins/maven-site-plugin";
170         from = "http://maven.apache.org";
171         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
172         to = "http://maven.apache.org/plugins/maven-site-plugin";
173         from = "http://maven.apache.org/";
174         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
175 
176         // Tests between files as described in MIDEA-102
177         to = "C:/dev/voca/gateway/parser/gateway-parser.iml";
178         from = "C:/dev/voca/gateway/";
179         assertEquals( "Child file using Windows drive letter",
180                       "parser" + File.separator + "gateway-parser.iml", tool.getRelativePath( to, from ) );
181         to = "C:/foo/child";
182         from = "C:/foo/master";
183         assertEquals( "Sibling directory using Windows drive letter",
184                       ".." + File.separator + "child", tool.getRelativePath( to, from ) );
185         to = "/myproject/myproject-module1";
186         from = "/myproject/myproject";
187         assertEquals( "Sibling directory with similar name",
188                       ".." + File.separator + "myproject-module1", tool.getRelativePath( to, from ) );
189 
190         // Normalized paths as described in MSITE-284
191         assertEquals( ".." + File.separator + "project-module-1" + File.separator + "src" + File.separator + "site",
192                       tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\src\\site",
193                                             "Z:\\dir\\project\\project-module-1\\..\\project-parent" ) );
194         assertEquals( ".." + File.separator + ".." + File.separator + ".." + File.separator + "project-parent",
195                       tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\..\\project-parent",
196                                             "Z:\\dir\\project\\project-module-1\\src\\site" ) );
197 
198         assertEquals( ".." + File.separator + "foo", tool.getRelativePath( "../../foo/foo", "../../foo/bar" ) );
199     }
200 
201     /**
202      * @throws Exception
203      */
204     public void testGetSiteDescriptorFromBasedir()
205         throws Exception
206     {
207         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
208         assertNotNull( tool );
209 
210         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
211         assertEquals( tool.getSiteDescriptorFromBasedir( null, project.getBasedir(), null ).toString(),
212             project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
213         assertEquals( tool.getSiteDescriptorFromBasedir( null, project.getBasedir(), Locale.ENGLISH ).toString(),
214             project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
215         String siteDir = "src/blabla";
216         assertEquals( tool.getSiteDescriptorFromBasedir( siteDir, project.getBasedir(), null ).toString(),
217             project.getBasedir() + File.separator + "src" + File.separator + "blabla" + File.separator + "site.xml" );
218     }
219 
220     /**
221      * @throws Exception
222      */
223     public void testGetSiteDescriptorFromRepository()
224         throws Exception
225     {
226         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
227         assertNotNull( tool );
228 
229         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
230         project.setGroupId( "org.apache.maven" );
231         project.setArtifactId( "maven-site" );
232         project.setVersion( "1.0" );
233         String result = getLocalRepoDir() + File.separator + "org" + File.separator + "apache" + File.separator
234             + "maven" + File.separator + "maven-site" + File.separator + "1.0" + File.separator
235             + "maven-site-1.0-site.xml";
236 
237         assertEquals( tool.getSiteDescriptorFromRepository( project, getLocalRepo(),
238                                                             project.getRemoteArtifactRepositories(), Locale.ENGLISH )
239             .toString(), result );
240     }
241 
242     /**
243      * @throws Exception
244      */
245     public void testGetDecorationModel()
246         throws Exception
247     {
248         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
249         assertNotNull( tool );
250 
251         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
252         project.setGroupId( "org.apache.maven" );
253         project.setArtifactId( "maven-site" );
254         project.setVersion( "1.0" );
255         String siteDirectory = "src/site";
256         List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
257 
258         project.setBasedir( null ); // get it from repo
259 
260         DecorationModel model =
261             tool.getDecorationModel( project, reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories(),
262                                      siteDirectory, Locale.getDefault() );
263         assertNotNull( model );
264         assertNotNull( model.getBannerLeft() );
265         assertEquals( "Maven", model.getBannerLeft().getName() );
266         assertEquals( "images/apache-maven-project-2.png", model.getBannerLeft().getSrc() );
267         assertEquals( "http://maven.apache.org/", model.getBannerLeft().getHref() );
268         assertNotNull( model.getBannerRight() );
269         assertNull( model.getBannerRight().getName() );
270         assertEquals( "images/maven-logo-2.gif", model.getBannerRight().getSrc() );
271         assertNull( model.getBannerRight().getHref() );
272     }
273 
274     /**
275      * @throws Exception
276      */
277     public void testGetDefaultDecorationModel()
278         throws Exception
279     {
280         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
281         assertNotNull( tool );
282 
283         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "no-site-test" );
284         String siteDirectory = "src/site";
285         List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
286 
287         DecorationModel model =
288             tool.getDecorationModel( project, reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories(),
289                                      siteDirectory, Locale.getDefault() );
290         assertNotNull( model );
291     }
292 
293 }