1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File XWikiMessageToolTest.java

 

Code metrics

6
101
19
2
312
219
22
0.22
5.32
9.5
1.16

Classes

Class Line # Actions
XWikiMessageToolTest 41 100 0% 21 2
0.98387198.4%
XWikiMessageToolTest.TestResources 60 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 11 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 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    * Unit tests for the {@link com.xpn.xwiki.web.XWikiMessageTool} class.
38    *
39    * @version $Id: 15777d14ad86300c259772910af84b92ddd70e3c $
40    */
 
41    public class XWikiMessageToolTest extends AbstractBridgedXWikiComponentTestCase
42    {
43    private Mock mockXWiki;
44   
45    private XWikiMessageTool tool;
46   
 
47  11 toggle @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   
 
60    public class TestResources extends ListResourceBundle
61    {
62    private final Object[][] contents = {{"key", "value"}};
63   
 
64  4 toggle @Override
65    public Object[][] getContents()
66    {
67  4 return contents;
68    }
69    }
70   
 
71  2 toggle 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   
 
85  5 toggle private XWikiDocument createDocument(long id, String name, String content, boolean isNew)
86    {
87  5 return (XWikiDocument) createMockDocument(id, name, content, isNew).proxy();
88    }
89   
 
90  7 toggle 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   
 
104  2 toggle 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   
 
110  2 toggle 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    {
 
117  8 toggle @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    // Tests
148   
149    /**
150    * When no preference exist the returned value is the value of the key.
151    */
 
152  1 toggle 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   
 
161  1 toggle 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    * When the key is null the returned value is null.
171    */
 
172  1 toggle public void testGetWhenKeyIsNull()
173    {
174  1 assertNull(this.tool.get(null));
175    }
176   
 
177  1 toggle 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   
 
193  1 toggle 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    * Validate usage of parameters in bundles
205    */
 
206  1 toggle 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    * Verify that a document listed as a bundle document that doesn't exist is not returned as a bundle document.
227    */
 
228  1 toggle 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   
 
237  1 toggle 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    // After this call, the value should be in cache.
247  1 this.tool.get("key");
248   
249    // We verify that the second time the getContent method is NOT called as the value is
250    // returned from cache
251  1 document.expects(never()).method("getContent");
252  1 this.tool.get("key");
253    }
254   
 
255  1 toggle 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    // First time get any key just to put the doc properties in cache
265  1 assertEquals("modifiedKey", this.tool.get("modifiedKey"));
266   
267    // Now modify the document content to add a new key and change the document's date. We add
268    // one second to ensure the new date is definitely newer than the old one.
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    // Even though the document has been cached it's reloaded because its date has changed
273  1 assertEquals("found", this.tool.get("modifiedKey"));
274    }
275   
 
276  1 toggle 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    // Switch to french
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   
 
298  1 toggle 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    }