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

File DefaultLESSResourcesCacheTest.java

 

Code metrics

0
56
8
1
221
141
8
0.14
7
8
1

Classes

Class Line # Actions
DefaultLESSResourcesCacheTest 48 56 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 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.cache;
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.CacheFactory;
27    import org.xwiki.cache.CacheManager;
28    import org.xwiki.cache.config.CacheConfiguration;
29    import org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference;
30    import org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference;
31    import org.xwiki.lesscss.internal.skin.FSSkinReference;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import static org.junit.Assert.assertEquals;
35    import static org.mockito.ArgumentMatchers.eq;
36    import static org.mockito.Mockito.mock;
37    import static org.mockito.Mockito.never;
38    import static org.mockito.Mockito.times;
39    import static org.mockito.Mockito.verify;
40    import static org.mockito.Mockito.when;
41   
42    /**
43    * Test class for {@link DefaultLESSResourcesCache}.
44    *
45    * @since 6.1M2
46    * @version $Id: 495791ae45a4e1172d42ca1a778d9c5b4d9b17e2 $
47    */
 
48    public class DefaultLESSResourcesCacheTest
49    {
50    @Rule
51    public MockitoComponentMockingRule<DefaultLESSResourcesCache> mocker =
52    new MockitoComponentMockingRule<>(DefaultLESSResourcesCache.class);
53   
54    private CacheManager cacheManager;
55   
56    private Cache<String> cache;
57   
58    private CacheKeyFactory cacheKeyFactory;
59   
 
60  6 toggle @Before
61    public void setUp() throws Exception
62    {
63  6 cacheManager = mocker.getInstance(CacheManager.class);
64  6 cache = mock(Cache.class);
65  6 CacheFactory cacheFactory = mock(CacheFactory.class);
66  6 when(cacheManager.getCacheFactory()).thenReturn(cacheFactory);
67  6 CacheConfiguration configuration = new CacheConfiguration("lesscss.skinfiles.cache");
68  6 when(cacheFactory.<String>newCache(eq(configuration))).thenReturn(cache);
69  6 cacheKeyFactory = mocker.getInstance(CacheKeyFactory.class);
70   
71  6 LESSSkinFileResourceReference lessSkinFileResourceReference =
72    new LESSSkinFileResourceReference("lessResource", null, null);
73  6 when(cacheKeyFactory.getCacheKey(eq(lessSkinFileResourceReference), eq(new FSSkinReference("skin")),
74    eq(new NamedColorThemeReference("colorTheme")), eq(true))).thenReturn("12_lessResource_4_skin_10_colorTheme");
75    }
76   
 
77  19 toggle private LESSSkinFileResourceReference createLESSSkinFileResourceReference(String fileName)
78    {
79  19 return new LESSSkinFileResourceReference(fileName, null, null);
80    }
81   
 
82  1 toggle @Test
83    public void get() throws Exception
84    {
85    // Mock
86  1 when(cache.get("12_lessResource_4_skin_10_colorTheme")).thenReturn("Expected output");
87   
88    // Test
89  1 String result = mocker.getComponentUnderTest().get(new LESSSkinFileResourceReference("lessResource", null, null),
90    new FSSkinReference("skin"), new NamedColorThemeReference("colorTheme"));
91   
92    // Verify
93  1 assertEquals("Expected output", result);
94    }
95   
 
96  1 toggle @Test
97    public void set() throws Exception
98    {
99    // Test
100  1 mocker.getComponentUnderTest().set(
101    new LESSSkinFileResourceReference("lessResource", null, null), new FSSkinReference("skin"),
102    new NamedColorThemeReference("colorTheme"), "css");
103   
104    // Verify
105  1 verify(cache).set(eq("12_lessResource_4_skin_10_colorTheme"), eq("css"));
106    }
107   
 
108  1 toggle @Test
109    public void clear() throws Exception
110    {
111    // Test
112  1 mocker.getComponentUnderTest().clear();
113   
114    // Verify
115  1 verify(cache).removeAll();
116    }
117   
 
118  1 toggle @Test
119    public void clearFromSkin() throws Exception
120    {
121    // Mocks
122  1 LESSSkinFileResourceReference file1 = createLESSSkinFileResourceReference("file1");
123  1 LESSSkinFileResourceReference file2 = createLESSSkinFileResourceReference("file2");
124  1 when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin1")),
125    eq(new NamedColorThemeReference("colorTheme")), eq(true))).thenReturn("k1");
126  1 when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin2")),
127    eq(new NamedColorThemeReference("colorTheme")), eq(true))).thenReturn("k3");
128  1 when(cacheKeyFactory.getCacheKey(eq(file2), eq(new FSSkinReference("skin1")),
129    eq(new NamedColorThemeReference("colorTheme")), eq(true))).thenReturn("k4");
130   
131    // Add the first one twice
132  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
133    new NamedColorThemeReference("colorTheme"), "css1");
134  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
135    new NamedColorThemeReference("colorTheme"), "css1");
136   
137    // Others
138  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin2"),
139    new NamedColorThemeReference("colorTheme"), "css2");
140  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file2"), new FSSkinReference("skin1"),
141    new NamedColorThemeReference("colorTheme"), "css3");
142   
143    // Test
144  1 mocker.getComponentUnderTest().clearFromSkin(new FSSkinReference("skin1"));
145   
146    // Verify
147  1 verify(cache, times(1)).remove("k1");
148  1 verify(cache).remove("k4");
149  1 verify(cache, never()).remove("k3");
150    }
151   
 
152  1 toggle @Test
153    public void clearFromColorTheme() throws Exception
154    {
155    // Mocks
156  1 LESSSkinFileResourceReference file1 = createLESSSkinFileResourceReference("file1");
157  1 LESSSkinFileResourceReference file2 = createLESSSkinFileResourceReference("file2");
158   
159  1 when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin1")),
160    eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k1");
161  1 when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin1")),
162    eq(new NamedColorThemeReference("colorTheme2")), eq(true))).thenReturn("k3");
163  1 when(cacheKeyFactory.getCacheKey(eq(file2), eq(new FSSkinReference("skin2")),
164    eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k4");
165    // Add the first one twice
166  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
167    new NamedColorThemeReference("colorTheme1"), "css1");
168  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
169    new NamedColorThemeReference("colorTheme1"), "css1");
170   
171    // Others
172  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
173    new NamedColorThemeReference("colorTheme2"), "css2");
174  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file2"), new FSSkinReference("skin2"),
175    new NamedColorThemeReference("colorTheme1"), "css3");
176   
177    // Test
178  1 mocker.getComponentUnderTest().clearFromColorTheme(new NamedColorThemeReference("colorTheme1"));
179   
180    // Verify
181  1 verify(cache, times(1)).remove("k1");
182  1 verify(cache).remove("k4");
183  1 verify(cache, never()).remove("k3");
184    }
185   
 
186  1 toggle @Test
187    public void clearFromLESSResource() throws Exception
188    {
189    // Mocks
190  1 LESSSkinFileResourceReference file1 = createLESSSkinFileResourceReference("file1");
191  1 LESSSkinFileResourceReference file2 = createLESSSkinFileResourceReference("file2");
192   
193  1 when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin1")),
194    eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k1");
195  1 when(cacheKeyFactory.getCacheKey(eq(file2), eq(new FSSkinReference("skin1")),
196    eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k3");
197  1 when(cacheKeyFactory.getCacheKey(eq(file1), eq(new FSSkinReference("skin2")),
198    eq(new NamedColorThemeReference("colorTheme1")), eq(true))).thenReturn("k4");
199   
200    // Add the first one twice
201  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
202    new NamedColorThemeReference("colorTheme1"), "css1");
203  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin1"),
204    new NamedColorThemeReference("colorTheme1"), "css1");
205   
206    // Others
207  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file2"), new FSSkinReference("skin1"),
208    new NamedColorThemeReference("colorTheme1"), "css");
209  1 mocker.getComponentUnderTest().set(createLESSSkinFileResourceReference("file1"), new FSSkinReference("skin2"),
210    new NamedColorThemeReference("colorTheme1"), "css3");
211   
212    // Test
213  1 mocker.getComponentUnderTest().clearFromLESSResource(createLESSSkinFileResourceReference("file1"));
214   
215    // Verify
216  1 verify(cache, times(1)).remove("k1");
217  1 verify(cache).remove("k4");
218  1 verify(cache, never()).remove("k3");
219    }
220   
221    }