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

File ColorThemeListenerTest.java

 

Code metrics

0
45
6
1
173
111
6
0.13
7.5
6
1

Classes

Class Line # Actions
ColorThemeListenerTest 60 45 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 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.colortheme.ColorThemeReference;
35    import org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory;
36    import org.xwiki.lesscss.internal.colortheme.DocumentColorThemeReference;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.EntityReference;
40    import org.xwiki.model.reference.LocalDocumentReference;
41    import org.xwiki.observation.event.Event;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43   
44    import com.xpn.xwiki.doc.XWikiDocument;
45    import com.xpn.xwiki.objects.BaseObject;
46   
47    import static org.junit.Assert.assertEquals;
48    import static org.mockito.ArgumentMatchers.eq;
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.verify;
51    import static org.mockito.Mockito.verifyZeroInteractions;
52    import static org.mockito.Mockito.when;
53   
54    /**
55    * Test class for {@link org.xwiki.lesscss.internal.listeners.ColorThemeListener}.
56    *
57    * @since 6.3M2
58    * @version $Id: 23653f0690593f1d97a66cd5a2123d43ca21517f $
59    */
 
60    public class ColorThemeListenerTest
61    {
62    @Rule
63    public MockitoComponentMockingRule<ColorThemeListener> mocker =
64    new MockitoComponentMockingRule<>(ColorThemeListener.class);
65   
66    private LESSResourcesCache lessResourcesCache;
67   
68    private ColorThemeCache colorThemeCache;
69   
70    private ColorThemeReferenceFactory colorThemeReferenceFactory;
71   
 
72  5 toggle @Before
73    public void setUp() throws Exception
74    {
75  5 lessResourcesCache = mocker.getInstance(LESSResourcesCache.class);
76  5 colorThemeCache = mocker.getInstance(ColorThemeCache.class);
77  5 colorThemeReferenceFactory = mocker.getInstance(ColorThemeReferenceFactory.class);
78    }
79   
 
80  1 toggle @Test
81    public void getName() throws Exception
82    {
83  1 assertEquals("LESS Color Theme Listener", mocker.getComponentUnderTest().getName());
84    }
85   
 
86  1 toggle @Test
87    public void getEvents() throws Exception
88    {
89  1 List<Event> eventsToObserve = Arrays.<Event>asList(
90    new DocumentCreatedEvent(),
91    new DocumentUpdatedEvent(),
92    new DocumentDeletedEvent());
93   
94  1 assertEquals(eventsToObserve, mocker.getComponentUnderTest().getEvents());
95    }
96   
 
97  1 toggle @Test
98    public void onEventWhenFlamingoThemeChanged() throws Exception
99    {
100    // Mocks
101  1 Event event = mock(Event.class);
102  1 XWikiDocument doc = mock(XWikiDocument.class);
103  1 Object data = new Object();
104   
105  1 EntityReference classReference = new LocalDocumentReference("FlamingoThemesCode", "ThemeClass");
106  1 List<BaseObject> objects = new ArrayList<>();
107  1 BaseObject object = mock(BaseObject.class);
108  1 objects.add(object);
109  1 when(doc.getXObjects(classReference)).thenReturn(objects);
110   
111  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
112  1 when(doc.getDocumentReference()).thenReturn(documentReference);
113   
114  1 ColorThemeReference colorThemeReference = new DocumentColorThemeReference(documentReference, null);
115  1 when(colorThemeReferenceFactory.createReference(eq(documentReference))).thenReturn(colorThemeReference);
116   
117    // Test
118  1 mocker.getComponentUnderTest().onEvent(event, doc, data);
119   
120    // Verify
121  1 verify(lessResourcesCache).clearFromColorTheme(colorThemeReference);
122  1 verify(colorThemeCache).clearFromColorTheme(colorThemeReference);
123    }
124   
 
125  1 toggle @Test
126    public void onEventWhenColorThemeChanged() throws Exception
127    {
128    // Mocks
129  1 Event event = mock(Event.class);
130  1 XWikiDocument doc = mock(XWikiDocument.class);
131  1 Object data = new Object();
132   
133  1 EntityReference classReference = new LocalDocumentReference("ColorThemes", "ColorThemeClass");
134  1 List<BaseObject> objects = new ArrayList<>();
135  1 BaseObject object = mock(BaseObject.class);
136  1 objects.add(object);
137  1 when(doc.getXObjects(classReference)).thenReturn(objects);
138   
139  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
140  1 when(doc.getDocumentReference()).thenReturn(documentReference);
141   
142  1 ColorThemeReference colorThemeReference = new DocumentColorThemeReference(documentReference, null);
143  1 when(colorThemeReferenceFactory.createReference(eq(documentReference))).thenReturn(colorThemeReference);
144   
145    // Test
146  1 mocker.getComponentUnderTest().onEvent(event, doc, data);
147   
148    // Verify
149  1 verify(lessResourcesCache).clearFromColorTheme(colorThemeReference);
150  1 verify(colorThemeCache).clearFromColorTheme(colorThemeReference);
151    }
152   
 
153  1 toggle @Test
154    public void onEventWhenNoObject() throws Exception
155    {
156    // Mocks
157  1 Event event = mock(Event.class);
158  1 XWikiDocument doc = mock(XWikiDocument.class);
159  1 Object data = new Object();
160   
161  1 EntityReference classReference = new EntityReference("ColorThemeClass", EntityType.DOCUMENT,
162    new EntityReference("ColorThemes", EntityType.SPACE));
163  1 List<BaseObject> objects = new ArrayList<>();
164  1 when(doc.getXObjects(classReference)).thenReturn(objects);
165   
166    // Test
167  1 mocker.getComponentUnderTest().onEvent(event, doc, data);
168   
169    // Verify
170  1 verifyZeroInteractions(lessResourcesCache);
171  1 verifyZeroInteractions(colorThemeCache);
172    }
173    }