/
codebase/src/net/sourceforge/pain/admin/console/command/
codebase/src/net/sourceforge/pain/data/role/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/util/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
mudlibs/tinylib/
mudlibs/tinylib/area/
mudlibs/tinylib/etc/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/prototype/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/deploy/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/util/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/impl/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/command/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/shutdown/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/social/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/util/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.db;

import junit.framework.*;
import net.sourceforge.pain.db.data.*;

import java.io.*;

/**
 * Checking error handling of database
 */
public final class ErrorHandlingTest extends TestCase {


    private PainDB db;

    public ErrorHandlingTest() {
        super("ErrorHandlingTest");
    }

    protected void setUp() throws Exception {
        db = new PainDB(getName() + ".db");
        db.ALLOW_PLAIN_WRITE = true; // allow work without transactions
        db.MANUAL_FLUSH_MODE = true; // commit will not flush (objects stays dirty)

    }

    protected void tearDown() throws Exception {
        if (db != null) {
            File file = new File(db.getDbFileName());
            if (!db.isClosed()) {
                db.forceClose();
            }
            db = null;
            file.delete();
        }
    }

    /**
     * explicit commits error handling
     */
    public void testExplicitCommit1() throws Exception {
        Exception exc = null;
        try {
            db.commitTransaction();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);
        exc = null;
        db.beginTransaction();
        db.commitTransaction();

        try {
            db.commitTransaction();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        db.beginTransaction();
        db.beginTransaction();
        db.beginTransaction();
        db.commitTransaction();
        db.commitTransaction();
        db.commitTransaction();

        try {
            db.commitTransaction();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

    }

    /**
     * explicit rollbacks error handling
     */
    public void testExplicitRollback1() {
        Exception exc = null;
        try {
            db.rollbackTransaction();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);
        exc = null;
        db.beginTransaction();
        db.rollbackTransaction();

        try {
            db.rollbackTransaction();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        db.beginTransaction();
        db.beginTransaction();
        db.beginTransaction();
        db.rollbackTransaction();
        db.rollbackTransaction();
        db.rollbackTransaction();

        try {
            db.rollbackTransaction();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);
    }

    /**
     * deleted object access error handling
     */
    public void testDeletedAccess1() throws Exception {
        Exception exc = null;
        AllFieldTypesObject obj;
        db.beginTransaction();
        obj = new AllFieldTypesObject(db);
        db.rollbackTransaction();
        try {
            obj.getOid();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        db.beginTransaction();
        obj = new AllFieldTypesObject(db);
        db.beginTransaction();
        obj.delete();
        assertTrue(obj.isDeleted());
        _testAllTypesExceptionOnGet(obj);
        db.commitTransaction();
        assertTrue(obj.isDeleted());
        _testAllTypesExceptionOnGet(obj);
        db.commitTransaction();
    }

    private void _testAllTypesExceptionOnGet(AllFieldTypesObject obj) {
        Exception exc = null;
        try {
            obj.getARRAY_LIST();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getARRAY_OF_BYTE();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getARRAY_OF_CHAR();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getARRAY_OF_INT();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getARRAY_OF_STRING();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getBOOLEAN();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getBYTE();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getCHAR();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getDOUBLE();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getFLOAT();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);


        exc = null;
        try {
            obj.getINT();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);


        exc = null;
        try {
            obj.getINT_KEY_MAP();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);


        exc = null;
        try {
            obj.getLINKED_LIST();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getLONG();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);


        exc = null;
        try {
            obj.getREFERENCE();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getREFERENCE_SET();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);


        exc = null;
        try {
            obj.getSHORT();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getSTRING();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getSTRING_KEY_MAP();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);

        exc = null;
        try {
            obj.getSTRING_SET();
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);
    }

    /**
     * detached object access error handling
     */
    public void testDetachedAccess1() throws Exception {
        AllFieldTypesObject obj = new AllFieldTypesObject(db);
        obj.delete();
        assertTrue(obj.isDetached());
        _testAllTypesExceptionOnGet(obj);

        db.beginTransaction();
        obj = new AllFieldTypesObject(db);
        db.rollbackTransaction();
        assertTrue(obj.isDetached());
        _testAllTypesExceptionOnGet(obj);

        db.beginTransaction();
        obj = new AllFieldTypesObject(db);
        assertTrue(obj.isNew());
        db.beginTransaction();
        assertTrue(obj.isNew());
        obj.delete();
        assertTrue(obj.isDeleted());
        db.commitTransaction();
        assertTrue(obj.isDeleted());
        db.commitTransaction();
        assertTrue(obj.isDetached());
        _testAllTypesExceptionOnGet(obj);
    }
}