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

File WikiTemplatePropertyGroupProviderTest.java

 

Code metrics

0
37
4
1
143
88
5
0.14
9.25
4
1.25

Classes

Class Line # Actions
WikiTemplatePropertyGroupProviderTest 53 37 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 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.wiki.template.internal;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.test.mockito.MockitoComponentMockingRule;
28    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
29    import org.xwiki.wiki.manager.WikiManagerException;
30    import org.xwiki.wiki.properties.WikiPropertyGroup;
31    import org.xwiki.wiki.properties.WikiPropertyGroupException;
32    import org.xwiki.wiki.properties.WikiPropertyGroupProvider;
33    import org.xwiki.wiki.template.WikiTemplatePropertyGroup;
34   
35    import com.xpn.xwiki.XWiki;
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.objects.BaseObject;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.junit.Assert.assertFalse;
42    import static org.junit.Assert.assertTrue;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Test class for {@link WikiTemplatePropertyGroupProvider}.
49    *
50    * @since 5.4.2
51    * @version $Id: 633ddddb3494ef4ea20617b357cd71e6cd1aab37 $
52    */
 
53    public class WikiTemplatePropertyGroupProviderTest
54    {
55    @Rule
56    public MockitoComponentMockingRule<WikiPropertyGroupProvider> mocker =
57    new MockitoComponentMockingRule(WikiTemplatePropertyGroupProvider.class, WikiPropertyGroupProvider.class,
58    "template");
59   
60    private Provider<XWikiContext> xcontextProvider;
61   
62    private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper;
63   
64    private XWikiContext xcontext;
65   
66    private XWiki xwiki;
67   
 
68  3 toggle @Before
69    public void setUp() throws Exception
70    {
71  3 wikiDescriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class);
72  3 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
73  3 xcontext = mock(XWikiContext.class);
74  3 when(xcontextProvider.get()).thenReturn(xcontext);
75  3 xwiki = mock(com.xpn.xwiki.XWiki.class);
76  3 when(xcontext.getWiki()).thenReturn(xwiki);
77    }
78   
 
79  1 toggle @Test
80    public void get() throws Exception
81    {
82  1 XWikiDocument descriptorDocument = mock(XWikiDocument.class);
83  1 when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId")).thenReturn(descriptorDocument);
84  1 BaseObject object = mock(BaseObject.class);
85  1 when(descriptorDocument.getXObject(eq(WikiTemplateClassDocumentInitializer.SERVER_CLASS))).thenReturn(object);
86  1 when(object.getIntValue("iswikitemplate", 0)).thenReturn(1);
87   
88    // Test
89  1 WikiPropertyGroup result = mocker.getComponentUnderTest().get("wikiId");
90   
91    // Verify
92  1 assertEquals(true, result.get("isTemplate"));
93  1 assertTrue(result instanceof WikiTemplatePropertyGroup);
94  1 assertTrue(((WikiTemplatePropertyGroup)result).isTemplate());
95   
96  1 XWikiDocument descriptorDocument2 = mock(XWikiDocument.class);
97  1 when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId2")).thenReturn(descriptorDocument2);
98  1 BaseObject object2 = mock(BaseObject.class);
99  1 when(descriptorDocument2.getXObject(eq(WikiTemplateClassDocumentInitializer.SERVER_CLASS))).thenReturn(object2);
100  1 when(object2.getIntValue("iswikitemplate", 0)).thenReturn(0);
101   
102    // Test
103  1 WikiPropertyGroup result2 = mocker.getComponentUnderTest().get("wikiId2");
104   
105    // Verify
106  1 assertEquals(false, result2.get("isTemplate"));
107  1 assertTrue(result2 instanceof WikiTemplatePropertyGroup);
108  1 assertFalse(((WikiTemplatePropertyGroup) result2).isTemplate());
109    }
110   
 
111  1 toggle @Test
112    public void getWhenNoObject() throws Exception
113    {
114  1 XWikiDocument descriptorDocument = mock(XWikiDocument.class);
115  1 when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId")).thenReturn(descriptorDocument);
116   
117    // Test
118  1 WikiPropertyGroup result = mocker.getComponentUnderTest().get("wikiId");
119   
120    // Verify
121  1 assertEquals(false, result.get("isTemplate"));
122    }
123   
 
124  1 toggle @Test
125    public void getWhenException() throws Exception
126    {
127  1 WikiManagerException exception = new WikiManagerException("error in WikiManager");
128  1 when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId")).thenThrow(exception);
129   
130    // Test
131  1 boolean exceptionCaught = false;
132  1 try {
133  1 mocker.getComponentUnderTest().get("wikiId");
134    } catch(WikiPropertyGroupException e) {
135  1 exceptionCaught = true;
136  1 assertEquals("Unable to load descriptor document for wiki [wikiId].", e.getMessage());
137  1 assertEquals(exception, e.getCause());
138    }
139   
140  1 assertTrue(exceptionCaught);
141    }
142   
143    }