KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > importexport > TestCmsImportExport


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/importexport/TestCmsImportExport.java,v $
3  * Date : $Date: 2006/07/19 13:19:12 $
4  * Version: $Revision: 1.17 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.importexport;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.file.CmsUser;
39 import org.opencms.file.types.CmsResourceTypeFolder;
40 import org.opencms.file.types.CmsResourceTypeXmlPage;
41 import org.opencms.i18n.CmsEncoder;
42 import org.opencms.i18n.CmsLocaleManager;
43 import org.opencms.main.OpenCms;
44 import org.opencms.report.CmsShellReport;
45 import org.opencms.security.CmsDefaultPasswordHandler;
46 import org.opencms.security.I_CmsPasswordHandler;
47 import org.opencms.staticexport.CmsLink;
48 import org.opencms.staticexport.CmsLinkTable;
49 import org.opencms.test.OpenCmsTestCase;
50 import org.opencms.test.OpenCmsTestProperties;
51 import org.opencms.util.CmsResourceTranslator;
52 import org.opencms.xml.page.CmsXmlPage;
53 import org.opencms.xml.page.CmsXmlPageFactory;
54
55 import java.util.ArrayList JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.List JavaDoc;
58
59 import junit.extensions.TestSetup;
60 import junit.framework.Test;
61 import junit.framework.TestSuite;
62
63 /**
64  * Comment for <code>TestCmsImportExport</code>.<p>
65  */

66 public class TestCmsImportExport extends OpenCmsTestCase {
67
68     /**
69      * Default JUnit constructor.<p>
70      *
71      * @param arg0 JUnit parameters
72      */

73     public TestCmsImportExport(String JavaDoc arg0) {
74
75         super(arg0);
76     }
77
78     /**
79      * Test suite for this test class.<p>
80      *
81      * @return the test suite
82      */

83     public static Test suite() {
84
85         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
86
87         TestSuite suite = new TestSuite();
88         suite.setName(TestCmsImportExport.class.getName());
89
90         suite.addTest(new TestCmsImportExport("testUserImport"));
91         suite.addTest(new TestCmsImportExport("testImportResourceTranslator"));
92         suite.addTest(new TestCmsImportExport("testImportResourceTranslatorMultipleSite"));
93
94         TestSetup wrapper = new TestSetup(suite) {
95
96             protected void setUp() {
97
98                 setupOpenCms("simpletest", "/sites/default/");
99             }
100
101             protected void tearDown() {
102
103                 removeOpenCms();
104             }
105         };
106
107         return wrapper;
108     }
109
110     /**
111      * Tests if the user passwords are imported correctly.<p>
112      * The password digests are hex-128 (legacy) encoded, and
113      * should be converted to base-64.
114      *
115      * @throws Exception if something goes wrong
116      */

117     public void testUserImport() throws Exception JavaDoc {
118
119         CmsObject cms = getCmsObject();
120         I_CmsPasswordHandler passwordHandler = new CmsDefaultPasswordHandler();
121         CmsUser user;
122
123         // check if passwords will be converted
124
if ("com.opencms.legacy.CmsLegacyPasswordHandler".equals(OpenCms.getPasswordHandler().getClass().getName())) {
125             echo("Skipping test since digest encoding is set to legacy");
126             return;
127         }
128
129         echo("Testing passwords of imported users");
130
131         // check the admin user
132
user = cms.readUser("Admin");
133         assertEquals(user.getPassword(), passwordHandler.digest("admin", "MD5", CmsEncoder.ENCODING_UTF_8));
134
135         // check the guest user
136
user = cms.readUser("Guest");
137         assertEquals(user.getPassword(), passwordHandler.digest("", "MD5", CmsEncoder.ENCODING_UTF_8));
138
139         // check the test1 user
140
user = cms.readUser("test1");
141         assertEquals(user.getPassword(), passwordHandler.digest("test1", "MD5", CmsEncoder.ENCODING_UTF_8));
142
143         // check the test2 user
144
user = cms.readUser("test2");
145         assertEquals(user.getPassword(), passwordHandler.digest("test2", "MD5", CmsEncoder.ENCODING_UTF_8));
146     }
147
148     /**
149      * Tests the resource translation during import with multiple sites.<p>
150      *
151      * @throws Exception if something goes wrong
152      */

153     public void testImportResourceTranslatorMultipleSite() throws Exception JavaDoc {
154
155         echo("Testing resource translator with multiple sites");
156         CmsObject cms = getCmsObject();
157
158         cms.getRequestContext().setSiteRoot("/");
159
160         // create a second site
161
cms.createResource("/sites/mysite", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
162         cms.unlockResource("/sites/mysite");
163
164         cms.createResource("/sites/othersite", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
165         cms.unlockResource("/sites/othersite");
166
167         CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator();
168
169         CmsResourceTranslator folderTranslator = new CmsResourceTranslator(new String JavaDoc[] {
170             "s#^/sites(.*)#/sites$1#",
171             "s#^/system(.*)#/system$1#",
172             "s#^/content/bodys(.*)#/system/bodies$1#",
173             "s#^/pics(.*)#/system/galleries/pics$1#",
174             "s#^/download(.*)#/system/galleries/download$1#",
175             "s#^/externallinks(.*)#/system/galleries/externallinks$1#",
176             "s#^/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#",
177             "s#^/content(.*)#/system$1#",
178             "s#^/othertest(.*)#/sites/othersite$1#",
179             "s#^/(.*)#/sites/mysite/$1#"}, false);
180
181         // set modified folder translator
182
OpenCms.getResourceManager().setTranslators(folderTranslator, OpenCms.getResourceManager().getFileTranslator());
183
184         // update OpenCms context to ensure new translator is used
185
cms = getCmsObject();
186
187         // set root sitr
188
cms.getRequestContext().setSiteRoot("/");
189
190         // import the files
191
String JavaDoc importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip");
192         OpenCms.getImportExportManager().importData(
193             cms,
194             importFile,
195             "/",
196             new CmsShellReport(cms.getRequestContext().getLocale()));
197
198         // now switch to "mysite"
199
cms.getRequestContext().setSiteRoot("/sites/mysite/");
200
201         // check the results of the import
202
CmsXmlPage page;
203         CmsFile file;
204         CmsLinkTable table;
205         List JavaDoc links;
206         Iterator JavaDoc i;
207         List JavaDoc siblings;
208
209         // test "/importtest/index.html"
210
file = cms.readFile("/importtest/index.html");
211         page = CmsXmlPageFactory.unmarshal(cms, file);
212
213         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
214         links = new ArrayList JavaDoc();
215         i = table.iterator();
216         while (i.hasNext()) {
217             CmsLink link = (CmsLink)i.next();
218             links.add(link.toString());
219         }
220         assertTrue(links.size() == 2);
221         assertTrue(links.contains("/sites/mysite/importtest/page2.html"));
222         assertTrue(links.contains("/sites/mysite/importtest/page3.html"));
223
224         siblings = cms.readSiblings("/importtest/index.html", CmsResourceFilter.ALL);
225         i = siblings.iterator();
226         links = new ArrayList JavaDoc();
227         while (i.hasNext()) {
228             CmsResource sibling = (CmsResource)i.next();
229             links.add(sibling.getRootPath());
230         }
231         assertEquals(2, links.size());
232         assertTrue(links.contains("/sites/mysite/importtest/index.html"));
233         assertTrue(links.contains("/sites/mysite/importtest/linktest.html"));
234
235         // test "/importtest/page2.html"
236
file = cms.readFile("/importtest/page2.html");
237         page = CmsXmlPageFactory.unmarshal(cms, file);
238
239         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
240         links = new ArrayList JavaDoc();
241         i = table.iterator();
242         while (i.hasNext()) {
243             CmsLink link = (CmsLink)i.next();
244             links.add(link.toString());
245         }
246         assertEquals(2, links.size());
247         assertTrue(links.contains("/system/galleries/pics/_anfang/bg_teaser_test2.jpg"));
248         assertTrue(links.contains("/sites/mysite/importtest/index.html"));
249
250         // test "/importtest/linktest.html" (sibling of "/importtest/index.html")
251
file = cms.readFile("/importtest/linktest.html");
252         assertEquals(CmsResourceTypeXmlPage.getStaticTypeId(), file.getTypeId());
253         page = CmsXmlPageFactory.unmarshal(cms, file);
254
255         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
256         links = new ArrayList JavaDoc();
257         i = table.iterator();
258         while (i.hasNext()) {
259             CmsLink link = (CmsLink)i.next();
260             links.add(link.toString());
261             System.out.println("Link: " + link.toString());
262         }
263         assertEquals(2, links.size());
264         assertTrue(links.contains("/sites/mysite/importtest/page2.html"));
265         assertTrue(links.contains("/sites/mysite/importtest/page3.html"));
266
267         siblings = cms.readSiblings("/importtest/linktest.html", CmsResourceFilter.ALL);
268         i = siblings.iterator();
269         links = new ArrayList JavaDoc();
270         while (i.hasNext()) {
271             CmsResource sibling = (CmsResource)i.next();
272             links.add(sibling.getRootPath());
273             System.out.println("Sibling: " + sibling.toString());
274         }
275         assertEquals(2, links.size());
276         assertTrue(links.contains("/sites/mysite/importtest/index.html"));
277         assertTrue(links.contains("/sites/mysite/importtest/linktest.html"));
278
279         // now switch to "othersite"
280
cms.getRequestContext().setSiteRoot("/sites/othersite/");
281
282         // test "/othertest/index.html"
283
file = cms.readFile("/index.html");
284         page = CmsXmlPageFactory.unmarshal(cms, file);
285
286         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
287         links = new ArrayList JavaDoc();
288         i = table.iterator();
289         while (i.hasNext()) {
290             CmsLink link = (CmsLink)i.next();
291             links.add(link.toString());
292             System.out.println("Link: " + link.toString());
293         }
294         assertTrue(links.size() == 2);
295         assertTrue(links.contains("/sites/mysite/importtest/page2.html"));
296         assertTrue(links.contains("/sites/mysite/importtest/page3.html"));
297
298         // reset the translation rules
299
OpenCms.getResourceManager().setTranslators(
300             oldFolderTranslator,
301             OpenCms.getResourceManager().getFileTranslator());
302     }
303
304     /**
305      * Tests the resource translation during import.<p>
306      *
307      * @throws Exception if something goes wrong
308      */

309     public void testImportResourceTranslator() throws Exception JavaDoc {
310
311         echo("Testing resource translator for import");
312         CmsObject cms = getCmsObject();
313
314         cms.getRequestContext().setSiteRoot("/");
315
316         // need to create the "galleries" folder manually
317
cms.createResource("/system/galleries", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
318         cms.unlockResource("/system/galleries");
319
320         CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator();
321
322         CmsResourceTranslator folderTranslator = new CmsResourceTranslator(new String JavaDoc[] {
323             "s#^/sites/default/content/bodys(.*)#/system/bodies$1#",
324             "s#^/sites/default/pics/system(.*)#/system/workplace/resources$1#",
325             "s#^/sites/default/pics(.*)#/system/galleries/pics$1#",
326             "s#^/sites/default/download(.*)#/system/galleries/download$1#",
327             "s#^/sites/default/externallinks(.*)#/system/galleries/externallinks$1#",
328             "s#^/sites/default/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#",
329             "s#^/sites/default/content(.*)#/system$1#"}, false);
330
331         // set modified folder translator
332
OpenCms.getResourceManager().setTranslators(folderTranslator, OpenCms.getResourceManager().getFileTranslator());
333
334         // update OpenCms context to ensure new translator is used
335
cms = getCmsObject();
336
337         cms.getRequestContext().setSiteRoot("/sites/default");
338
339         // import the files
340
String JavaDoc importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip");
341         OpenCms.getImportExportManager().importData(
342             cms,
343             importFile,
344             "/",
345             new CmsShellReport(cms.getRequestContext().getLocale()));
346
347         // check the results of the import
348
CmsXmlPage page;
349         CmsFile file;
350         CmsLinkTable table;
351         List JavaDoc links;
352         Iterator JavaDoc i;
353         List JavaDoc siblings;
354
355         // test "/importtest/index.html"
356
file = cms.readFile("/importtest/index.html");
357         page = CmsXmlPageFactory.unmarshal(cms, file);
358
359         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
360         links = new ArrayList JavaDoc();
361         i = table.iterator();
362         while (i.hasNext()) {
363             CmsLink link = (CmsLink)i.next();
364             links.add(link.toString());
365         }
366         assertTrue(links.size() == 2);
367         assertTrue(links.contains("/sites/default/importtest/page2.html"));
368         assertTrue(links.contains("/sites/default/importtest/page3.html"));
369
370         siblings = cms.readSiblings("/importtest/index.html", CmsResourceFilter.ALL);
371         i = siblings.iterator();
372         links = new ArrayList JavaDoc();
373         while (i.hasNext()) {
374             CmsResource sibling = (CmsResource)i.next();
375             links.add(sibling.getRootPath());
376         }
377         assertEquals(2, links.size());
378         assertTrue(links.contains("/sites/default/importtest/index.html"));
379         assertTrue(links.contains("/sites/default/importtest/linktest.html"));
380
381         // test "/importtest/page2.html"
382
file = cms.readFile("/importtest/page2.html");
383         page = CmsXmlPageFactory.unmarshal(cms, file);
384
385         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
386         links = new ArrayList JavaDoc();
387         i = table.iterator();
388         while (i.hasNext()) {
389             CmsLink link = (CmsLink)i.next();
390             links.add(link.toString());
391             System.out.println("Link: " + link.toString());
392         }
393         assertEquals(2, links.size());
394         assertTrue(links.contains("/system/galleries/pics/_anfang/bg_teaser_test2.jpg"));
395         assertTrue(links.contains("/sites/default/importtest/index.html"));
396
397         // test "/importtest/linktest.html" (sibling of "/importtest/index.html")
398
file = cms.readFile("/importtest/linktest.html");
399         assertEquals(CmsResourceTypeXmlPage.getStaticTypeId(), file.getTypeId());
400         page = CmsXmlPageFactory.unmarshal(cms, file);
401
402         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
403         links = new ArrayList JavaDoc();
404         i = table.iterator();
405         while (i.hasNext()) {
406             CmsLink link = (CmsLink)i.next();
407             links.add(link.toString());
408             System.out.println("Link: " + link.toString());
409         }
410         assertEquals(2, links.size());
411         assertTrue(links.contains("/sites/default/importtest/page2.html"));
412         assertTrue(links.contains("/sites/default/importtest/page3.html"));
413
414         siblings = cms.readSiblings("/importtest/linktest.html", CmsResourceFilter.ALL);
415         i = siblings.iterator();
416         links = new ArrayList JavaDoc();
417         while (i.hasNext()) {
418             CmsResource sibling = (CmsResource)i.next();
419             links.add(sibling.getRootPath());
420         }
421         assertEquals(2, links.size());
422         assertTrue(links.contains("/sites/default/importtest/index.html"));
423         assertTrue(links.contains("/sites/default/importtest/linktest.html"));
424
425         // test "/othertest/index.html"
426
file = cms.readFile("/othertest/index.html");
427         page = CmsXmlPageFactory.unmarshal(cms, file);
428
429         table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
430         links = new ArrayList JavaDoc();
431         i = table.iterator();
432         while (i.hasNext()) {
433             CmsLink link = (CmsLink)i.next();
434             links.add(link.toString());
435             System.out.println("Link: " + link.toString());
436         }
437         assertTrue(links.size() == 2);
438         assertTrue(links.contains("/sites/default/importtest/page2.html"));
439         assertTrue(links.contains("/sites/default/importtest/page3.html"));
440
441         // clean up for the next test
442
cms.getRequestContext().setSiteRoot("/");
443         cms.lockResource("/sites/default");
444         cms.lockResource("/system");
445         cms.deleteResource("/sites/default/importtest", CmsResource.DELETE_PRESERVE_SIBLINGS);
446         cms.deleteResource("/system/bodies", CmsResource.DELETE_PRESERVE_SIBLINGS);
447         cms.deleteResource("/system/galleries/pics", CmsResource.DELETE_PRESERVE_SIBLINGS);
448         cms.unlockResource("/sites/default");
449         cms.unlockResource("/system");
450         cms.publishProject();
451
452         // reset the translation rules
453
OpenCms.getResourceManager().setTranslators(
454             oldFolderTranslator,
455             OpenCms.getResourceManager().getFileTranslator());
456     }
457 }
458
Popular Tags