1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.converter

File DefaultHTMLConverterTest.java

 

Code metrics

0
44
5
1
187
106
5
0.11
8.8
5
1

Classes

Class Line # Actions
DefaultHTMLConverterTest 63 44 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 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.wysiwyg.server.internal.converter;
21   
22    import java.io.StringReader;
23    import java.util.Collections;
24   
25    import org.junit.Assert;
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.ArgumentCaptor;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.gwt.wysiwyg.client.cleaner.HTMLCleaner;
32    import org.xwiki.gwt.wysiwyg.client.converter.HTMLConverter;
33    import org.xwiki.rendering.block.Block;
34    import org.xwiki.rendering.block.XDOM;
35    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
36    import org.xwiki.rendering.listener.MetaData;
37    import org.xwiki.rendering.parser.Parser;
38    import org.xwiki.rendering.parser.StreamParser;
39    import org.xwiki.rendering.renderer.BlockRenderer;
40    import org.xwiki.rendering.renderer.PrintRenderer;
41    import org.xwiki.rendering.renderer.PrintRendererFactory;
42    import org.xwiki.rendering.renderer.printer.WikiPrinter;
43    import org.xwiki.rendering.syntax.Syntax;
44    import org.xwiki.rendering.syntax.SyntaxFactory;
45    import org.xwiki.rendering.transformation.RenderingContext;
46    import org.xwiki.rendering.transformation.Transformation;
47    import org.xwiki.rendering.transformation.TransformationContext;
48    import org.xwiki.test.annotation.AfterComponent;
49    import org.xwiki.test.mockito.MockitoComponentMockingRule;
50   
51    import static org.junit.Assert.assertEquals;
52    import static org.mockito.ArgumentMatchers.any;
53    import static org.mockito.ArgumentMatchers.same;
54    import static org.mockito.Mockito.mock;
55    import static org.mockito.Mockito.verify;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link DefaultHTMLConverter}.
60    *
61    * @version $Id: df57f9a34aaa34436284fa8e484e5fce3b043bba $
62    */
 
63    public class DefaultHTMLConverterTest
64    {
65    /**
66    * A component manager that automatically mocks all dependencies of {@link DefaultHTMLConverter}.
67    */
68    @Rule
69    public MockitoComponentMockingRule<HTMLConverter> mocker = new MockitoComponentMockingRule<HTMLConverter>(
70    DefaultHTMLConverter.class);
71   
 
72  3 toggle @AfterComponent
73    public void overrideComponent() throws Exception
74    {
75  3 mocker.registerComponent(RenderingContext.class, mock(MutableRenderingContext.class));
76    }
77   
 
78  3 toggle @Before
79    public void configure() throws Exception
80    {
81  3 this.mocker.registerComponent(ComponentManager.class, "context", this.mocker);
82    }
83   
84    /**
85    * Unit test for {@link DefaultHTMLConverter#fromHTML(String, String)}.
86    */
 
87  1 toggle @Test
88    public void fromHTML() throws Exception
89    {
90  1 String html = "some HTML";
91  1 String syntaxId = "syntax/x.y";
92   
93    // Verify the HTML is cleaned.
94  1 HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
95  1 when(cleaner.clean(html)).thenReturn(html);
96   
97  1 PrintRendererFactory printRendererFactory = this.mocker.registerMockComponent(PrintRendererFactory.class, syntaxId);
98   
99  1 PrintRenderer printRenderer = mock(PrintRenderer.class);
100  1 when(printRendererFactory.createRenderer(any(WikiPrinter.class))).thenReturn(printRenderer);
101   
102  1 Assert.assertEquals("", mocker.getComponentUnderTest().fromHTML(html, syntaxId));
103   
104    // Verify the HTML is converted to the specified syntax.
105  1 StreamParser xhtmlStreamParser = mocker.getInstance(StreamParser.class, "xhtml/1.0");
106  1 verify(xhtmlStreamParser).parse(any(StringReader.class), same(printRenderer));
107    }
108   
109    /**
110    * Unit test for {@link DefaultHTMLConverter#toHTML(String, String)}.
111    */
 
112  1 toggle @Test
113    public void toHTML() throws Exception
114    {
115  1 String source = "wiki syntax";
116  1 String syntaxId = "syntax/x.y";
117   
118    // The source should be parsed.
119  1 Parser parser = this.mocker.registerMockComponent(Parser.class, syntaxId);
120   
121  1 XDOM xdom = new XDOM(Collections.<Block> emptyList());
122  1 when(parser.parse(any(StringReader.class))).thenReturn(xdom);
123   
124  1 Assert.assertEquals("", mocker.getComponentUnderTest().toHTML(source, syntaxId));
125   
126    // Verify that the macro transformations have been executed.
127  1 Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
128  1 RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
129   
130    // It's very important to verify that a transformation context id is set as otherwise if the content being
131    // edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
132    // one won't be visible from the other ones (For example see http://jira.xwiki.org/browse/XWIKI-11695).
133  1 ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
134  1 verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation),
135    txContextArgument.capture(), same(xdom));
136  1 assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
137   
138    // Verify the XDOM is rendered to Annotated XHTML.
139  1 BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
140  1 verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
141    }
142   
143    /**
144    * Unit test for {@link DefaultHTMLConverter#parseAndRender(String, String)}.
145    */
 
146  1 toggle @Test
147    public void parseAndRender() throws Exception
148    {
149  1 String html = "some HTML";
150  1 String syntaxId = "syntax/x.y";
151   
152    // Verify the HTML is cleaned.
153  1 HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
154  1 when(cleaner.clean(html)).thenReturn(html);
155   
156    // Verify the HTML is parsed into XDOM.
157  1 XDOM xdom = new XDOM(Collections.<Block> emptyList());
158  1 Parser xhtmlParser = mocker.getInstance(Parser.class, "xhtml/1.0");
159  1 when(xhtmlParser.parse(any(StringReader.class))).thenReturn(xdom);
160   
161    // Verify the specified syntax is used.
162  1 SyntaxFactory syntaxFactory = mocker.getInstance(SyntaxFactory.class);
163  1 Syntax syntax = mock(Syntax.class);
164  1 when(syntaxFactory.createSyntaxFromIdString(syntaxId)).thenReturn(syntax);
165   
166  1 Assert.assertEquals("", mocker.getComponentUnderTest().parseAndRender(html, syntaxId));
167   
168    // Verify that the macro transformations have been executed.
169  1 Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
170  1 RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
171   
172    // It's very important to verify that a transformation context id is set as otherwise if the content being
173    // edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
174    // one won't be visible from the other ones (For example see http://jira.xwiki.org/browse/XWIKI-11695).
175  1 ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
176  1 verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation),
177    txContextArgument.capture(), same(xdom));
178  1 assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
179   
180    // Verify the XDOM is rendered to Annotated XHTML.
181  1 BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
182  1 verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
183   
184    // Verify that the syntax meta data has been set.
185  1 Assert.assertSame(syntax, xdom.getMetaData().getMetaData(MetaData.SYNTAX));
186    }
187    }