001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.cache.tests;
066:
067: import com.jcorporate.expresso.core.cache.CacheException;
068: import com.jcorporate.expresso.core.cache.CacheManager;
069: import com.jcorporate.expresso.core.cache.CacheSystem;
070: import com.jcorporate.expresso.core.dbobj.ValidValue;
071: import com.jcorporate.expresso.services.test.ExpressoTestCase;
072: import com.jcorporate.expresso.services.test.TestSystemInitializer;
073: import junit.framework.Test;
074: import junit.framework.TestSuite;
075: import org.apache.log4j.Logger;
076:
077: /**
078: * <p>Title: </p>
079: * <p>Description: </p>
080: * <p>Copyright: </p>
081: * <p>Company: </p>
082: *
083: * @author Michael Rimov
084: * @version $Revision: 1.3 $ on $Date: 2004/11/17 20:48:22 $
085: */
086:
087: public class TTLTest extends ExpressoTestCase {
088:
089: private Logger log = Logger.getLogger(TTLTest.class);
090:
091: private String cacheName = "testcache";
092:
093: public TTLTest(String name) throws Exception {
094: super (name);
095: }
096:
097: public static void main(String[] args) {
098: //Set the system properties we need
099: junit.textui.TestRunner.run(suite());
100:
101: }
102:
103: /**
104: * Define the suite of tests that verify each function of the cache
105: *
106: * @return junit.framework.TestSuite
107: */
108: public static Test suite() {
109: TestSuite suite = new TestSuite(TTLTest.class);
110: return suite;
111: } /* suite() */
112:
113: /**
114: * Sets up the fixture, for example, open a network connection.
115: * This method is called before a test is executed.
116: */
117: public void setUp() throws Exception {
118: try {
119: CacheManager.createCache(TestSystemInitializer
120: .getTestContext(), cacheName, false);
121:
122: /* Create an ordered cache as well */
123: CacheManager.createCache(TestSystemInitializer
124: .getTestContext(), cacheName + "O", true);
125: } catch (java.util.ConcurrentModificationException cme) {
126: log.error("Error Setting Up", cme);
127: throw cme;
128: }
129: } /* setUp() */
130:
131: /**
132: * Tests to make sure the TTL system is running.
133: */
134: public synchronized void testUnorderedCacheTTL() {
135: try {
136: CacheSystem cs = CacheManager
137: .getCacheSystem(TestSystemInitializer
138: .getTestContext());
139:
140: ValidValue b = new ValidValue("b", "BE!");
141:
142: if (CacheManager.existsCache(TestSystemInitializer
143: .getTestContext(), cacheName)) {
144: if (log.isInfoEnabled()) {
145: log.info("Clearing cache: " + cacheName);
146: }
147: CacheManager.clear(TestSystemInitializer
148: .getTestContext(), cacheName);
149: }
150:
151: cs.addItem(cacheName, b, 500);
152:
153: Thread.sleep(1000);
154:
155: ValidValue retrieved = (ValidValue) CacheManager.getItem(
156: TestSystemInitializer.getTestContext(), cacheName,
157: "b");
158:
159: assertTrue("retrieved should expires and be null",
160: retrieved == null);
161: } catch (CacheException ex) {
162: ex.printStackTrace();
163: fail("Cache exception manipulating cache");
164: } catch (Exception e) {
165: e.printStackTrace();
166: fail("Exception manipulating cache");
167: }
168: }
169:
170: /**
171: * Tests to make sure the TTL system is running.
172: */
173: public synchronized void testOrderedCacheTTL() {
174: try {
175: CacheSystem cs = CacheManager
176: .getCacheSystem(TestSystemInitializer
177: .getTestContext());
178:
179: ValidValue b = new ValidValue("b", "BE!");
180:
181: if (CacheManager.existsCache(TestSystemInitializer
182: .getTestContext(), cacheName + "O")) {
183: if (log.isInfoEnabled()) {
184: log.info("Clearing cache: " + cacheName + "O");
185: }
186: CacheManager.clear(TestSystemInitializer
187: .getTestContext(), cacheName + "O");
188: }
189:
190: cs.addItem(cacheName + "O", b, 500);
191:
192: Thread.sleep(1000);
193:
194: ValidValue retrieved = (ValidValue) cs.getItem(cacheName
195: + "O", "b");
196:
197: assertTrue("retrieved should expire and be null",
198: retrieved == null);
199:
200: cs.addItem(cacheName + "0", b, 500);
201:
202: Thread.sleep(1000);
203: java.util.List l = cs.getItems(cacheName + "0");
204: assertTrue("retrieved should expire and be null", l == null);
205:
206: } catch (CacheException ex) {
207: ex.printStackTrace();
208: fail("Cache exception manipulating cache");
209: } catch (Exception e) {
210: e.printStackTrace();
211: fail("Exception manipulating cache");
212: }
213: }
214:
215: }
|