org.apache.derbyTesting.functionTests.tests.lang
Class TriggerTest

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.apache.derbyTesting.junit.BaseTestCase
              extended by org.apache.derbyTesting.junit.BaseJDBCTestCase
                  extended by org.apache.derbyTesting.functionTests.tests.lang.TriggerTest
All Implemented Interfaces:
junit.framework.Test

public class TriggerTest
extends BaseJDBCTestCase

Test triggers.


Field Summary
private static java.lang.ThreadLocal TRIGGER_INFO
          Thread local that a trigger can access to allow recording information about the firing.
 
Fields inherited from class org.apache.derbyTesting.junit.BaseTestCase
DEFAULT_DB_DIR, DERBY_LOG, ERRORSTACKTRACEFILE
 
Constructor Summary
TriggerTest(java.lang.String name)
           
 
Method Summary
private  void actionTypesCompareMainToAction(int actionCount, java.lang.String type)
          Compare the contents of the main table to the action table.
private  void actionTypesCompareMainToActionForUpdate(java.lang.String type, int id)
          Compare the values for an update trigger.
private  void actionTypesDeleteTest(java.lang.String type)
          Test deletes with the specified types in the action statement.
private  void actionTypesInsertTest(java.lang.String type)
          Execute three insert statements.
private  void actionTypesSetup(java.lang.String type)
          Setup the tables and triggers for a single type for actionTypeTest
private  void actionTypesUpdateTest(java.lang.String type)
          Test updates of the specified types in the action statement.
private  void actionTypeTest(java.lang.String type)
          Test that the action statement of a trigger can work with a specific datatype.
private  int assertFiringOrder(java.lang.String iud, int modifiedRowCount)
          Look at the ordered information in the thread local and ensure it reflects correct sequenceing of triggers created in testFiringOrder.
private  int assertFiringOrder(java.lang.String iud, int modifiedRowCount, boolean noAfter)
           
private  void assertInputStreamContents(java.io.InputStream is, int size, byte expectedValue)
           
private  void assertReaderContents(java.io.Reader r, int size, char expectedCharValue)
           
private  int[] createRandomTriggers()
           
static java.lang.Object getRandomValue(java.util.Random r, int jdbcType, int precision)
          Generate a random object (never null) for a given JDBC type.
protected  void initializeConnection(java.sql.Connection conn)
          Allow a sub-class to initialize a connection to provide consistent connection state for its tests.
static void logTriggerInfo(java.lang.String info)
          Record the trigger information in the thread local.
private  byte[] makeArray(int size, byte b)
           
private  char[] makeArray(int size, char c)
           
private  java.io.CharArrayReader makeCharArrayReader(char c, int size)
          Make a CharArrayReader
private static byte[] randomBinary(java.util.Random r, int len)
           
private static java.lang.String randomString(java.util.Random r, int len)
           
static void setRandomValue(java.util.Random r, java.sql.PreparedStatement ps, int column, int jdbcType, int precision)
           
protected  void setUp()
           
static junit.framework.Test suite()
          Run only in embedded as TRIGGERs are server side logic.
protected  void tearDown()
          Tear down this fixture, sub-classes should call super.tearDown().
 void testBlobInTriggerTable()
          Test for DERBY-3238 trigger fails with IOException if triggering table has large lob.
private  void testBlobInTriggerTable(int blobSize)
          Create a table with after update trigger on non-lob column.
 void testClobInTriggerTable()
          Test for DERBY-3238 trigger fails with IOException if triggering table has large lob.
private  void testClobInTriggerTable(int clobSize)
          Create a table with after update trigger on non-lob column.
 void testDerby4095NewTriggerRows()
          Test that a nested loop join that accesses the TriggerNewTransitionRowsVTI can reopen the ResultSet properly when it re-executes.
 void testDerby4095OldTriggerRows()
          Test that a nested loop join that accesses the TriggerOldTransitionRowsVTI can reopen the ResultSet properly when it re-executes.
 void testFiringConstraintOrder()
          Test that a order of firing is before triggers, constraint checking and after triggers.
 void testFiringOrder()
          Test the firing order of triggers.
 void testNPEinTriggerFire()
          Test for DERBY-3718 NPE when a trigger is fired
 void testTypesInActionStatement()
          Test that the action statement of a trigger can work with all datatypes.
 void testUpdateTriggerOnClobColumn()
           
 
Methods inherited from class org.apache.derbyTesting.junit.BaseJDBCTestCase
assertCallError, assertCheckTable, assertCompileError, assertEquals, assertEquals, assertEquals, assertEquivalentDataType, assertGetIntError, assertNextError, assertPreparedStatementError, assertSQLExceptionEquals, assertSQLState, assertSQLState, assertStatementError, assertStatementError, assertStatementError, assertStatementError, assertTableRowCount, assertUpdateCount, assertUpdateCount, checkAllConsistency, checkEstimatedRowCount, commit, createStatement, createStatement, createStatement, dropTable, dropTable, getConnection, getDatabaseProperty, getLastSQLException, openConnection, openDefaultConnection, openDefaultConnection, openUserConnection, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, rollback, runScript, runScript, runSQLCommands, setAutoCommit, usingDB2Client, usingDerbyNetClient, usingEmbedded
 
Methods inherited from class org.apache.derbyTesting.junit.BaseTestCase
alarm, assertEquals, assertEquals, assertEquals, assertExecJavaCmdAsExpected, assertSecurityManager, assertThrowableEquals, execJavaCmd, fail, getFailureFolder, getJavaExecutableName, getSystemProperty, getTestConfiguration, getTestResource, openTestResource, println, printStackTrace, readProcessOutput, removeDirectory, removeDirectory, removeSystemProperty, runBare, setSystemProperty, traceit
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TRIGGER_INFO

private static java.lang.ThreadLocal TRIGGER_INFO
Thread local that a trigger can access to allow recording information about the firing.

Constructor Detail

TriggerTest

public TriggerTest(java.lang.String name)
Method Detail

suite

public static junit.framework.Test suite()
Run only in embedded as TRIGGERs are server side logic. Also the use of a ThreadLocal to check state requires embedded.


initializeConnection

protected void initializeConnection(java.sql.Connection conn)
                             throws java.sql.SQLException
Description copied from class: BaseJDBCTestCase
Allow a sub-class to initialize a connection to provide consistent connection state for its tests. Called once for each time these method calls open a connection: Default action is to not modify the connection's state from the initialization provided by the data source.

Overrides:
initializeConnection in class BaseJDBCTestCase
Parameters:
conn - Connection to be intialized
Throws:
java.sql.SQLException - Error setting the initial state.

setUp

protected void setUp()
              throws java.lang.Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
java.lang.Exception

tearDown

protected void tearDown()
                 throws java.lang.Exception
Description copied from class: BaseJDBCTestCase
Tear down this fixture, sub-classes should call super.tearDown(). This cleanups & closes the connection if it is open and any statement objects returned through the utility methods.

Overrides:
tearDown in class BaseJDBCTestCase
Throws:
java.lang.Exception

testFiringOrder

public void testFiringOrder()
                     throws java.sql.SQLException
Test the firing order of triggers. Should be: Before operations after operations For multiple triggers within the same group (before or after) firing order is determined by create order.

Throws:
java.sql.SQLException

createRandomTriggers

private int[] createRandomTriggers()
                            throws java.sql.SQLException
Throws:
java.sql.SQLException

testFiringConstraintOrder

public void testFiringConstraintOrder()
                               throws java.sql.SQLException
Test that a order of firing is before triggers, constraint checking and after triggers.

Throws:
java.sql.SQLException

assertFiringOrder

private int assertFiringOrder(java.lang.String iud,
                              int modifiedRowCount)
Look at the ordered information in the thread local and ensure it reflects correct sequenceing of triggers created in testFiringOrder.

Parameters:
iud -
Returns:
the number of triggers checked

assertFiringOrder

private int assertFiringOrder(java.lang.String iud,
                              int modifiedRowCount,
                              boolean noAfter)

logTriggerInfo

public static void logTriggerInfo(java.lang.String info)
Record the trigger information in the thread local. Called as a SQL procedure.

Parameters:
info - trigger information

testNPEinTriggerFire

public void testNPEinTriggerFire()
                          throws java.sql.SQLException
Test for DERBY-3718 NPE when a trigger is fired

Throws:
java.sql.SQLException
java.io.IOException

testClobInTriggerTable

public void testClobInTriggerTable()
                            throws java.sql.SQLException,
                                   java.io.IOException
Test for DERBY-3238 trigger fails with IOException if triggering table has large lob.

Throws:
java.sql.SQLException
java.io.IOException

testClobInTriggerTable

private void testClobInTriggerTable(int clobSize)
                             throws java.sql.SQLException,
                                    java.io.IOException
Create a table with after update trigger on non-lob column. Insert clob of size clobSize into table and perform update on str1 column to fire trigger. Helper method called from testClobInTriggerTable

Parameters:
clobSize - size of clob to test
Throws:
java.sql.SQLException
java.io.IOException

makeArray

private char[] makeArray(int size,
                         char c)

makeArray

private byte[] makeArray(int size,
                         byte b)

testBlobInTriggerTable

public void testBlobInTriggerTable()
                            throws java.sql.SQLException,
                                   java.io.IOException
Test for DERBY-3238 trigger fails with IOException if triggering table has large lob.

Throws:
java.sql.SQLException
java.io.IOException

testBlobInTriggerTable

private void testBlobInTriggerTable(int blobSize)
                             throws java.sql.SQLException,
                                    java.io.IOException
Create a table with after update trigger on non-lob column. Insert two blobs of size blobSize into table and perform update on str1 column to fire trigger. Helper method called from testBlobInTriggerTable

Parameters:
blobSize - size of blob to test.
Throws:
java.sql.SQLException
java.io.IOException

assertInputStreamContents

private void assertInputStreamContents(java.io.InputStream is,
                                       int size,
                                       byte expectedValue)
                                throws java.io.IOException
Throws:
java.io.IOException

testUpdateTriggerOnClobColumn

public void testUpdateTriggerOnClobColumn()
                                   throws java.sql.SQLException,
                                          java.io.IOException
Throws:
java.sql.SQLException
java.io.IOException

assertReaderContents

private void assertReaderContents(java.io.Reader r,
                                  int size,
                                  char expectedCharValue)
                           throws java.io.IOException
Throws:
java.io.IOException

makeCharArrayReader

private java.io.CharArrayReader makeCharArrayReader(char c,
                                                    int size)
Make a CharArrayReader

Parameters:
c - character to repeat
size - size of array
Returns:
CharArrayReader of specified character repeating the specified character

testTypesInActionStatement

public void testTypesInActionStatement()
                                throws java.sql.SQLException,
                                       java.io.IOException
Test that the action statement of a trigger can work with all datatypes.

Throws:
java.sql.SQLException
java.io.IOException

actionTypeTest

private void actionTypeTest(java.lang.String type)
                     throws java.sql.SQLException,
                            java.io.IOException
Test that the action statement of a trigger can work with a specific datatype.

Parameters:
type - SQL type to be tested
Throws:
java.sql.SQLException
java.io.IOException

actionTypesSetup

private void actionTypesSetup(java.lang.String type)
                       throws java.sql.SQLException
Setup the tables and triggers for a single type for actionTypeTest

Throws:
java.sql.SQLException

actionTypesInsertTest

private void actionTypesInsertTest(java.lang.String type)
                            throws java.sql.SQLException,
                                   java.io.IOException
Execute three insert statements. NULL as the value for the type one row insert with random value for the type three row insert with random values for the type Check that the data in the action table matches the main table (see the after insert trigger definitions).

Parameters:
type -
Throws:
java.sql.SQLException
java.io.IOException

actionTypesUpdateTest

private void actionTypesUpdateTest(java.lang.String type)
                            throws java.sql.SQLException,
                                   java.io.IOException
Test updates of the specified types in the action statement.

Parameters:
type -
Throws:
java.sql.SQLException
java.io.IOException

actionTypesCompareMainToActionForUpdate

private void actionTypesCompareMainToActionForUpdate(java.lang.String type,
                                                     int id)
                                              throws java.sql.SQLException,
                                                     java.io.IOException
Compare the values for an update trigger.

Parameters:
type -
id -
Throws:
java.sql.SQLException
java.io.IOException

actionTypesDeleteTest

private void actionTypesDeleteTest(java.lang.String type)
                            throws java.sql.SQLException,
                                   java.io.IOException
Test deletes with the specified types in the action statement.

Parameters:
type -
Throws:
java.sql.SQLException
java.io.IOException

actionTypesCompareMainToAction

private void actionTypesCompareMainToAction(int actionCount,
                                            java.lang.String type)
                                     throws java.sql.SQLException,
                                            java.io.IOException
Compare the contents of the main table to the action table. See the trigger defintions for details.

Parameters:
actionCount -
type -
Throws:
java.sql.SQLException
java.io.IOException

setRandomValue

public static void setRandomValue(java.util.Random r,
                                  java.sql.PreparedStatement ps,
                                  int column,
                                  int jdbcType,
                                  int precision)
                           throws java.sql.SQLException,
                                  java.io.IOException
Throws:
java.sql.SQLException
java.io.IOException

getRandomValue

public static java.lang.Object getRandomValue(java.util.Random r,
                                              int jdbcType,
                                              int precision)
                                       throws java.io.IOException
Generate a random object (never null) for a given JDBC type. Object is suitable for PreparedStatement.setObject() either with or without passing in jdbcType to setObject.
For character types a String object or a StringReaderWithLength is returned.
For binary types a byte[] or an instance of InputStream is returned. If an inputstream is returned then it can only be read once and in.available() returns the total number of bytes available to read. For BLOB types a random value is returned up to either the passed in precision or 256k. This is to provide a general purpose value that can be more than a page.

Caller should check the return type using instanceof and use setCharacterStream() for Reader objects and setBinaryStream for InputStreams. (work in progress)

Throws:
java.io.IOException

randomBinary

private static byte[] randomBinary(java.util.Random r,
                                   int len)

randomString

private static java.lang.String randomString(java.util.Random r,
                                             int len)

testDerby4095OldTriggerRows

public void testDerby4095OldTriggerRows()
                                 throws java.sql.SQLException
Test that a nested loop join that accesses the TriggerOldTransitionRowsVTI can reopen the ResultSet properly when it re-executes.

Throws:
java.sql.SQLException

testDerby4095NewTriggerRows

public void testDerby4095NewTriggerRows()
                                 throws java.sql.SQLException
Test that a nested loop join that accesses the TriggerNewTransitionRowsVTI can reopen the ResultSet properly when it re-executes.

Throws:
java.sql.SQLException

Built on Sat 2009-11-28 18:44:18-0800, from revision 885187

Apache Derby V10.6 Internals - Copyright © 2004,2008 The Apache Software Foundation. All Rights Reserved.