1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.block

File BlockTest.java

 
testReplaceBlock: Provided Block to replace is not a child
 

Code metrics

0
157
13
1
354
236
13
0.08
12.08
13
1

Classes

Class Line # Actions
BlockTest 49 157 0% 13 24
0.8588235485.9%
 

Contributing tests

This file is covered by 13 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.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.AbstractMacroBlock;
35    import org.xwiki.rendering.block.MacroBlock;
36    import org.xwiki.rendering.block.MacroMarkerBlock;
37    import org.xwiki.rendering.block.match.AnyBlockMatcher;
38    import org.xwiki.rendering.block.match.BlockNavigatorTest;
39    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
40    import org.xwiki.rendering.listener.reference.ResourceReference;
41    import org.xwiki.rendering.listener.reference.ResourceType;
42   
43    /**
44    * Unit tests for Block manipulation, testing {@link AbstractBlock}.
45    *
46    * @version $Id: b7a4a95823a4e191b7110e75680a1c98a5a6ee58 $
47    * @since 1.5M2
48    */
 
49    public class BlockTest
50    {
51    @Rule
52    public ExpectedException thrown = ExpectedException.none();
53   
 
54  1 toggle @Test
55    public void testInsertChildAfter()
56    {
57  1 Block wb1 = new WordBlock("block1");
58  1 Block wb2 = new WordBlock("block2");
59  1 ParagraphBlock pb = new ParagraphBlock(Arrays.asList(wb1, wb2));
60   
61  1 Block wb = new WordBlock("block");
62   
63  1 pb.insertChildAfter(wb, wb1);
64  1 Assert.assertSame(wb, pb.getChildren().get(1));
65  1 Assert.assertSame(wb1, wb.getPreviousSibling());
66  1 Assert.assertSame(wb2, wb.getNextSibling());
67  1 Assert.assertSame(wb, wb1.getNextSibling());
68  1 Assert.assertSame(wb, wb2.getPreviousSibling());
69   
70  1 pb.insertChildAfter(wb, wb2);
71  1 Assert.assertSame(wb, pb.getChildren().get(3));
72  1 Assert.assertSame(wb2, wb.getPreviousSibling());
73  1 Assert.assertSame(wb, wb2.getNextSibling());
74  1 Assert.assertNull(wb.getNextSibling());
75    }
76   
 
77  1 toggle @Test
78    public void testInsertChildBefore()
79    {
80  1 Block wb1 = new WordBlock("block1");
81  1 Block wb2 = new WordBlock("block2");
82   
83  1 List<Block> children = new ArrayList<Block>();
84  1 children.add(wb1);
85  1 children.add(wb2);
86   
87  1 ParagraphBlock pb = new ParagraphBlock(children);
88   
89  1 Block wb = new WordBlock("block");
90   
91  1 pb.insertChildBefore(wb, wb1);
92  1 Assert.assertSame(wb, pb.getChildren().get(0));
93   
94  1 pb.insertChildBefore(wb, wb2);
95  1 Assert.assertSame(wb, pb.getChildren().get(2));
96    }
97   
 
98  0 toggle @Test
99    public void testReplaceBlock()
100    {
101    // It's important all blocks have same content to make sure replacement api don't find the position of the
102    // old block using Object#equals
103  0 Block word1 = new WordBlock("block1");
104  0 Block word2 = new WordBlock("block2");
105  0 Block word3 = new WordBlock("block3");
106   
107  0 Block parentBlock = new ParagraphBlock(Arrays.asList(word1, word2));
108   
109    // replace by one
110  0 parentBlock.replaceChild(word3, word1);
111   
112  0 Assert.assertEquals(2, parentBlock.getChildren().size());
113  0 Assert.assertSame(word3, parentBlock.getChildren().get(0));
114  0 Assert.assertSame(word2, parentBlock.getChildren().get(1));
115  0 Assert.assertSame(word2, word3.getNextSibling());
116  0 Assert.assertSame(word3, word2.getPreviousSibling());
117   
118    // replace by nothing
119  0 parentBlock.replaceChild(Collections.<Block>emptyList(), word2);
120   
121  0 Assert.assertEquals(1, parentBlock.getChildren().size());
122  0 Assert.assertSame(word3, parentBlock.getChildren().get(0));
123  0 Assert.assertNull(word3.getNextSibling());
124  0 Assert.assertNull(word3.getPreviousSibling());
125   
126    // replace by several
127  0 parentBlock.replaceChild(Arrays.asList(word1, word2), word3);
128   
129  0 Assert.assertEquals(2, parentBlock.getChildren().size());
130  0 Assert.assertSame(word1, parentBlock.getChildren().get(0));
131  0 Assert.assertSame(word2, parentBlock.getChildren().get(1));
132  0 Assert.assertSame(word2, word1.getNextSibling());
133  0 Assert.assertSame(word1, word2.getPreviousSibling());
134   
135    // Provide not existing block to replace
136  0 this.thrown.expect(InvalidParameterException.class);
137  0 Test failure here parentBlock.replaceChild(word3, new WordBlock("not existing"));
138    }
139   
 
140  1 toggle @Test
141    public void testClone()
142    {
143  1 WordBlock wb = new WordBlock("block");
144  1 ImageBlock ib = new ImageBlock(new ResourceReference("document@attachment", ResourceType.ATTACHMENT), true);
145  1 DocumentResourceReference linkReference = new DocumentResourceReference("reference");
146  1 LinkBlock lb = new LinkBlock(Arrays.asList((Block) new WordBlock("label")), linkReference, false);
147  1 Block pb = new ParagraphBlock(Arrays.<Block>asList(wb, ib, lb));
148  1 XDOM rootBlock = new XDOM(Arrays.<Block>asList(pb));
149   
150  1 XDOM newRootBlock = rootBlock.clone();
151   
152  1 Assert.assertNotSame(rootBlock, newRootBlock);
153  1 Assert.assertNotSame(rootBlock.getMetaData(), newRootBlock.getMetaData());
154   
155  1 Block newPB = newRootBlock.getChildren().get(0);
156   
157  1 Assert.assertNotSame(pb, newPB);
158   
159  1 Assert.assertNotSame(wb, newPB.getChildren().get(0));
160  1 Assert.assertNotSame(ib, newPB.getChildren().get(1));
161  1 Assert.assertNotSame(lb, newPB.getChildren().get(2));
162   
163  1 Assert.assertEquals(wb.getWord(), ((WordBlock) newPB.getChildren().get(0)).getWord());
164  1 Assert.assertNotSame(ib.getReference(), ((ImageBlock) newPB.getChildren().get(1)).getReference());
165  1 Assert.assertNotSame(lb.getReference(), ((LinkBlock) newPB.getChildren().get(2)).getReference());
166    }
167   
 
168  1 toggle @Test
169    public void testGetNextSibling()
170    {
171  1 WordBlock b1 = new WordBlock("b1");
172  1 WordBlock b2 = new WordBlock("b2");
173  1 ParagraphBlock p = new ParagraphBlock(Arrays.<Block>asList(b1, b2));
174   
175  1 Assert.assertSame(b2, b1.getNextSibling());
176  1 Assert.assertNull(b2.getNextSibling());
177  1 Assert.assertNull(p.getNextSibling());
178  1 Assert.assertNull(new ParagraphBlock(Collections.<Block>emptyList()).getNextSibling());
179    }
180   
 
181  1 toggle @Test
182    public void testRemoveBlock()
183    {
184  1 WordBlock b1 = new WordBlock("b1");
185  1 WordBlock b1bis = new WordBlock("b1");
186  1 WordBlock b2 = new WordBlock("b2");
187  1 ParagraphBlock p1 = new ParagraphBlock(Arrays.<Block>asList(b1, b1bis, b2));
188   
189  1 p1.removeBlock(b1bis);
190  1 Assert.assertEquals(2, p1.getChildren().size());
191  1 Assert.assertSame(b1, p1.getChildren().get(0));
192  1 Assert.assertSame(b2, p1.getChildren().get(1));
193   
194  1 p1.removeBlock(b1);
195  1 Assert.assertEquals(1, p1.getChildren().size());
196  1 Assert.assertSame(b2, p1.getChildren().get(0));
197  1 Assert.assertNull(b1.getPreviousSibling());
198  1 Assert.assertNull(b1.getNextSibling());
199  1 Assert.assertNull(b2.getPreviousSibling());
200   
201  1 p1.removeBlock(b2);
202  1 Assert.assertEquals(0, p1.getChildren().size());
203  1 Assert.assertNull(b2.getPreviousSibling());
204  1 Assert.assertNull(b2.getNextSibling());
205    }
206   
 
207  1 toggle @Test
208    public void testGetBlocks()
209    {
210  1 Assert.assertEquals(Arrays.asList(BlockNavigatorTest.parentBlock, BlockNavigatorTest.rootBlock),
211    BlockNavigatorTest.contextBlock.getBlocks(AnyBlockMatcher.ANYBLOCKMATCHER, Block.Axes.ANCESTOR));
212    }
213   
 
214  1 toggle @Test
215    public void testGetFirstBlock()
216    {
217  1 Assert.assertSame(BlockNavigatorTest.parentBlock,
218    BlockNavigatorTest.contextBlock.getFirstBlock(AnyBlockMatcher.ANYBLOCKMATCHER, Block.Axes.ANCESTOR));
219    }
220   
 
221  1 toggle @Test
222    public void testSetChildren()
223    {
224  1 ParagraphBlock paragraphBlock = new ParagraphBlock(Collections.EMPTY_LIST);
225   
226  1 List<Block> blocks = Arrays.<Block>asList(new WordBlock("1"), new WordBlock("2"));
227  1 paragraphBlock.setChildren(blocks);
228   
229  1 Assert.assertArrayEquals(blocks.toArray(), paragraphBlock.getChildren().toArray());
230   
231  1 blocks = Arrays.<Block>asList(new WordBlock("3"), new WordBlock("4"));
232  1 paragraphBlock.setChildren(blocks);
233   
234  1 Assert.assertArrayEquals(blocks.toArray(), paragraphBlock.getChildren().toArray());
235   
236  1 blocks = Arrays.<Block>asList();
237  1 paragraphBlock.setChildren(blocks);
238   
239  1 Assert.assertArrayEquals(blocks.toArray(), paragraphBlock.getChildren().toArray());
240    }
241   
 
242  1 toggle @Test
243    public void testSetGetParameter()
244    {
245  1 WordBlock wordBlock = new WordBlock("word");
246   
247  1 wordBlock.setParameter("param", "value");
248   
249  1 Assert.assertEquals("value", wordBlock.getParameter("param"));
250   
251  1 wordBlock.setParameter("param", "value2");
252   
253  1 Assert.assertEquals("value2", wordBlock.getParameter("param"));
254    }
255   
 
256  1 toggle @Test
257    public void testSetGetParameters()
258    {
259  1 WordBlock wordBlock = new WordBlock("word");
260   
261  1 Map<String, String> parameters = new HashMap<String, String>();
262  1 parameters.put("param1", "value1");
263  1 parameters.put("param2", "value2");
264   
265  1 wordBlock.setParameters(parameters);
266   
267  1 Assert.assertEquals(parameters, wordBlock.getParameters());
268   
269  1 Map<String, String> parameters2 = new HashMap<String, String>();
270  1 parameters.put("param21", "value21");
271  1 parameters.put("param22", "value22");
272   
273  1 wordBlock.setParameters(parameters2);
274   
275  1 Assert.assertEquals(parameters2, wordBlock.getParameters());
276    }
277   
 
278  1 toggle @Test
279    public void testGetRoot()
280    {
281  1 Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.rootBlock.getRoot());
282  1 Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.contextBlock.getRoot());
283  1 Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.contextBlockChild1.getRoot());
284  1 Assert.assertSame(BlockNavigatorTest.rootBlock, BlockNavigatorTest.contextBlockChild11.getRoot());
285    }
286   
 
287  1 toggle @Test
288    public void testAbstractBlockEquals()
289    {
290  1 final String ID = "Test id";
291  1 final String CONTENT = "Test content";
292  1 final boolean IS_INLINE = true;
293  1 final Map<String, String> PARAMETERS = new HashMap<>();
294   
295  1 PARAMETERS.put("TestKey", "TestValue");
296   
297  1 AbstractMacroBlock macroBlock1, macroBlock2, macroBlock3;
298   
299  1 macroBlock1 = new MacroBlock(ID, PARAMETERS, CONTENT, IS_INLINE);
300  1 macroBlock2 = new MacroBlock(ID, PARAMETERS, CONTENT, IS_INLINE);
301  1 macroBlock3 = new MacroBlock(ID, PARAMETERS, CONTENT, IS_INLINE);
302   
303    //must be reflexive.
304  1 Assert.assertEquals(macroBlock1, macroBlock1);
305   
306    //must be symmetric.
307  1 Assert.assertEquals(macroBlock1, macroBlock2);
308  1 Assert.assertEquals(macroBlock2, macroBlock1);
309   
310    //must be transitive.
311  1 Assert.assertEquals(macroBlock1, macroBlock2);
312  1 Assert.assertEquals(macroBlock2, macroBlock3);
313  1 Assert.assertEquals(macroBlock1, macroBlock3);
314   
315    //must be consistent (already checked).
316   
317    //equals(null) == false.
318  1 Assert.assertFalse(macroBlock1.equals(null));
319  1 Assert.assertFalse(macroBlock2.equals(null));
320  1 Assert.assertFalse(macroBlock3.equals(null));
321   
322    //hashCode must be equal.
323  1 Assert.assertEquals(macroBlock1.hashCode(), macroBlock2.hashCode());
324   
325  1 List<Block> childBlocks = new ArrayList<>();
326  1 AbstractMacroBlock macroMarkerBlock1, macroMarkerBlock2, macroMarkerBlock3;
327   
328  1 macroMarkerBlock1 = new MacroBlock(ID, PARAMETERS, CONTENT, IS_INLINE);
329  1 macroMarkerBlock2 = new MacroBlock(ID, PARAMETERS, CONTENT, IS_INLINE);
330  1 macroMarkerBlock3 = new MacroBlock(ID, PARAMETERS, CONTENT, IS_INLINE);
331   
332    //must be reflexive.
333  1 Assert.assertEquals(macroMarkerBlock1, macroMarkerBlock1);
334   
335    //must be symmetric.
336  1 Assert.assertEquals(macroMarkerBlock1, macroMarkerBlock2);
337  1 Assert.assertEquals(macroMarkerBlock2, macroMarkerBlock1);
338   
339    //must be transitive.
340  1 Assert.assertEquals(macroMarkerBlock1, macroMarkerBlock2);
341  1 Assert.assertEquals(macroMarkerBlock2, macroMarkerBlock3);
342  1 Assert.assertEquals(macroMarkerBlock1, macroMarkerBlock3);
343   
344    //must be consistent (already checked).
345   
346    //equals(null) == false.
347  1 Assert.assertFalse(macroMarkerBlock1.equals(null));
348  1 Assert.assertFalse(macroMarkerBlock2.equals(null));
349  1 Assert.assertFalse(macroMarkerBlock3.equals(null));
350   
351    //hashCode must be equal.
352  1 Assert.assertEquals(macroMarkerBlock1.hashCode(), macroMarkerBlock2.hashCode());
353    }
354    }