1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.escaping

File ApplicationTest.java

 

Code metrics

8
24
7
1
162
73
11
0.46
3.43
7
1.57

Classes

Class Line # Actions
ApplicationTest 52 24 0% 11 6
0.8461538684.6%
 

Contributing tests

This file is covered by 771 tests. .

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.test.escaping;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.regex.Pattern;
26   
27    import org.junit.Assume;
28    import org.junit.Test;
29    import org.junit.runner.RunWith;
30    import org.xwiki.test.escaping.framework.AbstractEscapingTest;
31    import org.xwiki.test.escaping.framework.AbstractVelocityEscapingTest;
32    import org.xwiki.test.escaping.framework.EscapingError;
33    import org.xwiki.test.escaping.framework.SingleXWikiExecutor;
34    import org.xwiki.test.escaping.framework.XMLEscapingValidator;
35    import org.xwiki.test.escaping.suite.ArchiveSuite;
36    import org.xwiki.validator.ValidationError;
37   
38   
39    /**
40    * Runs automatically generated escaping tests for all XWiki documents found in XWiki Enterprise XAR file.
41    * <p>
42    * The following configuration properties are supported (set in maven):
43    * <ul>
44    * <li>localRepository: Path to maven repository, where XWiki files can be found</li>
45    * <li>pathToXWikiXar: Used to read all documents</li>
46    * </ul></p>
47    *
48    * @version $Id: 38880ca0a2d6a4b4661c99723b1219eb7ed34621 $
49    * @since 2.5M1
50    */
51    @RunWith(ArchiveSuite.class)
 
52    public class ApplicationTest extends AbstractVelocityEscapingTest
53    {
54    /**
55    * Get the path to the archive from system properties defined in the maven build configuration.
56    *
57    * @return local path to the XAR archive to use
58    */
 
59  1 toggle @ArchiveSuite.ArchivePathGetter
60    public static String getArchivePath()
61    {
62  1 return System.getProperty("localRepository") + "/" + System.getProperty("pathToXWikiXar");
63    }
64   
65    /**
66    * Initialize the test.
67    *
68    * @throws Exception on errors
69    */
 
70  1 toggle @ArchiveSuite.BeforeSuite
71    public static void init() throws Exception
72    {
73  1 SingleXWikiExecutor.getExecutor().start();
74   
75    // for tests using "language" parameter
76  1 AbstractEscapingTest.setMultiLanguageMode(true);
77    }
78   
79    /**
80    * Clean up.
81    *
82    * @throws Exception on errors
83    */
 
84  1 toggle @ArchiveSuite.AfterSuite
85    public static void shutdown() throws Exception
86    {
87    // restore single language mode
88  1 AbstractEscapingTest.setMultiLanguageMode(false);
89   
90  1 SingleXWikiExecutor.getExecutor().stop();
91    }
92   
93    /**
94    * Create new ApplicationTest for all *.xml files in subdirectories.
95    */
 
96  773 toggle public ApplicationTest()
97    {
98  773 super(Pattern.compile(".+/.+\\.xml"));
99    }
100   
101    /**
102    * Test escaping of all found parameters for flamingo.
103    */
 
104  771 toggle @Test
105    public void testParametersInFlamingo()
106    {
107    // NOTE: results in other skins seem to be the same
108  771 testParameterEscaping("flamingo");
109    }
110   
111    /**
112    * Run the tests for all parameters for given skin.
113    *
114    * @param skin skin to use
115    */
 
116  771 toggle private void testParameterEscaping(String skin)
117    {
118    // skip the test if there are no parameters to test
119  771 Assume.assumeTrue(!this.userInput.isEmpty());
120   
121  771 String space = this.name.replaceAll("/.+$", "");
122  771 String page = this.name.replaceAll("^.+/", "").replaceAll("\\..+$", "");
123   
124    // all found parameters
125    // TODO need to also consider parameters from page header templates bound to variables
126  771 List<EscapingError> errors = new ArrayList<EscapingError>();
127  771 for (String parameter : this.userInput) {
128  2320 String url = createUrl(space, page, parameter, XMLEscapingValidator.getTestString(), skin);
129  2320 List<ValidationError> val_errors = getUnderEscapingErrors(url);
130  2320 if (!val_errors.isEmpty()) {
131  0 errors.add(new EscapingError("* Parameter: \"" + parameter + "\"", this.name, url, val_errors));
132    }
133    }
134  771 if (!errors.isEmpty()) {
135  0 throw new EscapingError("Escaping test failed.", errors);
136    }
137    }
138   
139    /**
140    * Convenience method to create the target URL with one parameter.
141    *
142    * @param space space name to use
143    * @param page page name to use
144    * @param parameter parameter name to add, omitted if null or empty string
145    * @param value parameter value, empty string is used if null
146    * @param skin skin name to use
147    * @return the resulting absolute URL
148    * @see #createUrl(String, String, String, java.util.Map)
149    */
 
150  2320 toggle protected String createUrl(String space, String page, String parameter, String value, String skin)
151    {
152  2320 HashMap<String, String> parameters = new HashMap<String, String>();
153  2320 if (skin != null) {
154  2320 parameters.put("skin", skin);
155    }
156  2320 if (parameter != null) {
157  2320 parameters.put(parameter, value);
158    }
159  2320 return createUrl(null, space, page, parameters);
160    }
161    }
162