1 31 32 package org.opencms.file; 33 34 import org.opencms.file.types.CmsResourceTypeFolder; 35 import org.opencms.file.types.CmsResourceTypePlain; 36 import org.opencms.main.CmsException; 37 import org.opencms.main.CmsIllegalArgumentException; 38 import org.opencms.main.CmsRuntimeException; 39 import org.opencms.test.OpenCmsTestCase; 40 import org.opencms.test.OpenCmsTestProperties; 41 42 import java.util.List ; 43 44 import junit.extensions.TestSetup; 45 import junit.framework.Test; 46 import junit.framework.TestSuite; 47 48 55 public class TestResourceOperations extends OpenCmsTestCase { 56 57 62 public TestResourceOperations(String arg0) { 63 super(arg0); 64 } 65 66 71 public static Test suite() { 72 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 73 74 TestSuite suite = new TestSuite(); 75 suite.setName(TestResourceOperations.class.getName()); 76 77 suite.addTest(new TestResourceOperations("testGetFolderPath")); 78 suite.addTest(new TestResourceOperations("testGetName")); 79 suite.addTest(new TestResourceOperations("testGetParentFolder")); 80 suite.addTest(new TestResourceOperations("testGetPathLevel")); 81 suite.addTest(new TestResourceOperations("testGetPathPart")); 82 suite.addTest(new TestResourceOperations("testIsFolder")); 83 suite.addTest(new TestResourceOperations("testGetFolderPath")); 84 suite.addTest(new TestResourceOperations("testResourceNames")); 85 suite.addTest(new TestResourceOperations("testCreateResources")); 86 suite.addTest(new TestResourceOperations("testCreateReadFile")); 87 suite.addTest(new TestResourceOperations("testPublishFile")); 88 suite.addTest(new TestResourceOperations("testCreateSibling")); 89 suite.addTest(new TestResourceOperations("testCreateAccessFolders")); 90 91 TestSetup wrapper = new TestSetup(suite) { 92 93 protected void setUp() { 94 setupOpenCms(null, null, false); 95 } 96 97 protected void tearDown() { 98 removeOpenCms(); 99 } 100 }; 101 102 return wrapper; 103 } 104 105 110 public void testGetFolderPath() throws Throwable { 111 112 echo("Testing testGetFolderPath"); 113 114 assertEquals(CmsResource.getFolderPath("/system/def/file.html"), "/system/def/"); 115 116 assertEquals(CmsResource.getFolderPath("/system/def/"), "/system/def/"); 117 } 118 119 124 public void testGetName() throws Throwable { 125 126 echo("Testing testGetName"); 127 128 assertEquals(CmsResource.getName("/system/workplace/"), "workplace/"); 129 } 130 131 136 public void testGetParentFolder() throws Throwable { 137 138 echo("Testing testGetParentFolder"); 139 140 assertEquals(CmsResource.getParentFolder("/system/workplace/"), "/system/"); 141 } 142 143 148 public void testGetPathLevel() throws Throwable { 149 150 echo("Testing testGetPathLevel"); 151 152 assertEquals(CmsResource.getPathLevel("/"), 0); 153 154 assertEquals(CmsResource.getPathLevel("/foo/"), 1); 155 156 assertEquals(CmsResource.getPathLevel("/foo/bar/"), 2); 157 } 158 159 164 public void testGetPathPart() throws Throwable { 165 166 echo("Testing testGetPathPart"); 167 168 assertEquals(CmsResource.getPathPart("/foo/bar/", 0), "/"); 169 assertEquals(CmsResource.getPathPart("/foo/bar/", 1), "/foo/"); 170 assertEquals(CmsResource.getPathPart("/foo/bar/", 2), "/foo/bar/"); 171 assertEquals(CmsResource.getPathPart("/foo/bar/", -1), "/foo/bar/"); 173 assertEquals(CmsResource.getPathPart("/foo/bar/", -2), "/foo/"); 174 } 175 176 181 public void testIsFolder() throws Throwable { 182 183 echo("Testing testIsFolder"); 184 } 185 186 191 public void testResourceNames() throws Throwable { 192 193 CmsObject cms = getCmsObject(); 194 echo("Testing invalid resource names"); 195 196 CmsRuntimeException exc; 197 198 exc = null; 200 try { 201 cms.createResource("/Resource Name", CmsResourceTypePlain.getStaticTypeId(), null, null); 202 } catch (CmsIllegalArgumentException e) { 203 exc = e; 204 } 205 206 assertTrue(exc instanceof CmsIllegalArgumentException); 207 208 exc = null; 210 try { 211 cms.createResource("/ ResourceName", CmsResourceTypePlain.getStaticTypeId(), null, null); 212 } catch (CmsIllegalArgumentException e) { 213 exc = e; 214 } 215 216 assertTrue(exc instanceof CmsIllegalArgumentException); 217 218 exc = null; 220 try { 221 cms.createResource("/ResourceName ", CmsResourceTypePlain.getStaticTypeId(), null, null); 222 } catch (CmsIllegalArgumentException e) { 223 exc = e; 224 } 225 226 assertTrue(exc instanceof CmsIllegalArgumentException); 227 228 exc = null; 230 try { 231 cms.createResource("/Resource#Name", CmsResourceTypePlain.getStaticTypeId(), null, null); 232 } catch (CmsIllegalArgumentException e) { 233 exc = e; 234 } 235 236 assertTrue(exc instanceof CmsIllegalArgumentException); 237 } 238 239 244 public void testCreateResources() throws Throwable { 245 246 CmsObject cms = getCmsObject(); 247 echo("Testing resource creation"); 248 249 cms.createResource("/folder1", CmsResourceTypeFolder.getStaticTypeId()); 251 252 cms.createResource("/resource2", CmsResourceTypePlain.getStaticTypeId()); 254 255 cms.createResource("/folder1/resource3", CmsResourceTypePlain.getStaticTypeId()); 257 258 assertIsFolder(cms, "/folder1/"); 260 261 assertResourceType(cms, "/resource2", CmsResourceTypePlain.getStaticTypeId()); 263 264 assertResourceType(cms, "/folder1/resource3", CmsResourceTypePlain.getStaticTypeId()); 266 267 268 } 269 270 275 public void testCreateAccessFolders() throws Throwable { 276 277 CmsObject cms = getCmsObject(); 278 echo("Testing folder creation and access"); 279 280 CmsException exc; 281 282 cms.createResource("/cafolder1", CmsResourceTypeFolder.getStaticTypeId()); 284 285 cms.createResource("/cafolder2/", CmsResourceTypeFolder.getStaticTypeId()); 287 288 assertTrue(CmsResource.isFolder(cms.readResource("/cafolder2").getRootPath())); 291 292 assertTrue(CmsResource.isFolder(cms.readResource("/cafolder1/").getRootPath())); 295 296 List l; 299 l = cms.getSubFolders("/"); 300 for (int i=0; i<l.size(); i++) { 301 CmsResource r = (CmsResource)l.get(i); 302 if (!(CmsResource.isFolder(r.getRootPath()))) { 303 fail("Invalid folder name returned via getRootPath (" + r.getRootPath() + ")"); 304 } 305 } 306 307 exc = null; 309 try { 310 cms.createResource("/cafolder1", CmsResourceTypePlain.getStaticTypeId()); 311 } catch (CmsException e) { 312 exc = e; 313 } 314 assertTrue(exc instanceof CmsVfsResourceAlreadyExistsException); 315 } 316 317 322 public void testCreateReadFile() throws Throwable { 323 324 CmsObject cms = getCmsObject(); 325 echo("Testing file creation"); 326 327 String content = "this is a test content"; 328 329 cms.createResource("/file1", CmsResourceTypePlain.getStaticTypeId(), content.getBytes(), null); 331 332 this.assertContent(cms, "/file1" , content.getBytes()); 334 } 335 336 341 public void testPublishFile() throws Throwable { 342 343 CmsObject cms = getCmsObject(); 344 echo("Testing file publishing"); 345 346 String content = "this is a test content"; 347 348 cms.getRequestContext().setSiteRoot("/"); 350 351 cms.createResource("/file2", CmsResourceTypePlain.getStaticTypeId(), content.getBytes(), null); 353 354 cms.unlockResource("/file2"); 356 357 cms.publishResource("/file2"); 359 360 cms.getRequestContext().setCurrentProject(cms.readProject("Online")); 362 363 this.assertContent(cms, "/file2" , content.getBytes()); 365 366 cms.getRequestContext().setCurrentProject(cms.readProject("Offline")); 368 } 369 370 375 public void testCreateSibling() throws Throwable { 376 377 CmsObject cms = getCmsObject(); 378 echo("Testing sibling creation"); 379 380 cms.createResource("/resource4", CmsResourceTypePlain.getStaticTypeId()); 382 383 assertSiblingCount(cms, "/resource4", 1); 385 386 cms.createSibling("/resource4", "/sibling1", null); 388 389 assertResourceType(cms, "/resource4", CmsResourceTypePlain.getStaticTypeId()); 391 392 assertResourceType(cms, "/sibling1", CmsResourceTypePlain.getStaticTypeId()); 394 395 assertSiblingCount(cms, "/resource4", 2); 397 assertSiblingCount(cms, "/sibling1", 2); 398 } 399 400 } | Popular Tags |