1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.annotation.renderer

File AnnotationXHTMLRendererTest.java

 

Code metrics

0
93
8
1
270
155
8
0.09
11.62
8
1

Classes

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