KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > A_Isolation


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_Isolation.java,v 1.7 2005/04/20 08:04:53 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Enumeration JavaDoc;
29
30 import org.objectweb.jonas.jtests.beans.ebasic.Simple;
31 import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
32 import org.objectweb.jonas.jtests.util.JTestCase;
33
34 /**
35  * test cases common to both suites CMP and BMP.
36  */

37 public abstract class A_Isolation extends JTestCase {
38
39     public A_Isolation(String JavaDoc name) {
40         super(name);
41     }
42
43     protected void setUp() {
44         super.setUp();
45         useBeans("ebasic", true);
46     }
47
48     /**
49      * return SimpleHome, that can be either BMP or CMP bean.
50      */

51     abstract public SimpleHome getHome();
52
53     /**
54      * Access an instance outside tx and inside tx and verify that state
55      * has changed and is ok.
56      * pre condition: an element with "pk4" as primary key must exist in the database
57      */

58     public void testIsolation1() throws Exception JavaDoc {
59         int val1 = 11;
60         int val2 = 12;
61         Simple s1 = getHome().findByPrimaryKey("pk4");
62         s1.setInfo(val1);
63         utx.begin();
64         try {
65             assertEquals(val1, s1.getInfo());
66             s1.setInfo(val2);
67         } finally {
68             utx.commit();
69         }
70         assertEquals(val2, s1.getInfo());
71     }
72
73     /**
74      * test finder method inside a transaction.
75      */

76     public void testFinderInTx() throws Exception JavaDoc {
77         int found2 = 0;
78         int found3 = 0;
79         int newval = 14;
80         utx.begin();
81         try {
82             Simple s1 = getHome().create("n1", 1001, newval);
83             Simple s2 = getHome().create("n2", 1002, 2);
84             s2.setNumTest(newval);
85             Enumeration JavaDoc list = getHome().findInfoForNum(newval);
86             while (list.hasMoreElements()) {
87                 list.nextElement();
88                 found2++;
89             }
90             assertEquals("first try", 2, found2);
91         } finally {
92             // rolback avoids cleaning objects
93
utx.rollback();
94         }
95     }
96
97     /**
98      * test finder method outside a transaction.
99      */

100     public void testFinderOutTx() throws Exception JavaDoc {
101         int found2 = 0;
102         int found3 = 0;
103         int newval = 14;
104         Simple s1 = null;
105         Simple s2 = null;
106         try {
107             s1 = getHome().create("n3", 1001, newval);
108             s2 = getHome().create("n4", 1002, 2);
109             s2.setNumTest(newval);
110             Enumeration JavaDoc list = getHome().findInfoForNum(newval);
111             while (list.hasMoreElements()) {
112                 list.nextElement();
113                 found2++;
114             }
115             assertEquals("first try", 2, found2);
116         } finally {
117             // cleaning
118
if (s1 != null) {
119                 s1.remove();
120             }
121             if (s2 != null) {
122                 s2.remove();
123             }
124         }
125     }
126
127     /**
128      * test finder method inside a transaction.
129      */

130     public void testFinderInTx2() throws Exception JavaDoc {
131         int found2 = 0;
132         int found3 = 0;
133         int newval = 14;
134         utx.begin();
135         try {
136             Simple s1 = getHome().create("n5", 1001, newval);
137             Simple s2 = getHome().create("n6", 1002, 2);
138             Simple s3 = getHome().create("n7", 1003, 3);
139             s2.setNumTest(newval);
140             Enumeration JavaDoc list = getHome().findInfoForNum(newval);
141             while (list.hasMoreElements()) {
142                 list.nextElement();
143                 found2++;
144             }
145             s3.setNumTest(newval);
146             list = getHome().findInfoForNum(newval);
147             while (list.hasMoreElements()) {
148                 list.nextElement();
149                 found3++;
150             }
151             assertEquals("second try", 3, found3);
152             assertEquals("first try", 2, found2);
153         } finally {
154             // rollback avoids cleaning objects
155
utx.rollback();
156         }
157     }
158 }
159
Popular Tags