1. Project Clover database mar. avr. 16 2024 08:19:06 CEST
  2. Package org.apache.maven.doxia.siterenderer

File DefaultSiteRendererTest.java

 

Code metrics

0
38
4
1
175
102
4
0,11
9,5
4
1

Classes

Class Line # Actions
DefaultSiteRendererTest 62 38 0% 4 42
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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.siterenderer;
20   
21    import static org.apache.commons.io.IOUtils.closeQuietly;
22    import static org.apache.commons.io.IOUtils.copy;
23    import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
24    import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
25   
26    import java.io.ByteArrayInputStream;
27    import java.io.File;
28    import java.io.FileInputStream;
29    import java.io.FileOutputStream;
30    import java.io.IOException;
31    import java.io.InputStream;
32    import java.nio.charset.StandardCharsets;
33    import java.nio.file.Path;
34    import java.util.HashMap;
35    import java.util.Locale;
36    import java.util.Map;
37    import java.util.jar.JarOutputStream;
38    import java.util.zip.ZipEntry;
39   
40    import javax.inject.Inject;
41   
42    import org.apache.maven.artifact.Artifact;
43    import org.apache.maven.artifact.DefaultArtifact;
44    import org.apache.maven.artifact.versioning.VersionRange;
45    import org.apache.maven.doxia.site.SiteModel;
46    import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Reader;
47    import org.codehaus.plexus.PlexusContainer;
48    import org.codehaus.plexus.testing.PlexusTest;
49    import org.codehaus.plexus.util.FileUtils;
50    import org.devacfr.testing.jupiter.TestCase;
51    import org.junit.jupiter.api.AfterEach;
52    import org.junit.jupiter.api.BeforeEach;
53    import org.junit.jupiter.api.Disabled;
54    import org.junit.jupiter.api.Test;
55   
56    /**
57    * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
58    * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
59    */
60    @PlexusTest
61    @Disabled("not work when clover report is enabled")
 
62    public class DefaultSiteRendererTest extends TestCase {
63   
64    /**
65    * All output produced by this test will go here.
66    */
67    private static final String OUTPUT = "target/output";
68   
69    /**
70    * The renderer used to produce output.
71    */
72    private Renderer renderer;
73   
74    /**
75    * The locale before executing tests.
76    */
77    private Locale oldLocale;
78   
79    @Inject
80    private PlexusContainer container;
81   
82    private final File skinJar = new File(getBasedir(), "target/test-classes/skin.jar");
83   
84    private final File minimalSkinJar = new File(getBasedir(), "target/test-classes/minimal-skin.jar");
85   
86    /**
87    * @throws java.lang.Exception
88    * if something goes wrong.
89    */
 
90  0 toggle @BeforeEach
91    protected void setUp() throws Exception {
92  0 renderer = container.lookup(Renderer.class);
93   
94  0 InputStream skinIS = getResource("velocity-toolmanager.vm").openStream();
95  0 JarOutputStream jarOS = new JarOutputStream(new FileOutputStream(skinJar));
96  0 try {
97  0 jarOS.putNextEntry(new ZipEntry("META-INF/maven/site.vm"));
98  0 copy(skinIS, jarOS);
99  0 jarOS.closeEntry();
100    } finally {
101  0 closeQuietly(skinIS);
102  0 closeQuietly(jarOS);
103    }
104   
105  0 skinIS = new ByteArrayInputStream(
106    "<main id=\"contentBox\">$bodyContent</main>".getBytes(StandardCharsets.UTF_8));
107  0 jarOS = new JarOutputStream(new FileOutputStream(minimalSkinJar));
108  0 try {
109  0 jarOS.putNextEntry(new ZipEntry("META-INF/maven/site.vm"));
110  0 copy(skinIS, jarOS);
111  0 jarOS.closeEntry();
112    } finally {
113  0 closeQuietly(skinIS);
114  0 closeQuietly(jarOS);
115    }
116   
117  0 oldLocale = Locale.getDefault();
118  0 Locale.setDefault(Locale.ENGLISH);
119    }
120   
121    /**
122    * @throws java.lang.Exception
123    * if something goes wrong.
124    */
 
125  0 toggle @AfterEach
126    protected void tearDown() throws Exception {
127  0 container.release(renderer);
128   
129  0 Locale.setDefault(oldLocale);
130    }
131   
132    /**
133    * @throws Exception
134    * if something goes wrong.
135    */
 
136  0 toggle @Test
137    public void shouldAcceptSnippet() throws Exception {
138    // Safety
139  0 FileUtils.deleteDirectory(getTestFile(OUTPUT));
140   
141    // ----------------------------------------------------------------------
142    // Render the site from src/test/resources/site to OUTPUT
143    // ----------------------------------------------------------------------
144  0 final SiteModel model = new SiteXpp3Reader()
145    .read(new FileInputStream(getTestFile("src/test/resources/site/site.xml")));
146   
147  0 final Path targetSite = getTestFile(OUTPUT).toPath();
148  0 final Path srcSite = getTestFile("src/test/resources/site").toPath();
149  0 final SiteRenderingContext ctxt = getSiteRenderingContext(model, srcSite, false);
150   
151  0 ctxt.setRootDirectory(getTestFile(""));
152  0 renderer.render(renderer.locateDocumentFiles(ctxt, true).values(), ctxt, targetSite.toFile());
153   
154  0 verify(targetSite.resolve("snippet.html"), getPackagePath().resolve("snippet.approved.html"));
155    }
156   
 
157  0 toggle private SiteRenderingContext getSiteRenderingContext(final SiteModel model,
158    final Path siteDir,
159    final boolean validate) throws RendererException, IOException {
160  0 final File skinFile = minimalSkinJar;
161   
162  0 final Map<String, String> attributes = new HashMap<>();
163  0 attributes.put("outputEncoding", "UTF-8");
164   
165  0 final Artifact skin = new DefaultArtifact("org.group", "artifact", VersionRange.createFromVersion("1.1"), null,
166    "jar", "", null);
167  0 skin.setFile(skinFile);
168  0 final SiteRenderingContext siteRenderingContext = renderer
169    .createContextForSkin(skin, attributes, model, "defaultWindowTitle", Locale.ENGLISH);
170  0 siteRenderingContext.addSiteDirectory(siteDir.toFile());
171  0 siteRenderingContext.setValidate(validate);
172   
173  0 return siteRenderingContext;
174    }
175    }