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

File CurrentMacroEntityReferenceResolverTest.java

 

Code metrics

0
22
4
1
112
76
6
0.27
5.5
4
1.5

Classes

Class Line # Actions
CurrentMacroEntityReferenceResolverTest 48 22 0% 6 2
0.923076992.3%
 

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.transformation.macro;
21   
22    import static org.mockito.Mockito.*;
23   
24    import java.util.Arrays;
25    import java.util.Collections;
26   
27    import org.junit.Assert;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.AttachmentReference;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.MetaDataBlock;
37    import org.xwiki.rendering.block.WordBlock;
38    import org.xwiki.rendering.block.XDOM;
39    import org.xwiki.rendering.listener.MetaData;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41   
42    /**
43    * Unit tests for {@link CurrentMacroEntityReferenceResolver}.
44    *
45    * @version $Id: d0ca28781bd2e759acfe8f7dea1d55faafb528b4 $
46    * @since 4.3M1
47    */
 
48    public class CurrentMacroEntityReferenceResolverTest
49    {
50    @Rule
51    public MockitoComponentMockingRule<EntityReferenceResolver<String>> mocker =
52    new MockitoComponentMockingRule<EntityReferenceResolver<String>>(CurrentMacroEntityReferenceResolver.class);
53   
 
54  1 toggle @Test
55    public void resolveWhenNoBlockPassed() throws Exception
56    {
57  1 try {
58  1 mocker.getComponentUnderTest().resolve("something", EntityType.DOCUMENT);
59  0 Assert.fail("Should have thrown an IllegalArgumentException here");
60    } catch (IllegalArgumentException expected) {
61  1 Assert.assertEquals("You must pass one parameter of type [org.xwiki.rendering.block.Block]",
62    expected.getMessage());
63    }
64    }
65   
 
66  1 toggle @Test
67    public void resolveWhenWrongParameterPassed() throws Exception
68    {
69  1 try {
70  1 mocker.getComponentUnderTest()
71    .resolve("something", EntityType.ATTACHMENT, "wrong param type must be Block");
72  0 Assert.fail("Should have thrown an IllegalArgumentException here");
73    } catch (IllegalArgumentException expected) {
74  1 Assert.assertEquals("You must pass one parameter of type [org.xwiki.rendering.block.Block]",
75    expected.getMessage());
76    }
77    }
78   
 
79  1 toggle @Test
80    public void resolveWhenNoMetaDataBlock() throws Exception
81    {
82  1 EntityReference expectedReference = new DocumentReference("wiki", "Space", "Page");
83  1 EntityReferenceResolver<String> currentEntityReferenceResolver =
84    mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "current");
85  1 when(currentEntityReferenceResolver.resolve("Space.Page", EntityType.DOCUMENT)).thenReturn(expectedReference);
86   
87  1 Block block = new WordBlock("whatever");
88  1 Assert.assertEquals(expectedReference,
89    mocker.getComponentUnderTest().resolve("Space.Page", EntityType.DOCUMENT, block));
90    }
91   
 
92  1 toggle @Test
93    public void resolveWhenMetaDataBlock() throws Exception
94    {
95  1 DocumentReference baseReference = new DocumentReference("basewiki", "basespace", "basepage");
96  1 EntityReference expectedReference = new AttachmentReference("file", baseReference);
97  1 EntityReferenceResolver<String> currentEntityReferenceResolver =
98    mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "current");
99  1 when(currentEntityReferenceResolver.resolve("basewiki:basespace.basepage", EntityType.DOCUMENT)).thenReturn(
100    baseReference);
101  1 when(currentEntityReferenceResolver.resolve("file", EntityType.ATTACHMENT, baseReference)).thenReturn(
102    expectedReference);
103   
104  1 Block wordBlock = new WordBlock("whatever");
105  1 MetaData metaData =
106    new MetaData(Collections.<String, Object> singletonMap(MetaData.BASE, "basewiki:basespace.basepage"));
107  1 new XDOM(Arrays.<Block> asList(new MetaDataBlock(Arrays.<Block> asList(wordBlock), metaData)));
108   
109  1 Assert.assertEquals(expectedReference,
110    mocker.getComponentUnderTest().resolve("file", EntityType.ATTACHMENT, wordBlock));
111    }
112    }