1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.refactoring.internal.splitter

File DefaultDocumentSplitterTest.java

 

Code metrics

0
50
2
1
155
95
2
0.04
25
2
1

Classes

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