1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
48 |
|
|
49 |
|
@since |
50 |
|
@version |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (66) |
Complexity: 13 |
Complexity Density: 0.24 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
64 |
11 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
77 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
87 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
97 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
109 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
117 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
125 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void clear() throws Exception |
137 |
|
{ |
138 |
1 |
mocker.getComponentUnderTest().clear(); |
139 |
1 |
verify(cache).removeAll(); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
142 |
1 |
@Test... |
143 |
|
public void clearByName() throws Exception |
144 |
|
{ |
145 |
1 |
mocker.getComponentUnderTest().clear("key"); |
146 |
1 |
verify(cache).remove("NAMED:key"); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
149 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
156 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
1PASS
|
|
165 |
1 |
@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 |
|
} |