📄 localcachetest.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.storage.tests;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.rapla.RaplaTestCase;
import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Period;
import org.rapla.entities.domain.internal.AllocatableImpl;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.AttributeType;
import org.rapla.entities.dynamictype.Classification;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
import org.rapla.entities.dynamictype.internal.AttributeImpl;
import org.rapla.entities.dynamictype.internal.DynamicTypeImpl;
import org.rapla.entities.storage.internal.SimpleIdentifier;
import org.rapla.storage.CachableStorageOperator;
import org.rapla.storage.LocalCache;
public class LocalCacheTest extends RaplaTestCase {
Locale locale;
public LocalCacheTest(String name) {
super(name);
}
public static Test suite() {
return new TestSuite(LocalCacheTest.class);
}
protected void setUp() throws Exception {
super.setUp();
}
public DynamicTypeImpl createDynamicType() throws Exception {
AttributeImpl attribute = new AttributeImpl(AttributeType.STRING);
attribute.setKey("name");
attribute.setId(new SimpleIdentifier(Attribute.TYPE,1));
DynamicTypeImpl dynamicType = new DynamicTypeImpl();
dynamicType.setElementKey("defaultResource");
dynamicType.setId(new SimpleIdentifier(DynamicType.TYPE,1));
dynamicType.addAttribute(attribute);
dynamicType.setAnnotation(DynamicTypeAnnotations.KEY_NAME_FORMAT,"{name}");
return dynamicType;
}
public AllocatableImpl createResource(int id,DynamicType type,String name) {
AllocatableImpl resource = new AllocatableImpl();
resource.setId(new SimpleIdentifier(Allocatable.TYPE,id));
Classification classification = type.newClassification();
classification.setValue("name",name);
resource.setClassification(classification);
return resource;
}
public void testAllocatable() throws Exception {
LocalCache cache = new LocalCache( Locale.GERMANY );
DynamicTypeImpl type = createDynamicType();
type.setReadOnly( true );
cache.put( type );
AllocatableImpl resource1 = createResource(1,type,"Adrian");
cache.put(resource1);
AllocatableImpl resource2 = createResource(2,type,"Beta");
cache.put(resource2);
AllocatableImpl resource3 = createResource(3,type,"Ceta");
cache.put(resource3);
resource1.getClassification().setValue("name","Zeta");
cache.put(resource1);
Allocatable[] resources = (Allocatable[])
cache.getCollection( Allocatable.TYPE).toArray(Allocatable.ALLOCATABLE_ARRAY);
assertEquals(3, resources.length);
assertTrue(resources[1].getName(locale).equals("Beta"));
}
public void test2() throws Exception {
CachableStorageOperator storage = (CachableStorageOperator)
getContext().lookup(CachableStorageOperator.ROLE + "/file");
storage.connect();
LocalCache cache = storage.getCache();
{
Iterator it = cache.getCollection(Period.TYPE).iterator();
Period period = (Period) it.next();
Collection reservations = cache.getReservations(null,period.getStart(),period.getEnd());
assertEquals(0,reservations.size());
period = (Period) it.next();
reservations = cache.getReservations(null,period.getStart(),period.getEnd());
assertEquals(2, reservations.size());
User user = cache.getUser("homer");
reservations = cache.getReservations(user,null,null);
assertEquals(3, reservations.size());
reservations = cache.getReservations(user,period.getStart(),period.getEnd());
assertEquals(2, reservations.size());
}
{
Iterator it = cache.getIterator(Allocatable.TYPE);
assertEquals("erwin",((Allocatable)it.next()).getName(locale));
assertEquals("Room A66",((Allocatable)it.next()).getName(locale));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -