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

File ImportTest.java

 

Code metrics

12
205
12
1
501
335
19
0.09
17.08
12
1.58

Classes

Class Line # Actions
ImportTest 58 205 0% 19 0
1.0100%
 

Contributing tests

This file is covered by 7 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   
21    package com.xpn.xwiki.plugin.packaging;
22   
23    import java.io.File;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.HashMap;
27    import java.util.Iterator;
28    import java.util.List;
29    import java.util.Locale;
30    import java.util.Map;
31   
32    import org.apache.commons.io.FileUtils;
33    import org.jmock.Mock;
34    import org.jmock.core.Invocation;
35    import org.jmock.core.stub.CustomStub;
36    import org.jmock.core.stub.VoidStub;
37    import org.xwiki.extension.ExtensionId;
38    import org.xwiki.extension.event.ExtensionInstalledEvent;
39    import org.xwiki.extension.repository.InstalledExtensionRepository;
40    import org.xwiki.extension.repository.LocalExtensionRepository;
41    import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
42    import org.xwiki.localization.LocalizationContext;
43    import org.xwiki.model.reference.DocumentReference;
44    import org.xwiki.observation.EventListener;
45    import org.xwiki.observation.ObservationManager;
46   
47    import com.xpn.xwiki.XWiki;
48    import com.xpn.xwiki.XWikiConfig;
49    import com.xpn.xwiki.XWikiContext;
50    import com.xpn.xwiki.doc.XWikiDocument;
51    import com.xpn.xwiki.store.XWikiHibernateRecycleBinStore;
52    import com.xpn.xwiki.store.XWikiHibernateStore;
53    import com.xpn.xwiki.store.XWikiHibernateVersioningStore;
54    import com.xpn.xwiki.store.XWikiStoreInterface;
55    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
56    import com.xpn.xwiki.user.api.XWikiRightService;
57   
 
58    public class ImportTest extends AbstractPackageTest
59    {
60    private Package pack;
61   
62    private XWiki xwiki;
63   
64    private Mock mockXWikiStore;
65   
66    private Mock mockRecycleBinStore;
67   
68    private Mock mockXWikiVersioningStore;
69   
70    private Mock mockRightService;
71   
72    private Map<String, XWikiDocument> docs = new HashMap<String, XWikiDocument>();
73   
 
74  7 toggle @Override
75    protected void setUp() throws Exception
76    {
77  7 super.setUp();
78   
79  7 this.pack = new Package();
80  7 this.xwiki = new XWiki();
81  7 getContext().setWiki(this.xwiki);
82  7 this.xwiki.setConfig(new XWikiConfig());
83   
84  7 Mock mockLocalizationContext = registerMockComponent(LocalizationContext.class);
85  7 mockLocalizationContext.stubs().method("getCurrentLocale").will(returnValue(Locale.ROOT));
86   
87    // mock a store that would also handle translations
88  7 this.mockXWikiStore =
89    mock(XWikiHibernateStore.class, new Class[] {XWiki.class, XWikiContext.class}, new Object[] {this.xwiki,
90    getContext()});
91  7 this.mockXWikiStore.stubs().method("loadXWikiDoc")
92    .will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc")
93    {
 
94  74 toggle @Override
95    public Object invoke(Invocation invocation) throws Throwable
96    {
97  74 XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
98  74 String documentKey = shallowDoc.getFullName();
99  74 if (!shallowDoc.getLanguage().equals("")) {
100  19 documentKey += "." + shallowDoc.getLanguage();
101    }
102  74 if (docs.containsKey(documentKey)) {
103  29 return docs.get(documentKey);
104    } else {
105  45 return shallowDoc;
106    }
107    }
108    });
109  7 this.mockXWikiStore.stubs().method("saveXWikiDoc")
110    .will(new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc")
111    {
 
112  16 toggle @Override
113    public Object invoke(Invocation invocation) throws Throwable
114    {
115  16 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
116  16 document.setNew(false);
117  16 document.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
118    // if this is a translated document, append a language prefix
119  16 String documentKey = document.getFullName();
120  16 if (!document.getLanguage().equals("")) {
121  7 documentKey += "." + document.getLanguage();
122    }
123  16 docs.put(documentKey, document);
124  16 return null;
125    }
126    });
127  7 this.mockXWikiStore.stubs().method("deleteXWikiDoc")
128    .will(new CustomStub("Implements XWikiStoreInterface.deleteXWikiDoc")
129    {
 
130  3 toggle @Override
131    public Object invoke(Invocation invocation) throws Throwable
132    {
133  3 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
134    // delete the document from the map
135  3 String documentKey = document.getFullName();
136  3 if (!document.getLanguage().equals("")) {
137  1 documentKey += "." + document.getLanguage();
138    }
139  3 docs.remove(documentKey);
140  3 return null;
141    }
142    });
143  7 this.mockXWikiStore.stubs().method("getTranslationList")
144    .will(new CustomStub("Implements XWikiStoreInterface.getTranslationList")
145    {
 
146  3 toggle @Override
147    public Object invoke(Invocation invocation) throws Throwable
148    {
149  3 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
150    // search for this document in the map and return it's translations
151  3 List translationList = new ArrayList();
152  8 for (Iterator pairsIt = docs.entrySet().iterator(); pairsIt.hasNext();) {
153  5 Map.Entry currentEntry = (Map.Entry) pairsIt.next();
154  5 if (((String) currentEntry.getKey()).startsWith(document.getFullName())
155    && !((XWikiDocument) currentEntry.getValue()).getLanguage().equals("")) {
156    // yeeey, it's a translation
157  2 translationList.add(((XWikiDocument) currentEntry.getValue()).getLanguage());
158    }
159    }
160  3 return translationList;
161    }
162    });
163  7 this.mockXWikiStore.stubs().method("injectCustomMapping").will(returnValue(false));
164   
165  7 this.mockRecycleBinStore =
166    mock(XWikiHibernateRecycleBinStore.class, new Class[] {XWikiContext.class}, new Object[] {getContext()});
167  7 this.mockRecycleBinStore.stubs().method("saveToRecycleBin").will(VoidStub.INSTANCE);
168   
169  7 this.mockXWikiVersioningStore =
170    mock(XWikiHibernateVersioningStore.class, new Class[] {XWiki.class, XWikiContext.class}, new Object[] {
171    this.xwiki, getContext()});
172  7 this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
173  7 this.mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null));
174   
175  7 this.xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
176  7 this.xwiki.setRecycleBinStore((XWikiHibernateRecycleBinStore) this.mockRecycleBinStore.proxy());
177  7 this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore.proxy());
178   
179    // mock the right service
180  7 this.mockRightService = mock(XWikiRightService.class);
181  7 this.mockRightService.stubs().method("checkAccess").will(returnValue(true));
182  7 this.mockRightService.stubs().method("hasWikiAdminRights").will(returnValue(true));
183  7 this.mockRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
184  7 this.xwiki.setRightService((XWikiRightService) this.mockRightService.proxy());
185    }
186   
187    /**
188    * Test the regular document import.
189    *
190    * @throws Exception
191    */
 
192  1 toggle public void testImportDocument() throws Exception
193    {
194  1 XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
195  1 doc1.setDefaultLanguage("en");
196   
197  1 byte[] zipFile = this.createZipFile(new XWikiDocument[] {doc1}, new String[] {"ISO-8859-1"}, null);
198   
199    // make sure no data is in the packager from the other tests run
200  1 this.pack = new Package();
201    // import and install this document
202  1 this.pack.Import(zipFile, getContext());
203  1 this.pack.install(getContext());
204   
205    // check if it is there
206  1 XWikiDocument foundDocument =
207    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImport"), getContext());
208  1 assertFalse(foundDocument.isNew());
209   
210  1 XWikiDocument nonExistingDocument =
211    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportNonexisting"), getContext());
212  1 assertTrue(nonExistingDocument.isNew());
213   
214  1 XWikiDocument foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
215  1 assertSame(foundDocument, foundTranslationDocument);
216   
217  1 XWikiDocument doc1Translation = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
218  1 doc1Translation.setLanguage("fr");
219  1 doc1Translation.setDefaultLanguage("en");
220  1 this.xwiki.saveDocument(doc1Translation, getContext());
221  1 foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
222  1 assertNotSame(foundDocument, foundTranslationDocument);
223    }
224   
225    /**
226    * Test the regular document import when the XAR is tagged as extension.
227    *
228    * @throws Exception
229    */
 
230  1 toggle public void testImportExtension() throws Exception
231    {
232  1 ExtensionId extensionId = new ExtensionId("test", "1.0");
233   
234  1 XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
235  1 doc1.setDefaultLanguage("en");
236   
237  1 byte[] zipFile = this.createZipFile(new XWikiDocument[] {doc1}, new String[] {"ISO-8859-1"}, extensionId);
238   
239    // Store the extension in the local repository
240  1 DefaultLocalExtension localExtension = new DefaultLocalExtension(null, extensionId, "xar");
241  1 File file = File.createTempFile("temp", ".xar");
242  1 FileUtils.writeByteArrayToFile(file, zipFile);
243  1 localExtension.setFile(file);
244  1 LocalExtensionRepository localeRepository = getComponentManager().getInstance(LocalExtensionRepository.class);
245  1 localeRepository.storeExtension(localExtension);
246   
247    // Listen to extension installed event
248  1 Mock extensionListener = mock(EventListener.class);
249  1 extensionListener.stubs().method("getEvents").will(returnValue(Arrays.asList(new ExtensionInstalledEvent())));
250  1 extensionListener.stubs().method("getName").will(returnValue("extension installed listener"));
251  1 extensionListener.expects(once()).method("onEvent");
252  1 ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
253  1 observationManager.addListener((EventListener) extensionListener.proxy());
254   
255    // make sure no data is in the packager from the other tests run
256  1 this.pack = new Package();
257    // import and install this document
258  1 this.pack.Import(zipFile, getContext());
259  1 this.pack.install(getContext());
260   
261    // check if it is there
262  1 XWikiDocument foundDocument =
263    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImport"), getContext());
264  1 assertFalse(foundDocument.isNew());
265   
266  1 XWikiDocument nonExistingDocument =
267    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportNonexisting"), getContext());
268  1 assertTrue(nonExistingDocument.isNew());
269   
270  1 XWikiDocument foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
271  1 assertSame(foundDocument, foundTranslationDocument);
272   
273  1 XWikiDocument doc1Translation = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
274  1 doc1Translation.setLanguage("fr");
275  1 doc1Translation.setDefaultLanguage("en");
276  1 this.xwiki.saveDocument(doc1Translation, getContext());
277  1 foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
278  1 assertNotSame(foundDocument, foundTranslationDocument);
279   
280    // Check that the extension has been registered
281  1 InstalledExtensionRepository installedExtensionRepository =
282    getComponentManager().getInstance(InstalledExtensionRepository.class);
283  1 assertNotNull(installedExtensionRepository.getInstalledExtension(extensionId));
284  1 assertNotNull(installedExtensionRepository.getInstalledExtension(extensionId.getId(), "wiki:"
285    + getContext().getWikiId()));
286    }
287   
288    /**
289    * Test the regular document import with non-ascii document title.
290    *
291    * @throws Exception
292    */
 
293  1 toggle public void testImportDocumentNonAsciiTitle() throws Exception
294    {
295  1 XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "\u60A8\u597D\u4E16\u754C"));
296  1 doc1.setDefaultLanguage("zh");
297   
298  1 byte[] zipFile = this.createZipFile(new XWikiDocument[] {doc1}, new String[] {"UTF-8"}, "UTF-8", null);
299   
300    // make sure no data is in the packager from the other tests run
301  1 this.pack = new Package();
302    // import and install this document
303  1 this.pack.Import(zipFile, getContext());
304  1 this.pack.install(getContext());
305   
306    // check if it is there
307  1 XWikiDocument foundDocument =
308    this.xwiki.getDocument(new DocumentReference("Test", "Test", "\u60A8\u597D\u4E16\u754C"), getContext());
309  1 assertFalse(foundDocument.isNew());
310   
311  1 XWikiDocument nonExistingDocument =
312    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportNonexisting"), getContext());
313  1 assertTrue(nonExistingDocument.isNew());
314   
315  1 XWikiDocument foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
316  1 assertSame(foundDocument, foundTranslationDocument);
317   
318  1 XWikiDocument doc1Translation =
319    new XWikiDocument(new DocumentReference("Test", "Test", "\u60A8\u597D\u4E16\u754C"));
320  1 doc1Translation.setLanguage("fr");
321  1 doc1Translation.setDefaultLanguage("zh");
322  1 this.xwiki.saveDocument(doc1Translation, getContext());
323  1 foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
324  1 assertNotSame(foundDocument, foundTranslationDocument);
325    }
326   
327    /**
328    * Test the regular document import with non-ascii document title and non-utf8 platform encoding.
329    *
330    * @throws Exception
331    */
 
332  1 toggle public void testImportDocumentNonAsciiTitleNonUtf8PlatformEncoding() throws Exception
333    {
334  1 String oldEncoding = System.getProperty("file.encoding");
335  1 System.setProperty("file.encoding", "ISO-8859-1");
336   
337  1 XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "\u60A8\u597D\u4E16\u754C"));
338  1 doc1.setDefaultLanguage("zh");
339   
340  1 byte[] zipFile =
341    this.createZipFileUsingCommonsCompress(new XWikiDocument[] {doc1}, new String[] {"UTF-8"}, "UTF-8", null);
342   
343    // make sure no data is in the packager from the other tests run
344  1 this.pack = new Package();
345   
346    // import and install this document
347  1 this.pack.Import(zipFile, getContext());
348  1 this.pack.install(getContext());
349   
350  1 System.setProperty("file.encoding", oldEncoding);
351   
352    // check if it is there
353  1 XWikiDocument foundDocument =
354    this.xwiki.getDocument(new DocumentReference("Test", "Test", "\u60A8\u597D\u4E16\u754C"), getContext());
355  1 assertFalse(foundDocument.isNew());
356   
357  1 XWikiDocument nonExistingDocument =
358    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportNonexisting"), getContext());
359  1 assertTrue(nonExistingDocument.isNew());
360   
361  1 XWikiDocument foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
362  1 assertSame(foundDocument, foundTranslationDocument);
363   
364  1 XWikiDocument doc1Translation =
365    new XWikiDocument(new DocumentReference("Test", "Test", "\u60A8\u597D\u4E16\u754C"));
366  1 doc1Translation.setLanguage("fr");
367  1 doc1Translation.setDefaultLanguage("zh");
368  1 this.xwiki.saveDocument(doc1Translation, getContext());
369  1 foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
370  1 assertNotSame(foundDocument, foundTranslationDocument);
371    }
372   
373    /**
374    * Test the regular document import. Test XAR file built with commons compress.
375    *
376    * @throws Exception
377    */
 
378  1 toggle public void testImportDocumentXarCreatedByCommonsCompress() throws Exception
379    {
380  1 XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
381  1 doc1.setDefaultLanguage("en");
382   
383  1 byte[] zipFile =
384    this.createZipFileUsingCommonsCompress(new XWikiDocument[] {doc1}, new String[] {"ISO-8859-1"}, null);
385   
386    // make sure no data is in the packager from the other tests run
387  1 this.pack = new Package();
388    // import and install this document
389  1 this.pack.Import(zipFile, getContext());
390  1 this.pack.install(getContext());
391   
392    // check if it is there
393  1 XWikiDocument foundDocument =
394    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImport"), getContext());
395  1 assertFalse(foundDocument.isNew());
396   
397  1 XWikiDocument nonExistingDocument =
398    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportNonexisting"), getContext());
399  1 assertTrue(nonExistingDocument.isNew());
400   
401  1 XWikiDocument foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
402  1 assertSame(foundDocument, foundTranslationDocument);
403   
404  1 XWikiDocument doc1Translation = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
405  1 doc1Translation.setLanguage("fr");
406  1 doc1Translation.setDefaultLanguage("en");
407  1 this.xwiki.saveDocument(doc1Translation, getContext());
408  1 foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
409  1 assertNotSame(foundDocument, foundTranslationDocument);
410    }
411   
412    /**
413    * Test the import with document overwrite.
414    *
415    * @throws Exception
416    */
 
417  1 toggle public void testImportOverwriteDocument() throws Exception
418    {
419  1 XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "DocImportOverwrite"));
420  1 doc1.setDefaultLanguage("en");
421   
422  1 byte[] zipFile = this.createZipFile(new XWikiDocument[] {doc1}, new String[] {"ISO-8859-1"}, null);
423   
424    // make sure no data is in the packager from the other tests run
425  1 this.pack = new Package();
426    // import and install this document
427  1 this.pack.Import(zipFile, getContext());
428  1 this.pack.install(getContext());
429   
430    // check if it is there
431  1 XWikiDocument foundDocument =
432    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportOverwrite"), getContext());
433  1 assertFalse(foundDocument.isNew());
434   
435    // create the overwriting document
436  1 String newContent = "This is new content";
437  1 XWikiDocument overwritingDoc = new XWikiDocument(new DocumentReference("Test", "Test", "DocImportOverwrite"));
438  1 overwritingDoc.setContent(newContent);
439   
440  1 zipFile = this.createZipFile(new XWikiDocument[] {overwritingDoc}, new String[] {"ISO-8859-1"}, null);
441   
442    // use a new packager because we need to clean-up import data (files list, doucument data)
443  1 this.pack = new Package();
444    // import and install
445  1 this.pack.Import(zipFile, getContext());
446  1 this.pack.install(getContext());
447   
448    // check if the document is there
449  1 XWikiDocument foundOverwritingDoc =
450    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportOverwrite"), getContext());
451  1 assertFalse(foundOverwritingDoc.isNew());
452  1 assertEquals(foundOverwritingDoc.getContent(), newContent);
453  1 assertEquals(foundDocument, foundOverwritingDoc.getOriginalDocument());
454    }
455   
456    /**
457    * Test the import of translation files, with overwrite.
458    *
459    * @throws Exception
460    */
 
461  1 toggle public void testImportTranslationsOverwrite() throws Exception
462    {
463  1 XWikiDocument original = new XWikiDocument(new DocumentReference("Test", "Test", "DocTranslation"));
464  1 original.setDefaultLanguage("en");
465  1 original.setTranslation(0);
466  1 XWikiDocument translation = new XWikiDocument(new DocumentReference("Test", "Test", "DocTranslation"));
467  1 translation.setLanguage("fr");
468  1 translation.setDefaultLanguage("en");
469  1 translation.setTranslation(1);
470  1 translation.setOriginalDocument(original);
471   
472    // import and install those twice with tests
473  1 byte[] zipFile =
474    this.createZipFile(new XWikiDocument[] {original, translation}, new String[] {"ISO-8859-1", "ISO-8859-1"},
475    null);
476   
477    // make sure no data is in the packager from the other tests run
478  1 this.pack = new Package();
479  1 this.pack.Import(zipFile, getContext());
480  1 this.pack.install(getContext());
481  1 XWikiDocument foundDocument =
482    this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocTranslation"), getContext());
483  1 assertFalse(foundDocument.isNew());
484    // get the translation
485  1 XWikiDocument translationDoc = foundDocument.getTranslatedDocument("fr", getContext());
486  1 assertFalse(translationDoc.isNew());
487   
488    // use a new packager because we need to clean-up import data (files list, doucument data)
489  1 this.pack = new Package();
490    // import again and do the same tests
491  1 this.pack.Import(zipFile, getContext());
492  1 this.pack.install(getContext());
493  1 foundDocument = this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocTranslation"), getContext());
494    // might not be the best method to test the document is in the store though...
495  1 assertFalse(foundDocument.isNew());
496    // get the translation
497  1 translationDoc = foundDocument.getTranslatedDocument("fr", getContext());
498  1 assertFalse(translationDoc.isNew());
499    }
500   
501    }