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

File IconThemeListenerTest.java

 

Code metrics

0
28
6
1
142
84
6
0.21
4.67
6
1

Classes

Class Line # Actions
IconThemeListenerTest 56 28 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.icon.internal;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.bridge.event.DocumentDeletedEvent;
29    import org.xwiki.bridge.event.DocumentUpdatedEvent;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.icon.IconSetCache;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.LocalDocumentReference;
34    import org.xwiki.test.mockito.MockitoComponentMockingRule;
35    import org.xwiki.observation.event.Event;
36   
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.objects.BaseObject;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.junit.Assert.assertTrue;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.atLeastOnce;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.never;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Test class for {@link org.xwiki.icon.internal.IconThemeListener}.
52    *
53    * @since 6.2M1
54    * @version $Id: 542b8e9291d354c97e114631d38c50de1c5448f0 $
55    */
 
56    public class IconThemeListenerTest
57    {
58    @Rule
59    public MockitoComponentMockingRule<IconThemeListener> mocker =
60    new MockitoComponentMockingRule<>(IconThemeListener.class);
61   
62    private IconSetCache iconSetCache;
63   
 
64  5 toggle @Before
65    public void setUp() throws ComponentLookupException
66    {
67  5 iconSetCache = mocker.getInstance(IconSetCache.class);
68    }
69   
 
70  1 toggle @Test
71    public void onEvent() throws Exception
72    {
73    // Mocks
74  1 XWikiDocument doc = mock(XWikiDocument.class);
75  1 List<BaseObject> list = new ArrayList<BaseObject>();
76  1 BaseObject obj = mock(BaseObject.class);
77  1 list.add(obj);
78  1 when(obj.getStringValue("name")).thenReturn("icontheme1");
79   
80  1 DocumentReference docRef = new DocumentReference("wikiA", "b", "c");
81  1 when(doc.getDocumentReference()).thenReturn(docRef);
82   
83  1 LocalDocumentReference iconThemeClassRef = new LocalDocumentReference("IconThemesCode", "IconThemeClass");
84  1 when(doc.getXObjects(eq(iconThemeClassRef))).thenReturn(list);
85   
86    // Tests
87  1 mocker.getComponentUnderTest().onEvent(null, doc, null);
88   
89    // Verify
90  1 verify(iconSetCache, atLeastOnce()).clear(docRef);
91  1 verify(iconSetCache, atLeastOnce()).clear("icontheme1", "wikiA");
92    }
93   
 
94  1 toggle @Test
95    public void onEventWhenNoObjects() throws Exception
96    {
97    // Mocks
98  1 XWikiDocument doc = mock(XWikiDocument.class);
99  1 when(doc.getXObject(any(DocumentReference.class))).thenReturn(null);
100   
101    // Tests
102  1 mocker.getComponentUnderTest().onEvent(null, doc, null);
103   
104    // Verify
105  1 verify(iconSetCache, never()).clear(any(DocumentReference.class));
106    }
107   
 
108  1 toggle @Test
109    public void onEventWhenEmptyListObjects() throws Exception
110    {
111    // Mocks
112  1 XWikiDocument doc = mock(XWikiDocument.class);
113  1 List<BaseObject> list = new ArrayList<BaseObject>();
114   
115  1 LocalDocumentReference iconThemeClassRef = new LocalDocumentReference("IconThemesCode", "IconThemeClass");
116  1 when(doc.getXObjects(eq(iconThemeClassRef))).thenReturn(list);
117   
118    // Tests
119  1 mocker.getComponentUnderTest().onEvent(null, doc, null);
120   
121    // Verify
122  1 verify(iconSetCache, never()).clear(any(DocumentReference.class));
123    }
124   
 
125  1 toggle @Test
126    public void getEvents() throws Exception
127    {
128  1 List<Event> results = mocker.getComponentUnderTest().getEvents();
129   
130    // Verify
131  1 assertEquals(2, results.size());
132  1 assertTrue(results.get(0) instanceof DocumentUpdatedEvent);
133  1 assertTrue(results.get(1) instanceof DocumentDeletedEvent);
134    }
135   
 
136  1 toggle @Test
137    public void getName() throws Exception
138    {
139  1 assertEquals("Icon Theme listener.", mocker.getComponentUnderTest().getName());
140    }
141   
142    }