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

File SSXListenerTest.java

 

Code metrics

0
35
4
1
150
102
4
0.11
8.75
4
1

Classes

Class Line # Actions
SSXListenerTest 62 35 0% 4 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.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.component.manager.ComponentManager;
33    import org.xwiki.lesscss.internal.cache.ColorThemeCache;
34    import org.xwiki.lesscss.internal.cache.LESSResourcesCache;
35    import org.xwiki.lesscss.internal.resources.LESSObjectPropertyResourceReference;
36    import org.xwiki.lesscss.resources.LESSResourceReferenceFactory;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.EntityReferenceSerializer;
40    import org.xwiki.model.reference.ObjectPropertyReference;
41    import org.xwiki.observation.event.Event;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
44   
45    import com.xpn.xwiki.doc.XWikiDocument;
46    import com.xpn.xwiki.objects.BaseObject;
47    import com.xpn.xwiki.objects.BaseObjectReference;
48    import com.xpn.xwiki.web.Utils;
49   
50    import static org.junit.Assert.assertEquals;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.eq;
53    import static org.mockito.Mockito.atLeastOnce;
54    import static org.mockito.Mockito.mock;
55    import static org.mockito.Mockito.verify;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * @since 6.4RC1
60    * @version $Id: 5e9170f1a33e32554aece377469e87d5abd885cb $
61    */
 
62    public class SSXListenerTest
63    {
64    @Rule
65    public MockitoComponentMockingRule<SSXListener> mocker =
66    new MockitoComponentMockingRule<>(SSXListener.class);
67   
68    private LESSResourcesCache lessResourcesCache;
69   
70    private ColorThemeCache colorThemeCache;
71   
72    private WikiDescriptorManager wikiDescriptorManager;
73   
74    private LESSResourceReferenceFactory lessResourceReferenceFactory;
75   
 
76  3 toggle @Before
77    public void setUp() throws Exception
78    {
79  3 lessResourcesCache = mocker.getInstance(LESSResourcesCache.class);
80  3 colorThemeCache = mocker.getInstance(ColorThemeCache.class);
81  3 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
82  3 lessResourceReferenceFactory = mocker.getInstance(LESSResourceReferenceFactory.class);
83    }
84   
 
85  1 toggle @Test
86    public void getName() throws Exception
87    {
88  1 assertEquals("LESS SSX objects listener", mocker.getComponentUnderTest().getName());
89    }
90   
 
91  1 toggle @Test
92    public void getEvents() throws Exception
93    {
94  1 List<Event> eventsToObserve = Arrays.<Event>asList(
95    new DocumentCreatedEvent(),
96    new DocumentUpdatedEvent(),
97    new DocumentDeletedEvent());
98   
99  1 assertEquals(eventsToObserve, mocker.getComponentUnderTest().getEvents());
100    }
101   
 
102  1 toggle @Test
103    public void onEvent() throws Exception
104    {
105    // Mocks
106  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wiki");
107  1 XWikiDocument doc = mock(XWikiDocument.class);
108  1 BaseObject obj1 = mock(BaseObject.class);
109  1 BaseObject obj2 = mock(BaseObject.class);
110  1 List<BaseObject> objList = new ArrayList<>();
111  1 DocumentReference ssxDocRef = new DocumentReference("wiki", "XWiki", "StyleSheetExtension");
112  1 when(doc.getXObjects(eq(ssxDocRef))).thenReturn(objList);
113  1 objList.add(obj1);
114  1 objList.add(null);
115  1 objList.add(obj2);
116  1 when(obj1.getStringValue("contentType")).thenReturn("CSS");
117  1 when(obj2.getStringValue("contentType")).thenReturn("LESS");
118  1 when(obj2.getNumber()).thenReturn(2);
119  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Doc");
120  1 when(doc.getDocumentReference()).thenReturn(documentReference);
121   
122    // Because BaseObjectReference uses components from the Utils class, we need to set up the component manager
123  1 ComponentManager rootComponentManager = mock(ComponentManager.class);
124  1 Utils.setComponentManager(rootComponentManager);
125  1 ComponentManager contextComponentManager = mock(ComponentManager.class);
126  1 when(rootComponentManager.getInstance(ComponentManager.class, "context")).thenReturn(contextComponentManager);
127   
128    // Mock to serialize the object
129  1 EntityReferenceSerializer entityReferenceSerializer = mock(EntityReferenceSerializer.class);
130  1 when(contextComponentManager.getInstance(EntityReferenceSerializer.TYPE_STRING, "compactwiki"))
131    .thenReturn(entityReferenceSerializer);
132  1 when(entityReferenceSerializer.serialize(any(EntityReference.class), any(EntityReference.class)))
133    .thenReturn("objName");
134   
135  1 ObjectPropertyReference objPropertyReference =
136    new ObjectPropertyReference("code", new BaseObjectReference(ssxDocRef, 2, documentReference));
137  1 LESSObjectPropertyResourceReference lessObjectPropertyResourceReference =
138    new LESSObjectPropertyResourceReference(objPropertyReference, null, null);
139  1 when(lessResourceReferenceFactory.createReferenceForXObjectProperty(eq(objPropertyReference)))
140    .thenReturn(lessObjectPropertyResourceReference);
141   
142    // Test
143  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), doc, new Object());
144   
145    // Verify
146   
147  1 verify(lessResourcesCache, atLeastOnce()).clearFromLESSResource(lessObjectPropertyResourceReference);
148  1 verify(colorThemeCache, atLeastOnce()).clearFromLESSResource(lessObjectPropertyResourceReference);
149    }
150    }