Source code: com/xpn/xwiki/test/PackageTest.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2005 Jérémi Joslin, XpertNet, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details, published at
15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
16 * root folder of this distribution.
17 */
18
19 package com.xpn.xwiki.test;
20
21 import com.xpn.xwiki.plugin.packaging.Package;
22 import com.xpn.xwiki.plugin.packaging.DocumentInfo;
23 import com.xpn.xwiki.doc.XWikiDocument;
24 import com.xpn.xwiki.XWikiException;
25 import org.apache.velocity.VelocityContext;
26
27 import java.io.IOException;
28 import java.io.ByteArrayOutputStream;
29 import java.util.List;
30
31
32 public class PackageTest extends HibernateTestCase {
33
34
35 public void setUp() throws Exception {
36 super.setUp();
37 prepareData();
38 }
39
40 public void prepareData() throws XWikiException {
41 XWikiDocument doc = new XWikiDocument("Test", "first");
42 doc.setContent("blop, first test page");
43 getXWiki().saveDocument(doc, getXWikiContext());
44
45 doc = new XWikiDocument("Test", "second");
46 doc.setContent("blop, second test page");
47 getXWiki().saveDocument(doc, getXWikiContext());
48
49 doc = new XWikiDocument("Test", "third");
50 doc.setContent("blop, third test page");
51 getXWiki().saveDocument(doc, getXWikiContext());
52
53 getXWikiContext().put("vcontext", new VelocityContext());
54 }
55
56 public void testExportWiki() throws IOException, XWikiException {
57 Package myPackage = new Package();
58 myPackage.setWithVersions(false);
59 myPackage.addAllWikiDocuments(getXWikiContext());
60 assertEquals(3, myPackage.getFiles().size());
61 testDocName("Test.first", myPackage.getFiles());
62 testDocName("Test.second", myPackage.getFiles());
63 testDocName("Test.third", myPackage.getFiles());
64 assertEquals(myPackage.isBackupPack(), true);
65 }
66
67
68 public void testPackageConfig() throws XWikiException, IOException {
69 Package myPackage = new Package();
70 ByteArrayOutputStream os = new ByteArrayOutputStream();
71
72 myPackage.setName("Package Test");
73 myPackage.setDescription("Package Description");
74 myPackage.setLicence("GPL");
75 myPackage.setAuthorName("Jeremi Joslin");
76 myPackage.add("Test.first", getXWikiContext());
77 myPackage.add("Test.third", getXWikiContext());
78 myPackage.export(os, getXWikiContext());
79
80 Package myOtherPackage = new Package();
81 myOtherPackage.Import(os.toByteArray(), getXWikiContext());
82 assertEquals(myOtherPackage.getName(), myPackage.getName());
83 assertEquals(myOtherPackage.getDescription(), myPackage.getDescription());
84 assertEquals(myOtherPackage.getLicence(), myPackage.getLicence());
85 assertEquals(myOtherPackage.getAuthorName(), myPackage.getAuthorName());
86 testDocName("Test.first", myOtherPackage.getFiles());
87 testDocName("Test.third", myOtherPackage.getFiles());
88 assertEquals(myOtherPackage.isBackupPack(), false);
89
90 testImportPackage(myOtherPackage);
91
92 myOtherPackage = new Package();
93 myOtherPackage.Import(os.toByteArray(), getXWikiContext());
94 testOverwrite(myOtherPackage);
95 }
96
97 public void testImportPackage(Package pack) throws XWikiException {
98 // Setup database xwikitest2
99 getXWikiContext().setDatabase("xwikitest2");
100 StoreHibernateTest.cleanUp(getXWikiContext().getWiki().getHibernateStore(), getXWikiContext());
101
102 XWikiDocument doc = getXWikiContext().getWiki().getDocument("Test.first", getXWikiContext());
103 assertTrue(doc.isNew());
104
105 pack.install(getXWikiContext());
106
107 doc = getXWikiContext().getWiki().getDocument("Test.first", getXWikiContext());
108 assertFalse(doc.isNew());
109
110 doc = getXWikiContext().getWiki().getDocument("Test.third", getXWikiContext());
111 assertFalse(doc.isNew());
112
113 }
114
115 public void testOverwrite(Package pack) throws XWikiException {
116 XWikiDocument doc = getXWikiContext().getWiki().getDocument("Test.third", getXWikiContext());
117 doc.setContent("test overwrite");
118 getXWiki().saveDocument(doc, getXWikiContext());
119
120 int ret = pack.testInstall(getXWikiContext());
121 assertEquals(ret, DocumentInfo.INSTALL_ALREADY_EXIST);
122 for (int i = 0; i < pack.getFiles().size(); i++)
123 assertEquals(((DocumentInfo)pack.getFiles().get(i)).testInstall(getXWikiContext()), DocumentInfo.INSTALL_ALREADY_EXIST);
124 ret = pack.install(getXWikiContext());
125 assertEquals(ret, DocumentInfo.INSTALL_OK);
126
127 doc = getXWikiContext().getWiki().getDocument("Test.third", getXWikiContext());
128 assertEquals("test overwrite", doc.getContent());
129
130
131 for (int i = 0; i < pack.getFiles().size(); i++)
132 ((DocumentInfo)pack.getFiles().get(i)).setAction(DocumentInfo.ACTION_OVERWRITE);
133 ret = pack.install(getXWikiContext());
134 assertEquals(ret, DocumentInfo.INSTALL_OK);
135
136 doc = getXWikiContext().getWiki().getDocument("Test.third", getXWikiContext());
137 assertEquals("blop, third test page", doc.getContent());
138
139 }
140
141 public void testDocName(String fileName, List files)
142 {
143 for (int i = 0; i < files.size(); i++)
144 {
145 DocumentInfo doc = (DocumentInfo) files.get(i);
146 if (doc.getFullName().compareTo(fileName) == 0)
147 return;
148 }
149 assertTrue("Document not found: " + fileName, false);
150 }
151
152
153
154 public void testPackageAPI()
155 {
156
157 }
158
159
160
161 }