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

File DefaultIconSetCacheTest.java

 

Code metrics

0
54
12
1
188
137
13
0.24
4.5
12
1.08

Classes

Class Line # Actions
DefaultIconSetCacheTest 52 54 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 11 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 org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.cache.Cache;
26    import org.xwiki.cache.CacheException;
27    import org.xwiki.cache.CacheFactory;
28    import org.xwiki.cache.CacheManager;
29    import org.xwiki.cache.config.CacheConfiguration;
30    import org.xwiki.component.phase.InitializationException;
31    import org.xwiki.component.util.DefaultParameterizedType;
32    import org.xwiki.icon.IconSet;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36   
37    import static org.junit.Assert.assertEquals;
38    import static org.junit.Assert.assertNotNull;
39    import static org.junit.Assert.assertTrue;
40    import static org.mockito.ArgumentMatchers.any;
41    import static org.mockito.ArgumentMatchers.eq;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.verify;
44    import static org.mockito.Mockito.when;
45   
46    /**
47    * Test class for {@link org.xwiki.icon.internal.DefaultIconSetCache}.
48    *
49    * @since 6.2M1
50    * @version $Id: b4d40bfecc583bc734ca2c8ab4220c256f251936 $
51    */
 
52    public class DefaultIconSetCacheTest
53    {
54    @Rule
55    public MockitoComponentMockingRule<DefaultIconSetCache> mocker =
56    new MockitoComponentMockingRule<>(DefaultIconSetCache.class);
57   
58    private CacheManager cacheManager;
59   
60    private EntityReferenceSerializer<String> entityReferenceSerializer;
61   
62    private Cache<IconSet> cache;
63   
 
64  11 toggle @Before
65    public void setUp() throws Exception
66    {
67  11 cacheManager = mocker.getInstance(CacheManager.class);
68  11 entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null,
69    EntityReferenceSerializer.class, String.class));
70  11 cache = mock(Cache.class);
71  11 CacheFactory cacheFactory = mock(CacheFactory.class);
72  11 when(cacheManager.getCacheFactory()).thenReturn(cacheFactory);
73  11 CacheConfiguration configuration = new CacheConfiguration("iconset");
74  11 when(cacheFactory.<IconSet>newCache(eq(configuration))).thenReturn(cache);
75    }
76   
 
77  1 toggle @Test
78    public void getByName() throws Exception
79    {
80  1 IconSet iconSet = new IconSet("key");
81  1 when(cache.get("NAMED:key")).thenReturn(iconSet);
82   
83  1 IconSet result = mocker.getComponentUnderTest().get("key");
84  1 assertTrue(iconSet == result);
85    }
86   
 
87  1 toggle @Test
88    public void getByNameAndWiki() throws Exception
89    {
90  1 IconSet iconSet = new IconSet("key");
91  1 when(cache.get("NAMED:6wikiId_key")).thenReturn(iconSet);
92   
93  1 IconSet result = mocker.getComponentUnderTest().get("key", "wikiId");
94  1 assertTrue(iconSet == result);
95    }
96   
 
97  1 toggle @Test
98    public void getByDocRef() throws Exception
99    {
100  1 IconSet iconSet = new IconSet("key");
101  1 DocumentReference docRef = new DocumentReference("a","b","c");
102  1 when(entityReferenceSerializer.serialize(docRef)).thenReturn("a:b.c");
103  1 when(cache.get("DOC:a:b.c")).thenReturn(iconSet);
104   
105  1 IconSet result = mocker.getComponentUnderTest().get(docRef);
106  1 assertTrue(iconSet == result);
107    }
108   
 
109  1 toggle @Test
110    public void putByName() throws Exception
111    {
112  1 IconSet iconSet = new IconSet("key");
113  1 mocker.getComponentUnderTest().put("key", iconSet);
114  1 verify(cache).set("NAMED:key", iconSet);
115    }
116   
 
117  1 toggle @Test
118    public void putByNameAndWiki() throws Exception
119    {
120  1 IconSet iconSet = new IconSet("key");
121  1 mocker.getComponentUnderTest().put("key", "wikiId", iconSet);
122  1 verify(cache).set("NAMED:6wikiId_key", iconSet);
123    }
124   
 
125  1 toggle @Test
126    public void putByDocRef() throws Exception
127    {
128  1 IconSet iconSet = new IconSet("key");
129  1 DocumentReference docRef = new DocumentReference("a","b","c");
130  1 when(entityReferenceSerializer.serialize(docRef)).thenReturn("a:b.c");
131  1 mocker.getComponentUnderTest().put(docRef, iconSet);
132  1 verify(cache).set("DOC:a:b.c", iconSet);
133    }
134   
 
135  1 toggle @Test
136    public void clear() throws Exception
137    {
138  1 mocker.getComponentUnderTest().clear();
139  1 verify(cache).removeAll();
140    }
141   
 
142  1 toggle @Test
143    public void clearByName() throws Exception
144    {
145  1 mocker.getComponentUnderTest().clear("key");
146  1 verify(cache).remove("NAMED:key");
147    }
148   
 
149  1 toggle @Test
150    public void clearByNameAndWiki() throws Exception
151    {
152  1 mocker.getComponentUnderTest().clear("key", "wikiId");
153  1 verify(cache).remove("NAMED:6wikiId_key");
154    }
155   
 
156  1 toggle @Test
157    public void clearByDocRef() throws Exception
158    {
159  1 DocumentReference docRef = new DocumentReference("a","b","c");
160  1 when(entityReferenceSerializer.serialize(docRef)).thenReturn("a:b.c");
161  1 mocker.getComponentUnderTest().clear(docRef);
162  1 verify(cache).remove("DOC:a:b.c");
163    }
164   
 
165  1 toggle @Test
166    public void initializeWhenError() throws Exception
167    {
168  1 DefaultIconSetCache cache = mocker.getComponentUnderTest();
169  1 CacheFactory cacheFactory = mock(CacheFactory.class);
170  1 when(cacheManager.getCacheFactory()).thenReturn(cacheFactory);
171   
172  1 Exception exception = new CacheException("ERROR");
173  1 when(cacheFactory.newCache(any(CacheConfiguration.class))).thenThrow(exception);
174   
175  1 Exception exceptionCaught = null;
176  1 try {
177  1 cache.initialize();
178    } catch(InitializationException e){
179  1 exceptionCaught = e;
180    }
181   
182  1 assertNotNull(exceptionCaught);
183  1 assertEquals("Failed to initialize the IconSet Cache.", exceptionCaught.getMessage());
184  1 assertEquals(exception, exceptionCaught.getCause());
185    }
186   
187   
188    }