1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Date; |
24 |
|
import java.util.List; |
25 |
|
import java.util.ListResourceBundle; |
26 |
|
|
27 |
|
import org.jmock.Mock; |
28 |
|
import org.jmock.core.Invocation; |
29 |
|
import org.jmock.core.stub.CustomStub; |
30 |
|
|
31 |
|
import com.xpn.xwiki.XWiki; |
32 |
|
import com.xpn.xwiki.XWikiContext; |
33 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
34 |
|
import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase; |
35 |
|
|
36 |
|
|
37 |
|
@link |
38 |
|
|
39 |
|
@version |
40 |
|
|
|
|
| 98.4% |
Uncovered Elements: 2 (124) |
Complexity: 21 |
Complexity Density: 0.21 |
|
41 |
|
public class XWikiMessageToolTest extends AbstractBridgedXWikiComponentTestCase |
42 |
|
{ |
43 |
|
private Mock mockXWiki; |
44 |
|
|
45 |
|
private XWikiMessageTool tool; |
46 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
47 |
11 |
@Override... |
48 |
|
protected void setUp() throws Exception |
49 |
|
{ |
50 |
11 |
super.setUp(); |
51 |
|
|
52 |
11 |
this.mockXWiki = mock(XWiki.class, new Class[] {}, new Object[] {}); |
53 |
11 |
getContext().setWiki((XWiki) this.mockXWiki.proxy()); |
54 |
|
|
55 |
11 |
this.mockXWiki.stubs().method("getDefaultLanguage").will(returnValue("en")); |
56 |
|
|
57 |
11 |
this.tool = new XWikiMessageTool(new TestResources(), getContext()); |
58 |
|
} |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
60 |
|
public class TestResources extends ListResourceBundle |
61 |
|
{ |
62 |
|
private final Object[][] contents = {{"key", "value"}}; |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
4 |
@Override... |
65 |
|
public Object[][] getContents() |
66 |
|
{ |
67 |
4 |
return contents; |
68 |
|
} |
69 |
|
} |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
71 |
2 |
private XWikiDocument createDocument(String name, String content, String language, String defaultLanguage,... |
72 |
|
boolean isNew) |
73 |
|
{ |
74 |
2 |
XWikiDocument doc = new XWikiDocument(); |
75 |
|
|
76 |
2 |
doc.setFullName(name); |
77 |
2 |
doc.setContent(content); |
78 |
2 |
doc.setLanguage(language); |
79 |
2 |
doc.setDefaultLanguage(defaultLanguage); |
80 |
2 |
doc.setNew(isNew); |
81 |
|
|
82 |
2 |
return doc; |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
5 |
private XWikiDocument createDocument(long id, String name, String content, boolean isNew)... |
86 |
|
{ |
87 |
5 |
return (XWikiDocument) createMockDocument(id, name, content, isNew).proxy(); |
88 |
|
} |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
90 |
7 |
private Mock createMockDocument(long id, String name, String content, boolean isNew)... |
91 |
|
{ |
92 |
7 |
Mock mockDocument = mock(XWikiDocument.class); |
93 |
7 |
XWikiDocument document = (XWikiDocument) mockDocument.proxy(); |
94 |
7 |
mockDocument.stubs().method("getTranslatedDocument").will(returnValue(document)); |
95 |
7 |
mockDocument.stubs().method("isNew").will(returnValue(isNew)); |
96 |
7 |
mockDocument.stubs().method("getId").will(returnValue(new Long(id))); |
97 |
7 |
mockDocument.stubs().method("getDate").will(returnValue(new Date())); |
98 |
7 |
mockDocument.stubs().method("getContent").will(returnValue(content)); |
99 |
7 |
mockDocument.stubs().method("getFullName").will(returnValue(name)); |
100 |
7 |
mockDocument.stubs().method("getRealLanguage").will(returnValue("en")); |
101 |
7 |
return mockDocument; |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
104 |
2 |
private XWikiDocument createDocumentWithTrans(long id, String name, String content, String transContent,... |
105 |
|
boolean isNew) |
106 |
|
{ |
107 |
2 |
return (XWikiDocument) createMockDocumentWithTrans(id, name, content, transContent, isNew).proxy(); |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
110 |
2 |
private Mock createMockDocumentWithTrans(long id, String name, String content, String transContent, boolean isNew)... |
111 |
|
{ |
112 |
2 |
Mock mockDocument = mock(XWikiDocument.class); |
113 |
2 |
final XWikiDocument document = (XWikiDocument) mockDocument.proxy(); |
114 |
2 |
final XWikiDocument transdocument = createDocument(name, transContent, "fr", "", false); |
115 |
2 |
mockDocument.stubs().method("getTranslatedDocument").will(new CustomStub("Implements getTranslatedDocument") |
116 |
|
{ |
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
117 |
8 |
@Override... |
118 |
|
public Object invoke(Invocation invocation) throws Throwable |
119 |
|
{ |
120 |
8 |
if (invocation.parameterValues.size() == 1) { |
121 |
6 |
XWikiContext context = (XWikiContext) invocation.parameterValues.get(0); |
122 |
6 |
String lang = context.getLanguage(); |
123 |
6 |
if ("fr".equals(lang)) |
124 |
2 |
return transdocument; |
125 |
|
else |
126 |
4 |
return document; |
127 |
|
} else { |
128 |
2 |
String lang = (String) invocation.parameterValues.get(0); |
129 |
2 |
if ("fr".equals(lang)) |
130 |
0 |
return transdocument; |
131 |
|
else |
132 |
2 |
return document; |
133 |
|
} |
134 |
|
} |
135 |
|
}); |
136 |
2 |
mockDocument.stubs().method("isNew").will(returnValue(isNew)); |
137 |
2 |
mockDocument.stubs().method("getId").will(returnValue(new Long(id))); |
138 |
2 |
mockDocument.stubs().method("getDate").will(returnValue(new Date())); |
139 |
2 |
mockDocument.stubs().method("getContent").will(returnValue(content)); |
140 |
2 |
mockDocument.stubs().method("getFullName").will(returnValue(name)); |
141 |
2 |
mockDocument.stubs().method("getLanguage").will(returnValue("")); |
142 |
2 |
mockDocument.stubs().method("getDefaultLanguage").will(returnValue("en")); |
143 |
2 |
mockDocument.stubs().method("getRealLanguage").will(returnValue("en")); |
144 |
2 |
return mockDocument; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
152 |
1 |
public void testGetWhenPreferenceDoesNotExist()... |
153 |
|
{ |
154 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue(null)); |
155 |
1 |
this.mockXWiki.stubs().method("Param").will(returnValue(null)); |
156 |
1 |
this.mockXWiki.stubs().method("getDefaultLanguage").will(returnValue("en")); |
157 |
|
|
158 |
1 |
assertEquals("invalid", this.tool.get("invalid")); |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
161 |
1 |
public void testGetWhenNoTranslationAvailable()... |
162 |
|
{ |
163 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue(null)); |
164 |
1 |
this.mockXWiki.stubs().method("Param").will(returnValue(null)); |
165 |
|
|
166 |
1 |
assertEquals("value", this.tool.get("key")); |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
172 |
1 |
public void testGetWhenKeyIsNull()... |
173 |
|
{ |
174 |
1 |
assertNull(this.tool.get(null)); |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
177 |
1 |
public void testGetWhenInXWikiPreferences()... |
178 |
|
{ |
179 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue("Space1.Doc1, Space2.Doc2")); |
180 |
1 |
this.mockXWiki.stubs().method("getDocument").with(eq("Space1.Doc1"), ANYTHING) |
181 |
|
.will(returnValue(createDocument(111111L, "Space1.Doc1", "somekey=somevalue", false))); |
182 |
1 |
this.mockXWiki |
183 |
|
.stubs() |
184 |
|
.method("getDocument") |
185 |
|
.with(eq("Space2.Doc2"), ANYTHING) |
186 |
|
.will( |
187 |
|
returnValue(createDocument(222222L, "Space2.Doc2", "someKey=someValue\n" |
188 |
|
+ "keyInXWikiPreferences=eureka", false))); |
189 |
|
|
190 |
1 |
assertEquals("eureka", this.tool.get("keyInXWikiPreferences")); |
191 |
|
} |
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
193 |
1 |
public void testGetWhenInXWikiConfigurationFile()... |
194 |
|
{ |
195 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue(null)); |
196 |
1 |
this.mockXWiki.stubs().method("Param").will(returnValue("Space1.Doc1")); |
197 |
1 |
this.mockXWiki.stubs().method("getDocument").with(eq("Space1.Doc1"), ANYTHING) |
198 |
|
.will(returnValue(createDocument(111111L, "Space1.Doc1", "keyInXWikiCfg=gotcha", false))); |
199 |
|
|
200 |
1 |
assertEquals("gotcha", this.tool.get("keyInXWikiCfg")); |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
206 |
1 |
public void testGetWithParameters()... |
207 |
|
{ |
208 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue(null)); |
209 |
1 |
this.mockXWiki.stubs().method("Param").will(returnValue("Space1.Doc1")); |
210 |
1 |
this.mockXWiki |
211 |
|
.stubs() |
212 |
|
.method("getDocument") |
213 |
|
.with(eq("Space1.Doc1"), ANYTHING) |
214 |
|
.will( |
215 |
|
returnValue(createDocument(111111L, "Space1.Doc1", |
216 |
|
"key=We have {0} new documents with {1} objects. {2}", false))); |
217 |
|
|
218 |
1 |
List<String> params = new ArrayList<String>(); |
219 |
1 |
params.add("12"); |
220 |
1 |
params.add("3"); |
221 |
|
|
222 |
1 |
assertEquals("We have 12 new documents with 3 objects. {2}", this.tool.get("key", params)); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
228 |
1 |
public void testGetDocumentBundlesWhenDocumentDoesNotExist()... |
229 |
|
{ |
230 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue("Space1.Doc1")); |
231 |
1 |
this.mockXWiki.stubs().method("getDocument").with(eq("Space1.Doc1"), ANYTHING) |
232 |
|
.will(returnValue(createDocument(111111L, "Space1.Doc1", "", true))); |
233 |
1 |
List<XWikiDocument> docs = this.tool.getDocumentBundles(); |
234 |
1 |
assertEquals(0, docs.size()); |
235 |
|
} |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
237 |
1 |
public void testGetReturnsFromCacheWhenCalledTwice()... |
238 |
|
{ |
239 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue("Space1.Doc1")); |
240 |
|
|
241 |
1 |
Mock document = createMockDocument(11111L, "Space1.Doc1", "key=value", false); |
242 |
|
|
243 |
1 |
this.mockXWiki.stubs().method("getDocument").with(eq("Space1.Doc1"), ANYTHING) |
244 |
|
.will(returnValue(document.proxy())); |
245 |
|
|
246 |
|
|
247 |
1 |
this.tool.get("key"); |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
1 |
document.expects(never()).method("getContent"); |
252 |
1 |
this.tool.get("key"); |
253 |
|
} |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
255 |
1 |
public void testGetWhenDocumentModifiedAfterItIsInCache()... |
256 |
|
{ |
257 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue("Space1.Doc1")); |
258 |
|
|
259 |
1 |
Mock document = createMockDocument(11111L, "Space1.Doc1", "key=value", false); |
260 |
|
|
261 |
1 |
this.mockXWiki.stubs().method("getDocument").with(eq("Space1.Doc1"), ANYTHING) |
262 |
|
.will(returnValue(document.proxy())); |
263 |
|
|
264 |
|
|
265 |
1 |
assertEquals("modifiedKey", this.tool.get("modifiedKey")); |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
1 |
document.stubs().method("getContent").will(returnValue("modifiedKey=found")); |
270 |
1 |
document.stubs().method("getDate").will(returnValue(new Date(System.currentTimeMillis() + 1000L))); |
271 |
|
|
272 |
|
|
273 |
1 |
assertEquals("found", this.tool.get("modifiedKey")); |
274 |
|
} |
275 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
276 |
1 |
public void testGetWhenWithTranslation()... |
277 |
|
{ |
278 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue("Space1.Doc1")); |
279 |
1 |
this.mockXWiki |
280 |
|
.stubs() |
281 |
|
.method("getDocument") |
282 |
|
.with(eq("Space1.Doc1"), ANYTHING) |
283 |
|
.will( |
284 |
|
returnValue(createDocumentWithTrans(111111L, "Space1.Doc1", "somekey=somevalue\nsomekey2=somevalue2", |
285 |
|
"somekey=somevaluetrans", false))); |
286 |
|
|
287 |
1 |
getContext().setLanguage("en"); |
288 |
1 |
assertEquals("somevalue", this.tool.get("somekey")); |
289 |
1 |
assertEquals("somevalue2", this.tool.get("somekey2")); |
290 |
|
|
291 |
|
|
292 |
1 |
getContext().setLanguage("fr"); |
293 |
1 |
this.mockXWiki.stubs().method("getDefaultLanguage").will(returnValue("en")); |
294 |
1 |
assertEquals("somevaluetrans", this.tool.get("somekey")); |
295 |
1 |
assertEquals("somevalue2", this.tool.get("somekey2")); |
296 |
|
} |
297 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
298 |
1 |
public void testGetWhenWithUTF8Translation()... |
299 |
|
{ |
300 |
1 |
this.mockXWiki.stubs().method("getXWikiPreference").will(returnValue("Space1.Doc1")); |
301 |
1 |
this.mockXWiki |
302 |
|
.stubs() |
303 |
|
.method("getDocument") |
304 |
|
.with(eq("Space1.Doc1"), ANYTHING) |
305 |
|
.will( |
306 |
|
returnValue(createDocumentWithTrans(111111L, "Space1.Doc1", |
307 |
|
"somekey=some\u00E9value\nsomekey2=some\\u00E9value2", "somekey=somevaluetrans", false))); |
308 |
|
|
309 |
1 |
assertEquals("some\u00E9value", this.tool.get("somekey")); |
310 |
1 |
assertEquals("some\u00E9value2", this.tool.get("somekey2")); |
311 |
|
} |
312 |
|
} |