1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.block; |
21 |
|
|
22 |
|
import java.security.InvalidParameterException; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import org.junit.Assert; |
31 |
|
import org.junit.Rule; |
32 |
|
import org.junit.Test; |
33 |
|
import org.junit.rules.ExpectedException; |
34 |
|
import org.xwiki.rendering.block.match.AnyBlockMatcher; |
35 |
|
import org.xwiki.rendering.block.match.BlockNavigatorTest; |
36 |
|
import org.xwiki.rendering.listener.reference.DocumentResourceReference; |
37 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
38 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
39 |
|
|
40 |
|
|
41 |
|
@link |
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 82.2% |
Uncovered Elements: 24 (135) |
Complexity: 12 |
Complexity Density: 0.1 |
|
46 |
|
public class BlockTest |
47 |
|
{ |
48 |
|
@Rule |
49 |
|
public ExpectedException thrown = ExpectedException.none(); |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
51 |
1 |
@Test... |
52 |
|
public void testInsertChildAfter() |
53 |
|
{ |
54 |
1 |
Block wb1 = new WordBlock("block1"); |
55 |
1 |
Block wb2 = new WordBlock("block2"); |
56 |
1 |
ParagraphBlock pb = new ParagraphBlock(Arrays.asList(wb1, wb2)); |
57 |
|
|
58 |
1 |
Block wb = new WordBlock("block"); |
59 |
|
|
60 |
1 |
pb.insertChildAfter(wb, wb1); |
61 |
1 |
Assert.assertSame(wb, pb.getChildren().get(1)); |
62 |
1 |
Assert.assertSame(wb1, wb.getPreviousSibling()); |
63 |
1 |
Assert.assertSame(wb2, wb.getNextSibling()); |
64 |
1 |
Assert.assertSame(wb, wb1.getNextSibling()); |
65 |
1 |
Assert.assertSame(wb, wb2.getPreviousSibling()); |
66 |
|
|
67 |
1 |
pb.insertChildAfter(wb, wb2); |
68 |
1 |
Assert.assertSame(wb, pb.getChildren().get(3)); |
69 |
1 |
Assert.assertSame(wb2, wb.getPreviousSibling()); |
70 |
1 |
Assert.assertSame(wb, wb2.getNextSibling()); |
71 |
1 |
Assert.assertNull(wb.getNextSibling()); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void testInsertChildBefore() |
76 |
|
{ |
77 |
1 |
Block wb1 = new WordBlock("block1"); |
78 |
1 |
Block wb2 = new WordBlock("block2"); |
79 |
|
|
80 |
1 |
List<Block> children = new ArrayList<Block>(); |
81 |
1 |
children.add(wb1); |
82 |
1 |
children.add(wb2); |
83 |
|
|
84 |
1 |
ParagraphBlock pb = new ParagraphBlock(children); |
85 |
|
|
86 |
1 |
Block wb = new WordBlock("block"); |
87 |
|
|
88 |
1 |
pb.insertChildBefore(wb, wb1); |
89 |
1 |
Assert.assertSame(wb, pb.getChildren().get(0)); |
90 |
|
|
91 |
1 |
pb.insertChildBefore(wb, wb2); |
92 |
1 |
Assert.assertSame(wb, pb.getChildren().get(2)); |
93 |
|
} |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 1 |
Complexity Density: 0.04 |
3FAIL
|
|
95 |
0 |
@Test... |
96 |
|
public void testReplaceBlock() |
97 |
|
{ |
98 |
|
|
99 |
|
|
100 |
0 |
Block word1 = new WordBlock("block1"); |
101 |
0 |
Block word2 = new WordBlock("block2"); |
102 |
0 |
Block word3 = new WordBlock("block3"); |
103 |
|
|
104 |
0 |
Block parentBlock = new ParagraphBlock(Arrays.asList(word1, word2)); |
105 |
|
|
106 |
|
|
107 |
0 |
parentBlock.replaceChild(word3, word1); |
108 |
|
|
109 |
0 |
Assert.assertEquals(2, parentBlock.getChildren().size()); |
110 |
0 |
Assert.assertSame(word3, parentBlock.getChildren().get(0)); |
111 |
0 |
Assert.assertSame(word2, parentBlock.getChildren().get(1)); |
112 |
0 |
Assert.assertSame(word2, word3.getNextSibling()); |
113 |
0 |
Assert.assertSame(word3, word2.getPreviousSibling()); |
114 |
|
|
115 |
|
|
116 |
0 |
parentBlock.replaceChild(Collections.<Block>emptyList(), word2); |
117 |
|
|
118 |
0 |
Assert.assertEquals(1, parentBlock.getChildren().size()); |
119 |
0 |
Assert.assertSame(word3, parentBlock.getChildren().get(0)); |
120 |
0 |
Assert.assertNull(word3.getNextSibling()); |
121 |
0 |
Assert.assertNull(word3.getPreviousSibling()); |
122 |
|
|
123 |
|
|
124 |
0 |
parentBlock.replaceChild(Arrays.asList(word1, word2), word3); |
125 |
|
|
126 |
0 |
Assert.assertEquals(2, parentBlock.getChildren().size()); |
127 |
0 |
Assert.assertSame(word1, parentBlock.getChildren().get(0)); |
128 |
0 |
Assert.assertSame(word2, parentBlock.getChildren().get(1)); |
129 |
0 |
Assert.assertSame(word2, word1.getNextSibling()); |
130 |
0 |
Assert.assertSame(word1, word2.getPreviousSibling()); |
131 |
|
|
132 |
|
|
133 |
0 |
this.thrown.expect(InvalidParameterException.class); |
134 |
0 |
parentBlock.replaceChild(word3, new WordBlock("not existing")); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void testClone() |
139 |
|
{ |
140 |
1 |
WordBlock wb = new WordBlock("block"); |
141 |
1 |
ImageBlock ib = new ImageBlock(new ResourceReference("document@attachment", ResourceType.ATTACHMENT), true); |
142 |
1 |
DocumentResourceReference linkReference = new DocumentResourceReference("reference"); |
143 |
1 |
LinkBlock lb = new LinkBlock(Arrays.asList((Block) new WordBlock("label")), linkReference, false); |
144 |
1 |
Block pb = new ParagraphBlock(Arrays.<Block>asList(wb, ib, lb)); |
145 |
1 |
XDOM rootBlock = new XDOM(Arrays.<Block>asList(pb)); |
146 |
|
|
147 |
1 |
XDOM newRootBlock = rootBlock.clone(); |
148 |
|
|
149 |
1 |
Assert.assertNotSame(rootBlock, newRootBlock); |
150 |
1 |
Assert.assertNotSame(rootBlock.getMetaData(), newRootBlock.getMetaData()); |
151 |
|
|
152 |
1 |
Block newPB = newRootBlock.getChildren().get(0); |
153 |
|
|
154 |
1 |
Assert.assertNotSame(pb, newPB); |
155 |
|
|
156 |
1 |
Assert.assertNotSame(wb, newPB.getChildren().get(0)); |
157 |
1 |
Assert.assertNotSame(ib, newPB.getChildren().get(1)); |
158 |
1 |
Assert.assertNotSame(lb, newPB.getChildren().get(2)); |
159 |
|
|
160 |
1 |
Assert.assertEquals(wb.getWord(), ((WordBlock) newPB.getChildren().get(0)).getWord()); |
161 |
1 |
Assert.assertNotSame(ib.getReference(), ((ImageBlock) newPB.getChildren().get(1)).getReference()); |
162 |
1 |
Assert.assertNotSame(lb.getReference(), ((LinkBlock) newPB.getChildren().get(2)).getReference()); |
163 |
|
} |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
165 |
1 |
@Test... |
166 |
|
public void testGetNextSibling() |
167 |
|
{ |
168 |
1 |
WordBlock b1 = new WordBlock("b1"); |
169 |
1 |
WordBlock b2 = new WordBlock("b2"); |
170 |
1 |
ParagraphBlock p = new ParagraphBlock(Arrays.<Block>asList(b1, b2)); |
171 |
|
|
172 |
1 |
Assert.assertSame(b2, b1.getNextSibling()); |
173 |
1 |
Assert.assertNull(b2.getNextSibling()); |
174 |
1 |
Assert.assertNull(p.getNextSibling()); |
175 |
1 |
Assert.assertNull(new ParagraphBlock(Collections.<Block>emptyList()).getNextSibling()); |
176 |
|
} |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
178 |
1 |
@Test... |
179 |
|
public void testRemoveBlock() |
180 |
|
{ |
181 |
1 |
WordBlock b1 = new WordBlock("b1"); |
182 |
1 |
WordBlock b1bis = new WordBlock("b1"); |
183 |
1 |
WordBlock b2 = new WordBlock("b2"); |
184 |
1 |
ParagraphBlock p1 = new ParagraphBlock(Arrays.<Block>asList(b1, b1bis, b2)); |
185 |
|
|
186 |
1 |
p1.removeBlock(b1bis); |
187 |
1 |
Assert.assertEquals(2, p1.getChildren().size()); |
188 |
1 |
Assert.assertSame(b1, p1.getChildren().get(0)); |
189 |
1 |
Assert.assertSame(b2, p1.getChildren().get(1)); |
190 |
|
|
191 |
1 |
p1.removeBlock(b1); |
192 |
1 |
Assert.assertEquals(1, p1.getChildren().size()); |
193 |
1 |
Assert.assertSame(b2, p1.getChildren().get(0)); |
194 |
1 |
Assert.assertNull(b1.getPreviousSibling()); |
195 |
1 |
Assert.assertNull(b1.getNextSibling()); |
196 |
1 |
Assert.assertNull(b2.getPreviousSibling()); |
197 |
|
|
198 |
1 |
p1.removeBlock(b2); |
199 |
1 |
Assert.assertEquals(0, p1.getChildren().size()); |
200 |
1 |
Assert.assertNull(b2.getPreviousSibling()); |
201 |
1 |
Assert.assertNull(b2.getNextSibling()); |
202 |
|
} |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
204 |
1 |
@Test... |
205 |
|
public void testGetBlocks() |
206 |
|
{ |
207 |
1 |
Assert.assertEquals(Arrays.asList(BlockNavigatorTest.parentBlock, BlockNavigatorTest.rootBlock), |
208 |
|
BlockNavigatorTest.contextBlock.getBlocks(AnyBlockMatcher.ANYBLOCKMATCHER, Block.Axes.ANCESTOR)); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void testGetFirstBlock() |
213 |
|
{ |
214 |
1 |
Assert.assertSame(BlockNavigatorTest.parentBlock, |
215 |
|
BlockNavigatorTest.contextBlock.getFirstBlock(AnyBlockMatcher.ANYBLOCKMATCHER, Block.Axes.ANCESTOR)); |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
218 |
1 |
@Test... |
219 |
|
public void testSetChildren() |
220 |
|
{ |
221 |
1 |
ParagraphBlock paragraphBlock = new ParagraphBlock(Collections.EMPTY_LIST); |
222 |
|
|
223 |
1 |
List<Block> blocks = Arrays.<Block>asList(new WordBlock("1"), new WordBlock("2")); |
224 |
1 |
paragraphBlock.setChildren(blocks); |
225 |
|
|
226 |
1 |
Assert.assertArrayEquals(blocks.toArray(), paragraphBlock.getChildren().toArray()); |
227 |
|
|
228 |
1 |
blocks = Arrays.<Block>asList(new WordBlock("3"), new WordBlock("4")); |
229 |
1 |
paragraphBlock.setChildren(blocks); |
230 |
|
|
231 |
1 |
Assert.assertArrayEquals(blocks.toArray(), paragraphBlock.getChildren().toArray()); |
232 |
|
|
233 |
1 |
blocks = Arrays.<Block>asList(); |
234 |
1 |
paragraphBlock.setChildren(blocks); |
235 |
|
|
236 |
1 |
Assert.assertArrayEquals(blocks.toArray(), paragraphBlock.getChildren().toArray()); |
237 |
|
} |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
239 |
1 |
@Test... |
240 |
|
public void testSetGetParameter() |
241 |
|
{ |
242 |
1 |
WordBlock wordBlock = new WordBlock("word"); |
243 |
|
|
244 |
1 |
wordBlock.setParameter("param", "value"); |
245 |
|
|
246 |
1 |
Assert.assertEquals("value", wordBlock.getParameter("param")); |
247 |
|
|
248 |
1 |
wordBlock.setParameter("param", "value2"); |
249 |
|
|
250 |
1 |
Assert.assertEquals("value2", wordBlock.getParameter("param")); |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
253 |
1 |
@Test... |
254 |
|
public void testSetGetParameters() |
255 |
|
{ |
256 |
1 |
WordBlock wordBlock = new WordBlock("word"); |
257 |
|
|
258 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
259 |
1 |
parameters.put("param1", "value1"); |
260 |
1 |
parameters.put("param2", "value2"); |
261 |
|
|
262 |
1 |
wordBlock.setParameters(parameters); |
263 |
|
|
264 |
1 |
Assert.assertEquals(parameters, wordBlock.getParameters()); |
265 |
|
|
266 |
1 |
Map<String, String> parameters2 = new HashMap<String, String>(); |
267 |
1 |
parameters.put("param21", "value21"); |
268 |
1 |
parameters.put("param22", "value22"); |
269 |
|
|
270 |
1 |
wordBlock.setParameters(parameters2); |
271 |
|
|
272 |
1 |
Assert.assertEquals(parameters2, wordBlock.getParameters()); |
273 |
|
} |
274 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
275 |
1 |
@Test... |
276 |
|
public void testGetRoot() |
277 |
|
{ |
278 |
1 |
Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.rootBlock.getRoot()); |
279 |
1 |
Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.contextBlock.getRoot()); |
280 |
1 |
Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.contextBlockChild1.getRoot()); |
281 |
1 |
Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.contextBlockChild11.getRoot()); |
282 |
|
} |
283 |
|
} |