KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > region > NewLocalTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.pojo.region;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.Fqn;
16 import org.jboss.cache.pojo.InternalConstant;
17 import org.jboss.cache.pojo.PojoCache;
18 import org.jboss.cache.pojo.PojoCacheFactory;
19 import org.jboss.cache.pojo.test.Person;
20
21 import java.util.Map JavaDoc;
22
23 /**
24  * Additional basic tests
25  *
26  * @author Ben Wang
27  */

28
29 public class NewLocalTest extends TestCase
30 {
31    Log log_ = LogFactory.getLog(org.jboss.cache.pojo.region.NewLocalTest.class);
32    PojoCache cache_;
33
34    public NewLocalTest(String JavaDoc name)
35    {
36       super(name);
37    }
38
39    protected void setUp() throws Exception JavaDoc
40    {
41       super.setUp();
42       log_.info("setUp() ....");
43       String JavaDoc configFile = "META-INF/local-service.xml";
44       boolean toStart = false;
45       cache_ = PojoCacheFactory.createCache(configFile, toStart);
46       cache_.start();
47       cache_.getCache().getRegion(Fqn.fromString("SESSION"), true);
48    }
49
50    protected void tearDown() throws Exception JavaDoc
51    {
52       super.tearDown();
53       cache_.stop();
54    }
55
56 // public void testDummy() {}
57

58    /**
59     * Not applied anymore.
60     *
61     * @throws Exception
62     */

63    public void XtestBadFqn() throws Exception JavaDoc
64    {
65       log_.info("testBadFqn() ....");
66       Person test = new Person();
67       test.setName("Ben");
68       test.setAge(10);
69       cache_.attach("/a", test);
70       Person result = (Person) cache_.detach("/a");
71       assertEquals(" ", test, result);
72       result.setAge(20);
73
74       try
75       {
76          cache_.attach(InternalConstant.JBOSS_INTERNAL_STRING, test);
77          fail("putObject under JBoss_Internal should fail");
78       }
79       catch (IllegalArgumentException JavaDoc iex)
80       {
81          // ok
82
}
83
84       try
85       {
86          cache_.detach(InternalConstant.JBOSS_INTERNAL_STRING);
87          fail("putObject under JBoss_Internal should fail");
88       }
89       catch (IllegalArgumentException JavaDoc iex)
90       {
91          // ok
92
}
93    }
94
95    public void testPutRemove() throws Exception JavaDoc
96    {
97       log_.info("testPutRemove() ....");
98       Person test = new Person();
99       test.setName("Ben");
100       test.setAge(10);
101       cache_.attach("/a", test);
102       Person result = (Person) cache_.find("/a");
103       assertEquals(" ", test, result);
104       result.setAge(20);
105       cache_.detach("/a");
106       assertNull("Object should be null ", cache_.find("/a"));
107       assertEquals("Age should be updated as ", 20, test.getAge());
108    }
109
110    public void testPutRemoveNodeExistence() throws Exception JavaDoc
111    {
112       log_.info("testPutRemove() ....");
113       Person test = new Person();
114       test.setName("Ben");
115       test.setAge(10);
116       cache_.attach("person", test);
117       Person result = (Person) cache_.find("person");
118       assertEquals(" ", test, result);
119       result.setAge(20);
120       cache_.detach("person");
121       assertNull("Object should be null ", cache_.find("person"));
122       assertEquals("Age should be updated as ", 20, test.getAge());
123
124       assertNull("DataNode should not exisit ", cache_.getCache().getRoot().get("person"));
125    }
126
127    public void testFindObjects() throws Exception JavaDoc
128    {
129       log_.info("testFindObjects() ....");
130       Map JavaDoc map = cache_.findAll("/");
131       assertEquals("Objects size should be ", 0, map.size());
132       Person ben = new Person();
133       ben.setName("Ben");
134       ben.setAge(10);
135       cache_.attach("/a/b/c", ben);
136       cache_.attach("/e", ben); // multiple keys, same pojo
137
Person joe = new Person();
138       joe.setName("Joe");
139       joe.setAge(10);
140       cache_.attach("/f/joe", joe);
141       map = cache_.findAll("/");
142       assertEquals("Objects size should be ", 3, map.size());
143
144       map = cache_.findAll("/a");
145       assertEquals("Objects size should be ", 1, map.size());
146       cache_.detach("/e");
147       map = cache_.findAll("/");
148       assertEquals("Objects size should be ", 2, map.size());
149    }
150
151    public static Test suite() throws Exception JavaDoc
152    {
153       return new TestSuite(org.jboss.cache.pojo.region.NewLocalTest.class);
154    }
155
156
157    public static void main(String JavaDoc[] args) throws Exception JavaDoc
158    {
159       junit.textui.TestRunner.run(org.jboss.cache.pojo.region.NewLocalTest.suite());
160    }
161
162 }
163
Popular Tags