1 package org.apache.maven.doxia.tools;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43 public class SiteToolTest
44 extends PlexusTestCase
45 {
46
47
48
49
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
70
71
72
73 protected File getLocalRepoDir()
74 throws Exception
75 {
76 return new File( getLocalRepo().getBasedir() );
77 }
78
79
80
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
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
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
152
153
154 to = "file:///tmp/bloop";
155 from = "scp://localhost:/tmp/blop";
156
157
158
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
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
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
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
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
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 );
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
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 }