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

File RenderingScriptServiceTest.java

 

Code metrics

0
48
16
1
230
171
16
0.33
3
16
1

Classes

Class Line # Actions
RenderingScriptServiceTest 53 48 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 15 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.rendering.script;
21   
22    import java.io.StringReader;
23    import java.util.Collections;
24   
25    import org.junit.Assert;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.mockito.invocation.InvocationOnMock;
29    import org.mockito.stubbing.Answer;
30    import org.xwiki.component.internal.ContextComponentManagerProvider;
31    import org.xwiki.rendering.block.Block;
32    import org.xwiki.rendering.block.XDOM;
33    import org.xwiki.rendering.parser.ParseException;
34    import org.xwiki.rendering.parser.Parser;
35    import org.xwiki.rendering.renderer.BlockRenderer;
36    import org.xwiki.rendering.renderer.printer.WikiPrinter;
37    import org.xwiki.rendering.syntax.Syntax;
38    import org.xwiki.rendering.syntax.SyntaxFactory;
39    import org.xwiki.test.annotation.ComponentList;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41    import org.xwiki.test.mockito.StringReaderMatcher;
42   
43    import static org.mockito.ArgumentMatchers.any;
44    import static org.mockito.Mockito.*;
45   
46    /**
47    * Unit tests for {@link RenderingScriptService}.
48    *
49    * @version $Id: fa99d76f4a50ed8b1f9eecb2e3e8b83cf2a3d45a $
50    * @since 3.2M3
51    */
52    @ComponentList({ContextComponentManagerProvider.class})
 
53    public class RenderingScriptServiceTest
54    {
55    @Rule
56    public MockitoComponentMockingRule<RenderingScriptService> mocker = new MockitoComponentMockingRule<>(
57    RenderingScriptService.class);
58   
 
59  1 toggle @Test
60    public void parseAndRender() throws Exception
61    {
62  1 Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
63  1 when(parser.parse(argThat(new StringReaderMatcher("some [[TODO]] stuff"))))
64    .thenReturn(new XDOM(Collections.<Block>emptyList()));
65   
66  1 BlockRenderer blockRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "xwiki/2.0");
67  1 doAnswer(new Answer<Void>()
68    {
 
69  1 toggle @Override
70    public Void answer(InvocationOnMock invocationOnMock) throws Throwable
71    {
72  1 WikiPrinter printer = (WikiPrinter) invocationOnMock.getArguments()[1];
73  1 printer.print("some ~[~[TODO]] stuff");
74  1 return null;
75    }
76    }).when(blockRenderer).render(any(XDOM.class), any());
77   
78  1 XDOM xdom = this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0");
79  1 Assert.assertEquals("some ~[~[TODO]] stuff", this.mocker.getComponentUnderTest().render(xdom, "xwiki/2.0"));
80    }
81   
 
82  1 toggle @Test
83    public void parseAndRenderWhenErrorInParse() throws Exception
84    {
85  1 Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
86  1 when(parser.parse(new StringReader("some [[TODO]] stuff"))).thenThrow(new ParseException(("error")));
87   
88  1 Assert.assertNull(this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0"));
89    }
90   
 
91  1 toggle @Test
92    public void parseAndRenderWhenErrorInRender() throws Exception
93    {
94  1 Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
95  1 when(parser.parse(new StringReader("some [[TODO]] stuff")))
96    .thenReturn(new XDOM(Collections.<Block>emptyList()));
97   
98  1 XDOM xdom = this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0");
99  1 Assert.assertNull(this.mocker.getComponentUnderTest().render(xdom, "unknown"));
100    }
101   
 
102  1 toggle @Test
103    public void resolveSyntax() throws Exception
104    {
105  1 SyntaxFactory syntaxFactory = this.mocker.getInstance(SyntaxFactory.class);
106  1 when(syntaxFactory.createSyntaxFromIdString("xwiki/2.1")).thenReturn(Syntax.XWIKI_2_1);
107   
108  1 Assert.assertEquals(Syntax.XWIKI_2_1, this.mocker.getComponentUnderTest().resolveSyntax("xwiki/2.1"));
109    }
110   
 
111  1 toggle @Test
112    public void resolveSyntaxWhenInvalid() throws Exception
113    {
114  1 SyntaxFactory syntaxFactory = this.mocker.getInstance(SyntaxFactory.class);
115  1 when(syntaxFactory.createSyntaxFromIdString("unknown")).thenThrow(new ParseException("invalid"));
116   
117  1 Assert.assertNull(this.mocker.getComponentUnderTest().resolveSyntax("unknown"));
118    }
119   
 
120  1 toggle @Test
121    public void escape10Syntax() throws Exception
122    {
123    // Since the logic is pretty simple (prepend every character with an escape character), the below tests are
124    // mostly for exemplification.
125    // Note: Java escaped string "\\" == "\" (real string).
126  1 Assert.assertEquals("\\\\", this.mocker.getComponentUnderTest().escape("\\", Syntax.XWIKI_1_0));
127  1 Assert.assertEquals("\\*\\t\\e\\s\\t\\*", this.mocker.getComponentUnderTest()
128    .escape("*test*", Syntax.XWIKI_1_0));
129  1 Assert.assertEquals("\\a\\\\\\\\\\[\\l\\i\\n\\k\\>\\X\\.\\Y\\]",
130    this.mocker.getComponentUnderTest().escape("a\\\\[link>X.Y]", Syntax.XWIKI_1_0));
131  1 Assert.assertEquals("\\{\\p\\r\\e\\}\\v\\e\\r\\b\\a\\t\\i\\m\\{\\/\\p\\r\\e\\}", this.mocker
132    .getComponentUnderTest().escape("{pre}verbatim{/pre}", Syntax.XWIKI_1_0));
133  1 Assert.assertEquals("\\{\\m\\a\\c\\r\\o\\:\\s\\o\\m\\e\\=\\p\\a\\r\\a\\m\\e\\t\\e\\r\\}"
134    + "\\c\\o\\n\\t\\e\\n\\t" + "\\{\\m\\a\\c\\r\\o\\}",
135    this.mocker.getComponentUnderTest().escape("{macro:some=parameter}content{macro}", Syntax.XWIKI_1_0));
136    }
137   
 
138  1 toggle @Test
139    public void escape20Syntax() throws Exception
140    {
141    // Since the logic is pretty simple (prepend every character with an escape character), the below tests are
142    // mostly for exemplification.
143  1 Assert.assertEquals("~~", this.mocker.getComponentUnderTest().escape("~", Syntax.XWIKI_2_0));
144  1 Assert.assertEquals("~*~*~t~e~s~t~*~*", this.mocker.getComponentUnderTest()
145    .escape("**test**", Syntax.XWIKI_2_0));
146    // Note: Java escaped string "\\" == "\" (real string).
147  1 Assert.assertEquals("~a~\\~\\~[~[~l~i~n~k~>~>~X~.~Y~]~]",
148    this.mocker.getComponentUnderTest().escape("a\\\\[[link>>X.Y]]", Syntax.XWIKI_2_0));
149  1 Assert.assertEquals("~{~{~{~v~e~r~b~a~t~i~m~}~}~}",
150    this.mocker.getComponentUnderTest().escape("{{{verbatim}}}", Syntax.XWIKI_2_0));
151  1 Assert
152    .assertEquals(
153    "~{~{~m~a~c~r~o~ ~s~o~m~e~=~'~p~a~r~a~m~e~t~e~r~'~}~}~c~o~n~t~e~n~t~{~{~/~m~a~c~r~o~}~}",
154    this.mocker.getComponentUnderTest().escape("{{macro some='parameter'}}content{{/macro}}",
155    Syntax.XWIKI_2_0));
156    }
157   
 
158  1 toggle @Test
159    public void escape21Syntax() throws Exception
160    {
161    // Since the logic is pretty simple (prepend every character with an escape character), the below tests are
162    // mostly for exemplification.
163  1 Assert.assertEquals("~~", this.mocker.getComponentUnderTest().escape("~", Syntax.XWIKI_2_1));
164  1 Assert.assertEquals("~*~*~t~e~s~t~*~*", this.mocker.getComponentUnderTest()
165    .escape("**test**", Syntax.XWIKI_2_1));
166    // Note: Java escaped string "\\" == "\" (real string).
167  1 Assert.assertEquals("~a~\\~\\~[~[~l~i~n~k~>~>~X~.~Y~]~]",
168    this.mocker.getComponentUnderTest().escape("a\\\\[[link>>X.Y]]", Syntax.XWIKI_2_1));
169  1 Assert.assertEquals("~{~{~{~v~e~r~b~a~t~i~m~}~}~}",
170    this.mocker.getComponentUnderTest().escape("{{{verbatim}}}", Syntax.XWIKI_2_1));
171  1 Assert
172    .assertEquals(
173    "~{~{~m~a~c~r~o~ ~s~o~m~e~=~'~p~a~r~a~m~e~t~e~r~'~}~}~c~o~n~t~e~n~t~{~{~/~m~a~c~r~o~}~}",
174    this.mocker.getComponentUnderTest().escape("{{macro some='parameter'}}content{{/macro}}",
175    Syntax.XWIKI_2_1));
176    }
177   
 
178  1 toggle @Test
179    public void escapeSpaces() throws Exception
180    {
181  1 Assert.assertEquals("\\a\\ \\*\\t\\e\\s\\t\\*",
182    this.mocker.getComponentUnderTest().escape("a *test*", Syntax.XWIKI_1_0));
183  1 Assert.assertEquals("~a~ ~*~*~t~e~s~t~*~*",
184    this.mocker.getComponentUnderTest().escape("a **test**", Syntax.XWIKI_2_0));
185  1 Assert.assertEquals("~a~ ~*~*~t~e~s~t~*~*",
186    this.mocker.getComponentUnderTest().escape("a **test**", Syntax.XWIKI_2_1));
187    }
188   
 
189  1 toggle @Test
190    public void escapeNewLines() throws Exception
191    {
192  1 Assert.assertEquals("\\a\\\n\\b", this.mocker.getComponentUnderTest().escape("a\nb", Syntax.XWIKI_1_0));
193  1 Assert.assertEquals("~a~\n~b", this.mocker.getComponentUnderTest().escape("a\nb", Syntax.XWIKI_2_0));
194  1 Assert.assertEquals("~a~\n~b", this.mocker.getComponentUnderTest().escape("a\nb", Syntax.XWIKI_2_1));
195    }
196   
 
197  1 toggle @Test
198    public void escapeWithNullInput() throws Exception
199    {
200  1 Assert.assertNull("Unexpected non-null output for null input",
201    this.mocker.getComponentUnderTest().escape(null, Syntax.XWIKI_2_1));
202    }
203   
 
204  1 toggle @Test
205    public void escapeWithEmptyInput() throws Exception
206    {
207  1 Assert.assertEquals("", this.mocker.getComponentUnderTest().escape("", Syntax.XWIKI_2_1));
208    }
209   
 
210  1 toggle @Test
211    public void escapeWithNullSyntax() throws Exception
212    {
213  1 Assert.assertNull("Unexpected non-null output for null syntax",
214    this.mocker.getComponentUnderTest().escape("anything", null));
215    }
216   
 
217  1 toggle @Test
218    public void escapeWithNullInputAndSyntax() throws Exception
219    {
220  1 Assert.assertNull("Unexpected non-null output for null input and syntax", this.mocker.getComponentUnderTest()
221    .escape(null, null));
222    }
223   
 
224  1 toggle @Test
225    public void escapeWithUnsupportedSyntax() throws Exception
226    {
227  1 Assert.assertNull("Unexpected non-null output for unsupported syntax", this.mocker.getComponentUnderTest()
228    .escape("unsupported", Syntax.XHTML_1_0));
229    }
230    }