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

File DefaultSkinReferenceFactoryTest.java

 

Code metrics

0
43
4
1
150
98
5
0.12
10.75
4
1.25

Classes

Class Line # Actions
DefaultSkinReferenceFactoryTest 51 43 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.lesscss.internal.skin;
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.component.util.DefaultParameterizedType;
28    import org.xwiki.lesscss.compiler.LESSCompilerException;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31    import org.xwiki.model.reference.WikiReference;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
34   
35    import com.xpn.xwiki.XWiki;
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.XWikiDocument;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.junit.Assert.assertNotNull;
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    * @since 6.4RC1
49    * @version $Id: 0e8187267e4ea75408de83f5bdda9697941256b9 $
50    */
 
51    public class DefaultSkinReferenceFactoryTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<DefaultSkinReferenceFactory> mocker =
55    new MockitoComponentMockingRule<>(DefaultSkinReferenceFactory.class);
56   
57    private Provider<XWikiContext> xcontextProvider;
58   
59    private DocumentReferenceResolver<String> documentReferenceResolver;
60   
61    private WikiDescriptorManager wikiDescriptorManager;
62   
63    private XWikiContext xcontext;
64   
65    private XWiki xwiki;
66   
 
67  3 toggle @Before
68    public void setUp() throws Exception
69    {
70  3 documentReferenceResolver = mocker.getInstance(new DefaultParameterizedType(null,
71    DocumentReferenceResolver.class, String.class));
72  3 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
73  3 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
74  3 xcontext = mock(XWikiContext.class);
75  3 when(xcontextProvider.get()).thenReturn(xcontext);
76  3 xwiki = mock(XWiki.class);
77  3 when(xcontext.getWiki()).thenReturn(xwiki);
78    }
79   
 
80  1 toggle @Test
81    public void createReferenceWhenSkinOnDB() throws Exception
82    {
83    // Mocks
84  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId");
85  1 DocumentReference skinDocRef = new DocumentReference("wikiId", "XWiki", "MySkin");
86  1 when(documentReferenceResolver.resolve(eq("XWiki.MySkin"), eq(new WikiReference("wikiId"))))
87    .thenReturn(skinDocRef);
88  1 when(xwiki.exists(skinDocRef, xcontext)).thenReturn(true);
89  1 XWikiDocument skinDoc = mock(XWikiDocument.class);
90  1 when(xwiki.getDocument(skinDocRef, xcontext)).thenReturn(skinDoc);
91  1 when(skinDoc.getXObjectSize(eq(new DocumentReference("wikiId", "XWiki", "XWikiSkins")))).thenReturn(1);
92   
93    // Test
94  1 SkinReference skinReference = mocker.getComponentUnderTest().createReference("XWiki.MySkin");
95   
96    // Verify
97  1 assertTrue(skinReference instanceof DocumentSkinReference);
98  1 DocumentSkinReference docSkinRef = (DocumentSkinReference) skinReference;
99  1 assertEquals(skinDocRef, docSkinRef.getSkinDocument());
100    }
101   
 
102  1 toggle @Test
103    public void createReferenceWhenSkinOnDBWithException() throws Exception
104    {
105    // Mocks
106  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId");
107  1 DocumentReference skinDocRef = new DocumentReference("wikiId", "XWiki", "MySkin");
108  1 when(documentReferenceResolver.resolve(eq("XWiki.MySkin"), eq(new WikiReference("wikiId"))))
109    .thenReturn(skinDocRef);
110  1 when(xwiki.exists(skinDocRef, xcontext)).thenReturn(true);
111  1 Exception exception = new XWikiException();
112  1 when(xwiki.getDocument(skinDocRef, xcontext)).thenThrow(exception);
113   
114    // Test
115  1 LESSCompilerException caughtException = null;
116  1 try {
117  1 mocker.getComponentUnderTest().createReference("XWiki.MySkin");
118    } catch(LESSCompilerException e) {
119  1 caughtException = e;
120    }
121   
122    // Verify
123  1 assertNotNull(caughtException);
124  1 assertEquals(exception, caughtException.getCause());
125  1 assertEquals("Unable to read document [wikiId:XWiki.MySkin]", caughtException.getMessage());
126    }
127   
 
128  1 toggle @Test
129    public void createReferenceWhenSkinOnFS() throws Exception
130    {
131    // Mocks
132  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId");
133  1 DocumentReference skinDocRef = new DocumentReference("wikiId", "Main", "Flamingo");
134  1 when(documentReferenceResolver.resolve(eq("flamingo"), eq(new WikiReference("wikiId"))))
135    .thenReturn(skinDocRef);
136  1 when(xwiki.exists(skinDocRef, xcontext)).thenReturn(true);
137  1 XWikiDocument skinDoc = mock(XWikiDocument.class);
138  1 when(xwiki.getDocument(skinDocRef, xcontext)).thenReturn(skinDoc);
139  1 when(skinDoc.getXObjectSize(eq(new DocumentReference("wikiId", "XWiki", "XWikiSkins")))).thenReturn(0);
140   
141    // Test
142  1 SkinReference skinReference = mocker.getComponentUnderTest().createReference("flamingo");
143   
144    // Verify
145  1 assertTrue(skinReference instanceof FSSkinReference);
146  1 FSSkinReference fsSkinRef = (FSSkinReference) skinReference;
147  1 assertEquals("flamingo", fsSkinRef.getSkinName());
148  1 assertEquals("SkinFS[flamingo]", fsSkinRef.toString());
149    }
150    }