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

📄 testreadresource.java

📁 cms是开源的框架
💻 JAVA
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestReadResource.java,v $
 * Date   : $Date: 2005/06/27 23:22:09 $
 * Version: $Revision: 1.8 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
package org.opencms.file;

import org.opencms.main.CmsException;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Unit test for the "readFileHeader" method of the CmsObject to test the release and expiration date.<p>
 * 
 * @author Michael Emmerich 
 * @version $Revision: 1.8 $
 */
public class TestReadResource extends OpenCmsTestCase {
  
    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */    
    public TestReadResource(String arg0) {
        super(arg0);       
    }
    
    /**
     * Test suite for this test class.<p>
     * 
     * @return the test suite
     */
    public static Test suite() {
        OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
        
        TestSuite suite = new TestSuite();
        suite.setName(TestReadResource.class.getName());
        
        suite.addTest(new TestReadResource("testReadBeforeReleaseDate"));
        suite.addTest(new TestReadResource("testReadInValidTimeRange"));
        suite.addTest(new TestReadResource("testReadAfterExpirationDate"));
        suite.addTest(new TestReadResource("testReadBeforeReleaseDateIgnore"));
        suite.addTest(new TestReadResource("testReadInValidTimeRangeIgnore"));
        suite.addTest(new TestReadResource("testReadAfterExpirationDateIgnore"));
        
        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {
                setupOpenCms("simpletest", "/sites/default/");                
            }
            
            protected void tearDown() {
                removeOpenCms();
            }
        };
        
        return wrapper;
    }       
    
    /**
     * Test readResource of a file before its release date.<p>
     * 
     * @param tc the OpenCmsTestCase
     * @param cms the CmsObject
     * @param resource1 the resource to touch
     * @param filter the filter to use
     * @throws Throwable if something goes wrong
     */
    public static void readBeforeReleaseDate(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsResourceFilter filter) throws Throwable {            
              
        tc.storeResources(cms, resource1);
      
        // preperation, modify the release date
        CmsFile preperationRes = cms.readFile(resource1, CmsResourceFilter.ALL);
        // set the release date to one hour in the future
        preperationRes .setDateReleased(System.currentTimeMillis() + (60 * 60 *1000));
        
        cms.lockResource(resource1);        
        cms.writeFile(preperationRes);
        cms.unlockResource(resource1);
        
        // now try to access the resource
        try {
            cms.readResource(resource1, filter);
            if (!filter.includeDeleted()) {
                // the file could be read, despite the release date set in the future
                fail("Resource "+ resource1+ " could be read before release date");
            }               
        } catch (CmsException e) {
            if (filter.includeDeleted()) {
                fail("Resource "+ resource1+ " could not be read");
            }  
        }      
    }

    /**
     * Test readResource of a file after its expirationrelease date.<p>
     * 
     * @param tc the OpenCmsTestCase
     * @param cms the CmsObject
     * @param resource1 the resource to touch
     * @param filter the filter to use
     * @throws Throwable if something goes wrong
     */
    public static void readAfterExpirationDate(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsResourceFilter filter) throws Throwable {            
              
        tc.storeResources(cms, resource1);
      
        // preperation, modify the expiration date
        CmsFile preperationRes = cms.readFile(resource1, CmsResourceFilter.ALL);
        // set the expiration date to one hour in the past
        preperationRes.setDateExpired(System.currentTimeMillis() - (60 * 60 *1000));

        cms.lockResource(resource1);          
        cms.writeFile(preperationRes);
        cms.unlockResource(resource1);          
        
        // now try to access the resource
        try {
            cms.readResource(resource1, filter);
            if (!filter.includeDeleted()) {
                // the file could be read, despite the expiration date was set to the past
                fail("Resource "+ resource1+ " could be read after the expiration date");
            }               
        } catch (CmsException e) {
            if (filter.includeDeleted()) {
                fail("Resource "+ resource1+ " could not be read");
            }  
        }      
    }
  
    /**
     * Test readResource of a file in its valid time range.<p>
     * 
     * @param tc the OpenCmsTestCase
     * @param cms the CmsObject
     * @param resource1 the resource to touch
     * @param filter the filter to use
     * @throws Throwable if something goes wrong
     */
    public static void readInValidTimeRange(OpenCmsTestCase tc, CmsObject cms, String resource1, CmsResourceFilter filter) throws Throwable {            
              
        tc.storeResources(cms, resource1);
      
        // preperation, modify the expiration date
        CmsFile preperationRes = cms.readFile(resource1, CmsResourceFilter.ALL);
        // set the release date to one hour in the future
        preperationRes.setDateReleased(System.currentTimeMillis()- (60 * 60 *1000));
        // set the expiration date to one hour in the past
        preperationRes.setDateExpired(System.currentTimeMillis() + (60 * 60 *1000));
        
        cms.lockResource(resource1);                  
        cms.writeFile(preperationRes);
        cms.unlockResource(resource1);          
        
        // now try to access the resource
        try {
            cms.readResource(resource1, filter);            
        } catch (CmsException e) {
                fail("Resource "+ resource1+ " could not be read");
        }      
    }
    
    /**
     * Test readResource of a file before its release date.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testReadBeforeReleaseDate() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing readFileHeader of a file before the release date");
        readBeforeReleaseDate(this, cms, "/folder1/page1.html", CmsResourceFilter.DEFAULT);
    }
    
    /**
     * Test readFileHeader of a file after its expiration date.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testReadAfterExpirationDate() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing readFileHeader of a file after the expiration date");
        readAfterExpirationDate(this, cms, "/folder1/page2.html", CmsResourceFilter.DEFAULT);
    }

    /**
     * Test readFileHeader of a file in its valid time range.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testReadInValidTimeRange() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing readFileHeader of a file in its valid time range");
        readInValidTimeRange(this, cms, "/folder1/page3.html", CmsResourceFilter.DEFAULT);
    }
    
    /**
     * Test readFileHeader of a file before its release date.<p>
     * The valid time range will be ignored.
     * 
     * @throws Throwable if something goes wrong
     */
    public void testReadBeforeReleaseDateIgnore() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing readFileHeader of a file before the release date, ignoring valid timerange");
        readBeforeReleaseDate(this, cms, "/folder1/page1.html", CmsResourceFilter.ALL);
    }
    
    /**
     * Test readFileHeader of a file after its expiration date.<p>
     * The valid time range will be ignored.
     * 
     * @throws Throwable if something goes wrong
     */
    public void testReadAfterExpirationDateIgnore() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing readFileHeader of a file after the expiration date, ignoring valid timerange");
        readAfterExpirationDate(this, cms, "/folder1/page2.html", CmsResourceFilter.ALL);
    }

    /**
     * Test readFileHeader of a file in its valid time range.<p>
     * The valid time range will be ignored.
     * 
     * @throws Throwable if something goes wrong
     */
    public void testReadInValidTimeRangeIgnore() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing readFileHeader of a file in its valid time range, ignoring valid timerange");
        readInValidTimeRange(this, cms, "/folder1/page3.html", CmsResourceFilter.ALL);
    }
    
}

⌨️ 快捷键说明

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