KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/importexport/TestCmsImportExportNonexistentUser.java,v $
3  * Date : $Date: 2005/07/28 15:53:10 $
4  * Version: $Revision: 1.12 $
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.CmsObject;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.types.CmsResourceTypePlain;
38 import org.opencms.main.OpenCms;
39 import org.opencms.report.CmsShellReport;
40 import org.opencms.test.OpenCmsTestCase;
41 import org.opencms.test.OpenCmsTestProperties;
42
43 import java.io.File JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46
47 import junit.extensions.TestSetup;
48 import junit.framework.Test;
49 import junit.framework.TestSuite;
50
51 /**
52  * Tests exporting/import VFS data with nonexistent users.<p>
53  *
54  * @author Thomas Weckert
55  * @version $Revision: 1.12 $
56  */

57 public class TestCmsImportExportNonexistentUser extends OpenCmsTestCase {
58
59     /**
60      * Default JUnit constructor.<p>
61      *
62      * @param arg0 JUnit parameters
63      */

64     public TestCmsImportExportNonexistentUser(String JavaDoc arg0) {
65         super(arg0);
66     }
67     
68     /**
69      * Test suite for this test class.<p>
70      *
71      * @return the test suite
72      */

73     public static Test suite() {
74         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
75         
76         TestSuite suite = new TestSuite();
77         suite.setName(TestCmsImportExport.class.getName());
78                 
79         suite.addTest(new TestCmsImportExportNonexistentUser("testImportExportNonexistentUser"));
80         
81         TestSetup wrapper = new TestSetup(suite) {
82             
83             protected void setUp() {
84                 setupOpenCms("simpletest", "/sites/default/");
85             }
86             
87             protected void tearDown() {
88                 removeOpenCms();
89             }
90         };
91         
92         return wrapper;
93     }
94     
95     /**
96      * Tests exporting and import of VFS data with a nonexistent/deleted user.<p>
97      *
98      * The username of the deleted user should in the export manifest be replaced
99      * by the name of the Admin user.<p>
100      *
101      * @throws Exception if something goes wrong
102      */

103     public void testImportExportNonexistentUser() throws Exception JavaDoc {
104         
105         String JavaDoc zipExportFilename = null;
106         CmsObject cms = getCmsObject();
107         
108         try {
109             String JavaDoc username = "tempuser";
110             String JavaDoc password = "password";
111             String JavaDoc filename = "/dummy1.txt";
112             String JavaDoc contentStr = "This is a comment. I love comments.";
113             zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testImportExportNonexistentUser.zip");
114             byte[] content = contentStr.getBytes();
115             CmsProject offlineProject = cms.getRequestContext().currentProject();
116             
117             // create a temporary user for this test case
118
cms.createUser(username, password, "Temporary user for import/export test case", null);
119             // add this user to the project managers user group
120
cms.addUserToGroup(username, OpenCms.getDefaultUsers().getGroupProjectmanagers());
121             
122             // switch to the temporary user, offline project and default site
123
cms.loginUser(username, password);
124             cms.getRequestContext().saveSiteRoot();
125             cms.getRequestContext().setSiteRoot("/sites/default/");
126             cms.getRequestContext().setCurrentProject(offlineProject);
127             
128             // create a dummy plain text file by the temporary user
129
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), content, null);
130             // publish the dummy plain text file
131
cms.unlockResource(filename);
132             cms.publishResource(filename);
133             
134             // switch back to the Admin user, offline project and default site
135
cms.loginUser("Admin", "admin");
136             cms.getRequestContext().setSiteRoot("/sites/default/");
137             cms.getRequestContext().setCurrentProject(offlineProject);
138             // delete the temporary user
139
cms.deleteUser(username);
140             
141             // export the dummy plain text file
142
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
143             vfsExportHandler.setFileName(zipExportFilename);
144             List JavaDoc exportPaths = new ArrayList JavaDoc(1);
145             exportPaths.add(filename);
146             vfsExportHandler.setExportPaths(exportPaths);
147             vfsExportHandler.setIncludeSystem(false);
148             vfsExportHandler.setIncludeUnchanged(true);
149             vfsExportHandler.setExportUserdata(false);
150             OpenCms.getImportExportManager().exportData(cms, vfsExportHandler, new CmsShellReport(cms.getRequestContext().getLocale()));
151             
152             // delete the dummy plain text file
153
cms.lockResource(filename);
154             cms.deleteResource(filename, CmsResource.DELETE_REMOVE_SIBLINGS);
155             // publish the deleted dummy plain text file
156
cms.unlockResource(filename);
157             cms.publishResource(filename);
158             
159             // re-import the exported dummy plain text file
160
OpenCms.getImportExportManager().importData(cms, zipExportFilename, "/", new CmsShellReport(cms.getRequestContext().getLocale()));
161         } catch (Exception JavaDoc e) {
162             
163             fail(e.toString());
164         } finally {
165             
166             try {
167                 if (zipExportFilename != null) {
168                     File JavaDoc file = new File JavaDoc(zipExportFilename);
169                     if (file.exists()) {
170                         file.delete();
171                     }
172                 }
173             } catch (Throwable JavaDoc t) {
174                 // intentionally left blank
175
}
176             
177             cms.getRequestContext().restoreSiteRoot();
178         }
179     }
180     
181 }
182
Popular Tags