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.java 1086335 2011-03-28 18:50:05Z ltheussl $
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/plugins/maven-site-plugin/";
150         from = "http://maven.apache.org";
151         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
152         to = "http://maven.apache.org/plugins/maven-site-plugin/";
153         from = "http://maven.apache.org/";
154         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
155         to = "http://maven.apache.org/plugins/maven-site-plugin";
156         from = "http://maven.apache.org";
157         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
158         to = "http://maven.apache.org/plugins/maven-site-plugin";
159         from = "http://maven.apache.org/";
160         assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
161 
162         // Tests between files as described in MIDEA-102
163         to = "C:/dev/voca/gateway/parser/gateway-parser.iml";
164         from = "C:/dev/voca/gateway/";
165         assertEquals( "Child file using Windows drive letter",
166                       "parser" + File.separator + "gateway-parser.iml", tool.getRelativePath( to, from ) );
167         to = "C:/foo/child";
168         from = "C:/foo/master";
169         assertEquals( "Sibling directory using Windows drive letter",
170                       ".." + File.separator + "child", tool.getRelativePath( to, from ) );
171         to = "/myproject/myproject-module1";
172         from = "/myproject/myproject";
173         assertEquals( "Sibling directory with similar name",
174                       ".." + File.separator + "myproject-module1", tool.getRelativePath( to, from ) );
175 
176         // Normalized paths as described in MSITE-284
177         assertEquals( ".." + File.separator + "project-module-1" + File.separator + "src" + File.separator + "site",
178                       tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\src\\site",
179                                             "Z:\\dir\\project\\project-module-1\\..\\project-parent" ) );
180         assertEquals( ".." + File.separator + ".." + File.separator + ".." + File.separator + "project-parent",
181                       tool.getRelativePath( "Z:\\dir\\project\\project-module-1\\..\\project-parent",
182                                             "Z:\\dir\\project\\project-module-1\\src\\site" ) );
183 
184         assertEquals( ".." + File.separator + "foo", tool.getRelativePath( "../../foo/foo", "../../foo/bar" ) );
185     }
186 
187     /**
188      * @throws Exception
189      */
190     public void testGetSiteDescriptorFromBasedir()
191         throws Exception
192     {
193         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
194         assertNotNull( tool );
195 
196         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
197         assertEquals( tool.getSiteDescriptorFromBasedir( null, project.getBasedir(), null ).toString(),
198             project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
199         assertEquals( tool.getSiteDescriptorFromBasedir( null, project.getBasedir(), Locale.ENGLISH ).toString(),
200             project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml" );
201         String siteDir = "src/blabla";
202         assertEquals( tool.getSiteDescriptorFromBasedir( siteDir, project.getBasedir(), null ).toString(),
203             project.getBasedir() + File.separator + "src" + File.separator + "blabla" + File.separator + "site.xml" );
204     }
205 
206     /**
207      * @throws Exception
208      */
209     public void testGetSiteDescriptorFromRepository()
210         throws Exception
211     {
212         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
213         assertNotNull( tool );
214 
215         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
216         project.setGroupId( "org.apache.maven" );
217         project.setArtifactId( "maven-site" );
218         project.setVersion( "1.0" );
219         String result = getLocalRepoDir() + File.separator + "org" + File.separator + "apache" + File.separator
220             + "maven" + File.separator + "maven-site" + File.separator + "1.0" + File.separator
221             + "maven-site-1.0-site.xml";
222 
223         assertEquals( tool.getSiteDescriptorFromRepository( project, getLocalRepo(),
224                                                             project.getRemoteArtifactRepositories(), Locale.ENGLISH )
225             .toString(), result );
226     }
227 
228     /**
229      * @throws Exception
230      */
231     public void testGetDecorationModel()
232         throws Exception
233     {
234         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
235         assertNotNull( tool );
236 
237         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "site-tool-test" );
238         project.setGroupId( "org.apache.maven" );
239         project.setArtifactId( "maven-site" );
240         project.setVersion( "1.0" );
241         String siteDirectory = "src/site";
242         List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
243 
244         project.setBasedir( null ); // get it from repo
245 
246         DecorationModel model =
247             tool.getDecorationModel( project, reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories(),
248                                      siteDirectory, Locale.getDefault(), "ISO-8859-1", "ISO-8859-1" );
249         assertNotNull( model );
250         assertNotNull( model.getBannerLeft() );
251         assertEquals( "Maven", model.getBannerLeft().getName() );
252         assertEquals( "images/apache-maven-project-2.png", model.getBannerLeft().getSrc() );
253         assertEquals( "http://maven.apache.org/", model.getBannerLeft().getHref() );
254         assertNotNull( model.getBannerRight() );
255         assertNull( model.getBannerRight().getName() );
256         assertEquals( "images/maven-logo-2.gif", model.getBannerRight().getSrc() );
257         assertNull( model.getBannerRight().getHref() );
258     }
259 
260     /**
261      * @throws Exception
262      */
263     public void testGetDefaultDecorationModel()
264         throws Exception
265     {
266         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
267         assertNotNull( tool );
268 
269         SiteToolMavenProjectStub project = new SiteToolMavenProjectStub( "no-site-test" );
270         String siteDirectory = "src/site";
271         List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
272 
273         DecorationModel model =
274             tool.getDecorationModel( project, reactorProjects, getLocalRepo(), project.getRemoteArtifactRepositories(),
275                                      siteDirectory, Locale.getDefault(), "UTF-8", "UTF-8" );
276         assertNotNull( model );
277     }
278 
279 }