1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.wysiwyg.internal.converter

File DefaultHTMLConverterTest.java

 

Code metrics

0
43
5
1
186
105
5
0.12
8.6
5
1

Classes

Class Line # Actions
DefaultHTMLConverterTest 62 43 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.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.rendering.block.Block;
32    import org.xwiki.rendering.block.XDOM;
33    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
34    import org.xwiki.rendering.listener.MetaData;
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.PrintRenderer;
39    import org.xwiki.rendering.renderer.PrintRendererFactory;
40    import org.xwiki.rendering.renderer.printer.WikiPrinter;
41    import org.xwiki.rendering.syntax.Syntax;
42    import org.xwiki.rendering.transformation.RenderingContext;
43    import org.xwiki.rendering.transformation.Transformation;
44    import org.xwiki.rendering.transformation.TransformationContext;
45    import org.xwiki.test.annotation.AfterComponent;
46    import org.xwiki.test.mockito.MockitoComponentMockingRule;
47    import org.xwiki.wysiwyg.cleaner.HTMLCleaner;
48    import org.xwiki.wysiwyg.converter.HTMLConverter;
49   
50    import static org.junit.Assert.assertEquals;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.same;
53    import static org.mockito.Mockito.mock;
54    import static org.mockito.Mockito.verify;
55    import static org.mockito.Mockito.when;
56   
57    /**
58    * Unit tests for {@link DefaultHTMLConverter}.
59    *
60    * @version $Id: 77d36568b8b3821286200f17cb1f9953b7727bff $
61    */
 
62    public class DefaultHTMLConverterTest
63    {
64    /**
65    * A component manager that automatically mocks all dependencies of {@link DefaultHTMLConverter}.
66    */
67    @Rule
68    public MockitoComponentMockingRule<HTMLConverter> mocker =
69    new MockitoComponentMockingRule<HTMLConverter>(DefaultHTMLConverter.class);
70   
 
71  3 toggle @AfterComponent
72    public void overrideComponent() throws Exception
73    {
74  3 mocker.registerComponent(RenderingContext.class, mock(MutableRenderingContext.class));
75    }
76   
 
77  3 toggle @Before
78    public void configure() throws Exception
79    {
80  3 this.mocker.registerComponent(ComponentManager.class, "context", this.mocker);
81    }
82   
83    /**
84    * Unit test for {@link DefaultHTMLConverter#fromHTML(String, String)}.
85    */
 
86  1 toggle @Test
87    public void fromHTML() throws Exception
88    {
89  1 String html = "some HTML";
90  1 String syntaxId = "syntax/x.y";
91   
92    // Verify the HTML is cleaned.
93  1 HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
94  1 when(cleaner.clean(html)).thenReturn(html);
95   
96  1 PrintRendererFactory printRendererFactory =
97    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 https://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  1 Assert.assertEquals("", mocker.getComponentUnderTest().parseAndRender(html, syntaxId));
162   
163    // Verify that the macro transformations have been executed.
164  1 Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
165  1 RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
166   
167    // It's very important to verify that a transformation context id is set as otherwise if the content being
168    // edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
169    // one won't be visible from the other ones (For example see https://jira.xwiki.org/browse/XWIKI-11695).
170  1 ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
171  1 verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation),
172    txContextArgument.capture(), same(xdom));
173  1 assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
174   
175    // Verify the XDOM is rendered to Annotated XHTML.
176  1 BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
177  1 verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
178   
179    // Verify that the syntax meta data has been set.
180  1 Syntax syntax = Syntax.valueOf(syntaxId);
181  1 Assert.assertEquals(syntax, xdom.getMetaData().getMetaData(MetaData.SYNTAX));
182   
183    // Verify that the syntax has been set on the rendering context.
184  1 verify(((MutableRenderingContext) renderingContext)).push(null, null, syntax, null, false, syntax);
185    }
186    }