KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > setup > TestCmsSetupXmlHelper


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/setup/TestCmsSetupXmlHelper.java,v $
3  * Date : $Date: 2006/03/27 14:52:58 $
4  * Version: $Revision: 1.2 $
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.setup;
33
34 import org.opencms.configuration.CmsConfigurationManager;
35 import org.opencms.configuration.CmsWorkplaceConfiguration;
36 import org.opencms.configuration.I_CmsXmlConfiguration;
37 import org.opencms.main.CmsSystemInfo;
38 import org.opencms.setup.xml.CmsSetupXmlHelper;
39 import org.opencms.test.OpenCmsTestCase;
40 import org.opencms.util.CmsFileUtil;
41
42 import java.io.File JavaDoc;
43
44 import org.dom4j.Document;
45
46 /**
47  * Tests the setup xml helper class.<p>
48  *
49  * @author Michael Moossen
50  *
51  * @version $Revision: 1.2 $
52  *
53  * @since 6.1.8
54  */

55 public class TestCmsSetupXmlHelper extends OpenCmsTestCase {
56
57     /**
58      * Default JUnit constructor.<p>
59      *
60      * @param arg0 JUnit parameters
61      */

62     public TestCmsSetupXmlHelper(String JavaDoc arg0) {
63
64         super(arg0);
65     }
66
67     /**
68      * Tests reading xml file.<p>
69      *
70      * @throws Exception if something goes wrong
71      */

72     public void testXmlModification() throws Exception JavaDoc {
73
74         String JavaDoc base = getTestDataPath(File.separator + "WEB-INF" + File.separator + CmsSystemInfo.FOLDER_CONFIG);
75         CmsSetupXmlHelper xmlHelper = new CmsSetupXmlHelper(base);
76
77         // create test file
78
String JavaDoc inputFile = "test.xml";
79         CmsFileUtil.copy(base + CmsWorkplaceConfiguration.DEFAULT_XML_FILE_NAME, base + inputFile);
80
81         // open test file
82
System.out.println("Modifying xml from " + base + inputFile);
83         Document ori = xmlHelper.getDocument(inputFile);
84
85         String JavaDoc baseXp = "/" + CmsConfigurationManager.N_ROOT + "/" + CmsWorkplaceConfiguration.N_WORKPLACE + "/";
86         // simple test
87
String JavaDoc xPath = baseXp + CmsWorkplaceConfiguration.N_AUTOLOCK;
88         String JavaDoc value = xmlHelper.getValue(inputFile, xPath);
89         assertEquals("true", value);
90         String JavaDoc expected = "false";
91         xmlHelper.setValue(inputFile, xPath, expected);
92         value = xmlHelper.getValue(inputFile, xPath);
93         assertEquals(expected, value);
94
95         // advanced test
96
xPath = baseXp
97             + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
98             + "/"
99             + I_CmsXmlConfiguration.N_RESOURCE
100             + "[2]/@"
101             + I_CmsXmlConfiguration.A_URI;
102         value = xmlHelper.getValue(inputFile, xPath);
103         assertEquals("/system/login/", value);
104         expected = "/";
105         xmlHelper.setValue(inputFile, xPath, expected);
106         value = xmlHelper.getValue(inputFile, xPath);
107         assertEquals(expected, value);
108
109         // test adding a new node
110
xPath = baseXp + "test1/test2/test3";
111         expected = "test4";
112         assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
113         value = xmlHelper.getValue(inputFile, xPath);
114         assertEquals(expected, value);
115
116         // test adding a new attribute
117
xPath = baseXp
118             + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
119             + "/"
120             + I_CmsXmlConfiguration.N_RESOURCE
121             + "[2]/@test-attr";
122         expected = "test-value";
123         assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
124         value = xmlHelper.getValue(inputFile, xPath);
125         assertEquals(expected, value);
126
127         // test adding a new node in a list
128
xPath = baseXp + "test1/test2[2]/test5";
129         expected = "test6";
130         assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
131         value = xmlHelper.getValue(inputFile, xPath);
132         assertEquals(expected, value);
133
134         // write modified file
135
xmlHelper.write(inputFile);
136
137         // restoring file
138
xPath = baseXp
139             + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
140             + "/"
141             + I_CmsXmlConfiguration.N_RESOURCE
142             + "[2]/@"
143             + I_CmsXmlConfiguration.A_URI;
144         value = xmlHelper.getValue(inputFile, xPath);
145         assertEquals("/", value);
146         expected = "/system/login/";
147         xmlHelper.setValue(inputFile, xPath, expected);
148         value = xmlHelper.getValue(inputFile, xPath);
149         assertEquals(expected, value);
150
151         xPath = baseXp + CmsWorkplaceConfiguration.N_AUTOLOCK;
152         value = xmlHelper.getValue(inputFile, xPath);
153         assertEquals("false", value);
154         expected = "true";
155         xmlHelper.setValue(inputFile, xPath, expected);
156         value = xmlHelper.getValue(inputFile, xPath);
157         assertEquals(expected, value);
158
159         // test removing a node
160
xPath = baseXp + "test1";
161         assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));
162         assertNull(xmlHelper.getValue(inputFile, xPath));
163
164         // test removing an attribute
165
xPath = baseXp
166             + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
167             + "/"
168             + I_CmsXmlConfiguration.N_RESOURCE
169             + "[2]/@test-attr";
170         assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));
171         assertNull(xmlHelper.getValue(inputFile, xPath));
172         assertNotNull(xmlHelper.getValue(inputFile, xPath.substring(0, xPath.lastIndexOf('/')) + "/@uri"));
173
174         // test removing non existent node
175
xPath = baseXp + "test1";
176         assertEquals(0, xmlHelper.setValue(inputFile, xPath, null));
177
178         // test removing non existent attribute
179
xPath = baseXp
180             + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
181             + "/"
182             + I_CmsXmlConfiguration.N_RESOURCE
183             + "[2]/@test-xxx";
184         assertEquals(0, xmlHelper.setValue(inputFile, xPath, null));
185
186         // write restored file
187
xmlHelper.write(inputFile);
188
189         // compare documents
190
xmlHelper.flushAll();
191         Document cur = xmlHelper.getDocument(inputFile);
192         assertEquals(ori, cur);
193
194         // remove test file
195
new File JavaDoc(base + inputFile).delete();
196     }
197 }
198
Popular Tags