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

File AnnotationXHTMLRendererTest.java

 

Code metrics

0
95
8
1
273
158
8
0.08
11.88
8
1

Classes

Class Line # Actions
AnnotationXHTMLRendererTest 52 95 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 124 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.annotation.renderer;
21   
22    import static org.junit.Assert.assertEquals;
23   
24    import java.io.StringReader;
25    import java.util.ArrayList;
26    import java.util.Collection;
27   
28    import org.junit.Test;
29    import org.junit.runner.RunWith;
30    import org.junit.runners.Parameterized;
31    import org.junit.runners.Parameterized.Parameters;
32    import org.xwiki.annotation.TestDocumentFactory;
33    import org.xwiki.rendering.block.XDOM;
34    import org.xwiki.rendering.internal.renderer.DefaultLinkLabelGenerator;
35    import org.xwiki.rendering.parser.Parser;
36    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
37    import org.xwiki.rendering.renderer.printer.WikiPrinter;
38    import org.xwiki.rendering.syntax.Syntax;
39    import org.xwiki.rendering.syntax.SyntaxFactory;
40    import org.xwiki.rendering.test.MockWikiModel;
41    import org.xwiki.rendering.transformation.TransformationContext;
42    import org.xwiki.rendering.transformation.TransformationManager;
43    import org.xwiki.test.jmock.AbstractComponentTestCase;
44   
45    /**
46    * Renderer tests for the XHTML annotations renderer, from the test files.
47    *
48    * @version $Id: b516c359110254775e570883bcb0d0163c310fa2 $
49    * @since 2.3M1
50    */
51    @RunWith(Parameterized.class)
 
52    public class AnnotationXHTMLRendererTest extends AbstractComponentTestCase
53    {
54    /**
55    * Document description files to run this test for.
56    */
57    private static Collection<String[]> files = new ArrayList<String[]>();
58   
59    /**
60    * The annotations renderer hint.
61    */
62    private static final String ANNOTATIONS_RENDERER_HINT = "annotations-xhtml/1.0";
63   
64    /**
65    * Mock document to run tests for.
66    */
67    protected String docName;
68   
69    /**
70    * Factory to load test documents.
71    */
72    protected TestDocumentFactory docFactory;
73   
 
74  1 toggle static {
75    // tests containing only plain text content
76  1 addFileToTest("renderer/plain/Plain1.test");
77  1 addFileToTest("renderer/plain/Plain2.test");
78  1 addFileToTest("renderer/plain/Plain3.test");
79  1 addFileToTest("renderer/plain/Plain4.test");
80  1 addFileToTest("renderer/plain/Plain5.test");
81  1 addFileToTest("renderer/plain/Plain6.test");
82   
83    // tests containing formatting
84  1 addFileToTest("renderer/format/Format1.test");
85  1 addFileToTest("renderer/format/Format2.test");
86  1 addFileToTest("renderer/format/Format3.test");
87  1 addFileToTest("renderer/format/Format4.test");
88  1 addFileToTest("renderer/format/Format5.test");
89   
90    // tests containing special characters in the annotated content
91  1 addFileToTest("renderer/specialchars/SpecialChars1.test");
92  1 addFileToTest("renderer/specialchars/SpecialChars2.test");
93  1 addFileToTest("renderer/specialchars/SpecialChars3.test");
94   
95    // tests for which the selection of the annotation appears more than once in the document content
96  1 addFileToTest("renderer/ambiguous/Ambiguous1.test");
97  1 addFileToTest("renderer/ambiguous/Ambiguous2.test");
98  1 addFileToTest("renderer/ambiguous/Ambiguous3.test");
99  1 addFileToTest("renderer/ambiguous/Ambiguous4.test");
100  1 addFileToTest("renderer/ambiguous/Ambiguous5.test");
101  1 addFileToTest("renderer/ambiguous/Ambiguous6.test");
102    // FIXME: fix support for empty selection annotations by making sure that, at each point, for the same
103    // annotation, startEvents & end events are sent in this order. FTM the convention is that annotations are
104    // closed before are opened, for which reason the annotation is something like: </span><span
105    // class="annotation annotationID0">...
106    // addFileToTest("renderer/ambiguous/Ambiguous7.test");
107   
108    // tests in which more than one annotation needs to be rendered in the content
109  1 addFileToTest("renderer/multiple/Multiple1.test");
110  1 addFileToTest("renderer/multiple/Multiple2.test");
111  1 addFileToTest("renderer/multiple/Multiple3.test");
112  1 addFileToTest("renderer/multiple/Multiple4.test");
113  1 addFileToTest("renderer/multiple/Multiple5.test");
114   
115    // tests containing links in the annotated content
116  1 addFileToTest("renderer/links/Links1.test");
117  1 addFileToTest("renderer/links/Links2.test");
118  1 addFileToTest("renderer/links/Links3.test");
119  1 addFileToTest("renderer/links/Links4.test");
120  1 addFileToTest("renderer/links/Links5.test");
121  1 addFileToTest("renderer/links/Links6.test");
122  1 addFileToTest("renderer/links/Links7.test");
123  1 addFileToTest("renderer/links/Links8.test");
124   
125    // tests containing macros generating content in the annotated content
126  1 addFileToTest("renderer/macros/Macros1.test");
127  1 addFileToTest("renderer/macros/Macros2.test");
128  1 addFileToTest("renderer/macros/Macros3.test");
129  1 addFileToTest("renderer/macros/Macros4.test");
130  1 addFileToTest("renderer/macros/Macros5.test");
131  1 addFileToTest("renderer/macros/Macros6.test");
132  1 addFileToTest("renderer/macros/Macros7.test");
133   
134    // tests where the annotated content is in a table
135  1 addFileToTest("renderer/tables/Tables1.test");
136  1 addFileToTest("renderer/tables/Tables2.test");
137   
138    // tests where the annotated content is inside some verbatim blocks
139  1 addFileToTest("renderer/verbatim/Verbatim1.test");
140  1 addFileToTest("renderer/verbatim/Verbatim2.test");
141  1 addFileToTest("renderer/verbatim/Verbatim3.test");
142  1 addFileToTest("renderer/verbatim/Verbatim4.test");
143  1 addFileToTest("renderer/verbatim/Verbatim5.test");
144  1 addFileToTest("renderer/verbatim/Verbatim6.test");
145  1 addFileToTest("renderer/verbatim/Verbatim7.test");
146  1 addFileToTest("renderer/verbatim/Verbatim8.test");
147  1 addFileToTest("renderer/verbatim/Verbatim9.test");
148  1 addFileToTest("renderer/verbatim/Verbatim10.test");
149  1 addFileToTest("renderer/verbatim/Verbatim11.test");
150   
151    // tests where annotations start and/or end in the middle of a word rather than at the beginning or end
152  1 addFileToTest("renderer/partialwords/PartialWords1.test");
153  1 addFileToTest("renderer/partialwords/PartialWords2.test");
154  1 addFileToTest("renderer/partialwords/PartialWords3.test");
155  1 addFileToTest("renderer/partialwords/PartialWords4.test");
156  1 addFileToTest("renderer/partialwords/PartialWords5.test");
157  1 addFileToTest("renderer/partialwords/PartialWords6.test");
158   
159  1 addFileToTest("renderer/spaces/Spaces1.test");
160  1 addFileToTest("renderer/spaces/Spaces2.test");
161  1 addFileToTest("renderer/spaces/Spaces3.test");
162    }
163   
164    /**
165    * Creates a test for the passed document. Will be instantiated by the parameterized runner for all the parameters.
166    *
167    * @param docName the document (and corpus filename) to run tests for
168    */
 
169  124 toggle public AnnotationXHTMLRendererTest(String docName)
170    {
171  124 this.docName = docName;
172    }
173   
174    /**
175    * Adds a file to the list of files to run tests for.
176    *
177    * @param docName the name of the document / file to test
178    */
 
179  62 toggle private static void addFileToTest(String docName)
180    {
181  62 files.add(new String[] {docName});
182    }
183   
184    /**
185    * @return list of corpus files to instantiate tests for
186    */
 
187  1 toggle @Parameters
188    public static Collection<String[]> data()
189    {
190  1 return files;
191    }
192   
 
193  124 toggle @Override
194    protected void registerComponents() throws Exception
195    {
196  124 super.registerComponents();
197   
198    // register wiki model mock so that we can use documents / attachments information
199  124 getComponentManager().registerComponent(MockWikiModel.getComponentDescriptor());
200    // make sure to use the default link label generator
201  124 registerComponent(DefaultLinkLabelGenerator.class);
202    }
203   
 
204  124 toggle @Override
205    public void setUp() throws Exception
206    {
207  124 super.setUp();
208   
209  124 docFactory = new TestDocumentFactory();
210    }
211   
212    /**
213    * Test rendering the annotations in the document description file results in the annotated html.
214    *
215    * @throws Exception in case something goes wrong looking up components and rendering
216    */
 
217  62 toggle @Test
218    public void getAnnotatedHTML() throws Exception
219    {
220  62 Parser parser = getComponentManager().getInstance(Parser.class, docFactory.getDocument(docName).getSyntax());
221  62 XDOM xdom = parser.parse(new StringReader(docFactory.getDocument(docName).getSource()));
222  62 SyntaxFactory syntaxFactory = getComponentManager().getInstance(SyntaxFactory.class);
223   
224    // run transformations
225  62 TransformationManager transformationManager = getComponentManager().getInstance(TransformationManager.class);
226  62 TransformationContext context = new TransformationContext(xdom,
227    syntaxFactory.createSyntaxFromIdString(docFactory.getDocument(docName).getSyntax()));
228  62 context.setTargetSyntax(Syntax.ANNOTATED_XHTML_1_0);
229  62 transformationManager.performTransformations(xdom, context);
230   
231  62 AnnotationPrintRenderer renderer =
232    getComponentManager().getInstance(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT);
233  62 WikiPrinter printer = new DefaultWikiPrinter();
234  62 renderer.setPrinter(printer);
235    // set the annotations for this renderer
236  62 renderer.setAnnotations(docFactory.getDocument(docName).getAnnotations());
237   
238  62 xdom.traverse(renderer);
239   
240  62 assertEquals("[" + docName + "] test failed", docFactory.getDocument(docName).getAnnotatedContent(), printer
241    .toString());
242    }
243   
244    /**
245    * Test rendering with the annotations renderer but without annotations doesn't alter the content.
246    *
247    * @throws Exception in case something goes wrong looking up components and rendering
248    */
 
249  62 toggle @Test
250    public void getAnnotatedHTMLWithoutAnnotations() throws Exception
251    {
252  62 Parser parser = getComponentManager().getInstance(Parser.class, docFactory.getDocument(docName).getSyntax());
253  62 XDOM xdom = parser.parse(new StringReader(docFactory.getDocument(docName).getSource()));
254  62 SyntaxFactory syntaxFactory = getComponentManager().getInstance(SyntaxFactory.class);
255   
256    // run transformations
257  62 TransformationManager transformationManager = getComponentManager().getInstance(TransformationManager.class);
258  62 TransformationContext context = new TransformationContext(xdom,
259    syntaxFactory.createSyntaxFromIdString(docFactory.getDocument(docName).getSyntax()));
260  62 context.setTargetSyntax(Syntax.ANNOTATED_XHTML_1_0);
261  62 transformationManager.performTransformations(xdom, context);
262   
263  62 AnnotationPrintRenderer renderer =
264    getComponentManager().getInstance(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT);
265  62 WikiPrinter printer = new DefaultWikiPrinter();
266  62 renderer.setPrinter(printer);
267   
268  62 xdom.traverse(renderer);
269   
270  62 assertEquals("[" + docName + "] test failed", docFactory.getDocument(docName).getRenderedContent(), printer
271    .toString());
272    }
273    }