1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.optimistic; |
8 |
| |
9 |
| import org.jboss.cache.CacheImpl; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.GlobalTransaction; |
12 |
| import org.jboss.cache.OptimisticTransactionEntry; |
13 |
| import org.jboss.cache.TransactionTable; |
14 |
| import org.jboss.cache.interceptors.Interceptor; |
15 |
| import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor; |
16 |
| import org.jboss.cache.loader.SamplePojo; |
17 |
| import org.jboss.cache.marshall.MethodDeclarations; |
18 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
19 |
| |
20 |
| import javax.transaction.Transaction; |
21 |
| import javax.transaction.TransactionManager; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class OptimisticCreateIfNotExistsInterceptorTest extends AbstractOptimisticTestCase |
27 |
| { |
28 |
| |
29 |
| protected TransactionManager txManager; |
30 |
| protected Transaction tx; |
31 |
| protected GlobalTransaction gtx; |
32 |
| protected TransactionTable table; |
33 |
| protected OptimisticTransactionEntry entry; |
34 |
| protected TransactionWorkspace workspace; |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
3
| public OptimisticCreateIfNotExistsInterceptorTest(String name)
|
40 |
| { |
41 |
3
| super(name);
|
42 |
| } |
43 |
| |
44 |
3
| protected void setupTransactionsInInvocationCtx(CacheImpl cache) throws Exception
|
45 |
| { |
46 |
3
| txManager = DummyTransactionManager.getInstance();
|
47 |
| |
48 |
3
| txManager.begin();
|
49 |
| |
50 |
| |
51 |
3
| table = cache.getTransactionTable();
|
52 |
| |
53 |
| |
54 |
3
| gtx = cache.getCurrentTransaction();
|
55 |
3
| tx = txManager.getTransaction();
|
56 |
3
| entry = (OptimisticTransactionEntry) table.get(gtx);
|
57 |
3
| workspace = entry.getTransactionWorkSpace();
|
58 |
| |
59 |
| |
60 |
3
| cache.getInvocationContext().setGlobalTransaction(gtx);
|
61 |
3
| cache.getInvocationContext().setTransaction(tx);
|
62 |
| } |
63 |
| |
64 |
1
| public void testNodeCreation() throws Exception
|
65 |
| { |
66 |
| |
67 |
1
| TestListener listener = new TestListener();
|
68 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
69 |
| |
70 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
71 |
1
| interceptor.setCache(cache);
|
72 |
1
| MockInterceptor dummy = new MockInterceptor();
|
73 |
1
| dummy.setCache(cache);
|
74 |
| |
75 |
1
| interceptor.setNext(dummy);
|
76 |
| |
77 |
1
| cache.setInterceptorChain(interceptor);
|
78 |
| |
79 |
1
| setupTransactionsInInvocationCtx(cache);
|
80 |
| |
81 |
1
| final SamplePojo pojo = new SamplePojo(21, "test");
|
82 |
1
| cache.put("/one/two", "key1", pojo);
|
83 |
| |
84 |
1
| assertEquals(3, workspace.getNodes().size());
|
85 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
86 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/")));
|
87 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
88 |
1
| assertTrue(entry.getLocks().isEmpty());
|
89 |
| |
90 |
1
| assertTrue(!cache.exists("/one/two"));
|
91 |
1
| assertEquals(MethodDeclarations.putKeyValMethodLocal, dummy.getCalled());
|
92 |
1
| txManager.commit();
|
93 |
| |
94 |
1
| cache.stop();
|
95 |
| |
96 |
| } |
97 |
| |
98 |
1
| public void testInvalidTransaction() throws Exception
|
99 |
| { |
100 |
| |
101 |
1
| TestListener listener = new TestListener();
|
102 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
103 |
| |
104 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
105 |
1
| interceptor.setCache(cache);
|
106 |
1
| MockInterceptor dummy = new MockInterceptor();
|
107 |
1
| dummy.setCache(cache);
|
108 |
| |
109 |
1
| interceptor.setNext(dummy);
|
110 |
| |
111 |
| |
112 |
1
| cache.setInterceptorChain(interceptor);
|
113 |
| |
114 |
1
| setupTransactionsInInvocationCtx(cache);
|
115 |
1
| final SamplePojo pojo = new SamplePojo(21, "test");
|
116 |
1
| cache.put("/one/two", "key1", pojo);
|
117 |
| |
118 |
1
| assertEquals(3, workspace.getNodes().size());
|
119 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
120 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/")));
|
121 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
122 |
1
| assertTrue(entry.getLocks().isEmpty());
|
123 |
| |
124 |
1
| assertTrue(!cache.exists("/one/two"));
|
125 |
1
| assertEquals(MethodDeclarations.putKeyValMethodLocal, dummy.getCalled());
|
126 |
| |
127 |
1
| txManager.commit();
|
128 |
| |
129 |
1
| cache.getInvocationContext().setGlobalTransaction(null);
|
130 |
1
| cache.getInvocationContext().setTransaction(null);
|
131 |
| |
132 |
1
| try
|
133 |
| { |
134 |
1
| cache.put("/one/two/three", "key1", pojo);
|
135 |
0
| assertTrue("Should never be reched", false);
|
136 |
| } |
137 |
| catch (Throwable t) |
138 |
| { |
139 |
1
| assertTrue(true);
|
140 |
| } |
141 |
1
| cache.stop();
|
142 |
| |
143 |
| } |
144 |
| |
145 |
1
| public void testMultiplePut() throws Exception
|
146 |
| { |
147 |
| |
148 |
1
| TestListener listener = new TestListener();
|
149 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
150 |
| |
151 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
152 |
1
| interceptor.setCache(cache);
|
153 |
1
| MockInterceptor dummy = new MockInterceptor();
|
154 |
1
| dummy.setCache(cache);
|
155 |
1
| interceptor.setNext(dummy);
|
156 |
| |
157 |
1
| cache.setInterceptorChain(interceptor);
|
158 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
159 |
| |
160 |
1
| setupTransactionsInInvocationCtx(cache);
|
161 |
| |
162 |
1
| cache.put("/one/two", "key1", pojo);
|
163 |
1
| cache.put("/one/two", "key2", pojo);
|
164 |
| |
165 |
1
| assertEquals(3, workspace.getNodes().size());
|
166 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
167 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/")));
|
168 |
1
| assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
169 |
1
| assertTrue(entry.getLocks().isEmpty());
|
170 |
| |
171 |
1
| assertTrue(!cache.exists("/one/two"));
|
172 |
1
| assertEquals(MethodDeclarations.putKeyValMethodLocal, dummy.getCalled());
|
173 |
| |
174 |
1
| txManager.commit();
|
175 |
| |
176 |
1
| cache.stop();
|
177 |
| |
178 |
| } |
179 |
| |
180 |
| |
181 |
| } |