| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.maven.doxia.tools; |
| 20 |
|
| 21 |
import javax.inject.Inject; |
| 22 |
|
| 23 |
import java.io.File; |
| 24 |
import java.io.StringReader; |
| 25 |
import java.io.Writer; |
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.Collections; |
| 29 |
import java.util.HashMap; |
| 30 |
import java.util.Iterator; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Locale; |
| 33 |
|
| 34 |
import org.apache.maven.doxia.site.LinkItem; |
| 35 |
import org.apache.maven.doxia.site.SiteModel; |
| 36 |
import org.apache.maven.doxia.site.Skin; |
| 37 |
import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Reader; |
| 38 |
import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Writer; |
| 39 |
import org.apache.maven.doxia.tools.stubs.MavenProjectStub; |
| 40 |
import org.apache.maven.doxia.tools.stubs.SiteToolMavenProjectStub; |
| 41 |
import org.apache.maven.project.MavenProject; |
| 42 |
import org.apache.maven.repository.internal.MavenRepositorySystemUtils; |
| 43 |
import org.codehaus.plexus.testing.PlexusTest; |
| 44 |
import org.codehaus.plexus.util.FileUtils; |
| 45 |
import org.codehaus.plexus.util.IOUtil; |
| 46 |
import org.codehaus.plexus.util.WriterFactory; |
| 47 |
import org.eclipse.aether.DefaultRepositorySystemSession; |
| 48 |
import org.eclipse.aether.RepositorySystemSession; |
| 49 |
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; |
| 50 |
import org.eclipse.aether.repository.LocalRepository; |
| 51 |
import org.junit.jupiter.api.Test; |
| 52 |
|
| 53 |
import static org.codehaus.plexus.testing.PlexusExtension.getTestFile; |
| 54 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 55 |
import static org.junit.jupiter.api.Assertions.assertFalse; |
| 56 |
import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 57 |
import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 58 |
import static org.junit.jupiter.api.Assertions.assertNull; |
| 59 |
import static org.junit.jupiter.api.Assertions.assertThrows; |
| 60 |
import static org.junit.jupiter.api.Assertions.assertTrue; |
| 61 |
|
| 62 |
/** |
| 63 |
* @author <a href="mailto:[email protected]">Vincent Siveton</a> |
| 64 |
*/ |
| 65 |
@SuppressWarnings("javadoc") |
| 66 |
@PlexusTest |
| 67 |
public class SiteToolTest { |
| 68 |
|
| 69 |
@Inject |
| 70 |
private DefaultSiteTool tool; |
| 71 |
|
| 72 |
/** |
| 73 |
* @return the local repo directory. |
| 74 |
* |
| 75 |
* @throws Exception |
| 76 |
*/ |
| 77 |
protected File getLocalRepoDir() throws Exception { |
| 78 |
return getTestFile("target/local-repo"); |
| 79 |
} |
| 80 |
|
| 81 |
protected RepositorySystemSession newRepoSession() throws Exception { |
| 82 |
DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); |
| 83 |
repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() |
| 84 |
.newInstance(repoSession, new LocalRepository(getLocalRepoDir()))); |
| 85 |
return repoSession; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @throws Exception |
| 90 |
*/ |
| 91 |
@Test |
| 92 |
public void testGetSkinArtifactFromRepository() throws Exception { |
| 93 |
assertNotNull(tool); |
| 94 |
|
| 95 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("site-tool-test"); |
| 96 |
Skin skin = new Skin(); |
| 97 |
skin.setGroupId("org.apache.maven.skins"); |
| 98 |
skin.setArtifactId("maven-fluido-skin"); |
| 99 |
assertNotNull( |
| 100 |
tool.getSkinArtifactFromRepository(newRepoSession(), project.getRemoteProjectRepositories(), skin)); |
| 101 |
} |
| 102 |
|
| 103 |
private void checkGetRelativePathDirectory(SiteTool tool, String relative, String to, String from) { |
| 104 |
assertEquals(relative, tool.getRelativePath(to, from)); |
| 105 |
assertEquals(relative, tool.getRelativePath(to + '/', from)); |
| 106 |
assertEquals(relative, tool.getRelativePath(to, from + '/')); |
| 107 |
assertEquals(relative, tool.getRelativePath(to + '/', from + '/')); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @throws Exception |
| 112 |
*/ |
| 113 |
@Test |
| 114 |
@SuppressWarnings({"deprecation"}) |
| 115 |
public void testGetRelativePath() throws Exception { |
| 116 |
assertNotNull(tool); |
| 117 |
|
| 118 |
checkGetRelativePathDirectory(tool, "", "http://maven.apache.org", "http://maven.apache.org"); |
| 119 |
|
| 120 |
checkGetRelativePathDirectory( |
| 121 |
tool, |
| 122 |
".." + File.separator + "..", |
| 123 |
"http://maven.apache.org", |
| 124 |
"http://maven.apache.org/plugins/maven-site-plugin"); |
| 125 |
|
| 126 |
checkGetRelativePathDirectory( |
| 127 |
tool, |
| 128 |
"plugins" + File.separator + "maven-site-plugin", |
| 129 |
"http://maven.apache.org/plugins/maven-site-plugin", |
| 130 |
"http://maven.apache.org"); |
| 131 |
|
| 132 |
checkGetRelativePathDirectory(tool, "", "dav:https://maven.apache.org", "dav:https://maven.apache.org"); |
| 133 |
|
| 134 |
checkGetRelativePathDirectory( |
| 135 |
tool, |
| 136 |
"plugins" + File.separator + "maven-site-plugin", |
| 137 |
"dav:http://maven.apache.org/plugins/maven-site-plugin", |
| 138 |
"dav:http://maven.apache.org"); |
| 139 |
|
| 140 |
checkGetRelativePathDirectory(tool, "", "scm:svn:https://maven.apache.org", "scm:svn:https://maven.apache.org"); |
| 141 |
|
| 142 |
checkGetRelativePathDirectory( |
| 143 |
tool, |
| 144 |
"plugins" + File.separator + "maven-site-plugin", |
| 145 |
"scm:svn:https://maven.apache.org/plugins/maven-site-plugin", |
| 146 |
"scm:svn:https://maven.apache.org"); |
| 147 |
|
| 148 |
String to = "http://maven.apache.org/downloads.html"; |
| 149 |
String from = "http://maven.apache.org/index.html"; |
| 150 |
|
| 151 |
// MSITE-600, MSHARED-203 |
| 152 |
to = "file:///tmp/bloop"; |
| 153 |
from = "scp://localhost:/tmp/blop"; |
| 154 |
assertEquals(tool.getRelativePath(to, from), to); |
| 155 |
|
| 156 |
// note: 'tmp' is the host here which is probably not the intention, but at least the result is correct |
| 157 |
to = "file://tmp/bloop"; |
| 158 |
from = "scp://localhost:/tmp/blop"; |
| 159 |
assertEquals(to, tool.getRelativePath(to, from)); |
| 160 |
|
| 161 |
// Tests between files as described in MIDEA-102 |
| 162 |
to = "C:/dev/voca/gateway/parser/gateway-parser.iml"; |
| 163 |
from = "C:/dev/voca/gateway/"; |
| 164 |
assertEquals( |
| 165 |
"parser" + File.separator + "gateway-parser.iml", |
| 166 |
tool.getRelativePath(to, from), |
| 167 |
"Child file using Windows drive letter"); |
| 168 |
to = "C:/foo/child"; |
| 169 |
from = "C:/foo/master"; |
| 170 |
assertEquals( |
| 171 |
".." + File.separator + "child", |
| 172 |
tool.getRelativePath(to, from), |
| 173 |
"Sibling directory using Windows drive letter"); |
| 174 |
to = "/myproject/myproject-module1"; |
| 175 |
from = "/myproject/myproject"; |
| 176 |
assertEquals( |
| 177 |
".." + File.separator + "myproject-module1", |
| 178 |
tool.getRelativePath(to, from), |
| 179 |
"Sibling directory with similar name"); |
| 180 |
|
| 181 |
// Normalized paths as described in MSITE-284 |
| 182 |
assertEquals( |
| 183 |
".." + File.separator + "project-module-1" + File.separator + "src" + File.separator + "site", |
| 184 |
tool.getRelativePath( |
| 185 |
"Z:\\dir\\project\\project-module-1\\src\\site", |
| 186 |
"Z:\\dir\\project\\project-module-1\\..\\project-parent")); |
| 187 |
assertEquals( |
| 188 |
".." + File.separator + ".." + File.separator + ".." + File.separator + "project-parent", |
| 189 |
tool.getRelativePath( |
| 190 |
"Z:\\dir\\project\\project-module-1\\..\\project-parent", |
| 191 |
"Z:\\dir\\project\\project-module-1\\src\\site")); |
| 192 |
|
| 193 |
assertEquals(".." + File.separator + "foo", tool.getRelativePath("../../foo/foo", "../../foo/bar")); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @throws Exception |
| 198 |
*/ |
| 199 |
@Test |
| 200 |
public void testGetSiteDescriptorFromBasedir() throws Exception { |
| 201 |
assertNotNull(tool); |
| 202 |
|
| 203 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("site-tool-test"); |
| 204 |
assertEquals( |
| 205 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), SiteTool.DEFAULT_LOCALE) |
| 206 |
.toString(), |
| 207 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 208 |
assertEquals( |
| 209 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.ENGLISH) |
| 210 |
.toString(), |
| 211 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 212 |
String siteDir = "src/blabla"; |
| 213 |
assertEquals( |
| 214 |
tool.getSiteDescriptor(new File(project.getBasedir(), siteDir), SiteTool.DEFAULT_LOCALE) |
| 215 |
.toString(), |
| 216 |
project.getBasedir() + File.separator + "src" + File.separator + "blabla" + File.separator |
| 217 |
+ "site.xml"); |
| 218 |
|
| 219 |
project = new SiteToolMavenProjectStub("site-tool-locales-test/full"); |
| 220 |
final Locale BAVARIAN = new Locale("de", "DE", "BY"); |
| 221 |
assertEquals( |
| 222 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), SiteTool.DEFAULT_LOCALE) |
| 223 |
.toString(), |
| 224 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 225 |
assertEquals( |
| 226 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), BAVARIAN) |
| 227 |
.toString(), |
| 228 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator |
| 229 |
+ "site_de_DE_BY.xml"); |
| 230 |
assertEquals( |
| 231 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.GERMANY) |
| 232 |
.toString(), |
| 233 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 234 |
assertEquals( |
| 235 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.ENGLISH) |
| 236 |
.toString(), |
| 237 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 238 |
assertEquals( |
| 239 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.GERMAN) |
| 240 |
.toString(), |
| 241 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 242 |
|
| 243 |
project = new SiteToolMavenProjectStub("site-tool-locales-test/language_country"); |
| 244 |
assertEquals( |
| 245 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), SiteTool.DEFAULT_LOCALE) |
| 246 |
.toString(), |
| 247 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 248 |
assertEquals( |
| 249 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), BAVARIAN) |
| 250 |
.toString(), |
| 251 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator |
| 252 |
+ "site_de_DE.xml"); |
| 253 |
assertEquals( |
| 254 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.GERMANY) |
| 255 |
.toString(), |
| 256 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator |
| 257 |
+ "site_de_DE.xml"); |
| 258 |
assertEquals( |
| 259 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.ENGLISH) |
| 260 |
.toString(), |
| 261 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 262 |
assertEquals( |
| 263 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.GERMAN) |
| 264 |
.toString(), |
| 265 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 266 |
|
| 267 |
project = new SiteToolMavenProjectStub("site-tool-locales-test/language"); |
| 268 |
assertEquals( |
| 269 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), SiteTool.DEFAULT_LOCALE) |
| 270 |
.toString(), |
| 271 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 272 |
assertEquals( |
| 273 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), BAVARIAN) |
| 274 |
.toString(), |
| 275 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator |
| 276 |
+ "site_de.xml"); |
| 277 |
assertEquals( |
| 278 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.GERMANY) |
| 279 |
.toString(), |
| 280 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator |
| 281 |
+ "site_de.xml"); |
| 282 |
assertEquals( |
| 283 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.ENGLISH) |
| 284 |
.toString(), |
| 285 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator + "site.xml"); |
| 286 |
assertEquals( |
| 287 |
tool.getSiteDescriptor(new File(project.getBasedir(), "src/site"), Locale.GERMAN) |
| 288 |
.toString(), |
| 289 |
project.getBasedir() + File.separator + "src" + File.separator + "site" + File.separator |
| 290 |
+ "site_de.xml"); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* @throws Exception |
| 295 |
*/ |
| 296 |
@Test |
| 297 |
public void testGetSiteDescriptorFromRepository() throws Exception { |
| 298 |
assertNotNull(tool); |
| 299 |
|
| 300 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("site-tool-test"); |
| 301 |
project.setGroupId("org.apache.maven"); |
| 302 |
project.setArtifactId("maven-site"); |
| 303 |
project.setVersion("1.0"); |
| 304 |
String result = getLocalRepoDir() + File.separator + "org" + File.separator + "apache" + File.separator |
| 305 |
+ "maven" + File.separator + "maven-site" + File.separator + "1.0" + File.separator |
| 306 |
+ "maven-site-1.0-site.xml"; |
| 307 |
|
| 308 |
assertEquals( |
| 309 |
tool.getSiteDescriptorFromRepository( |
| 310 |
project, |
| 311 |
newRepoSession(), |
| 312 |
project.getRemoteProjectRepositories(), |
| 313 |
SiteTool.DEFAULT_LOCALE) |
| 314 |
.toString(), |
| 315 |
result); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* @throws Exception |
| 320 |
*/ |
| 321 |
@Test |
| 322 |
public void testGetSiteModel() throws Exception { |
| 323 |
assertNotNull(tool); |
| 324 |
|
| 325 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("site-tool-test"); |
| 326 |
List<MavenProject> reactorProjects = new ArrayList<>(); |
| 327 |
|
| 328 |
// model from current local build |
| 329 |
SiteModel model = tool.getSiteModel( |
| 330 |
new File(project.getBasedir(), "src/site"), |
| 331 |
SiteTool.DEFAULT_LOCALE, |
| 332 |
project, |
| 333 |
reactorProjects, |
| 334 |
newRepoSession(), |
| 335 |
project.getRemoteProjectRepositories()); |
| 336 |
assertNotNull(model); |
| 337 |
assertNotNull(model.getBannerLeft()); |
| 338 |
assertEquals("Maven Site", model.getBannerLeft().getName()); |
| 339 |
assertEquals( |
| 340 |
"http://maven.apache.org/images/apache-maven-project.png", |
| 341 |
model.getBannerLeft().getImage().getSrc()); |
| 342 |
assertEquals("http://maven.apache.org/", model.getBannerLeft().getHref()); |
| 343 |
assertNotNull(model.getBannerRight()); |
| 344 |
assertNull(model.getBannerRight().getName()); |
| 345 |
assertEquals( |
| 346 |
"http://maven.apache.org/images/maven-small.gif", |
| 347 |
model.getBannerRight().getImage().getSrc()); |
| 348 |
assertNull(model.getBannerRight().getHref()); |
| 349 |
|
| 350 |
// model from repo: https://repo1.maven.org/maven2/org/apache/maven/maven/3.8.6/maven-3.8.6-site.xml |
| 351 |
project.setBasedir(null); |
| 352 |
project.setGroupId("org.apache.maven"); |
| 353 |
project.setArtifactId("maven"); |
| 354 |
project.setVersion("3.8.6"); |
| 355 |
SiteModel modelFromRepo = tool.getSiteModel( |
| 356 |
null, |
| 357 |
SiteTool.DEFAULT_LOCALE, |
| 358 |
project, |
| 359 |
reactorProjects, |
| 360 |
newRepoSession(), |
| 361 |
project.getRemoteProjectRepositories()); |
| 362 |
assertNotNull(modelFromRepo); |
| 363 |
assertNotNull(modelFromRepo.getBannerLeft()); |
| 364 |
assertEquals("dummy", modelFromRepo.getBannerLeft().getName()); |
| 365 |
assertEquals( |
| 366 |
"https://maven.apache.org/images/apache-maven-project.png", |
| 367 |
modelFromRepo.getBannerLeft().getImage().getSrc()); |
| 368 |
assertEquals("https://maven.apache.org/", modelFromRepo.getBannerLeft().getHref()); |
| 369 |
assertNull(modelFromRepo.getBannerRight()); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* @throws Exception |
| 374 |
*/ |
| 375 |
@Test |
| 376 |
public void testGetDefaultSiteModel() throws Exception { |
| 377 |
assertNotNull(tool); |
| 378 |
|
| 379 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("no-site-test"); |
| 380 |
String siteDirectory = "src/site"; |
| 381 |
List<MavenProject> reactorProjects = new ArrayList<>(); |
| 382 |
|
| 383 |
SiteModel model = tool.getSiteModel( |
| 384 |
new File(project.getBasedir(), siteDirectory), |
| 385 |
SiteTool.DEFAULT_LOCALE, |
| 386 |
project, |
| 387 |
reactorProjects, |
| 388 |
newRepoSession(), |
| 389 |
project.getRemoteProjectRepositories()); |
| 390 |
assertNotNull(model); |
| 391 |
} |
| 392 |
|
| 393 |
@Test |
| 394 |
public void testGetAvailableLocales() throws Exception { |
| 395 |
assertEquals(Collections.singletonList(SiteTool.DEFAULT_LOCALE), tool.getSiteLocales("default")); |
| 396 |
|
| 397 |
assertEquals( |
| 398 |
Arrays.asList(SiteTool.DEFAULT_LOCALE, Locale.FRENCH, Locale.ITALIAN), |
| 399 |
tool.getSiteLocales("default,fr,it")); |
| 400 |
|
| 401 |
// by default, only DEFAULT_LOCALE |
| 402 |
assertEquals(Collections.singletonList(SiteTool.DEFAULT_LOCALE), tool.getSiteLocales("")); |
| 403 |
} |
| 404 |
|
| 405 |
@Test |
| 406 |
public void testGetInterpolatedSiteDescriptorContent() throws Exception { |
| 407 |
assertNotNull(tool); |
| 408 |
|
| 409 |
File pomXmlFile = getTestFile("src/test/resources/unit/interpolated-site/pom.xml"); |
| 410 |
assertNotNull(pomXmlFile); |
| 411 |
assertTrue(pomXmlFile.exists()); |
| 412 |
|
| 413 |
File descriptorFile = getTestFile("src/test/resources/unit/interpolated-site/src/site/site.xml"); |
| 414 |
assertNotNull(descriptorFile); |
| 415 |
assertTrue(descriptorFile.exists()); |
| 416 |
|
| 417 |
String siteDescriptorContent = FileUtils.fileRead(descriptorFile); |
| 418 |
assertNotNull(siteDescriptorContent); |
| 419 |
assertTrue(siteDescriptorContent.contains("${project.name}")); |
| 420 |
assertFalse(siteDescriptorContent.contains( |
| 421 |
"Interpolatesite "quoted" & 'quoted' <sdf>")); |
| 422 |
|
| 423 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("interpolated-site"); |
| 424 |
List<MavenProject> reactorProjects = Collections.<MavenProject>singletonList(project); |
| 425 |
|
| 426 |
siteDescriptorContent = |
| 427 |
tool.getInterpolatedSiteDescriptorContent(new HashMap<>(), project, siteDescriptorContent); |
| 428 |
assertNotNull(siteDescriptorContent); |
| 429 |
assertFalse(siteDescriptorContent.contains("${project.name}")); |
| 430 |
assertTrue(siteDescriptorContent.contains( |
| 431 |
"Interpolatesite "quoted" & 'quoted' <sdf>")); |
| 432 |
|
| 433 |
SiteModel model = tool.getSiteModel( |
| 434 |
new File(project.getBasedir(), "src/site"), |
| 435 |
SiteTool.DEFAULT_LOCALE, |
| 436 |
project, |
| 437 |
reactorProjects, |
| 438 |
newRepoSession(), |
| 439 |
project.getRemoteProjectRepositories()); |
| 440 |
assertNotNull(model); |
| 441 |
|
| 442 |
assertEquals( |
| 443 |
"Test " + project.getName(), |
| 444 |
model.getBody().getMenus().get(0).getItems().get(1).getName()); |
| 445 |
} |
| 446 |
|
| 447 |
// MSHARED-217 -> DOXIATOOLS-34 -> DOXIASITETOOLS-118 |
| 448 |
@Test |
| 449 |
public void testSiteModelInheritanceAndInterpolation() throws Exception { |
| 450 |
assertNotNull(tool); |
| 451 |
|
| 452 |
SiteToolMavenProjectStub parentProject = new SiteToolMavenProjectStub("interpolation-parent-test"); |
| 453 |
parentProject.setDistgributionManagementSiteUrl("dav+https://davs.codehaus.org/site"); |
| 454 |
|
| 455 |
SiteToolMavenProjectStub childProject = new SiteToolMavenProjectStub("interpolation-child-test"); |
| 456 |
childProject.setParent(parentProject); |
| 457 |
childProject.setDistgributionManagementSiteUrl("dav+https://davs.codehaus.org/site/child"); |
| 458 |
|
| 459 |
List<MavenProject> reactorProjects = Collections.<MavenProject>singletonList(parentProject); |
| 460 |
|
| 461 |
SiteModel model = tool.getSiteModel( |
| 462 |
new File(childProject.getBasedir(), "src/site"), |
| 463 |
SiteTool.DEFAULT_LOCALE, |
| 464 |
childProject, |
| 465 |
reactorProjects, |
| 466 |
newRepoSession(), |
| 467 |
childProject.getRemoteProjectRepositories()); |
| 468 |
assertNotNull(model); |
| 469 |
|
| 470 |
writeModel(model, "unit/interpolation-child-test/effective-site.xml"); |
| 471 |
|
| 472 |
assertEquals("MSHARED-217 Child", model.getName()); |
| 473 |
// late (classical) interpolation |
| 474 |
assertEquals( |
| 475 |
"project.artifactId = mshared-217-child", model.getBannerLeft().getName()); |
| 476 |
// early interpolation: DOXIASITETOOLS-158 |
| 477 |
assertEquals( |
| 478 |
"this.artifactId = mshared-217-parent", model.getBannerRight().getName()); |
| 479 |
// href rebase |
| 480 |
assertEquals( |
| 481 |
"../../index.html", |
| 482 |
model.getBody().getBreadcrumbs().iterator().next().getHref()); |
| 483 |
Iterator<LinkItem> links = model.getBody().getLinks().iterator(); |
| 484 |
// late interpolation of pom content |
| 485 |
assertEquals("project.name = MSHARED-217 Child", links.next().getName()); |
| 486 |
assertEquals("name = name property", links.next().getName()); |
| 487 |
// early interpolation: DOXIASITETOOLS-158 |
| 488 |
assertEquals("this.name = MSHARED-217 Parent", links.next().getName()); |
| 489 |
|
| 490 |
// late interpolation of project properties |
| 491 |
assertEquals("my_property = from child pom.xml", links.next().getName()); |
| 492 |
// early interpolation of project properties: DOXIASITETOOLS-158 |
| 493 |
assertEquals("this.my_property = from parent pom.xml", links.next().getName()); |
| 494 |
|
| 495 |
// Env Var interpolation |
| 496 |
String envPath = links.next().getName(); |
| 497 |
assertTrue(envPath.startsWith("env.PATH = ")); |
| 498 |
assertFalse(envPath.contains("${")); |
| 499 |
assertNotSame("env.PATH = PATH property from pom", envPath); |
| 500 |
|
| 501 |
// property overrides env |
| 502 |
assertEquals("PATH = PATH property from pom", links.next().getName()); |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* @throws Exception |
| 507 |
*/ |
| 508 |
@Test |
| 509 |
public void testConvertOldToNewSiteModel() throws Exception { |
| 510 |
assertNotNull(tool); |
| 511 |
|
| 512 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("old-to-new-site-model-conversion-test"); |
| 513 |
List<MavenProject> reactorProjects = new ArrayList<MavenProject>(); |
| 514 |
|
| 515 |
// model from current local build |
| 516 |
SiteModel model = tool.getSiteModel( |
| 517 |
new File(project.getBasedir(), "src/site"), |
| 518 |
SiteTool.DEFAULT_LOCALE, |
| 519 |
project, |
| 520 |
reactorProjects, |
| 521 |
newRepoSession(), |
| 522 |
project.getRemoteProjectRepositories()); |
| 523 |
assertNotNull(model); |
| 524 |
|
| 525 |
File descriptorFile = |
| 526 |
getTestFile("src/test/resources/unit/old-to-new-site-model-conversion-test/src/site/new-site.xml"); |
| 527 |
assertNotNull(descriptorFile); |
| 528 |
assertTrue(descriptorFile.exists()); |
| 529 |
|
| 530 |
String siteDescriptorContent = FileUtils.fileRead(descriptorFile); |
| 531 |
SiteModel newModel = new SiteXpp3Reader().read(new StringReader(siteDescriptorContent)); |
| 532 |
assertNotNull(newModel); |
| 533 |
assertEquals(newModel, model); |
| 534 |
} |
| 535 |
|
| 536 |
@Test |
| 537 |
public void testRequireParent() throws SiteToolException, Exception { |
| 538 |
assertNotNull(tool); |
| 539 |
|
| 540 |
SiteToolMavenProjectStub project = new SiteToolMavenProjectStub("require-parent-test"); |
| 541 |
MavenProjectStub parentProject = new MavenProjectStub() { |
| 542 |
@Override |
| 543 |
public File getBasedir() { |
| 544 |
return null; // this should be a non reactor/local project |
| 545 |
} |
| 546 |
}; |
| 547 |
parentProject.setGroupId("org.apache.maven.shared.its"); |
| 548 |
parentProject.setArtifactId("mshared-217-parent"); |
| 549 |
parentProject.setVersion("1.0-SNAPSHOT"); |
| 550 |
project.setParent(parentProject); |
| 551 |
List<MavenProject> reactorProjects = new ArrayList<MavenProject>(); |
| 552 |
|
| 553 |
RepositorySystemSession repoSession = newRepoSession(); |
| 554 |
// coordinates for site descriptor: <groupId>:<artifactId>:xml:site:<version> |
| 555 |
new SiteToolMavenProjectStub("require-parent-test"); |
| 556 |
org.eclipse.aether.artifact.Artifact parentArtifact = new org.eclipse.aether.artifact.DefaultArtifact( |
| 557 |
"org.apache.maven.shared.its:mshared-217-parent:xml:site:1.0-SNAPSHOT"); |
| 558 |
File parentArtifactInRepoFile = new File( |
| 559 |
repoSession.getLocalRepository().getBasedir(), |
| 560 |
repoSession.getLocalRepositoryManager().getPathForLocalArtifact(parentArtifact)); |
| 561 |
|
| 562 |
// model from current local build |
| 563 |
assertThrows( |
| 564 |
SiteToolException.class, |
| 565 |
() -> tool.getSiteModel( |
| 566 |
new File(project.getBasedir(), "src/site"), |
| 567 |
SiteTool.DEFAULT_LOCALE, |
| 568 |
project, |
| 569 |
reactorProjects, |
| 570 |
repoSession, |
| 571 |
project.getRemoteProjectRepositories())); |
| 572 |
|
| 573 |
// now copy parent site descriptor to repo |
| 574 |
FileUtils.copyFile( |
| 575 |
getTestFile("src/test/resources/unit/require-parent-test/parent-site.xml"), parentArtifactInRepoFile); |
| 576 |
try { |
| 577 |
tool.getSiteModel( |
| 578 |
new File(project.getBasedir(), "src/site"), |
| 579 |
SiteTool.DEFAULT_LOCALE, |
| 580 |
project, |
| 581 |
reactorProjects, |
| 582 |
repoSession, |
| 583 |
project.getRemoteProjectRepositories()); |
| 584 |
} finally { |
| 585 |
parentArtifactInRepoFile.delete(); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
private void writeModel(SiteModel model, String to) throws Exception { |
| 590 |
Writer writer = WriterFactory.newXmlWriter(getTestFile("target/test-classes/" + to)); |
| 591 |
try { |
| 592 |
new SiteXpp3Writer().write(writer, model); |
| 593 |
} finally { |
| 594 |
IOUtil.close(writer); |
| 595 |
} |
| 596 |
} |
| 597 |
} |