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

File BlockTest.java

 
testReplaceBlock: Provided Block to replace is not a child
 

Code metrics

0
123
12
1
283
195
12
0.1
10.25
12
1

Classes

Class Line # Actions
BlockTest 46 123 0% 12 24
0.8222222382.2%
 

Contributing tests

No tests hitting this source file were found.

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.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    * Unit tests for Block manipulation, testing {@link AbstractBlock}.
42    *
43    * @version $Id: 671f07ffd696036c0e308d9ee8e7db4d8445670f $
44    * @since 1.5M2
45    */
 
46    public class BlockTest
47    {
48    @Rule
49    public ExpectedException thrown = ExpectedException.none();
50   
 
51  1 toggle @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   
 
74  1 toggle @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   
 
95  0 toggle @Test
96    public void testReplaceBlock()
97    {
98    // It's important all blocks have same content to make sure replacement api don't find the position of the
99    // old block using Object#equals
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    // replace by one
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    // replace by nothing
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    // replace by several
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    // Provide not existing block to replace
133  0 this.thrown.expect(InvalidParameterException.class);
134  0 Test failure here parentBlock.replaceChild(word3, new WordBlock("not existing"));
135    }
136   
 
137  1 toggle @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   
 
165  1 toggle @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   
 
178  1 toggle @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   
 
204  1 toggle @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   
 
211  1 toggle @Test
212    public void testGetFirstBlock()
213    {
214  1 Assert.assertSame(BlockNavigatorTest.parentBlock,
215    BlockNavigatorTest.contextBlock.getFirstBlock(AnyBlockMatcher.ANYBLOCKMATCHER, Block.Axes.ANCESTOR));
216    }
217   
 
218  1 toggle @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   
 
239  1 toggle @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   
 
253  1 toggle @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   
 
275  1 toggle @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    }