1 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 ; 43 import java.util.List ; 44 import java.util.Locale ; 45 46 import junit.extensions.TestSetup; 47 import junit.framework.Test; 48 import junit.framework.TestSuite; 49 50 56 public class TestOpenCmsSingleton extends OpenCmsTestCase { 57 58 63 public TestOpenCmsSingleton(String arg0) { 64 65 super(arg0); 66 } 67 68 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 105 public void testEncoding() throws Exception { 106 107 echo("Testing the encoding settings"); 108 109 String systemEncoding = OpenCms.getSystemInfo().getDefaultEncoding(); 110 String workplaceEncoding = OpenCms.getWorkplaceManager().getEncoding(); 111 112 CmsResourceTypeJsp jsp = (CmsResourceTypeJsp)OpenCms.getResourceManager().getResourceType( 114 CmsResourceTypeJsp.getStaticTypeId()); 115 List defaultProperties = jsp.getConfiguredDefaultProperties(); 116 Iterator i = defaultProperties.iterator(); 117 String 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 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 139 public void testInitCmsObject() throws Exception { 140 141 CmsObject cms; 142 143 echo("Testing access to initCmsObject methods"); 144 145 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 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 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 CmsContextInfo contextInfo = new CmsContextInfo(OpenCms.getDefaultUsers().getUserAdmin()); 171 172 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 String siteRoot = "/sites/default"; 187 String requestedUri = "/index.html"; 188 String 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 225 public void testLog() throws Exception { 226 227 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 boolean noException; 236 noException = true; 237 try { 238 CmsLog.getLog(this).error("This is a 'error' log message"); 239 } catch (RuntimeException 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 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 |