001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_Isolation.java 6583 2005-04-20 08:04:53Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.util.Enumeration;
029:
030: import org.objectweb.jonas.jtests.beans.ebasic.Simple;
031: import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
032: import org.objectweb.jonas.jtests.util.JTestCase;
033:
034: /**
035: * test cases common to both suites CMP and BMP.
036: */
037: public abstract class A_Isolation extends JTestCase {
038:
039: public A_Isolation(String name) {
040: super (name);
041: }
042:
043: protected void setUp() {
044: super .setUp();
045: useBeans("ebasic", true);
046: }
047:
048: /**
049: * return SimpleHome, that can be either BMP or CMP bean.
050: */
051: abstract public SimpleHome getHome();
052:
053: /**
054: * Access an instance outside tx and inside tx and verify that state
055: * has changed and is ok.
056: * pre condition: an element with "pk4" as primary key must exist in the database
057: */
058: public void testIsolation1() throws Exception {
059: int val1 = 11;
060: int val2 = 12;
061: Simple s1 = getHome().findByPrimaryKey("pk4");
062: s1.setInfo(val1);
063: utx.begin();
064: try {
065: assertEquals(val1, s1.getInfo());
066: s1.setInfo(val2);
067: } finally {
068: utx.commit();
069: }
070: assertEquals(val2, s1.getInfo());
071: }
072:
073: /**
074: * test finder method inside a transaction.
075: */
076: public void testFinderInTx() throws Exception {
077: int found2 = 0;
078: int found3 = 0;
079: int newval = 14;
080: utx.begin();
081: try {
082: Simple s1 = getHome().create("n1", 1001, newval);
083: Simple s2 = getHome().create("n2", 1002, 2);
084: s2.setNumTest(newval);
085: Enumeration list = getHome().findInfoForNum(newval);
086: while (list.hasMoreElements()) {
087: list.nextElement();
088: found2++;
089: }
090: assertEquals("first try", 2, found2);
091: } finally {
092: // rolback avoids cleaning objects
093: utx.rollback();
094: }
095: }
096:
097: /**
098: * test finder method outside a transaction.
099: */
100: public void testFinderOutTx() throws Exception {
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 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 {
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 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: }
|