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

File XHTMLRendererTest.java

 

Code metrics

0
62
9
1
182
114
9
0.15
6.89
9
1

Classes

Class Line # Actions
XHTMLRendererTest 46 62 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 4 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.internal.renderer.xhtml;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.List;
25   
26    import org.jmock.Expectations;
27    import org.junit.Test;
28    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
29    import org.xwiki.rendering.block.Block;
30    import org.xwiki.rendering.block.ImageBlock;
31    import org.xwiki.rendering.block.LinkBlock;
32    import org.xwiki.rendering.block.MetaDataBlock;
33    import org.xwiki.rendering.block.WordBlock;
34    import org.xwiki.rendering.block.XDOM;
35    import org.xwiki.rendering.listener.MetaData;
36    import org.xwiki.rendering.listener.reference.ResourceReference;
37    import org.xwiki.rendering.listener.reference.ResourceType;
38    import org.xwiki.rendering.renderer.PrintRenderer;
39    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
40    import org.xwiki.rendering.wiki.WikiModel;
41    import org.xwiki.test.jmock.AbstractComponentTestCase;
42   
43    /**
44    * Unit tests for {@link XHTMLRenderer}.
45    */
 
46    public class XHTMLRendererTest extends AbstractComponentTestCase
47    {
48    private PrintRenderer renderer;
49   
50    private WikiModel mockWikiModel;
51   
 
52  4 toggle @Override
53    protected void registerComponents() throws Exception
54    {
55    // Register a mock implementation of WikiModel in order to perform expectations on the generation of
56    // document and image URLs.
57  4 this.mockWikiModel = getMockery().mock(WikiModel.class);
58  4 DefaultComponentDescriptor<WikiModel> cd = new DefaultComponentDescriptor<WikiModel>();
59  4 cd.setRoleType(WikiModel.class);
60  4 getComponentManager().registerComponent(cd, this.mockWikiModel);
61   
62  4 this.renderer = getComponentManager().getInstance(PrintRenderer.class, "xhtml/1.0");
63    }
64   
65    /**
66    * Verify that when an XDOM contains a MetaDataBlock with a "source" metaData specified, then this "source" is
67    * set in the ResourceReference passed to the WikiModel call when getting a document link URL.
68    */
 
69  1 toggle @Test
70    public void testBeginLinkHasBaseResourceReferencePassedWhenSourceMetaDataAdded()
71    {
72  1 final ResourceReference blockReference = new ResourceReference("reference", ResourceType.DOCUMENT);
73  1 List<Block> linkBlocks = Arrays.asList((Block) new LinkBlock(Arrays.asList((Block) new WordBlock("label")),
74    blockReference, true));
75  1 MetaData metaData1 = new MetaData();
76  1 metaData1.addMetaData(MetaData.BASE, "base1");
77  1 MetaData metaData2 = new MetaData();
78  1 metaData2.addMetaData(MetaData.BASE, "base2");
79  1 XDOM xdom = new XDOM(Arrays.asList((Block) new MetaDataBlock(
80    Arrays.asList((Block) new MetaDataBlock(linkBlocks, metaData2)), metaData1)));
81   
 
82  1 toggle getMockery().checking(new Expectations() {{
83    // This is the part of the test verification: we verify that the passed Resource Reference has its base
84    // reference set.
85  1 ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
86  1 reference.addBaseReference("base1");
87  1 reference.addBaseReference("base2");
88  1 oneOf(mockWikiModel).isDocumentAvailable(reference);
89  1 will(returnValue(true));
90  1 oneOf(mockWikiModel).getDocumentViewURL(reference);
91  1 will(returnValue("viewurl"));
92    }});
93   
94  1 this.renderer.setPrinter(new DefaultWikiPrinter());
95  1 xdom.traverse(this.renderer);
96    }
97   
98    /**
99    * Verify that when an XDOM contains a MetaDataBlock with a "source" metaData specified, then this "source" is
100    * not used if the ResourceReference passed to the WikiModel already has a base reference specified.
101    */
 
102  1 toggle @Test
103    public void testBeginLinkDoesntUseSourceMetaDataIfBaseReferenceSpecified()
104    {
105  1 final ResourceReference blockReference = new ResourceReference("reference", ResourceType.DOCUMENT);
106  1 blockReference.addBaseReference("original base");
107   
108  1 List<Block> linkBlocks = Arrays.asList((Block) new LinkBlock(Arrays.asList((Block) new WordBlock("label")),
109    blockReference, true));
110  1 MetaData metaData = new MetaData();
111  1 metaData.addMetaData(MetaData.BASE, "base");
112  1 XDOM xdom = new XDOM(Arrays.asList((Block) new MetaDataBlock(linkBlocks, metaData)));
113   
 
114  1 toggle getMockery().checking(new Expectations() {{
115    // This is the part of the test verification: we verify that the passed Resource Reference has its base
116    // reference set.
117  1 ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
118  1 reference.addBaseReference("original base");
119  1 oneOf(mockWikiModel).isDocumentAvailable(reference);
120  1 will(returnValue(true));
121  1 oneOf(mockWikiModel).getDocumentViewURL(reference);
122  1 will(returnValue("viewurl"));
123    }});
124   
125  1 this.renderer.setPrinter(new DefaultWikiPrinter());
126  1 xdom.traverse(this.renderer);
127    }
128   
129    /**
130    * Verify that when an XDOM contains a MetaDataBlock with a "source" metaData specified, then this "source" is
131    * set in the ResourceReference passed to the WikiModel call when getting an image link URL.
132    */
 
133  1 toggle @Test
134    public void testOnImageHasBaseResourceReferencePassedWhenSourceMetaDataAdded()
135    {
136  1 final ResourceReference blockReference = new ResourceReference("reference", ResourceType.ATTACHMENT);
137  1 List<Block> imageBlocks = Arrays.asList((Block) new ImageBlock(blockReference, true));
138  1 MetaData metaData = new MetaData();
139  1 metaData.addMetaData(MetaData.BASE, "base");
140  1 XDOM xdom = new XDOM(Arrays.asList((Block) new MetaDataBlock(imageBlocks, metaData)));
141   
 
142  1 toggle getMockery().checking(new Expectations() {{
143    // This is the part of the test verification: we verify that the passed Resource Reference has its base
144    // reference set.
145  1 ResourceReference reference = new ResourceReference("reference", ResourceType.ATTACHMENT);
146  1 reference.addBaseReference("base");
147  1 oneOf(mockWikiModel).getImageURL(reference, Collections.<String, String>emptyMap());
148  1 will(returnValue("imageurl"));
149    }});
150   
151  1 this.renderer.setPrinter(new DefaultWikiPrinter());
152  1 xdom.traverse(this.renderer);
153    }
154   
155    /**
156    * Verify that when an XDOM contains a MetaDataBlock with a "source" metaData specified, then this "source" is
157    * not used if the ResourceReference passed to the WikiModel already has a base reference specified.
158    */
 
159  1 toggle @Test
160    public void testOnImageDoesntUseSourceMetaDataIfBaseReferenceSpecified()
161    {
162  1 final ResourceReference blockReference = new ResourceReference("reference", ResourceType.ATTACHMENT);
163  1 blockReference.addBaseReference("original base");
164   
165  1 List<Block> imageBlocks = Arrays.asList((Block) new ImageBlock(blockReference, true));
166  1 MetaData metaData = new MetaData();
167  1 metaData.addMetaData(MetaData.BASE, "base");
168  1 XDOM xdom = new XDOM(Arrays.asList((Block) new MetaDataBlock(imageBlocks, metaData)));
169   
 
170  1 toggle getMockery().checking(new Expectations() {{
171    // This is the part of the test verification: we verify that the passed Resource Reference has its base
172    // reference set.
173  1 ResourceReference reference = new ResourceReference("reference", ResourceType.ATTACHMENT);
174  1 reference.addBaseReference("original base");
175  1 oneOf(mockWikiModel).getImageURL(reference, Collections.<String, String>emptyMap());
176  1 will(returnValue("imageurl"));
177    }});
178   
179  1 this.renderer.setPrinter(new DefaultWikiPrinter());
180  1 xdom.traverse(this.renderer);
181    }
182    }