KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > options > ExplicitVersionsTest


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

7 package org.jboss.cache.options;
8
9 import junit.framework.Assert;
10 import junit.framework.TestCase;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.NodeSPI;
14 import org.jboss.cache.config.Configuration;
15 import org.jboss.cache.config.Option;
16 import org.jboss.cache.optimistic.DataVersion;
17 import org.jboss.cache.optimistic.DefaultDataVersion;
18
19 import javax.transaction.TransactionManager JavaDoc;
20
21 /**
22  * Tests the passing in of explicit {@see DataVersion} instances when using optimistic locking.
23  *
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  */

26 public class ExplicitVersionsTest extends TestCase
27 {
28
29    private CacheImpl cache;
30    private Fqn fqn = Fqn.fromString("/a");
31    private String JavaDoc key = "key";
32    private Option option = new Option();
33
34    protected void setUp() throws Exception JavaDoc
35    {
36       if (cache != null) tearDown();
37       cache = new CacheImpl();
38       cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
39       cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
40       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
41       cache.start();
42    }
43
44    protected void tearDown()
45    {
46       if (cache != null)
47       {
48          cache.stop();
49          cache = null;
50       }
51    }
52
53    public void testSimplePut() throws Exception JavaDoc
54    {
55       DataVersion version = new TestVersion("99");
56       cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
57       cache.put(fqn, key, "value");
58
59       //now retrieve the data from the cache.
60
Assert.assertEquals("value", cache.get(fqn, key));
61
62       // get a hold of the node
63
NodeSPI node = (NodeSPI) cache.get(fqn);
64       DataVersion versionFromCache = node.getVersion();
65
66       Assert.assertEquals(TestVersion.class, versionFromCache.getClass());
67       Assert.assertEquals("99", ((TestVersion) versionFromCache).getInternalVersion());
68    }
69
70    public void testFailingPut() throws Exception JavaDoc
71    {
72       DataVersion version = new TestVersion("99");
73       cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
74       cache.put(fqn, key, "value");
75
76       version = new TestVersion("25");
77       cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
78       TransactionManager JavaDoc mgr = cache.getTransactionManager();
79       mgr.begin();
80       cache.put(fqn, key, "value2");
81       try
82       {
83          mgr.commit();
84          Assert.assertTrue("expected to fail", false);
85       }
86       catch (Exception JavaDoc e)
87       {
88          // should fail.
89
Assert.assertTrue("expected to fail", true);
90       }
91    }
92
93    public void testIncompatibleVersionTypes() throws Exception JavaDoc
94    {
95       DataVersion version = new TestVersion("99");
96       cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
97       cache.put(fqn, key, "value");
98       TransactionManager JavaDoc mgr = cache.getTransactionManager();
99       mgr.begin();
100       cache.getInvocationContext().getOptionOverrides().setDataVersion(new DefaultDataVersion(777));
101       cache.put(fqn, key, "value2");
102       try
103       {
104          // this call will use implicit versioning and will hence fail.
105
mgr.commit();
106          Assert.assertTrue("expected to fail", false);
107       }
108       catch (Exception JavaDoc e)
109       {
110          // should fail.
111
Assert.assertTrue("expected to fail", true);
112       }
113    }
114
115    public void testExplicitVersionOnLeaf() throws Exception JavaDoc
116    {
117       cache.put("/org/domain/Entity", null);
118       assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
119
120       TestVersion v = new TestVersion("Arse");
121       cache.getInvocationContext().getOptionOverrides().setDataVersion(v);
122       cache.put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
123
124       assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
125       assertEquals(v, ((NodeSPI) cache.get("/org/domain/Entity/EntityInstance#1")).getVersion());
126    }
127
128 }
129
130 class TestVersion implements DataVersion
131 {
132    private String JavaDoc myVersion;
133
134    public TestVersion(String JavaDoc version)
135    {
136       myVersion = version;
137    }
138
139    public String JavaDoc getInternalVersion()
140    {
141       return myVersion;
142    }
143
144    public void setInternalVersion(String JavaDoc version)
145    {
146       myVersion = version;
147    }
148
149    public boolean newerThan(DataVersion other)
150    {
151       if (other instanceof TestVersion)
152       {
153          int myInt = Integer.parseInt(myVersion);
154          int otherInt = Integer.parseInt(((TestVersion) other).getInternalVersion());
155          return myInt > otherInt;
156       }
157       else throw new IllegalArgumentException JavaDoc("version type mismatch");
158    }
159
160
161    public String JavaDoc toString()
162    {
163       return "TestVersion-" + myVersion;
164    }
165
166    public boolean equals(Object JavaDoc other)
167    {
168       if (other instanceof TestVersion)
169       {
170          TestVersion oVersion = (TestVersion) other;
171          if (oVersion.myVersion == null && myVersion == null) return true;
172          if (myVersion != null) return myVersion.equals(oVersion.myVersion);
173       }
174       return false;
175    }
176 }
Popular Tags