| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 48 |
|
|
| 49 |
|
@version |
| 50 |
|
@since |
| 51 |
|
|
| 52 |
|
@ComponentList({ContextComponentManagerProvider.class}) |
| |
|
| 100% |
Uncovered Elements: 0 (64) |
Complexity: 16 |
Complexity Density: 0.33 |
|
| 53 |
|
public class RenderingScriptServiceTest |
| 54 |
|
{ |
| 55 |
|
@Rule |
| 56 |
|
public MockitoComponentMockingRule<RenderingScriptService> mocker = new MockitoComponentMockingRule<>( |
| 57 |
|
RenderingScriptService.class); |
| 58 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 59 |
1 |
@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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 69 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 82 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 91 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 102 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 111 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 120 |
1 |
@Test... |
| 121 |
|
public void escape10Syntax() throws Exception |
| 122 |
|
{ |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 138 |
1 |
@Test... |
| 139 |
|
public void escape20Syntax() throws Exception |
| 140 |
|
{ |
| 141 |
|
|
| 142 |
|
|
| 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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 158 |
1 |
@Test... |
| 159 |
|
public void escape21Syntax() throws Exception |
| 160 |
|
{ |
| 161 |
|
|
| 162 |
|
|
| 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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 178 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 189 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 197 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 204 |
1 |
@Test... |
| 205 |
|
public void escapeWithEmptyInput() throws Exception |
| 206 |
|
{ |
| 207 |
1 |
Assert.assertEquals("", this.mocker.getComponentUnderTest().escape("", Syntax.XWIKI_2_1)); |
| 208 |
|
} |
| 209 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 210 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 217 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 224 |
1 |
@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 |
|
} |