KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > optimisticlock > test > OptimisticLockUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.optimisticlock.test;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26
27 import javax.rmi.PortableRemoteObject JavaDoc;
28
29 import junit.framework.Test;
30 import org.jboss.test.JBossTestCase;
31 import org.jboss.test.cmp2.optimisticlock.interfaces.FacadeHome;
32 import org.jboss.test.cmp2.optimisticlock.interfaces.Facade;
33 import org.jboss.test.cmp2.optimisticlock.bug1006723.testsession.TestSessionRemoteHome;
34 import org.jboss.test.cmp2.optimisticlock.bug1006723.testsession.TestSessionRemote;
35
36
37 /**
38  * This class tests optimistic locking with different strategies.
39  *
40  * @author <a HREF="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
41  */

42 public class OptimisticLockUnitTestCase
43    extends JBossTestCase
44 {
45    // Constants -------------------------------------
46
private static final String JavaDoc ENTITY_GROUP_LOCKING = "local/EntityGroupLocking";
47    private static final String JavaDoc ENTITY_MODIFIED_LOCKING = "local/EntityModifiedLocking";
48    private static final String JavaDoc ENTITY_READ_LOCKING = "local/EntityReadLocking";
49    private static final String JavaDoc ENTITY_VERSION_LOCKING = "local/EntityVersionLocking";
50    private static final String JavaDoc ENTITY_EXPLICIT_VERSION_LOCKING = "local/EntityExplicitVersionLocking";
51    private static final String JavaDoc ENTITY_TIMESTAMP_LOCKING = "local/EntityTimestampLocking";
52    private static final String JavaDoc ENTITY_KEYGEN_LOCKING = "local/EntityKeyGeneratorLocking";
53
54    // Attributes ------------------------------------
55
private FacadeHome facadeHome;
56    /**
57     * entity primary key value
58     */

59    private static final Integer JavaDoc id = new Integer JavaDoc(1);
60
61    // Constructor -----------------------------------
62
public OptimisticLockUnitTestCase(String JavaDoc name)
63    {
64       super(name);
65    }
66
67    // TestCase overrides ----------------------------
68
public static Test suite() throws Exception JavaDoc
69    {
70       return JBossTestCase.getDeploySetup(OptimisticLockUnitTestCase.class, "cmp2-optimisticlock.jar");
71    }
72
73    // Tests -----------------------------------------
74
public void testBug1006723() throws Exception JavaDoc
75    {
76       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
77       TestSessionRemoteHome testSessionRemoteHome = (TestSessionRemoteHome)
78          PortableRemoteObject.narrow(ctx.lookup("ejb/TestSession"), TestSessionRemoteHome.class);
79       TestSessionRemote testSessionRemote = testSessionRemoteHome.create();
80       Long JavaDoc oID = testSessionRemote.setup();
81       testSessionRemote.test(oID);
82    }
83
84    public void testNullLockedFields() throws Exception JavaDoc
85    {
86       Facade facade = getFacadeHome().create();
87       facade.createCmpEntity(ENTITY_MODIFIED_LOCKING, id,
88          null, new Integer JavaDoc(1), null, "str2", null, new Double JavaDoc(2.2));
89       try
90       {
91          facade.testNullLockedFields(ENTITY_MODIFIED_LOCKING, id);
92       }
93       finally
94       {
95          tearDown(ENTITY_MODIFIED_LOCKING);
96       }
97    }
98
99    public void testKeygenStrategyPass() throws Exception JavaDoc
100    {
101       setup(ENTITY_KEYGEN_LOCKING);
102       Facade facade = getFacadeHome().create();
103       try
104       {
105          facade.testKeygenStrategyPass(ENTITY_KEYGEN_LOCKING, id);
106       }
107       finally
108       {
109          tearDown(ENTITY_KEYGEN_LOCKING);
110       }
111    }
112
113    public void testKeygenStrategyFail() throws Exception JavaDoc
114    {
115       setup(ENTITY_KEYGEN_LOCKING);
116       Facade facade = getFacadeHome().create();
117       try
118       {
119          facade.testKeygenStrategyFail(ENTITY_KEYGEN_LOCKING, id);
120          fail("Should have failed to update.");
121       }
122       catch(Exception JavaDoc e)
123       {
124       }
125       finally
126       {
127          tearDown(ENTITY_KEYGEN_LOCKING);
128       }
129    }
130
131    public void testTimestampStrategyPass() throws Exception JavaDoc
132    {
133       setup(ENTITY_TIMESTAMP_LOCKING);
134       Facade facade = getFacadeHome().create();
135       try
136       {
137          facade.testTimestampStrategyPass(ENTITY_TIMESTAMP_LOCKING, id);
138       }
139       finally
140       {
141          tearDown(ENTITY_TIMESTAMP_LOCKING);
142       }
143    }
144
145    public void testTimestampStrategyFail() throws Exception JavaDoc
146    {
147       setup(ENTITY_TIMESTAMP_LOCKING);
148       Facade facade = getFacadeHome().create();
149       try
150       {
151          facade.testTimestampStrategyFail(ENTITY_TIMESTAMP_LOCKING, id);
152          fail("Should have failed to update.");
153       }
154       catch(Exception JavaDoc e)
155       {
156       }
157       finally
158       {
159          tearDown(ENTITY_TIMESTAMP_LOCKING);
160       }
161    }
162
163    public void testVersionStrategyPass() throws Exception JavaDoc
164    {
165       setup(ENTITY_VERSION_LOCKING);
166       Facade facade = getFacadeHome().create();
167       try
168       {
169          facade.testVersionStrategyPass(ENTITY_VERSION_LOCKING, id);
170       }
171       finally
172       {
173          tearDown(ENTITY_VERSION_LOCKING);
174       }
175    }
176
177    public void testVerionStrategyFail() throws Exception JavaDoc
178    {
179       setup(ENTITY_VERSION_LOCKING);
180       Facade facade = getFacadeHome().create();
181       try
182       {
183          facade.testVersionStrategyFail(ENTITY_VERSION_LOCKING, id);
184          fail("Should have failed to update.");
185       }
186       catch(Exception JavaDoc e)
187       {
188       }
189       finally
190       {
191          tearDown(ENTITY_VERSION_LOCKING);
192       }
193    }
194
195    public void testExplicitVersionStrategyPass() throws Exception JavaDoc
196    {
197       setup(ENTITY_EXPLICIT_VERSION_LOCKING);
198       Facade facade = getFacadeHome().create();
199       try
200       {
201          facade.testVersionStrategyPass(ENTITY_EXPLICIT_VERSION_LOCKING, id);
202       }
203       finally
204       {
205          tearDown(ENTITY_EXPLICIT_VERSION_LOCKING);
206       }
207    }
208
209    public void testExplicitVerionStrategyFail() throws Exception JavaDoc
210    {
211       setup(ENTITY_EXPLICIT_VERSION_LOCKING);
212       Facade facade = getFacadeHome().create();
213       try
214       {
215          facade.testVersionStrategyFail(ENTITY_EXPLICIT_VERSION_LOCKING, id);
216          fail("Should have failed to update.");
217       }
218       catch(Exception JavaDoc e)
219       {
220       }
221       finally
222       {
223          tearDown(ENTITY_EXPLICIT_VERSION_LOCKING);
224       }
225    }
226
227    public void testExplicitVersionUpdateOnSync() throws Exception JavaDoc
228    {
229       setup(ENTITY_EXPLICIT_VERSION_LOCKING);
230       Facade facade = getFacadeHome().create();
231       try
232       {
233          facade.testExplicitVersionUpdateOnSync(ENTITY_EXPLICIT_VERSION_LOCKING, id);
234       }
235       catch(Exception JavaDoc e)
236       {
237          fail("Locked fields are not updated on sync: " + e.getMessage());
238       }
239       finally
240       {
241          tearDown(ENTITY_EXPLICIT_VERSION_LOCKING);
242       }
243    }
244
245    public void testGroupStrategyPass() throws Exception JavaDoc
246    {
247       setup(ENTITY_GROUP_LOCKING);
248       Facade facade = getFacadeHome().create();
249       try
250       {
251          facade.testGroupStrategyPass(ENTITY_GROUP_LOCKING, id);
252       }
253       finally
254       {
255          tearDown(ENTITY_GROUP_LOCKING);
256       }
257    }
258
259    public void testGroupStrategyFail() throws Exception JavaDoc
260    {
261       setup(ENTITY_GROUP_LOCKING);
262       Facade facade = getFacadeHome().create();
263       try
264       {
265          facade.testGroupStrategyFail(ENTITY_GROUP_LOCKING, id);
266          fail("Should have failed to update!");
267       }
268       catch(Exception JavaDoc e)
269       {
270       }
271       finally
272       {
273          tearDown(ENTITY_GROUP_LOCKING);
274       }
275    }
276
277    public void testReadStrategyPass() throws Exception JavaDoc
278    {
279       setup(ENTITY_READ_LOCKING);
280       Facade facade = getFacadeHome().create();
281       try
282       {
283          facade.testReadStrategyPass(ENTITY_READ_LOCKING, id);
284       }
285       finally
286       {
287          tearDown(ENTITY_READ_LOCKING);
288       }
289    }
290
291    public void testReadStrategyFail() throws Exception JavaDoc
292    {
293       setup(ENTITY_READ_LOCKING);
294       Facade facade = getFacadeHome().create();
295       try
296       {
297          facade.testReadStrategyFail(ENTITY_READ_LOCKING, id);
298          fail("Should have failed to update.");
299       }
300       catch(Exception JavaDoc e)
301       {
302       }
303       finally
304       {
305          tearDown(ENTITY_READ_LOCKING);
306       }
307    }
308
309    public void testModifiedStrategyPass() throws Exception JavaDoc
310    {
311       setup(ENTITY_MODIFIED_LOCKING);
312       Facade facade = getFacadeHome().create();
313       try
314       {
315          facade.testModifiedStrategyPass(ENTITY_MODIFIED_LOCKING, id);
316       }
317       finally
318       {
319          tearDown(ENTITY_MODIFIED_LOCKING);
320       }
321    }
322
323    public void testModifiedStrategyFail() throws Exception JavaDoc
324    {
325       setup(ENTITY_MODIFIED_LOCKING);
326       Facade facade = getFacadeHome().create();
327       try
328       {
329          facade.testModifiedStrategyFail(ENTITY_MODIFIED_LOCKING, id);
330          fail("Should have failed to update!");
331       }
332       catch(Exception JavaDoc e)
333       {
334          // expected
335
}
336       finally
337       {
338          tearDown(ENTITY_MODIFIED_LOCKING);
339       }
340    }
341
342    public void testUpdateLockOnSync() throws Exception JavaDoc
343    {
344       setup(ENTITY_VERSION_LOCKING);
345       Facade facade = getFacadeHome().create();
346       try
347       {
348          facade.testUpdateLockOnSync(ENTITY_VERSION_LOCKING, id);
349       }
350       catch(Exception JavaDoc e)
351       {
352          fail("Locked fields are not updated on sync!");
353       }
354       finally
355       {
356          tearDown(ENTITY_VERSION_LOCKING);
357       }
358    }
359
360    // Private
361

362    private void setup(String JavaDoc jndiName) throws Exception JavaDoc
363    {
364       Facade facade = getFacadeHome().create();
365       facade.createCmpEntity(jndiName, id,
366          "str1", new Integer JavaDoc(1), new Double JavaDoc(1.1),
367          "str2", new Integer JavaDoc(2), new Double JavaDoc(2.2));
368    }
369
370    private void tearDown(String JavaDoc jndiName) throws Exception JavaDoc
371    {
372       Facade facade = getFacadeHome().create();
373       facade.safeRemove(jndiName, id);
374    }
375
376    private FacadeHome getFacadeHome()
377       throws NamingException JavaDoc
378    {
379       if(facadeHome == null)
380       {
381          InitialContext JavaDoc ic = new InitialContext JavaDoc();
382          Object JavaDoc ref = ic.lookup(FacadeHome.JNDI_NAME);
383          facadeHome = (FacadeHome) PortableRemoteObject.narrow(ref, FacadeHome.class);
384       }
385       return facadeHome;
386    }
387 }
388
Popular Tags