1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
58 |
|
|
59 |
|
@version |
60 |
|
|
|
|
| 86.8% |
Uncovered Elements: 15 (114) |
Complexity: 17 |
Complexity Density: 0.19 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
71 |
1 |
@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 |
|
{ |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
82 |
2 |
@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 |
|
{ |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
95 |
5 |
@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 |
|
{ |
|
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
108 |
2 |
@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 |
|
{ |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
125 |
34 |
@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 |
|
{ |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
138 |
3 |
@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 |
|
{ |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
151 |
2 |
@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 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
164 |
48 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (50) |
Complexity: 1 |
Complexity Density: 0.02 |
1PASS
|
|
173 |
1 |
@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 |
|
|
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 |
|
} |