KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > TestPropertyDefinition


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestPropertyDefinition.java,v $
3  * Date : $Date: 2005/06/23 11:11:43 $
4  * Version: $Revision: 1.5 $
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.file;
33
34 import org.opencms.main.CmsException;
35 import org.opencms.test.OpenCmsTestCase;
36 import org.opencms.test.OpenCmsTestProperties;
37 import org.opencms.util.CmsUUID;
38
39 import java.util.List JavaDoc;
40
41 import junit.extensions.TestSetup;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /**
46  * Unit test for the "createPropertyDefinition", "readPropertyDefiniton" and
47  * "readAllPropertyDefintions" methods of the CmsObject.<p>
48  *
49  * @author Michael Emmerich
50  * @version $Revision: 1.5 $
51  */

52 public class TestPropertyDefinition extends OpenCmsTestCase {
53
54     /**
55      * Default JUnit constructor.<p>
56      *
57      * @param arg0 JUnit parameters
58      */

59     public TestPropertyDefinition(String JavaDoc arg0) {
60
61         super(arg0);
62     }
63
64     /**
65      * Test suite for this test class.<p>
66      *
67      * @return the test suite
68      */

69     public static Test suite() {
70         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
71
72         TestSuite suite = new TestSuite();
73         suite.setName(TestPropertyDefinition.class.getName());
74
75         suite.addTest(new TestPropertyDefinition("testCreatePropertyDefinition"));
76         suite.addTest(new TestPropertyDefinition("testCreateReadDeletePropertyDefinition"));
77         suite.addTest(new TestPropertyDefinition("testGetResourcesWithProperty"));
78         
79         TestSetup wrapper = new TestSetup(suite) {
80
81             protected void setUp() {
82
83                 setupOpenCms("simpletest", "/sites/default/");
84             }
85
86             protected void tearDown() {
87
88                 removeOpenCms();
89             }
90         };
91
92         return wrapper;
93     }
94     
95     /**
96      * Test the createPropertyDefintion method.<p>
97      * @param tc the OpenCmsTestCase
98      * @param cms the CmsObject
99      * @param propertyDefiniton1 the property definition to create
100      * @throws Throwable if something goes wrong
101      */

102     public static void createPropertyDefinition(OpenCmsTestCase tc, CmsObject cms, String JavaDoc propertyDefiniton1)
103     throws Throwable JavaDoc {
104
105         // get all propertydefintions
106
List JavaDoc allPropertydefintions = cms.readAllPropertyDefinitions();
107         // create a property defintion with a dummy id value (the real id is created by db)
108
CmsPropertyDefinition prop = new CmsPropertyDefinition(
109             CmsUUID.getNullUUID(),
110             propertyDefiniton1);
111
112         cms.createPropertyDefinition(propertyDefiniton1);
113
114         // check if the propertsdefintion was written
115
tc.assertPropertydefinitionExist(cms, prop);
116         // check if all other properties are still identical
117
tc.assertPropertydefinitions(cms, allPropertydefintions, prop);
118     }
119
120     /**
121      * Test the createPropertyDefintion method.<p>
122      *
123      * @throws Throwable if something goes wrong
124      */

125     public void testCreatePropertyDefinition() throws Throwable JavaDoc {
126
127         CmsObject cms = getCmsObject();
128         echo("Testing createPropetyDefinition and readPropertyDefiniton");
129         createPropertyDefinition(this, cms, "NewPropertyDefinition");
130     }
131
132     /**
133      * Test to create, read and delete a property definition through the cache driver.<p>
134      *
135      * @throws Throwable if something goes wrong
136      */

137     public void testCreateReadDeletePropertyDefinition() throws Throwable JavaDoc {
138
139         CmsObject cms = getCmsObject();
140
141         echo("Testing creation of a new property definition");
142         createReadDeletePropertyDefinition(cms);
143     }
144
145     /**
146      * Test to create, read and delete a property definition through the cache driver.<p>
147      *
148      * @param cms the CmsObject
149      * @throws Throwable if something goes wrong
150      */

151     public static void createReadDeletePropertyDefinition(CmsObject cms) throws Throwable JavaDoc {
152
153         CmsPropertyDefinition propertyDefinition = null;
154         String JavaDoc propertyDefinitionName = "locale-available";
155                 
156         // 1) create a property definition
157

158         try {
159             // create a new property definition
160
propertyDefinition = cms.createPropertyDefinition(propertyDefinitionName);
161         } catch (CmsException e) {
162             fail("Error creating property definition " + propertyDefinitionName + ", " + e.toString());
163         }
164
165         assertEquals(propertyDefinition.getName(), propertyDefinitionName);
166
167         // 2) read the created property definition
168

169         try {
170             // read the created property definition
171
propertyDefinition = null;
172             propertyDefinition = cms.readPropertyDefinition(propertyDefinitionName);
173         } catch (CmsException e) {
174             fail("Error reading property definition " + propertyDefinitionName + ", " + e.toString());
175         }
176
177         assertEquals(propertyDefinition.getName(), propertyDefinitionName);
178
179         // 3) check if the new created property is contained in the list of all property definitions
180

181         List JavaDoc allPropertyDefinitions = null;
182         try {
183             // read all property definitions
184
allPropertyDefinitions = cms.readAllPropertyDefinitions();
185         } catch (CmsException e) {
186             fail("Error reading all property definitions, " + e.toString());
187         }
188
189         boolean found = false;
190         for (int i = 0, n = allPropertyDefinitions.size(); i < n; i++) {
191             if (((CmsPropertyDefinition)allPropertyDefinitions.get(i)).equals(propertyDefinition)) {
192                 found = true;
193                 break;
194             }
195         }
196         
197         if (!found) {
198             fail("Property definition " + propertyDefinitionName + " is not in the list of all property definitions");
199         }
200         
201         // 4) delete the new property definition again
202

203         try {
204             // delete the property definition again
205
cms.deletePropertyDefinition(propertyDefinitionName);
206         } catch (CmsException e) {
207             fail("Error deleting property definition " + propertyDefinitionName + ", " + e.toString());
208         }
209         
210         // 5) re-read the delete property definition to check that it is not found
211

212         try {
213             // re-read the created property definition
214
propertyDefinition = null;
215             propertyDefinition = cms.readPropertyDefinition(propertyDefinitionName);
216         } catch (CmsException e) {
217             // intentionally left blank
218
}
219
220         assertNull(propertyDefinition);
221         
222         // 6) re-read the list of all property definitions to check that it is not contained
223

224         allPropertyDefinitions = null;
225         try {
226             // read all property definitions
227
allPropertyDefinitions = cms.readAllPropertyDefinitions();
228         } catch (CmsException e) {
229             fail("Error reading all property definitions, " + e.toString());
230         }
231
232         found = false;
233         for (int i = 0, n = allPropertyDefinitions.size(); i < n; i++) {
234             if (((CmsPropertyDefinition)allPropertyDefinitions.get(i)).equals(propertyDefinition)) {
235                 found = true;
236                 break;
237             }
238         }
239
240         if (found) {
241             fail("Property definition " + propertyDefinitionName + " is still in the list of all property definitions");
242         }
243         
244     }
245
246     /**
247      * Tests reading all resources that have a specific property definition set.<p>
248      *
249      * @throws Exception if something goes wrong
250      */

251     public void testGetResourcesWithProperty() throws Exception JavaDoc {
252         
253         echo("Testing reading all resources with a specific property");
254         CmsObject cms = getCmsObject();
255         List JavaDoc result;
256         
257         result = cms.readResourcesWithProperty(CmsPropertyDefinition.PROPERTY_TITLE);
258         assertEquals(63, result.size());
259         result = cms.readResourcesWithProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE);
260         assertEquals(25, result.size());
261     }
262     
263 }
Popular Tags