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

File SSXListenerTest.java

 

Code metrics

0
38
4
1
157
106
4
0.11
9.5
4
1

Classes

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