KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > TestOpenCmsSingleton


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/main/TestOpenCmsSingleton.java,v $
3  * Date : $Date: 2005/06/23 11:11:54 $
4  * Version: $Revision: 1.15 $
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.main;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.types.CmsResourceTypeJsp;
38 import org.opencms.test.OpenCmsTestCase;
39 import org.opencms.test.OpenCmsTestProperties;
40 import org.opencms.util.CmsMacroResolver;
41
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 import junit.extensions.TestSetup;
47 import junit.framework.Test;
48 import junit.framework.TestSuite;
49
50 /**
51  * Unit test the static OpenCms singleton object.<p>
52  *
53  * @author Alexander Kandzior
54  * @version $Revision: 1.15 $
55  */

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

63     public TestOpenCmsSingleton(String JavaDoc arg0) {
64
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
75         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
76
77         TestSuite suite = new TestSuite();
78         suite.setName(TestOpenCmsSingleton.class.getName());
79
80         suite.addTest(new TestOpenCmsSingleton("testInitCmsObject"));
81         suite.addTest(new TestOpenCmsSingleton("testLog"));
82         suite.addTest(new TestOpenCmsSingleton("testEncoding"));
83
84         TestSetup wrapper = new TestSetup(suite) {
85
86             protected void setUp() {
87
88                 setupOpenCms("simpletest", "/sites/default/");
89             }
90
91             protected void tearDown() {
92
93                 removeOpenCms();
94             }
95         };
96
97         return wrapper;
98     }
99
100     /**
101      * Test case for the encoding.<p>
102      *
103      * @throws Exception if the test fails
104      */

105     public void testEncoding() throws Exception JavaDoc {
106
107         echo("Testing the encoding settings");
108
109         String JavaDoc systemEncoding = OpenCms.getSystemInfo().getDefaultEncoding();
110         String JavaDoc workplaceEncoding = OpenCms.getWorkplaceManager().getEncoding();
111
112         // get content encoding default property from the JSP resource type
113
CmsResourceTypeJsp jsp = (CmsResourceTypeJsp)OpenCms.getResourceManager().getResourceType(
114             CmsResourceTypeJsp.getStaticTypeId());
115         List JavaDoc defaultProperties = jsp.getConfiguredDefaultProperties();
116         Iterator JavaDoc i = defaultProperties.iterator();
117         String JavaDoc jspEncoding = null;
118         while (i.hasNext()) {
119             CmsProperty property = (CmsProperty)i.next();
120             if (CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING.equals(property.getName())) {
121                 jspEncoding = property.getValue();
122                 // resolve the macro
123
CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
124                 jspEncoding = CmsMacroResolver.newInstance().setCmsObject(cms).resolveMacros(jspEncoding);
125                 break;
126             }
127         }
128
129         assertEquals("ISO-8859-1", systemEncoding);
130         assertEquals(systemEncoding, workplaceEncoding);
131         assertEquals(systemEncoding, jspEncoding);
132     }
133
134     /**
135      * Test case for the initCmsObject methods.<p>
136      *
137      * @throws Exception if the test fails
138      */

139     public void testInitCmsObject() throws Exception JavaDoc {
140
141         CmsObject cms;
142
143         echo("Testing access to initCmsObject methods");
144
145         // test creation of "Guest" user CmsObject
146
cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
147         if (!cms.getRequestContext().currentUser().getName().equals(OpenCms.getDefaultUsers().getUserGuest())) {
148             fail("'Guest' user could not be properly initialized!");
149         }
150
151         // test creation of "Export" user CmsObject
152
cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
153         if (!cms.getRequestContext().currentUser().getName().equals(OpenCms.getDefaultUsers().getUserExport())) {
154             fail("'Export' user could not be properly initialized!");
155         }
156
157         // test creation of "Admin" user CmsObject (this must fail)
158
boolean gotException = false;
159         cms = null;
160         try {
161             cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserAdmin());
162         } catch (CmsException e) {
163             gotException = true;
164         }
165         if (!gotException) {
166             fail("'Admin' user could be initialized without permission check (with username)!");
167         }
168
169         // create "Admin" context info
170
CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin());
171
172         // test creation of "Admin" user CmsObject with 2nd option (this must also fail)
173
gotException = false;
174         cms = null;
175         try {
176             cms = OpenCms.initCmsObject(null, contextInfo);
177         } catch (CmsException e) {
178             gotException = true;
179         }
180         if (!gotException) {
181             fail("'Admin' user could be initialized without permission check (with context info)!");
182         }
183
184         // now test creation of "Admin" user with admin permissions
185
// also check if created context is actually the context provided
186
String JavaDoc siteRoot = "/sites/default";
187         String JavaDoc requestedUri = "/index.html";
188         String JavaDoc encoding = "US-ASCII";
189
190         contextInfo.setSiteRoot(siteRoot);
191         contextInfo.setRequestedUri(requestedUri);
192         contextInfo.setLocale(Locale.CHINESE);
193         contextInfo.setEncoding(encoding);
194         try {
195             cms = OpenCms.initCmsObject(getCmsObject(), contextInfo);
196         } catch (CmsException e) {
197             fail("'Admin' user creation with valid Admin context didn't work!");
198         }
199         if (!cms.getRequestContext().currentUser().getName().equals(OpenCms.getDefaultUsers().getUserAdmin())) {
200             fail("'Admin' user could not be properly initialized with valid Admin context!");
201         }
202         if (cms == getCmsObject()) {
203             fail("'Admin' user Object is the same as creating instance, but must be a new Object!");
204         }
205
206         if (!cms.getRequestContext().getSiteRoot().equals(siteRoot)) {
207             fail("Site root in created context not as expected.");
208         }
209         if (!cms.getRequestContext().getUri().equals(requestedUri)) {
210             fail("Requested uri in created context not as expected.");
211         }
212         if (!cms.getRequestContext().getEncoding().equals(encoding)) {
213             fail("Encoding in created context not as expected.");
214         }
215         if (!cms.getRequestContext().getLocale().equals(Locale.CHINESE)) {
216             fail("Locale in created context not as expected.");
217         }
218     }
219
220     /**
221      * Test case for the logger.<p>
222      *
223      * @throws Exception if the test fails
224      */

225     public void testLog() throws Exception JavaDoc {
226
227         // first 4 log levels are uncritical
228
CmsLog.getLog(this).trace("This is a 'trace' log message");
229         CmsLog.getLog(this).debug("This is a 'debug' log message");
230         CmsLog.getLog(this).info("This is a 'info' log message");
231         CmsLog.getLog(this).warn("This is a 'warn' log message");
232
233         // is something is written to log level 'error' or 'fatal'
234
// a runtime exception must be thrown while unit tests are running
235
boolean noException;
236         noException = true;
237         try {
238             CmsLog.getLog(this).error("This is a 'error' log message");
239         } catch (RuntimeException JavaDoc e) {
240             noException = false;
241         }
242         if (noException) {
243             fail("Writing to 'error' log level did not cause test to fail.");
244         }
245         noException = true;
246         try {
247             CmsLog.getLog(this).fatal("This is a 'fatal' log message");
248         } catch (RuntimeException JavaDoc e) {
249             noException = false;
250         }
251         if (noException) {
252             fail("Writing to 'fatal' log level did not cause test to fail.");
253         }
254     }
255
256 }
257
Popular Tags