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

File SkinListenerTest.java

 

Code metrics

0
30
5
1
141
88
5
0.17
6
5
1

Classes

Class Line # Actions
SkinListenerTest 57 30 0% 5 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.lesscss.internal.listeners;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.bridge.event.DocumentCreatedEvent;
30    import org.xwiki.bridge.event.DocumentDeletedEvent;
31    import org.xwiki.bridge.event.DocumentUpdatedEvent;
32    import org.xwiki.lesscss.internal.cache.ColorThemeCache;
33    import org.xwiki.lesscss.internal.cache.LESSResourcesCache;
34    import org.xwiki.lesscss.internal.skin.DocumentSkinReference;
35    import org.xwiki.lesscss.internal.skin.SkinReferenceFactory;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.model.reference.EntityReference;
38    import org.xwiki.model.reference.LocalDocumentReference;
39    import org.xwiki.observation.event.Event;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41   
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.objects.BaseObject;
44   
45    import static org.junit.Assert.assertEquals;
46    import static org.mockito.Mockito.mock;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.verifyZeroInteractions;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * Test class for {@link SkinListener}.
53    *
54    * @since 7.0RC1
55    * @version $Id $
56    */
 
57    public class SkinListenerTest
58    {
59    @Rule
60    public MockitoComponentMockingRule<SkinListener> mocker =
61    new MockitoComponentMockingRule<>(SkinListener.class);
62   
63    private LESSResourcesCache lessResourcesCache;
64   
65    private ColorThemeCache colorThemeCache;
66   
67    private SkinReferenceFactory skinReferenceFactory;
68   
 
69  4 toggle @Before
70    public void setUp() throws Exception
71    {
72  4 lessResourcesCache = mocker.getInstance(LESSResourcesCache.class);
73  4 colorThemeCache = mocker.getInstance(ColorThemeCache.class);
74  4 skinReferenceFactory = mocker.getInstance(SkinReferenceFactory.class);
75    }
76   
 
77  1 toggle @Test
78    public void getName() throws Exception
79    {
80  1 assertEquals("LESS Skin Listener", mocker.getComponentUnderTest().getName());
81    }
82   
 
83  1 toggle @Test
84    public void getEvents() throws Exception
85    {
86  1 List<Event> eventsToObserve = Arrays.<Event>asList(
87    new DocumentCreatedEvent(),
88    new DocumentUpdatedEvent(),
89    new DocumentDeletedEvent());
90   
91  1 assertEquals(eventsToObserve, mocker.getComponentUnderTest().getEvents());
92    }
93   
 
94  1 toggle @Test
95    public void onEventWhenSkinIsChanged() throws Exception
96    {
97    // Mocks
98  1 Event event = mock(Event.class);
99  1 XWikiDocument doc = mock(XWikiDocument.class);
100  1 Object data = new Object();
101   
102  1 EntityReference classReference = new LocalDocumentReference("XWiki", "XWikiSkins");
103  1 List<BaseObject> objects = new ArrayList<>();
104  1 BaseObject object = mock(BaseObject.class);
105  1 objects.add(object);
106  1 when(doc.getXObjects(classReference)).thenReturn(objects);
107   
108  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
109  1 when(doc.getDocumentReference()).thenReturn(documentReference);
110   
111  1 DocumentSkinReference skinReference = new DocumentSkinReference(documentReference, null);
112  1 when(skinReferenceFactory.createReference(documentReference)).thenReturn(skinReference);
113   
114    // Test
115  1 mocker.getComponentUnderTest().onEvent(event, doc, data);
116   
117    // Verify
118  1 verify(lessResourcesCache).clearFromSkin(skinReference);
119  1 verify(colorThemeCache).clearFromSkin(skinReference);
120    }
121   
 
122  1 toggle @Test
123    public void onEventWhenNoObject() throws Exception
124    {
125    // Mocks
126  1 Event event = mock(Event.class);
127  1 XWikiDocument doc = mock(XWikiDocument.class);
128  1 Object data = new Object();
129   
130  1 EntityReference classReference = new LocalDocumentReference("XWiki", "XWikiSkins");
131  1 List<BaseObject> objects = new ArrayList<>();
132  1 when(doc.getXObjects(classReference)).thenReturn(objects);
133   
134    // Test
135  1 mocker.getComponentUnderTest().onEvent(event, doc, data);
136   
137    // Verify
138  1 verifyZeroInteractions(lessResourcesCache);
139  1 verifyZeroInteractions(colorThemeCache);
140    }
141    }