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

File SpacesConfigurationSourceTest.java

 

Code metrics

16
89
9
1
234
182
17
0.19
9.89
9
1.89

Classes

Class Line # Actions
SpacesConfigurationSourceTest 61 89 0% 17 15
0.868421186.8%
 

Contributing tests

This file is covered by 1 test. .

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.configuration.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.LinkedHashMap;
27    import java.util.List;
28    import java.util.Map;
29   
30    import javax.inject.Provider;
31   
32    import org.junit.Before;
33    import org.junit.Rule;
34    import org.junit.Test;
35    import org.mockito.invocation.InvocationOnMock;
36    import org.mockito.stubbing.Answer;
37    import org.xwiki.component.manager.ComponentLookupException;
38    import org.xwiki.configuration.ConfigurationSource;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.SpaceReference;
41    import org.xwiki.model.reference.WikiReference;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43   
44    import com.xpn.xwiki.XWikiContext;
45    import com.xpn.xwiki.doc.XWikiDocument;
46   
47    import static org.junit.Assert.assertEquals;
48    import static org.junit.Assert.assertFalse;
49    import static org.junit.Assert.assertNull;
50    import static org.junit.Assert.assertTrue;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.anyString;
53    import static org.mockito.ArgumentMatchers.same;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * Unit tests for {@link org.xwiki.configuration.internal.DocumentsConfigurationSource}.
58    *
59    * @version $Id: 9ae3fb8b625bbcda66d90b210c001f53d9e30e68 $
60    */
 
61    public class SpacesConfigurationSourceTest
62    {
63    @Rule
64    public MockitoComponentMockingRule<SpacesConfigurationSource> mocker =
65    new MockitoComponentMockingRule<>(SpacesConfigurationSource.class);
66   
67    private Map<String, Map<String, String>> spacesPreferences = new HashMap<>();
68   
69    private XWikiContext xcontext;
70   
 
71  1 toggle @Before
72    public void before() throws ComponentLookupException
73    {
74  1 this.xcontext = new XWikiContext();
75  1 Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
76  1 when(xcontextProvider.get()).thenReturn(xcontext);
77   
78  1 ConfigurationSource spaceConfiguration = this.mocker.getInstance(ConfigurationSource.class, "space");
79   
80  1 when(spaceConfiguration.getProperty(any(), same(String.class))).then(new Answer<String>()
81    {
 
82  2 toggle @Override
83    public String answer(InvocationOnMock invocation) throws Throwable
84    {
85  2 Map<String, String> spacePreferences = getSpacePreferences();
86  2 if (spacePreferences != null) {
87  2 return spacePreferences.get(invocation.getArgument(0));
88    }
89   
90  0 return null;
91    }
92    });
93  1 when(spaceConfiguration.getProperty(any())).then(new Answer<Object>()
94    {
 
95  5 toggle @Override
96    public Object answer(InvocationOnMock invocation) throws Throwable
97    {
98  5 Map<String, String> spacePreferences = getSpacePreferences();
99  5 if (spacePreferences != null) {
100  5 return spacePreferences.get(invocation.getArgument(0));
101    }
102   
103  0 return null;
104    }
105    });
106  1 when(spaceConfiguration.getProperty(any(), anyString())).then(new Answer<Object>()
107    {
 
108  2 toggle @Override
109    public Object answer(InvocationOnMock invocation) throws Throwable
110    {
111  2 Map<String, String> spacePreferences = getSpacePreferences();
112  2 if (spacePreferences != null) {
113  2 String key = invocation.getArgument(0);
114  2 if (spacePreferences.containsKey(key)) {
115  2 return spacePreferences.get(key);
116    }
117    }
118   
119  0 return invocation.getArgument(1);
120    }
121    });
122   
123  1 when(spaceConfiguration.containsKey(any())).then(new Answer<Boolean>()
124    {
 
125  34 toggle @Override
126    public Boolean answer(InvocationOnMock invocation) throws Throwable
127    {
128  34 Map<String, String> spacePreferences = getSpacePreferences();
129  34 if (spacePreferences != null) {
130  34 return spacePreferences.containsKey(invocation.getArgument(0));
131    }
132   
133  0 return false;
134    }
135    });
136  1 when(spaceConfiguration.getKeys()).then(new Answer<List<String>>()
137    {
 
138  3 toggle @Override
139    public List<String> answer(InvocationOnMock invocation) throws Throwable
140    {
141  3 Map<String, String> spacePreferences = getSpacePreferences();
142  3 if (spacePreferences != null) {
143  3 return new ArrayList<>(spacePreferences.keySet());
144    }
145   
146  0 return Collections.emptyList();
147    }
148    });
149  1 when(spaceConfiguration.isEmpty()).then(new Answer<Boolean>()
150    {
 
151  2 toggle @Override
152    public Boolean answer(InvocationOnMock invocation) throws Throwable
153    {
154  2 Map<String, String> spacePreferences = getSpacePreferences();
155  2 if (spacePreferences != null) {
156  2 return spacePreferences.isEmpty();
157    }
158   
159  0 return true;
160    }
161    });
162    }
163   
 
164  48 toggle private Map<String, String> getSpacePreferences()
165    {
166  48 if (xcontext.getDoc() != null) {
167  48 return spacesPreferences.get(xcontext.getDoc().getDocumentReference().getParent().getName());
168    }
169   
170  0 return null;
171    }
172   
 
173  1 toggle @Test
174    public void containsKey() throws Exception
175    {
176  1 WikiReference wikiReference = new WikiReference("wiki");
177  1 SpaceReference space1Reference = new SpaceReference("space1", wikiReference);
178  1 SpaceReference space2Reference = new SpaceReference("space2", space1Reference);
179   
180  1 Map<String, String> space1Preferences = new LinkedHashMap<>();
181  1 space1Preferences.put("pref", "prefvalue1");
182  1 space1Preferences.put("pref1", "pref1value1");
183  1 Map<String, String> space2Preferences = new LinkedHashMap<>();
184  1 space2Preferences.put("pref", "prefvalue2");
185  1 space2Preferences.put("pref2", "pref2value2");
186  1 this.spacesPreferences.put(space1Reference.getName(), space1Preferences);
187  1 this.spacesPreferences.put(space2Reference.getName(), space2Preferences);
188   
189    // Tests
190   
191  1 ConfigurationSource spaces = this.mocker.getComponentUnderTest();
192   
193  1 assertTrue(spaces.isEmpty());
194  1 assertFalse(spaces.containsKey("nopref"));
195  1 assertEquals(Arrays.asList(), spaces.getKeys());
196  1 assertNull(spaces.getProperty("nopref"));
197  1 assertNull(spaces.getProperty("nopref", String.class));
198  1 assertEquals("defaultvalue", spaces.getProperty("nopref", "defaultvalue"));
199   
200  1 xcontext.setDoc(new XWikiDocument(new DocumentReference("document", space1Reference)));
201  1 assertFalse(spaces.isEmpty());
202  1 assertFalse(spaces.containsKey("nopref"));
203  1 assertEquals(Arrays.asList("pref", "pref1"), spaces.getKeys());
204  1 assertNull(spaces.getProperty("nopref"));
205  1 assertNull(spaces.getProperty("nopref", String.class));
206  1 assertEquals("defaultvalue", spaces.getProperty("nopref", "defaultvalue"));
207  1 assertTrue(spaces.containsKey("pref"));
208  1 assertEquals("prefvalue1", spaces.getProperty("pref"));
209  1 assertEquals("prefvalue1", spaces.getProperty("pref", String.class));
210  1 assertEquals("prefvalue1", spaces.getProperty("pref", "defaultvalue"));
211  1 assertTrue(spaces.containsKey("pref1"));
212  1 assertEquals("pref1value1", spaces.getProperty("pref1"));
213  1 assertEquals("pref1value1", spaces.getProperty("pref1", String.class));
214  1 assertFalse(spaces.containsKey("pref2"));
215  1 assertNull(spaces.getProperty("pref2"));
216   
217  1 xcontext.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
218  1 assertFalse(spaces.isEmpty());
219  1 assertFalse(spaces.containsKey("nopref"));
220  1 assertEquals(Arrays.asList("pref", "pref2", "pref1"), spaces.getKeys());
221  1 assertNull(spaces.getProperty("nopref"));
222  1 assertNull(spaces.getProperty("nopref", String.class));
223  1 assertEquals("defaultvalue", spaces.getProperty("nopref", "defaultvalue"));
224  1 assertTrue(spaces.containsKey("pref"));
225  1 assertNull(spaces.getProperty("nopref"));
226  1 assertEquals("defaultvalue", spaces.getProperty("nopref", "defaultvalue"));
227  1 assertTrue(spaces.containsKey("pref1"));
228  1 assertEquals("prefvalue2", spaces.getProperty("pref"));
229  1 assertEquals("prefvalue2", spaces.getProperty("pref", "defaultvalue"));
230  1 assertEquals("pref1value1", spaces.getProperty("pref1"));
231  1 assertTrue(spaces.containsKey("pref2"));
232  1 assertEquals("pref2value2", spaces.getProperty("pref2"));
233    }
234    }