⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txtest.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.
//
// $Id$

package test.tx;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.log4j.Logger;
import org.ozoneDB.ExternalTransaction;
import org.ozoneDB.OzoneInterface;
import org.ozoneDB.TransactionException;
import test.OzoneTestCase;

import java.io.IOException;

/**
 * @author <a href="http://www.softwarebuero.de/">SMB</a>
 * @version $Revision$Date$
 */
public class TxTest extends OzoneTestCase {

    /**
     * log4j logger
     */
    private static Logger log = Logger.getLogger(TxTest.class);
    private ExternalTransaction tx;
    private Group group;

    public static void addSuite(TestSuite suite) {
        suite.addTest(new TxTest("testAll"));
    }


    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(TxTest.class);
        return suite;
    }


    public TxTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        db().reloadClasses();

        tx = db().newTransaction();
        tx.begin();
        group = (Group) db().objectForName("group1");
        if (group != null) {
            db().deleteObject(group);
        }
        group = (Group) db().createObject(GroupImpl.class.getName(), OzoneInterface.Public, "group1");
        tx.commit();
    }

    public void testMultipleThreadsPerTransaction() throws TransactionException, IOException, InterruptedException {
        // check multiple threads per transaction
        log.info("testMultipleThreadsPerTransaction(): 1: group.name()=\"" + group.name() + "\".");
        tx.begin();
        group.setName("Gruppe2");
        Thread t1 = new AccessThread(this, tx, "group1", "Gruppe2");
        t1.start();
        Thread t2 = new AccessThread(this, null, "group1", null);
        t2.start();

        Thread.sleep(3000);
        log.info("testMultipleThreadsPerTransaction(): 2: group.name()=\"" + group.name() + "\".");
        tx.rollback();
        log.info("testMultipleThreadsPerTransaction(): 3: group.name()=\"" + group.name() + "\".");
        assertTrue("After rollback, group name should not be Gruppe2", !group.name().equals("Gruppe2"));
    }

    public void testExternalCommit() throws TransactionException, IOException {
        // check external commit
        tx.begin();
        group.setName("Gruppe");
        assertTrue("Group name should be Gruppe", group.name().equals("Gruppe"));
        tx.commit();
        assertTrue("After commit, group name should be Gruppe", group.name().equals("Gruppe"));
    }

    public void testExternalAbortAfterCrash() throws TransactionException, IOException {
        // check external abort after crash
        tx.begin();
        try {
            group.setName("Gruppe");
            group.crash();
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }
        assertTrue("After crash and rollback, group name should not be Gruppe", !group.name().equals("Gruppe"));
    }

    public void testExternalAbort() throws TransactionException, IOException {
        // check external abort
        tx.begin();
        group.setName("Gruppe");
        assertTrue("Group name should be Gruppe", group.name().equals("Gruppe"));
        tx.rollback();
        assertTrue("After rollback, group name should not be Gruppe", !group.name().equals("Gruppe"));
    }


    protected static void print(Group g) throws Exception {
        log.debug(Thread.currentThread().getName() + " - Group:");
        User[] users = g.getAll();
        for (int i = 0; i < users.length; i++) {
            log.debug("    " + Thread.currentThread().getName() + ": " + users[i]);
        }
    }

}


class AccessThread extends Thread {

    /**
     * log4j logger
     */
    private static Logger log = Logger.getLogger(AccessThread.class);
    TxTest test;
    ExternalTransaction tx;
    String dbName;
    String name;

    public AccessThread(TxTest _test, ExternalTransaction _tx, String _dbName, String _name) {
        test = _test;
        tx = _tx;
        dbName = _dbName;
        name = _name;
    }


    public void run() {
        try {
            if (tx != null) {
                log.debug("thread(" + getName() + "): joining transaction...");
                tx.join();
            } else {
                log.debug("thread(" + getName() + "): creating new transaction...");
                //                tx = test.db().newTransaction();
                //                tx.begin();
            }

            Group group = (Group) test.db().objectForName(dbName);
            test.assertTrue("thread(" + getName() + "): group != null", group != null);

            log.debug("thread(" + getName() + "): " + group.toString());
            if (name != null) {
                test.assertTrue("group.name().equals (name)", group.name().equals(name));
            }
        } catch (Throwable e) {
            log.debug("Assertion failed in thread!", e);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -