KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > CacheObjectInfoImplTest


1 /* Open Source Java Caching Service
2 * Copyright (C) 2002 Frank Karlstrøm
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * The author can be contacted by email: fjankk@users.sourceforge.net
18 */

19 package org.fjank.jcache;
20
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24 import javax.util.jcache.Attributes;
25 import javax.util.jcache.CacheAccessFactory;
26 import javax.util.jcache.CacheObjectInfo;
27 import junit.framework.TestCase;
28
29 /**
30  * @author Frank Karlstrøm
31  *
32  *
33  */

34 public class CacheObjectInfoImplTest extends TestCase {
35     private CacheObject testObj;
36     private CacheObjectInfoImpl impl;
37     private static final long TTL = 5;
38     
39     protected void setUp() throws Exception JavaDoc {
40         testObj = new CacheObject("key", "value", new CacheGroup("group"), new CacheRegion("name", new AttributesImpl()), null);
41        Attributes att = CacheAccessFactory.getInstance().getDefaultAttributes();
42        att.setTimeToLive(TTL);
43         testObj.setAttributes(att);
44         impl = new CacheObjectInfoImpl(testObj);
45         
46     }
47     
48     public final void testGetRegion() {
49         impl.getRegion();
50     }
51
52     public final void testGetName() {
53         impl.getName();
54     }
55
56     public final void testGetType() {
57         impl.getType();
58     }
59
60     public final void testGetGroup() {
61         impl.getGroup();
62     }
63
64     public final void testGetRefCount() {
65         impl.getRefCount();
66     }
67
68     public final void testGetAccesses() {
69         impl.getAccesses();
70     }
71
72     public final void testGetExpire() throws ParseException JavaDoc {
73         SimpleDateFormat JavaDoc form = new SimpleDateFormat JavaDoc("EEE MMM dd HH:mm:ss zzz yyyy");
74         
75         Attributes att = testObj.getAttributes();
76         //TTL in seconds
77
long ttl = att.getTimeToLive();
78         //createtime in millis
79
long createTime = att.getCreateTime();
80         //expiry is in second precision, lets round ouf our own.
81
long expire = form.parse(impl.getExpire()).getTime();
82         long expectedExpiry = form.parse(form.format(new Date JavaDoc((ttl*1000)+createTime))).getTime();
83         assertEquals(expectedExpiry, expire);
84         //check that objects which never expire, never expire....
85
CacheObject obj = new CacheObject("key", "value", null, null, null);
86         Attributes att1 = CacheAccessFactory.getInstance().getDefaultAttributes();
87         obj.setAttributes(att1);
88         CacheObjectInfoImpl imp = new CacheObjectInfoImpl(obj);
89         assertEquals(CacheObjectInfo.NEVER_EXPIRES, imp.getExpire());
90         
91     }
92 }
93
Popular Tags