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

File BlockNavigatorTest.java

 

Code metrics

0
48
9
1
199
140
9
0.19
5.33
9
1

Classes

Class Line # Actions
BlockNavigatorTest 30 48 0% 9 14
0.7543859575.4%
 

Contributing tests

This file is covered by 2 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.match;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Assert;
25    import org.junit.Test;
26    import org.xwiki.rendering.block.Block;
27    import org.xwiki.rendering.block.ParagraphBlock;
28    import org.xwiki.rendering.block.WordBlock;
29   
 
30    public class BlockNavigatorTest
31    {
32    public static final WordBlock precedingBlockChild1 = new WordBlock("pc1");
33   
34    public static final WordBlock precedingBlockChild2 = new WordBlock("pc2");
35   
36    public static final ParagraphBlock precedingBlock = new ParagraphBlock(Arrays.<Block>asList(precedingBlockChild1,
37    precedingBlockChild2))
38    {
 
39  0 toggle @Override
40    public String toString()
41    {
42  0 return "precedingBlock";
43    }
44    };
45   
46    public static final WordBlock contextBlockChild21 = new WordBlock("cc21");
47   
48    public static final WordBlock contextBlockChild22 = new WordBlock("cc22");
49   
50    public static final ParagraphBlock contextBlockChild2 = new ParagraphBlock(Arrays.<Block>asList(
51    contextBlockChild21, contextBlockChild22))
52    {
 
53  0 toggle @Override
54    public String toString()
55    {
56  0 return "contextBlockChild2";
57    }
58    };
59   
60    public static final WordBlock contextBlockChild11 = new WordBlock("cc11");
61   
62    public static final WordBlock contextBlockChild12 = new WordBlock("cc12");
63   
64    public static final ParagraphBlock contextBlockChild1 = new ParagraphBlock(Arrays.<Block>asList(
65    contextBlockChild11, contextBlockChild12))
66    {
 
67  0 toggle @Override
68    public String toString()
69    {
70  0 return "contextBlockChild1";
71    }
72    };
73   
74    public static final ParagraphBlock contextBlock = new ParagraphBlock(Arrays.<Block>asList(contextBlockChild1,
75    contextBlockChild2))
76    {
 
77  0 toggle @Override
78    public String toString()
79    {
80  0 return "contextBlock";
81    }
82    };
83   
84    public static final WordBlock followingBlockChild1 = new WordBlock("fc1");
85   
86    public static final WordBlock followingBlockChild2 = new WordBlock("fc2");
87   
88    public static final ParagraphBlock followingBlock = new ParagraphBlock(Arrays.<Block>asList(followingBlockChild1,
89    followingBlockChild2))
90    {
 
91  0 toggle @Override
92    public String toString()
93    {
94  0 return "followingBlock";
95    }
96    };
97   
98    public static final ParagraphBlock parentBlock = new ParagraphBlock(Arrays.<Block>asList(precedingBlock,
99    contextBlock, followingBlock))
100    {
 
101  0 toggle @Override
102    public String toString()
103    {
104  0 return "parentBlock";
105    }
106    };
107   
108    public static final ParagraphBlock rootBlock = new ParagraphBlock(Arrays.<Block>asList(parentBlock))
109    {
 
110  0 toggle @Override
111    public String toString()
112    {
113  0 return "rootBlock";
114    }
115    };
116   
 
117  1 toggle @Test
118    public void testGetBlocks()
119    {
120  1 BlockNavigator navigator = new BlockNavigator();
121   
122  1 Assert.assertEquals(Arrays.asList(parentBlock, rootBlock),
123    navigator.getBlocks(contextBlock, Block.Axes.ANCESTOR));
124  1 Assert.assertEquals(Arrays.asList(contextBlock, parentBlock, rootBlock),
125    navigator.getBlocks(contextBlock, Block.Axes.ANCESTOR_OR_SELF));
126  1 Assert.assertEquals(Arrays.asList(contextBlockChild1, contextBlockChild2),
127    navigator.getBlocks(contextBlock, Block.Axes.CHILD));
128  1 Assert.assertEquals(Arrays.asList(contextBlockChild1, contextBlockChild11, contextBlockChild12,
129    contextBlockChild2, contextBlockChild21, contextBlockChild22), navigator.getBlocks(contextBlock,
130    Block.Axes.DESCENDANT));
131  1 Assert.assertEquals(Arrays.asList(contextBlock, contextBlockChild1, contextBlockChild11, contextBlockChild12,
132    contextBlockChild2, contextBlockChild21, contextBlockChild22), navigator.getBlocks(contextBlock,
133    Block.Axes.DESCENDANT_OR_SELF));
134  1 Assert.assertEquals(Arrays.asList(followingBlock, followingBlockChild1, followingBlockChild2),
135    navigator.getBlocks(contextBlock, Block.Axes.FOLLOWING));
136  1 Assert.assertEquals(Arrays.asList(followingBlock),
137    navigator.getBlocks(contextBlock, Block.Axes.FOLLOWING_SIBLING));
138  1 Assert.assertEquals(Arrays.asList(parentBlock), navigator.getBlocks(contextBlock, Block.Axes.PARENT));
139  1 Assert.assertEquals(Arrays.asList(precedingBlock, precedingBlockChild1, precedingBlockChild2),
140    navigator.getBlocks(contextBlock, Block.Axes.PRECEDING));
141  1 Assert.assertEquals(Arrays.asList(precedingBlock),
142    navigator.getBlocks(contextBlock, Block.Axes.PRECEDING_SIBLING));
143  1 Assert.assertEquals(Arrays.asList(contextBlock), navigator.getBlocks(contextBlock, Block.Axes.SELF));
144    }
145   
 
146  1 toggle @Test
147    public void testGetFirstBlock()
148    {
149  1 BlockNavigator navigator = new BlockNavigator();
150   
151  1 Assert.assertSame(parentBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR));
152  1 Assert.assertSame(contextBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR_OR_SELF));
153  1 Assert.assertSame(contextBlockChild1, navigator.getFirstBlock(contextBlock, Block.Axes.CHILD));
154  1 Assert.assertSame(contextBlockChild1, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT));
155  1 Assert.assertSame(contextBlock, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT_OR_SELF));
156  1 Assert.assertSame(followingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING));
157   
158  1 Assert.assertSame(followingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING_SIBLING));
159   
160  1 Assert.assertSame(parentBlock, navigator.getFirstBlock(contextBlock, Block.Axes.PARENT));
161  1 Assert.assertSame(precedingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING));
162   
163  1 Assert.assertSame(precedingBlock, navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING_SIBLING));
164   
165  1 Assert.assertSame(contextBlock, navigator.getFirstBlock(contextBlock, Block.Axes.SELF));
166   
167    // SameBlockMatcher
168   
169  1 navigator = new BlockNavigator(new SameBlockMatcher(rootBlock));
170   
171  1 Assert.assertSame(rootBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR_OR_SELF));
172  1 Assert.assertSame(rootBlock, navigator.getFirstBlock(contextBlock, Block.Axes.ANCESTOR));
173   
174  1 navigator = new BlockNavigator(new SameBlockMatcher(contextBlockChild22));
175   
176  1 Assert.assertSame(contextBlockChild22, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT));
177   
178  1 Assert.assertSame(contextBlockChild22, navigator.getFirstBlock(contextBlock, Block.Axes.DESCENDANT_OR_SELF));
179   
180  1 navigator = new BlockNavigator(new SameBlockMatcher(followingBlockChild2));
181   
182  1 Assert.assertSame(followingBlockChild2, navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING));
183   
184  1 navigator = new BlockNavigator(new SameBlockMatcher(contextBlockChild2));
185   
186  1 Assert.assertSame(contextBlockChild2, navigator.getFirstBlock(contextBlock, Block.Axes.CHILD));
187   
188  1 navigator = new BlockNavigator(new SameBlockMatcher(precedingBlockChild2));
189   
190  1 Assert.assertSame(precedingBlockChild2, navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING));
191   
192  1 navigator = new BlockNavigator(new SameBlockMatcher(new WordBlock("unexistingBlock")));
193   
194  1 Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.PRECEDING_SIBLING));
195  1 Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.PARENT));
196  1 Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.FOLLOWING_SIBLING));
197  1 Assert.assertNull(navigator.getFirstBlock(contextBlock, Block.Axes.SELF));
198    }
199    }