1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
60 |
|
|
61 |
|
@version |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (49) |
Complexity: 5 |
Complexity Density: 0.11 |
|
63 |
|
public class DefaultHTMLConverterTest |
64 |
|
{ |
65 |
|
|
66 |
|
@link |
67 |
|
|
68 |
|
@Rule |
69 |
|
public MockitoComponentMockingRule<HTMLConverter> mocker = new MockitoComponentMockingRule<HTMLConverter>( |
70 |
|
DefaultHTMLConverter.class); |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
3 |
@AfterComponent... |
73 |
|
public void overrideComponent() throws Exception |
74 |
|
{ |
75 |
3 |
mocker.registerComponent(RenderingContext.class, mock(MutableRenderingContext.class)); |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
3 |
@Before... |
79 |
|
public void configure() throws Exception |
80 |
|
{ |
81 |
3 |
this.mocker.registerComponent(ComponentManager.class, "context", this.mocker); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
@link |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
87 |
1 |
@Test... |
88 |
|
public void fromHTML() throws Exception |
89 |
|
{ |
90 |
1 |
String html = "some HTML"; |
91 |
1 |
String syntaxId = "syntax/x.y"; |
92 |
|
|
93 |
|
|
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 |
|
|
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 |
|
@link |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
112 |
1 |
@Test... |
113 |
|
public void toHTML() throws Exception |
114 |
|
{ |
115 |
1 |
String source = "wiki syntax"; |
116 |
1 |
String syntaxId = "syntax/x.y"; |
117 |
|
|
118 |
|
|
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 |
|
|
127 |
1 |
Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro"); |
128 |
1 |
RenderingContext renderingContext = mocker.getInstance(RenderingContext.class); |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
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 |
|
|
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 |
|
@link |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void parseAndRender() throws Exception |
148 |
|
{ |
149 |
1 |
String html = "some HTML"; |
150 |
1 |
String syntaxId = "syntax/x.y"; |
151 |
|
|
152 |
|
|
153 |
1 |
HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class); |
154 |
1 |
when(cleaner.clean(html)).thenReturn(html); |
155 |
|
|
156 |
|
|
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 |
|
|
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 |
|
|
169 |
1 |
Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro"); |
170 |
1 |
RenderingContext renderingContext = mocker.getInstance(RenderingContext.class); |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
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 |
|
|
181 |
1 |
BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0"); |
182 |
1 |
verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class)); |
183 |
|
|
184 |
|
|
185 |
1 |
Assert.assertSame(syntax, xdom.getMetaData().getMetaData(MetaData.SYNTAX)); |
186 |
|
} |
187 |
|
} |