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

File RenderingTest.java

 

Code metrics

22
65
6
1
221
141
17
0.26
10.83
6
2.83

Classes

Class Line # Actions
RenderingTest 53 65 0% 17 11
0.881720488.2%
 

Contributing tests

This file is covered by 887 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.rendering.test.integration;
21   
22    import java.io.StringReader;
23    import java.util.HashMap;
24    import java.util.Map;
25    import java.util.regex.Matcher;
26    import java.util.regex.Pattern;
27   
28    import org.junit.ComparisonFailure;
29    import org.junit.Test;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.configuration.ConfigurationSource;
32    import org.xwiki.rendering.block.XDOM;
33    import org.xwiki.rendering.listener.MetaData;
34    import org.xwiki.rendering.listener.WrappingListener;
35    import org.xwiki.rendering.parser.Parser;
36    import org.xwiki.rendering.parser.StreamParser;
37    import org.xwiki.rendering.renderer.BlockRenderer;
38    import org.xwiki.rendering.renderer.PrintRendererFactory;
39    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
40    import org.xwiki.rendering.renderer.printer.WikiPrinter;
41    import org.xwiki.rendering.syntax.SyntaxFactory;
42    import org.xwiki.rendering.transformation.TransformationContext;
43    import org.xwiki.rendering.transformation.TransformationManager;
44    import org.xwiki.test.internal.MockConfigurationSource;
45   
46    /**
47    * A generic JUnit Test used by {@link RenderingTestSuite} to parse some passed content and verify it matches some
48    * passed expectation. The format of the input/expectation is specified in {@link TestDataParser}.
49    *
50    * @version $Id: e73d144e6c91ce97e8d0adec695d03c2fe066a10 $
51    * @since 3.0RC1
52    */
 
53    public class RenderingTest
54    {
55    private String input;
56   
57    private String expected;
58   
59    private String parserId;
60   
61    private String targetSyntaxId;
62   
63    private boolean streaming;
64   
65    private boolean runTransformations;
66   
67    private Map<String, ?> configuration;
68   
69    private ComponentManager componentManager;
70   
 
71  887 toggle public RenderingTest(String input, String expected, String parserId, String targetSyntaxId,
72    boolean streaming, boolean runTransformations, Map<String, ?> configuration, ComponentManager componentManager)
73    {
74  887 this.input = input;
75  887 this.expected = expected;
76  887 this.parserId = parserId;
77  887 this.targetSyntaxId = targetSyntaxId;
78  887 this.streaming = streaming;
79  887 this.runTransformations = runTransformations;
80  887 this.configuration = configuration;
81  887 this.componentManager = componentManager;
82    }
83   
 
84  887 toggle @Test
85    public void execute() throws Throwable
86    {
87  887 Map<String, String> originalConfiguration = new HashMap<String, String>();
88  887 if (this.configuration != null) {
89  887 ConfigurationSource configurationSource = getComponentManager().getInstance(ConfigurationSource.class);
90   
91  887 if (configurationSource instanceof MockConfigurationSource) {
92  887 MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource;
93   
94  887 for (Map.Entry<String, ?> entry : this.configuration.entrySet()) {
95  2 originalConfiguration.put(entry.getKey(),
96    mockConfigurationSource.<String>getProperty(entry.getKey()));
97  2 mockConfigurationSource.setProperty(entry.getKey(), entry.getValue());
98    }
99    }
100    }
101   
102  887 try {
103  887 runTestInternal();
104    } finally {
105    // Revert Configuration that have been set
106  887 if (this.configuration != null) {
107  887 ConfigurationSource configurationSource = getComponentManager().getInstance(ConfigurationSource.class);
108   
109  887 if (configurationSource instanceof MockConfigurationSource) {
110  887 MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource;
111   
112  887 for (Map.Entry<String, String> entry : originalConfiguration.entrySet()) {
113  2 if (entry.getValue() == null) {
114  2 mockConfigurationSource.removeProperty(entry.getKey());
115    } else {
116  0 mockConfigurationSource.setProperty(entry.getKey(), entry.getValue());
117    }
118    }
119    }
120    }
121    }
122    }
123   
 
124  887 toggle private void runTestInternal() throws Throwable
125    {
126  887 WikiPrinter printer = new DefaultWikiPrinter();
127   
128  887 if (!this.streaming) {
129  883 Parser parser = getComponentManager().getInstance(Parser.class, this.parserId);
130  883 XDOM xdom = parser.parse(new StringReader(this.input));
131   
132  883 if (this.runTransformations) {
133  198 SyntaxFactory syntaxFactory = getComponentManager().getInstance(SyntaxFactory.class);
134  198 TransformationManager transformationManager =
135    getComponentManager().getInstance(TransformationManager.class);
136  198 TransformationContext txContext =
137    new TransformationContext(xdom, syntaxFactory.createSyntaxFromIdString(this.parserId));
138  198 txContext.setTargetSyntax(syntaxFactory.createSyntaxFromIdString(this.targetSyntaxId));
139  198 txContext.setId("test");
140  198 transformationManager.performTransformations(xdom, txContext);
141    }
142   
143  883 BlockRenderer renderer = getComponentManager().getInstance(BlockRenderer.class, this.targetSyntaxId);
144   
145    // remove source syntax from XDOM metadata
146  883 if (xdom.getMetaData() != null) {
147  883 Map<String, Object> metadataMap = new HashMap<String, Object>(xdom.getMetaData().getMetaData());
148  883 metadataMap.remove(MetaData.SYNTAX);
149  883 xdom = new XDOM(xdom.getChildren(), new MetaData(metadataMap));
150    }
151   
152  883 renderer.render(xdom, printer);
153    } else {
154  4 StreamParser parser = getComponentManager().getInstance(StreamParser.class, this.parserId);
155  4 PrintRendererFactory printRendererFactory =
156    getComponentManager().getInstance(PrintRendererFactory.class, this.targetSyntaxId);
157   
158    // remove source syntax from begin/endDocument metadata
159  4 WrappingListener listener = new SyntaxWrappingListener();
160  4 listener.setWrappedListener(printRendererFactory.createRenderer(printer));
161   
162  4 parser.parse(new StringReader(this.input), listener);
163    }
164   
165    // Verify the expected result against the result we got.
166  887 assertExpectedResult(this.expected, printer.toString());
167    }
168   
169    /**
170    * Compare the passed expected string with the passed result.
171    * We support regexes for comparison usng the format: ${{{regex:...}}}. For example:
172    * <code>
173    * .#-----------------------------------------------------
174    * .expect|event/1.0
175    * .#-----------------------------------------------------
176    * beginDocument
177    * beginMacroMarkerStandalone [useravatar] [username=XWiki.UserNotExisting]
178    * beginGroup [[class]=[xwikirenderingerror]]
179    * onWord [Failed to execute the [useravatar] macro]
180    * endGroup [[class]=[xwikirenderingerror]]
181    * beginGroup [[class]=[xwikirenderingerrordescription hidden]]
182    * onVerbatim [org.xwiki.rendering.macro.MacroExecutionException: User [XWiki.UserNotExisting]${{{regex:.*}}}]
183    * endGroup [[class]=[xwikirenderingerrordescription hidden]]
184    * endMacroMarkerStandalone [useravatar] [username=XWiki.UserNotExisting]
185    * endDocument
186    * </code>
187    */
 
188  887 toggle private void assertExpectedResult(String expected, String result)
189    {
190  887 StringBuilder builder = new StringBuilder();
191  887 normalizeExpectedValue(builder, expected);
192   
193  887 Pattern pattern = Pattern.compile(builder.toString(), Pattern.DOTALL);
194  887 Matcher matcher = pattern.matcher(result);
195  887 if (!matcher.matches()) {
196  0 throw new ComparisonFailure("", expected, result);
197    }
198    }
199   
 
200  906 toggle private void normalizeExpectedValue(StringBuilder builder, String expected)
201    {
202  906 int pos = expected.indexOf("${{{regex:");
203  906 if (pos > -1) {
204  19 builder.append(Pattern.quote(expected.substring(0, pos)));
205    // Find end of regex definition
206  19 int pos2 = expected.indexOf("}}}", pos + 10);
207  19 if (pos2 == -1) {
208  0 throw new RuntimeException("Invalid regex declaration: missing closing part }}}");
209    }
210  19 builder.append(expected.substring(pos + 10, pos2));
211  19 normalizeExpectedValue(builder, expected.substring(pos2 + 3));
212    } else {
213  887 builder.append(Pattern.quote(expected));
214    }
215    }
216   
 
217  3944 toggle public ComponentManager getComponentManager() throws Exception
218    {
219  3944 return this.componentManager;
220    }
221    }