db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Olav Sandstaa <Olav.Sands...@Sun.COM>
Subject How to start and stop Derby from within a test
Date Wed, 29 Mar 2006 14:33:24 GMT

Is there a standard/simple way of starting and stopping the database
and network server from a test that is written using the test
framework? Preferably a way that is working for both embedded and
network client tests? I have looked around in a few test, but have not
found any particular easy way to do this.

The reason I want to be able to stop and start Derby from a test is
that I am writing some tests for Connection.isValid() (see JIRA 1090)
and need to test the behavior of this call against a stopped database. 

The test I work on is located in jdbc4/TestConnectionMethods.java. The
code that I have running right now do the job for me right now, but if
would be good to get some feedback on on whether there are simpler and
more standard ways of achieving the same. The current code looks like
this:

Embedded mode:
==============

Stop Derby:

        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } catch(SQLException e) {
            // Ignore any exceptions from shutdown
        }

"Problem": stops all Derby databases (no problem in my test)

Start Derby:

        /*
         * Start Derby by getting a new connection and check that
         * the new connection is valid.
         */
        try {
            conn = ij.startJBMS();
        } catch (Exception e) {
            System.out.println("FAIL: failed to re-start database: " +
                               "Unexpected exception: " + e);
        }

Comment: This is the way it should be.


Network client mode:
====================

Stop Derby:

        /*
         * Test on stopped DB: stop Derby
         */
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
	 } catch (Exception e) {
             // Ignore any exceptions from shutdown
	 }

        // Stop the network server
        try {
            NetworkServerControl networkServer = new NetworkServerControl();
            networkServer.shutdown();
        } catch(Exception e) {
            System.out.println("INFO: network server shutdown returned: " + e);
        }

Start Derby:

        /*
         * Start the network server and get a new connection
         */
        try {
            // Start the network server - hard coded values (FixMe)
            NetworkServerControl networkServer = new NetworkServerControl(InetAddress.getByName("localhost"),
1527);
            networkServer.start(null);

            // Wait for the network server to start
            boolean started = false;
            int retries = 10;         // Max retries = max seconds to wait
            while (!started && retries > 0) {
		  try {
                    // Sleep 1 second and then ping the network server
		      Thread.sleep(1000);
                    networkServer.ping();

                    // If ping does not throw an exception the server has started
                    started = true;
                } catch(Exception e) {
                    retries--;
		  }
	     }

            // Check if we got a reply on ping
            if (!started) {
                System.out.println("FAIL: failed to start network server");
            }

            // Get a new connection to the database
            conn = ij.startJBMS();
        } catch (Exception e) {
            System.out.println("FAIL: failed to re-start database: " +
                               "Unexpected exception: " + e);
        }

There are currently some hard coded constants to localhost and 1527
that I could probably get rid of.

It would be very good if the test framework had utility functions for
doing these tasks that worked both for embedded and client server
tests (similar to ij.startJBMS()). There might be some, but I have not
found any. Any tips and comments on how to do this are appreciated.

I also had to disable the use of the security manager for the
test. With security manager, the last call to ij.startJBMS() gets the
following exception:

   Unexpected exception: java.sql.SQLException: DERBY SQL error: SQLCODE: -1, SQLSTATE: XJ040,
SQLERRMC: Failed to start database 'wombat', see the next exception for details.::SQLSTATE:
XJ001Java exception: 'access denied (java.io.FilePermission /export/home/tmp/derbyjdbc4/DerbyNetClient/TestConnectionMethods/wombat/log/logmirror.ctrl
read): java.security.AccessControlException'.

I do not know why, but disabling the security manager made this go
away. I probably should find a better way to get around this.

..olav

Mime
View raw message