001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.
006: //
007: // $Id: TxTest.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.test.tx;
010:
011: import javax.transaction.*;
012: import org.ozoneDB.*;
013: import org.ozoneDB.test.*;
014: import junit.framework.*;
015:
016: import org.apache.log4j.Category;
017:
018: /**
019: * @author <a href="http://www.softwarebuero.de/">SMB</a>
020: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:31 $
021: */
022: public class TxTest extends OzoneTestCase {
023:
024: /**
025: * log4j logger
026: */
027: private static Category fLog = Category.getInstance(TxTest.class);
028:
029: public static void addSuite(TestSuite suite) {
030: suite.addTest(new TxTest("testAll"));
031: }
032:
033: public static Test suite() {
034: TestSuite suite = new TestSuite();
035: suite.addTestSuite(TxTest.class);
036: return suite;
037: }
038:
039: public TxTest(String name) {
040: super (name);
041: }
042:
043: public void testAll() throws Exception {
044: db().reloadClasses();
045:
046: ExternalTransaction tx = db().newTransaction();
047: tx.begin();
048: Group group = (Group) db().objectForName("group1");
049: if (group != null) {
050: db().deleteObject(group);
051: }
052: group = (Group) db().createObject(GroupImpl.class.getName(),
053: OzoneInterface.Public, "group1");
054: tx.commit();
055:
056: // check external abort
057: tx.begin();
058: group.setName("Gruppe");
059: assert (group.name().equals("Gruppe"));
060: tx.rollback();
061: assert (!group.name().equals("Gruppe"));
062:
063: // check external abort after crash
064: tx.begin();
065: try {
066: group.setName("Gruppe");
067: group.crash();
068: tx.commit();
069: } catch (Exception e) {
070: tx.rollback();
071: }
072: assert (!group.name().equals("Gruppe"));
073:
074: // check external commit
075: tx.begin();
076: group.setName("Gruppe");
077: assert (group.name().equals("Gruppe"));
078: tx.commit();
079: assert (group.name().equals("Gruppe"));
080:
081: // check multiple threads per transaction
082: tx.begin();
083: group.setName("Gruppe2");
084: Thread t1 = new AccessThread(this , tx, "group1", "Gruppe2");
085: t1.start();
086: Thread t2 = new AccessThread(this , null, "group1", null);
087: t2.start();
088:
089: Thread.sleep(3000);
090: tx.rollback();
091: assert (!group.name().equals("Gruppe2"));
092: }
093:
094: protected static void print(Group g) throws Exception {
095: fLog.debug(Thread.currentThread().getName() + " - Group:");
096: User[] users = g.getAll();
097: for (int i = 0; i < users.length; i++) {
098: fLog.debug(" " + Thread.currentThread().getName() + ": "
099: + users[i]);
100: }
101: }
102:
103: }
104:
105: class AccessThread extends Thread {
106:
107: /**
108: * log4j logger
109: */
110: private static Category fLog = Category
111: .getInstance(AccessThread.class);
112:
113: TxTest test;
114:
115: ExternalTransaction tx;
116:
117: String dbName;
118:
119: String name;
120:
121: public AccessThread(TxTest _test, ExternalTransaction _tx,
122: String _dbName, String _name) {
123: test = _test;
124: tx = _tx;
125: dbName = _dbName;
126: name = _name;
127: }
128:
129: public void run() {
130: try {
131: if (tx != null) {
132: fLog.debug( "thread(" + getName() + "): joining transaction..." );
133: tx.join();
134: } else {
135: fLog.debug( "thread(" + getName() + "): creating new transaction..." );
136: // tx = test.db().newTransaction();
137: // tx.begin();
138: }
139:
140: Group group = (Group)test.db().objectForName( dbName );
141: test.assert("thread(" + getName() + "): group != null", group != null);
142:
143: fLog.debug( "thread(" + getName() + "): " + group.toString() );
144: if (name != null) {
145: test.assert("group.name().equals (name)", group.name().equals(name));
146: }
147: } catch (Throwable e) {
148: fLog.debug( "Assertion failed in thread!", e );
149: }
150: } }
|