Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
62   215   16   10.33
20   136   0.26   6
6     2.67  
1    
 
  RenderingTest       Line # 53 62 0% 16 22 75% 0.75
 
  (990)
 
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.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: 35c7fdb61786bdf810ea10ca94c0aab70984440d $
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  990 toggle public RenderingTest(String input, String expected, String parserId, String targetSyntaxId,
72    boolean streaming, boolean runTransformations, Map<String, ? > configuration, ComponentManager componentManager)
73    {
74  990 this.input = input;
75  990 this.expected = expected;
76  990 this.parserId = parserId;
77  990 this.targetSyntaxId = targetSyntaxId;
78  990 this.streaming = streaming;
79  990 this.runTransformations = runTransformations;
80  990 this.configuration = configuration;
81  990 this.componentManager = componentManager;
82    }
83   
 
84  990 toggle @Test
85    public void execute() throws Throwable
86    {
87  990 Map<String, String> originalConfiguration = new HashMap<String, String>();
88  990 if (this.configuration != null) {
89  990 ConfigurationSource configurationSource = getComponentManager().lookup(ConfigurationSource.class);
90   
91  990 if (configurationSource instanceof MockConfigurationSource) {
92  990 MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource;
93   
94  990 for (Map.Entry<String, ? > entry : this.configuration.entrySet()) {
95  0 originalConfiguration.put(entry.getKey(),
96    mockConfigurationSource.<String> getProperty(entry.getKey()));
97  0 mockConfigurationSource.setProperty(entry.getKey(), entry.getValue());
98    }
99    }
100    }
101   
102  990 try {
103  990 runTestInternal();
104    } finally {
105    // Revert Configuration that have been set
106  990 if (this.configuration != null) {
107  990 ConfigurationSource configurationSource = getComponentManager().lookup(ConfigurationSource.class);
108   
109  990 if (configurationSource instanceof MockConfigurationSource) {
110  990 MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource;
111   
112  990 for (Map.Entry<String, String> entry : originalConfiguration.entrySet()) {
113  0 if (entry.getValue() == null) {
114  0 mockConfigurationSource.removeProperty(entry.getKey());
115    } else {
116  0 mockConfigurationSource.setProperty(entry.getKey(), entry.getValue());
117    }
118    }
119    }
120    }
121    }
122    }
123   
 
124  990 toggle private void runTestInternal() throws Throwable
125    {
126  990 WikiPrinter printer = new DefaultWikiPrinter();
127   
128  990 if (!this.streaming) {
129  988 Parser parser = getComponentManager().lookup(Parser.class, this.parserId);
130  988 XDOM xdom = parser.parse(new StringReader(this.input));
131   
132  988 if (this.runTransformations) {
133  115 SyntaxFactory syntaxFactory = getComponentManager().lookup(SyntaxFactory.class);
134  115 TransformationManager transformationManager = getComponentManager().lookup(TransformationManager.class);
135  115 TransformationContext txContext =
136    new TransformationContext(xdom, syntaxFactory.createSyntaxFromIdString(this.parserId));
137  115 transformationManager.performTransformations(xdom, txContext);
138    }
139   
140  988 BlockRenderer renderer = getComponentManager().lookup(BlockRenderer.class, this.targetSyntaxId);
141   
142    // remove source syntax from XDOM metadata
143  988 Map<String, Object> metadataMap = new HashMap<String, Object>(xdom.getMetaData().getMetaData());
144  988 metadataMap.remove(MetaData.SYNTAX);
145  988 xdom = new XDOM(xdom.getChildren(), new MetaData(metadataMap));
146   
147  988 renderer.render(xdom, printer);
148    } else {
149  2 StreamParser parser = getComponentManager().lookup(StreamParser.class, this.parserId);
150  2 PrintRendererFactory printRendererFactory =
151    getComponentManager().lookup(PrintRendererFactory.class, this.targetSyntaxId);
152   
153    // remove source syntax from begin/endDocument metadata
154  2 WrappingListener listener = new SyntaxWrappingListener();
155  2 listener.setWrappedListener(printRendererFactory.createRenderer(printer));
156   
157  2 parser.parse(new StringReader(this.input), listener);
158    }
159   
160    // Verify the expected result against the result we got.
161  990 assertExpectedResult(this.expected, printer.toString());
162    }
163   
164    /**
165    * Compare the passed expected string with the passed result.
166    * We support regexes for comparison usng the format: ${{{regex:...}}}. For example:
167    * {@code
168    * .#-----------------------------------------------------
169    * .expect|event/1.0
170    * .#-----------------------------------------------------
171    * beginDocument
172    * beginMacroMarkerStandalone [useravatar] [username=XWiki.UserNotExisting]
173    * beginGroup [[class]=[xwikirenderingerror]]
174    * onWord [Failed to execute the [useravatar] macro]
175    * endGroup [[class]=[xwikirenderingerror]]
176    * beginGroup [[class]=[xwikirenderingerrordescription hidden]]
177    * onVerbatim [org.xwiki.rendering.macro.MacroExecutionException: User [XWiki.UserNotExisting]${{{regex:.*}}}]
178    * endGroup [[class]=[xwikirenderingerrordescription hidden]]
179    * endMacroMarkerStandalone [useravatar] [username=XWiki.UserNotExisting]
180    * endDocument}
181    */
 
182  990 toggle private void assertExpectedResult(String expected, String result)
183    {
184  990 StringBuilder builder = new StringBuilder();
185  990 normalizeExpectedValue(builder, expected);
186   
187  990 Pattern pattern = Pattern.compile(builder.toString(), Pattern.DOTALL);
188  990 Matcher matcher = pattern.matcher(result);
189  990 if (!matcher.matches()) {
190  0 throw new ComparisonFailure("", expected, result);
191    }
192    }
193   
 
194  990 toggle private void normalizeExpectedValue(StringBuilder builder, String expected)
195    {
196  990 int pos = expected.indexOf("${{{regex:");
197  990 if (pos > -1) {
198  0 builder.append(Pattern.quote(expected.substring(0, pos)));
199    // Find end of regex definition
200  0 int pos2 = expected.indexOf("}}}", pos + 10);
201  0 if (pos2 == -1) {
202  0 throw new RuntimeException("Invalid regex declaration: missing closing part }}}");
203    }
204  0 builder.append(expected.substring(pos + 10, pos2));
205  0 normalizeExpectedValue(builder, expected.substring(pos2 + 3));
206    } else {
207  990 builder.append(Pattern.quote(expected));
208    }
209    }
210   
 
211  4190 toggle public ComponentManager getComponentManager() throws Exception
212    {
213  4190 return this.componentManager;
214    }
215    }