1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
61 |
|
@version |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (42) |
Complexity: 4 |
Complexity Density: 0.11 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
77 |
3 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
86 |
1 |
@Test... |
87 |
|
public void getName() throws Exception |
88 |
|
{ |
89 |
1 |
assertEquals("LESS SSX objects listener", mocker.getComponentUnderTest().getName()); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
92 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
public void onEvent() throws Exception |
105 |
|
{ |
106 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
150 |
1 |
mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), doc, new Object()); |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
1 |
verify(lessResourcesCache, atLeastOnce()).clearFromLESSResource(lessObjectPropertyResourceReference); |
155 |
1 |
verify(colorThemeCache, atLeastOnce()).clearFromLESSResource(lessObjectPropertyResourceReference); |
156 |
|
} |
157 |
|
} |