1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.flamingo.test.ui

File HTMLExportTest.java

 

Code metrics

22
62
7
4
213
144
18
0.29
8.86
1.75
2.57

Classes

Class Line # Actions
HTMLExportTest 50 51 0% 12 10
0.861111186.1%
HTMLExportTest.PageValidator 58 0 - 0 0
-1.0 -
HTMLExportTest.TopPageValidator 65 6 0% 3 0
1.0100%
HTMLExportTest.NestedPageValidator 90 5 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.flamingo.test.ui;
21   
22    import java.io.InputStream;
23    import java.net.HttpURLConnection;
24    import java.net.URL;
25    import java.nio.charset.Charset;
26    import java.util.Arrays;
27    import java.util.List;
28    import java.util.zip.ZipEntry;
29    import java.util.zip.ZipInputStream;
30   
31    import org.apache.commons.io.IOUtils;
32    import org.apache.commons.lang3.StringUtils;
33    import org.junit.Rule;
34    import org.junit.Test;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.test.AllLogRule;
37    import org.xwiki.test.LogLevel;
38    import org.xwiki.test.ui.AbstractTest;
39    import org.xwiki.test.ui.SuperAdminAuthenticationRule;
40   
41    import static org.junit.Assert.assertEquals;
42    import static org.junit.Assert.assertTrue;
43    import static org.junit.Assert.fail;
44   
45    /**
46    * Verify that the HTML export features works fine.
47    *
48    * @version $Id: 4cd2955f30a3e63b4fdad72b1517c301b57cd1ea $
49    */
 
50    public class HTMLExportTest extends AbstractTest
51    {
52    @Rule
53    public SuperAdminAuthenticationRule adminAuthenticationRule = new SuperAdminAuthenticationRule(getUtil());
54   
55    @Rule
56    public AllLogRule logRule = new AllLogRule(LogLevel.WARN);
57   
 
58    private interface PageValidator
59    {
60    void validate(ZipInputStream zis, ZipEntry entry) throws Exception;
61   
62    void assertResult();
63    }
64   
 
65    private class TopPageValidator implements PageValidator
66    {
67    private boolean result;
68   
 
69  357 toggle @Override
70    public void validate(ZipInputStream zis, ZipEntry entry) throws Exception
71    {
72  357 if (entry.getName().equals("pages/xwiki/TopPage/WebHome.html")) {
73  2 String content = IOUtils.toString(zis, Charset.defaultCharset());
74   
75    // Verify that the content was rendered properly
76  2 assertTrue("Title should have contained 'Top content'", content.contains("Top content"));
77  2 assertTrue("Content should have contained 'Top title: Creator'",
78    content.contains("Top title: Creator"));
79  2 this.result = true;
80    }
81    }
82   
 
83  2 toggle @Override
84    public void assertResult()
85    {
86  2 assertTrue("Failed to find the pages/xwiki/TopPage/WebHome.html entry", this.result);
87    }
88    }
89   
 
90    private class NestedPageValidator implements PageValidator
91    {
92    private boolean result;
93   
 
94  180 toggle @Override
95    public void validate(ZipInputStream zis, ZipEntry entry) throws Exception
96    {
97  180 if (entry.getName().equals("pages/xwiki/TopPage/NestedPage/WebHome.html")) {
98  1 String content = IOUtils.toString(zis, Charset.defaultCharset());
99   
100    // Verify that the link to a locally exported page is correct
101  1 assertTrue("Content should have contained a local link to the Top page",
102    content.contains("<a href=\"../../../../pages/xwiki/TopPage/WebHome.html\">top</a>"));
103  1 this.result = true;
104    }
105    }
106   
 
107  1 toggle @Override
108    public void assertResult()
109    {
110  1 assertTrue("Failed to find the pages/xwiki/TopPage/NestedPage/WebHome.html entry", this.result);
111    }
112    }
113   
 
114  1 toggle @Test
115    public void exportHTML() throws Exception
116    {
117    // Step 1: Create 2 pages that we'll then export
118   
119  1 EntityReference topReference = getUtil().resolveDocumentReference("TopPage.WebHome");
120  1 getUtil().deletePage(topReference);
121  1 EntityReference nestedReference = getUtil().resolveDocumentReference("TopPage.NestedPage.WebHome");
122  1 getUtil().deletePage(nestedReference);
123   
124    // Note: Verify that Velocity is correctly evaluated
125  1 getUtil().createPage(topReference, "Top content", "Top title: $services.localization.render('creator')");
126    // Note: we define a link to the top page to verify that the export will resolve the links locally when the
127    // page linked is part of the export.
128  1 getUtil().createPage(nestedReference, "[[top>>TopPage.WebHome]]", "Nested Page");
129   
130    // Step 2: Call the export URL to get the ZIP and to assert its content, when no "pages" query string param is
131    // used (only the TopPage will be exported)
132  1 assertHTMLExportURL("http://localhost:8080/xwiki/bin/export/TopPage/WebHome?format=html",
133    Arrays.asList(new TopPageValidator()));
134   
135    // Step 3: Call the export URL to get the ZIP and to assert its content, when a "pages" query string param is
136    // used with some regex
137  1 assertHTMLExportURL("http://localhost:8080/xwiki/bin/export/UnexistingSpace/UnexistingPage?format=html"
138    + "&pages=TopPage.%25", Arrays.asList(new TopPageValidator(), new NestedPageValidator()));
139   
140    // Verify that there was no warning or more severe logs output to the console.
141  1 StringBuilder builder = new StringBuilder("Should not have got the following logs [\n");
142  1 for (int i = 0; i < this.logRule.size(); i++) {
143  0 builder.append(" - ["). append(this.logRule.getMessage(i)).append("]\n");
144    }
145  1 builder.append("]");
146  1 assertEquals(builder.toString(), 0, this.logRule.size());
147    }
148   
 
149  2 toggle private void assertHTMLExportURL(String htmlExportURL, List<PageValidator> validators) throws Exception
150    {
151  2 URL url = new URL(htmlExportURL);
152   
153  2 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
154   
155  2 InputStream is = connection.getInputStream();
156  2 ZipInputStream zis = new ZipInputStream(is);
157   
158  2 boolean foundResourcesDirectory = false;
159  2 boolean foundSkinsDirectory = false;
160  2 boolean foundSkinCSS = false;
161  2 boolean foundWebjars = false;
162   
163    // We must read the full stream as otherwise if we close it before we've fully read it
164    // then the server side will get a broken pipe since it's still trying to send data on it.
165  ? for (ZipEntry entry; (entry = zis.getNextEntry()) != null; zis.closeEntry()) {
166  357 for (PageValidator validator : validators) {
167  537 validator.validate(zis, entry);
168    }
169  357 if (entry.getName().endsWith(".vm")) {
170  0 fail("There shouldn't be any *.vm files in the generated zip!");
171  357 } else if (entry.getName().endsWith(".less")) {
172  0 fail("There shouldn't be any *.less files in the generated zip!");
173  357 } else if (entry.getName().equals("xwiki.properties")) {
174  0 fail("There shouldn't be any xwiki.properties file in the generated zip!");
175  357 } else if (entry.getName().startsWith("resources/")) {
176  128 foundResourcesDirectory = true;
177  128 IOUtils.readLines(zis, Charset.defaultCharset());
178  229 } else if (entry.getName().startsWith("skins/")) {
179  172 foundSkinsDirectory = true;
180    // Verify that the skin is correctly going to be applied by verifying the flamingo/style.css file is
181    // found and is correctly referenced. This fixes https://jira.xwiki.org/browse/XWIKI-9145
182  172 if (entry.getName().equals("skins/flamingo/style.css")) {
183  2 assertSkinIsActive(IOUtils.readLines(zis, Charset.defaultCharset()));
184  2 foundSkinCSS = true;
185    } else {
186  170 IOUtils.readLines(zis, Charset.defaultCharset());
187    }
188  57 } else if (entry.getName().startsWith("webjars")) {
189    // We verify here that webjars URLs have been properly exported
190  46 foundWebjars = true;
191  46 IOUtils.readLines(zis, Charset.defaultCharset());
192    } else {
193  11 IOUtils.readLines(zis, Charset.defaultCharset());
194    }
195    }
196   
197  2 for (PageValidator validator : validators) {
198  3 validator.assertResult();
199    }
200  2 assertTrue("Failed to find the resources/ directory entry", foundResourcesDirectory);
201  2 assertTrue("Failed to find the skins/ directory entry", foundSkinsDirectory);
202  2 assertTrue("Failed to find the link to colibri.css in style.css", foundSkinCSS);
203  2 assertTrue("Failed to find webjar resources in the HTML export", foundWebjars);
204   
205  2 zis.close();
206    }
207   
 
208  2 toggle private void assertSkinIsActive(List<String> content) throws Exception
209    {
210  2 assertTrue("style.css is not the one output by the flamingo skin", StringUtils.join(content.toArray())
211    .contains("skin-flamingo"));
212    }
213    }