⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcreatewriteresource.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
       }      
       
       // publish the project
       cms.unlockProject(cms.getRequestContext().currentProject().getId());
       cms.publishProject();    
       
       assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);       
   }   
    
   /**
    * Test the create resource method.<p>
    * 
    * @throws Throwable if something goes wrong
    */
   public void testCreateResource() throws Throwable {

       CmsObject cms = getCmsObject();     
       echo("Testing create resource");
       
       String resourcename = "/folder1/test2.html";
       long timestamp = System.currentTimeMillis()-1;        
              
       String contentStr = "Hello this is my other content";
       byte[] content = contentStr.getBytes();      
       
       cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), content, null);
       
       // ensure created resource type
       assertResourceType(cms, resourcename, CmsResourceTypePlain.getStaticTypeId());  
       // project must be current project
       assertProject(cms, resourcename, cms.getRequestContext().currentProject());
       // state must be "new"
       assertState(cms, resourcename, CmsResource.STATE_NEW);
       // date last modified 
       assertDateLastModifiedAfter(cms, resourcename, timestamp);
       // date created
       assertDateCreatedAfter(cms, resourcename, timestamp);
       // the user last modified must be the current user
       assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser()); 
       // check the content
       assertContent(cms, resourcename, content);
              
       // publish the project
       cms.unlockProject(cms.getRequestContext().currentProject().getId());
       cms.publishProject();    
       
       assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);       
   }
   
   /**
    * Test the create resource method for an already existing resource.<p>
    * 
    * @throws Throwable if something goes wrong
    */
   public void testCreateResourceAgain() throws Throwable {

       CmsObject cms = getCmsObject();     
       echo("Testing to create an existing resource again");

       String resourcename = "/folder1/test2.html";
       storeResources(cms, resourcename);
       long timestamp = System.currentTimeMillis();
       
       String contentStr = "Hello this is my NEW AND ALSO CHANGED other content";
       byte[] content = contentStr.getBytes();      

       assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
       cms.lockResource(resourcename);
       
       try {
           // resource exists and is not deleted, creation must throw exception
           cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), content, null);
       } catch (Throwable e) {
           if (!(e instanceof CmsVfsResourceAlreadyExistsException)) {
               fail("Existing resource '" + resourcename + "' was not detected!");
           }
       }    

       // read resource for comparing id's later
       CmsResource original = cms.readResource(resourcename);
       
       // delete resource and try again
       cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
       cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), content, null);
              
       // project must be current project
       assertProject(cms, resourcename, cms.getRequestContext().currentProject());
       // state must be "changed"
       assertState(cms, resourcename, CmsResource.STATE_CHANGED);
       // date last modified 
       assertDateLastModifiedAfter(cms, resourcename, timestamp);
       // the user last modified must be the current user
       assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
       // date created
       assertDateCreatedAfter(cms, resourcename, timestamp);
       // the user created must be the current user
       assertUserCreated(cms, resourcename, cms.getRequestContext().currentUser());       
       // check the content
       assertContent(cms, resourcename, content);     
       
       // compare id's
       CmsResource created = cms.readResource(resourcename);
       if (created.getResourceId().equals(original.getResourceId())) {
           fail("A created resource that replaced a deleted resource must not have the same resource id!");
       }     
       
       // publish the project
       cms.unlockProject(cms.getRequestContext().currentProject().getId());
       cms.publishProject();    
              
       assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);       
   }

    /**
     * Test the import resource method with a folder.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testImportFolder() throws Throwable {

        CmsObject cms = getCmsObject();     
        echo("Testing import resource for a folder");
        
        String resourcename = "/folder1/test1/";
        
        long timestamp = System.currentTimeMillis() - 87654321;        
        
        // create a new resource
        CmsResource resource = new CmsResource (
            CmsUUID.getNullUUID(),
            CmsUUID.getNullUUID(),
            resourcename,
            CmsResourceTypeFolder.getStaticTypeId(),
            true,
            0,
            cms.getRequestContext().currentProject().getId(),
            CmsResource.STATE_NEW,
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            timestamp, 
            cms.getRequestContext().currentUser().getId(),
            CmsResource.DATE_RELEASED_DEFAULT, 
            CmsResource.DATE_EXPIRED_DEFAULT,
            1, -1
        );
        
        cms.importResource(resourcename, resource, null, null);
        
        // ensure created resource is a folder
        assertIsFolder(cms, resourcename);  
        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, resourcename, CmsResource.STATE_NEW);
        // date last modified 
        assertDateLastModified(cms, resourcename, timestamp);
        // date created
        assertDateCreated(cms, resourcename, timestamp);
        // the user last modified must be the current user
        assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser()); 
        
        // publish the project
        cms.unlockProject(cms.getRequestContext().currentProject().getId());
        cms.publishProject();   
        
        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
    }  
    
    /**
     * Test the import resource method for an existing folder.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testImportFolderAgain() throws Throwable {

        CmsObject cms = getCmsObject();     
        echo("Testing to import an existing folder again");
        
        String resourcename = "/folder1/test1/";
        
        storeResources(cms, resourcename);
        long timestamp = System.currentTimeMillis() - 12345678;       

        // create a new folder
        CmsResource resource = new CmsResource (
            CmsUUID.getNullUUID(),
            CmsUUID.getNullUUID(),
            resourcename,
            CmsResourceTypeFolder.getStaticTypeId(),
            true,
            0,
            cms.getRequestContext().currentProject().getId(),
            CmsResource.STATE_NEW,
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            timestamp, 
            cms.getRequestContext().currentUser().getId(),
            CmsResource.DATE_RELEASED_DEFAULT, 
            CmsResource.DATE_EXPIRED_DEFAULT,
            1, -1
        );
        
        cms.importResource(resourcename, resource, null, null);
        
        // ensure created resource is a folder
        assertIsFolder(cms, resourcename);  
        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, resourcename, CmsResource.STATE_CHANGED);
        // date last modified 
        assertDateLastModified(cms, resourcename, timestamp);
        // the user last modified must be the current user
        assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());         
        // now evaluate the filter
        assertFilter(cms, resourcename, OpenCmsTestResourceFilter.FILTER_CREATE_RESOURCE);       
        
        // publish the project
        cms.unlockProject(cms.getRequestContext().currentProject().getId());
        cms.publishProject();     
        
        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);        
    }      
    
    /**
     * Test the import resource method.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testImportResource() throws Throwable {

        CmsObject cms = getCmsObject();     
        echo("Testing import resource");
        
        String resourcename = "/folder1/test1.html";
        
        String contentStr = "Hello this is my content";
        byte[] content = contentStr.getBytes();
        long timestamp = System.currentTimeMillis() - 87654321;        
        
        // create a new resource
        CmsResource resource = new CmsResource (
            CmsUUID.getNullUUID(),
            CmsUUID.getNullUUID(),
            resourcename,
            CmsResourceTypePlain.getStaticTypeId(),
            false,
            0,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -