testturbinecache.java

来自「jetspeed源代码」· Java 代码 · 共 512 行 · 第 1/2 页

JAVA
512
字号
/*
 * Copyright 2000-2001,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.apache.jetspeed.services.portletcache;

// Cactus and Junit imports
import junit.framework.Test;
import junit.framework.TestSuite;

// Turbine imports
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.cache.GlobalCacheService;
import org.apache.turbine.services.cache.CachedObject;
import org.apache.turbine.services.cache.ObjectExpiredException;
import org.apache.turbine.services.cache.Refreshable;
import org.apache.turbine.services.cache.RefreshableCachedObject;
import org.apache.turbine.util.TurbineConfig;
import org.apache.turbine.util.StringUtils;

// Jetspeed imports
import org.apache.jetspeed.test.JetspeedTestCase;

/**
 * TestTurbineCache
 *
 * @author <a href="paulsp@apache.org">Paul Spencer</a>
 * @version $Id: TestTurbineCache.java,v 1.1 2004/04/07 22:02:42 jford Exp $
 */
public class TestTurbineCache extends JetspeedTestCase {
    
    private static final String cacheKey = new String("CacheKey");
    private static final String cacheKey_2 = new String("CacheKey_2");
    private static final long TURBINE_CACHE_REFRESH = 5000; // in millis
    private static final long TEST_EXPIRETIME = TURBINE_CACHE_REFRESH + 1000;
    private static final long TEST_TIMETOLIVE = TEST_EXPIRETIME * 5;

    /**
     * Configuration object to run Turbine outside a servlet container
     * ( uses turbine.properties )
     */
    private static TurbineConfig config = null;
    
    /**
     * Sets up TurbineConfig using the system property:
     * <pre>turbine.properties</pre>
     */
    static
    {
        try
        {
            config = new TurbineConfig( "webapp",
            "/WEB-INF/conf/TurbineResources.properties");
            config.init();
        }
        catch (Exception e)
        {
            fail(StringUtils.stackTrace(e));
        }
    }

    /**
     * Defines the testcase name for JUnit.
     *
     * @param name the testcase's name.
     */
    public TestTurbineCache( String name ) {
        super( name );
    }
    
    /**
     * Start the tests.
     *
     * @param args the arguments. Not used
     */
    public static void main(String args[]) {
        junit.awtui.TestRunner.main( new String[] { TestTurbineCache.class.getName() } );
    }
 
    public void setup() {
//        getLogger().warn("Caching test may take a few minutes");
        System.out.println("Caching test may take a few minutes");
    }
    /**
     * Creates the test suite.
     *
     * @return a test suite (<code>TestSuite</code>) that includes all methods
     *         starting with "test"
     */
    public static Test suite() {
        // All methods starting with "test" will be executed in the test suite.
        return new TestSuite( TestTurbineCache.class );
    }
    
    /**
     * Simple test that verify an object can be created and deleted.
     * @throws Exception
     */
    public void testSimpleAddGetCacheObject() throws Exception {
        String testString = new String( "This is a test");
        Object retrievedObject = null;
        CachedObject cacheObject1 = null;
        
        GlobalCacheService globalCache = (GlobalCacheService)TurbineServices
        .getInstance()
        .getService( GlobalCacheService.SERVICE_NAME );
        
        // Create object
        cacheObject1 = new CachedObject(testString);
        assertNotNull( "Failed to create a cachable object 1", cacheObject1);
        
        // Add object to cache
        globalCache.addObject(cacheKey, cacheObject1);
        
        // Get object from cache
        retrievedObject = globalCache.getObject(cacheKey);
        assertNotNull( "Did not retrieved a cached object 1", retrievedObject);
        assertTrue( "Did not retrieved a correct, expected cached object 1", retrievedObject == cacheObject1);
        
        // Remove object from cache
        globalCache.removeObject(cacheKey);
        
        // Verify object removed from cache
        retrievedObject = null;
        cacheObject1 = null;
        try {
            retrievedObject = globalCache.getObject(cacheKey);
            assertNull( "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException", retrievedObject);
            assertNotNull( "Did not get expected ObjectExpiredException retrieving a deleted object", retrievedObject);
        } catch (ObjectExpiredException e) {
            assertNull( "Retrieved the deleted cached object 1, but caught expected ObjectExpiredException exception", retrievedObject);
        } catch (Exception e) {
            throw e;
        }
        
        // Remove object from cache that does NOT exist in the cache
        globalCache.removeObject(cacheKey);
    }
    
    /**
     * Simple test that adds, retrieves, and deletes 2 object.
     *
     * @throws Exception
     */
    public void test2ObjectAddGetCachedObject() throws Exception {
        String testString = new String( "This is a test");
        Object retrievedObject = null;
        CachedObject cacheObject1 = null;
        CachedObject cacheObject2 = null;
        
        GlobalCacheService globalCache = (GlobalCacheService)TurbineServices
        .getInstance()
        .getService( GlobalCacheService.SERVICE_NAME );
        
        // Create and add Object #1
        cacheObject1 = new CachedObject(testString);
        assertNotNull( "Failed to create a cachable object 1", cacheObject1);
        globalCache.addObject(cacheKey, cacheObject1);
        retrievedObject = globalCache.getObject(cacheKey);
        assertNotNull( "Did not retrieved a cached object 1", retrievedObject);
        assertEquals( "Did not retrieved correct cached object", cacheObject1, retrievedObject);
        
        // Create and add Object #2
        cacheObject2 = new CachedObject(testString);
        assertNotNull( "Failed to create a cachable object 2", cacheObject2);
        globalCache.addObject(cacheKey_2, cacheObject2);
        retrievedObject = globalCache.getObject(cacheKey_2);
        assertNotNull( "Did not retrieved a cached object 2", retrievedObject);
        assertEquals( "Did not retrieved correct cached object 2", cacheObject2, retrievedObject);
        
        // Get object #1
        retrievedObject = globalCache.getObject(cacheKey);
        assertNotNull( "Did not retrieved a cached object 1. Attempt #2", retrievedObject);
        assertEquals( "Did not retrieved correct cached object 1. Attempt #2", cacheObject1, retrievedObject);
        
        // Get object #1
        retrievedObject = globalCache.getObject(cacheKey);
        assertNotNull( "Did not retrieved a cached object 1. Attempt #3", retrievedObject);
        assertEquals( "Did not retrieved correct cached object 1. Attempt #3", cacheObject1, retrievedObject);
        
        // Get object #2
        retrievedObject = globalCache.getObject(cacheKey_2);
        assertNotNull( "Did not retrieved a cached object 2. Attempt #2", retrievedObject);
        assertEquals( "Did not retrieved correct cached object 2 Attempt #2", cacheObject2, retrievedObject);
        
        // Remove objects
        globalCache.removeObject(cacheKey);
        globalCache.removeObject(cacheKey_2);
    }
    
    /**
     * Verify that an object will throw the ObjectExpiredException
     * when it now longer exists in cache.
     *
     * @throws Exception
     */    
    public void testObjectExpiration() throws Exception {
        String testString = new String( "This is a test");
        Object retrievedObject = null;
        CachedObject cacheObject = null;
        
//        getLogger().warn("This test will take a few mintes");
        System.out.println("This test will take a few mintes");
        
        GlobalCacheService globalCache = (GlobalCacheService)TurbineServices
        .getInstance()
        .getService( GlobalCacheService.SERVICE_NAME );
        
        // Create and add Object that expires in 1000 millis (1 second)
        cacheObject = new CachedObject(testString, 1000);
        assertNotNull( "Failed to create a cachable object", cacheObject);
        long addTime = System.currentTimeMillis();
        globalCache.addObject(cacheKey, cacheObject);
        
        // Try to get un-expired object
        try {
            retrievedObject = null;
            retrievedObject = globalCache.getObject(cacheKey);
            assertNotNull( "Did not retrieved a cached object", retrievedObject);
            assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject);
        } catch (ObjectExpiredException e) {
            assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false);
        } catch (Exception e) {
            throw e;
        }
        
        // Sleep 1500 Millis (1.5 seconds)
        Thread.sleep(1500);
        
        // Try to get expired object
        try {
            retrievedObject = null;
            retrievedObject = globalCache.getObject(cacheKey);
            assertNull( "Retrieved the expired cached object  and did not get expected ObjectExpiredException", retrievedObject);
            assertNotNull( "Did not get expected ObjectExpiredException retrieving an expired object", retrievedObject);
        } catch (ObjectExpiredException e) {
            assertNull( "Retrieved the expired cached object, but caught expected ObjectExpiredException exception", retrievedObject);
        } catch (Exception e) {
            throw e;
        }
        
        // Remove objects
        globalCache.removeObject(cacheKey);

⌨️ 快捷键说明

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