1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.refactoring.internal.splitter

File DefaultDocumentSplitterTest.java

 

Code metrics

0
61
2
1
172
109
2
0.03
30.5
2
1

Classes

Class Line # Actions
DefaultDocumentSplitterTest 60 61 0% 2 0
1.0100%
 

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.refactoring.internal.splitter;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.refactoring.WikiDocument;
28    import org.xwiki.refactoring.splitter.DocumentSplitter;
29    import org.xwiki.refactoring.splitter.criterion.SplittingCriterion;
30    import org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion;
31    import org.xwiki.rendering.block.Block;
32    import org.xwiki.rendering.block.Block.Axes;
33    import org.xwiki.rendering.block.HeaderBlock;
34    import org.xwiki.rendering.block.IdBlock;
35    import org.xwiki.rendering.block.LinkBlock;
36    import org.xwiki.rendering.block.ParagraphBlock;
37    import org.xwiki.rendering.block.SectionBlock;
38    import org.xwiki.rendering.block.WordBlock;
39    import org.xwiki.rendering.block.XDOM;
40    import org.xwiki.rendering.block.match.ClassBlockMatcher;
41    import org.xwiki.rendering.listener.HeaderLevel;
42    import org.xwiki.rendering.listener.reference.ResourceReference;
43    import org.xwiki.rendering.listener.reference.ResourceType;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertSame;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.ArgumentMatchers.any;
50    import static org.mockito.ArgumentMatchers.anyInt;
51    import static org.mockito.Mockito.mock;
52    import static org.mockito.Mockito.when;
53   
54    /**
55    * Test case for {@link DefaultDocumentSplitter}.
56    *
57    * @version $Id: b6ffff014eb95887fa6b3ecfa2d783129d680415 $
58    * @since 1.9M1
59    */
 
60    public class DefaultDocumentSplitterTest
61    {
62    /**
63    * A component manager that automatically mocks all dependencies of the component under test.
64    */
65    @Rule
66    public MockitoComponentMockingRule<DocumentSplitter> mocker = new MockitoComponentMockingRule<DocumentSplitter>(
67    DefaultDocumentSplitter.class);
68   
 
69  1 toggle @Test
70    public void split() throws Exception
71    {
72  1 SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
73  1 when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(true, false, false, true);
74  1 when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(true, false, false, false);
75   
76  1 NamingCriterion namingCriterion = mock(NamingCriterion.class);
77  1 when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
78   
79    // = Chapter 1 =
80    // Once upon a time..
81    // == Section 1.1 ==
82    // In a kingdom far away..
83   
84  1 HeaderBlock header = new HeaderBlock(Arrays.<Block> asList(new WordBlock("Section 1.1")), HeaderLevel.LEVEL2);
85  1 ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block> asList(new WordBlock("In a kingdom far away..")));
86  1 SectionBlock subSection = new SectionBlock(Arrays.<Block> asList(header, paragraph));
87   
88  1 header = new HeaderBlock(Arrays.<Block> asList(new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
89  1 paragraph = new ParagraphBlock(Arrays.<Block> asList(new WordBlock("Once upon a time..")));
90  1 SectionBlock section = new SectionBlock(Arrays.<Block> asList(header, paragraph, subSection));
91   
92  1 XDOM xdom = new XDOM(Arrays.<Block> asList(section));
93  1 WikiDocument document = new WikiDocument("Space.Page", xdom, null);
94   
95  1 List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
96   
97  1 assertEquals(3, result.size());
98  1 assertSame(document, result.get(0));
99  1 assertEquals("Child1", result.get(1).getFullName());
100  1 assertSame(document, result.get(1).getParent());
101  1 assertEquals("Child2", result.get(2).getFullName());
102  1 assertSame(result.get(1), result.get(2).getParent());
103   
104  1 ClassBlockMatcher headerMatcher = new ClassBlockMatcher(HeaderBlock.class);
105  1 ClassBlockMatcher linkMatcher = new ClassBlockMatcher(LinkBlock.class);
106  1 assertTrue(document.getXdom().getBlocks(headerMatcher, Axes.DESCENDANT).isEmpty());
107  1 assertEquals(1, document.getXdom().getBlocks(linkMatcher, Axes.DESCENDANT).size());
108  1 assertEquals(1, result.get(1).getXdom().getBlocks(headerMatcher, Axes.DESCENDANT).size());
109  1 assertEquals(1, result.get(1).getXdom().getBlocks(linkMatcher, Axes.DESCENDANT).size());
110  1 assertEquals(1, result.get(2).getXdom().getBlocks(headerMatcher, Axes.DESCENDANT).size());
111  1 assertTrue(result.get(2).getXdom().getBlocks(linkMatcher, Axes.DESCENDANT).isEmpty());
112    }
113   
 
114  1 toggle @Test
115    public void updateAnchors() throws Exception
116    {
117  1 SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
118  1 when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(false, false, true, true);
119  1 when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(false, false, false);
120   
121  1 NamingCriterion namingCriterion = mock(NamingCriterion.class);
122  1 when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
123   
124    // [[link>>||anchor="chapter1"]]
125  1 ResourceReference reference = new ResourceReference("", ResourceType.DOCUMENT);
126  1 reference.setParameter("anchor", "chapter1");
127  1 LinkBlock link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
128  1 ParagraphBlock firstParagraph = new ParagraphBlock(Arrays.<Block>asList(link));
129   
130    // [[link>>path:||anchor="chapter1"]]
131  1 reference = new ResourceReference("", ResourceType.PATH);
132  1 reference.setParameter("anchor", "chapter1");
133  1 link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
134  1 ParagraphBlock secondParagraph = new ParagraphBlock(Arrays.<Block>asList(link));
135   
136    // = {{id name="chapter1"}}Chapter 1 =
137  1 HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new IdBlock("chapter1"), new WordBlock("Chapter 1")),
138    HeaderLevel.LEVEL1);
139  1 SectionBlock section1 = new SectionBlock(Arrays.<Block>asList(header));
140   
141    // = Chapter 2 ==
142    // [[link>>path:#chapter1]]
143  1 header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 2")), HeaderLevel.LEVEL1);
144  1 reference = new ResourceReference("#chapter1", ResourceType.PATH);
145  1 link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
146  1 SectionBlock section2 =
147    new SectionBlock(Arrays.<Block>asList(header, new ParagraphBlock(Arrays.<Block>asList(link))));
148   
149  1 XDOM xdom = new XDOM(Arrays.<Block>asList(firstParagraph, secondParagraph, section1, section2));
150  1 WikiDocument document = new WikiDocument("Space.Page", xdom, null);
151   
152  1 List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
153   
154  1 ClassBlockMatcher linkMatcher = new ClassBlockMatcher(LinkBlock.class);
155   
156  1 ResourceReference updatedReference =
157    document.getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
158  1 assertEquals("chapter1", updatedReference.getParameter("anchor"));
159  1 assertEquals(result.get(1).getFullName(), updatedReference.getReference());
160   
161    // Verify that the path reference was not updated.
162  1 updatedReference = document.getXdom().<LinkBlock>getBlocks(linkMatcher, Axes.DESCENDANT).get(1).getReference();
163  1 assertEquals("", updatedReference.getReference());
164  1 assertEquals("chapter1", updatedReference.getParameter("anchor"));
165   
166  1 updatedReference =
167    result.get(2).getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
168  1 assertEquals(ResourceType.DOCUMENT, updatedReference.getType());
169  1 assertEquals("chapter1", updatedReference.getParameter("anchor"));
170  1 assertEquals(result.get(1).getFullName(), updatedReference.getReference());
171    }
172    }