/* Open Source Java Caching Service
* Copyright (C) 2002 Frank Karlstr�m
* 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.
*
* 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
*
* The author can be contacted by email: fjankk@users.sourceforge.net
*/
package org.fjank.jcache;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.util.jcache.Attributes;
import javax.util.jcache.CacheAccessFactory;
import javax.util.jcache.CacheObjectInfo;
import junit.framework.TestCase;
/**
* @author Frank Karlstr�m
*
*
*/
public class CacheObjectInfoImplTest extends TestCase {
private CacheObject testObj;
private CacheObjectInfoImpl impl;
private static final long TTL = 5;
protected void setUp() throws Exception {
testObj = new CacheObject("key", "value", new CacheGroup("group"), new CacheRegion("name", new AttributesImpl()), null);
Attributes att = CacheAccessFactory.getInstance().getDefaultAttributes();
att.setTimeToLive(TTL);
testObj.setAttributes(att);
impl = new CacheObjectInfoImpl(testObj);
}
public final void testGetRegion() {
impl.getRegion();
}
public final void testGetName() {
impl.getName();
}
public final void testGetType() {
impl.getType();
}
public final void testGetGroup() {
impl.getGroup();
}
public final void testGetRefCount() {
impl.getRefCount();
}
public final void testGetAccesses() {
impl.getAccesses();
}
public final void testGetExpire() throws ParseException {
SimpleDateFormat form = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Attributes att = testObj.getAttributes();
//TTL in seconds
long ttl = att.getTimeToLive();
//createtime in millis
long createTime = att.getCreateTime();
//expiry is in second precision, lets round ouf our own.
long expire = form.parse(impl.getExpire()).getTime();
long expectedExpiry = form.parse(form.format(new Date((ttl*1000)+createTime))).getTime();
assertEquals(expectedExpiry, expire);
//check that objects which never expire, never expire....
CacheObject obj = new CacheObject("key", "value", null, null, null);
Attributes att1 = CacheAccessFactory.getInstance().getDefaultAttributes();
obj.setAttributes(att1);
CacheObjectInfoImpl imp = new CacheObjectInfoImpl(obj);
assertEquals(CacheObjectInfo.NEVER_EXPIRES, imp.getExpire());
}
}