KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > tx > TxTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package test.tx;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13 import org.apache.log4j.Logger;
14 import org.ozoneDB.ExternalTransaction;
15 import org.ozoneDB.OzoneInterface;
16 import org.ozoneDB.TransactionException;
17 import test.OzoneTestCase;
18
19 import java.io.IOException JavaDoc;
20
21 /**
22  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
23  * @version $Revision$Date$
24  */

25 public class TxTest extends OzoneTestCase {
26
27     /**
28      * log4j logger
29      */

30     private static Logger log = Logger.getLogger(TxTest.class);
31     private ExternalTransaction tx;
32     private Group group;
33
34     public static void addSuite(TestSuite suite) {
35         suite.addTest(new TxTest("testAll"));
36     }
37
38
39     public static Test suite() {
40         TestSuite suite = new TestSuite();
41         suite.addTestSuite(TxTest.class);
42         return suite;
43     }
44
45
46     public TxTest(String JavaDoc name) {
47         super(name);
48     }
49
50     protected void setUp() throws Exception JavaDoc {
51         db().reloadClasses();
52
53         tx = db().newTransaction();
54         tx.begin();
55         group = (Group) db().objectForName("group1");
56         if (group != null) {
57             db().deleteObject(group);
58         }
59         group = (Group) db().createObject(GroupImpl.class.getName(), OzoneInterface.Public, "group1");
60         tx.commit();
61     }
62
63     public void testMultipleThreadsPerTransaction() throws TransactionException, IOException JavaDoc, InterruptedException JavaDoc {
64         // check multiple threads per transaction
65
log.info("testMultipleThreadsPerTransaction(): 1: group.name()=\"" + group.name() + "\".");
66         tx.begin();
67         group.setName("Gruppe2");
68         Thread JavaDoc t1 = new AccessThread(this, tx, "group1", "Gruppe2");
69         t1.start();
70         Thread JavaDoc t2 = new AccessThread(this, null, "group1", null);
71         t2.start();
72
73         Thread.sleep(3000);
74         log.info("testMultipleThreadsPerTransaction(): 2: group.name()=\"" + group.name() + "\".");
75         tx.rollback();
76         log.info("testMultipleThreadsPerTransaction(): 3: group.name()=\"" + group.name() + "\".");
77         assertTrue("After rollback, group name should not be Gruppe2", !group.name().equals("Gruppe2"));
78     }
79
80     public void testExternalCommit() throws TransactionException, IOException JavaDoc {
81         // check external commit
82
tx.begin();
83         group.setName("Gruppe");
84         assertTrue("Group name should be Gruppe", group.name().equals("Gruppe"));
85         tx.commit();
86         assertTrue("After commit, group name should be Gruppe", group.name().equals("Gruppe"));
87     }
88
89     public void testExternalAbortAfterCrash() throws TransactionException, IOException JavaDoc {
90         // check external abort after crash
91
tx.begin();
92         try {
93             group.setName("Gruppe");
94             group.crash();
95             tx.commit();
96         } catch (Exception JavaDoc e) {
97             tx.rollback();
98         }
99         assertTrue("After crash and rollback, group name should not be Gruppe", !group.name().equals("Gruppe"));
100     }
101
102     public void testExternalAbort() throws TransactionException, IOException JavaDoc {
103         // check external abort
104
tx.begin();
105         group.setName("Gruppe");
106         assertTrue("Group name should be Gruppe", group.name().equals("Gruppe"));
107         tx.rollback();
108         assertTrue("After rollback, group name should not be Gruppe", !group.name().equals("Gruppe"));
109     }
110
111
112     protected static void print(Group g) throws Exception JavaDoc {
113         log.debug(Thread.currentThread().getName() + " - Group:");
114         User[] users = g.getAll();
115         for (int i = 0; i < users.length; i++) {
116             log.debug(" " + Thread.currentThread().getName() + ": " + users[i]);
117         }
118     }
119
120 }
121
122
123 class AccessThread extends Thread JavaDoc {
124
125     /**
126      * log4j logger
127      */

128     private static Logger log = Logger.getLogger(AccessThread.class);
129     TxTest test;
130     ExternalTransaction tx;
131     String JavaDoc dbName;
132     String JavaDoc name;
133
134     public AccessThread(TxTest _test, ExternalTransaction _tx, String JavaDoc _dbName, String JavaDoc _name) {
135         test = _test;
136         tx = _tx;
137         dbName = _dbName;
138         name = _name;
139     }
140
141
142     public void run() {
143         try {
144             if (tx != null) {
145                 log.debug("thread(" + getName() + "): joining transaction...");
146                 tx.join();
147             } else {
148                 log.debug("thread(" + getName() + "): creating new transaction...");
149                 // tx = test.db().newTransaction();
150
// tx.begin();
151
}
152
153             Group group = (Group) test.db().objectForName(dbName);
154             test.assertTrue("thread(" + getName() + "): group != null", group != null);
155
156             log.debug("thread(" + getName() + "): " + group.toString());
157             if (name != null) {
158                 test.assertTrue("group.name().equals (name)", group.name().equals(name));
159             }
160         } catch (Throwable JavaDoc e) {
161             log.debug("Assertion failed in thread!", e);
162         }
163     }
164 }
165
Popular Tags