Kathey Marsden wrote:
Your test would not print "FAIL" if it got the wrong exception,
I thought the check in the dumpSQLExceptions takes care of this case
where i check for the expected
SQLState , else I say we got an unexpected exception. Am I missing
something obvious ? :-(
You are right! My bad on the review.
Also, could you tell me why these are no longer local variables?
static Connection con;
static ResultSet rs;
static PreparedStatement stmt = null;
static PreparedStatement pStmt = null;
static Statement stmt1 = null;
static String returnValue = null;
This I had to change because I had two try-catch block in there.
Hence this would result in a compilation error
saying that "rs might not have been initialzed", hence I thought it
would be better not to have them as local variables.
I see. This is a really key point to understand if you are doing any
work in the server it could be very dangerous to switch local variables
to static variables there. I made a few cleanup changes in testRelative
to show you.
There are two ways to address the problem you describe with the
scoping of local variables.
1) Put your local variables before the try block and initialize them to
null. Look at PreparedStatement pStmt in test1 for an example of this.
It is declared and initialized to null before the try block and then
initialized inside the try block(s).
2) Pass variables along as a parameter. Look at the Connection con local
variable in main(). It is declared and initialized in main and then
passed to test1. This way it could potentially be passed to a test2()
method someday.
Apply the attached patch and study these cases carefully. See what
other cleanup changes you can make and send it on back as a patch. For
example, I suppose it wouldn't hurt to close the prepared statements and
connections. How would you close them so that they always got closed,
even if an error occurred? Some single line comments for each test
case would be helpful too.
I agree, these are definitely areas of improvement.
I have the document ready. What I am having some problem is testing
whether my document has the right formatting,etc by integrating
it into the Derby site and building the site using Forrest. It is a
painful task doing this and the build process is ultra slow :-( .
Forrest builds everything even for a small change and it takes almost
an hour , sometimes more to build the site :-(
I admire your resolve in building forrest Maybe take that hour when
your machine is tied up to print out testRelative.java and go over it in
detail and mark your improvements in pencil. Sometimes it is really a
good thing to work on an isolated piece of code like this. Really slow
down and hone your skills.
Ok. I am doing this. Hopefully we can make more progress here and make
this as a model test case :-)
~ Shreyas
Kathey
------------------------------------------------------------------------
Index:
java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/testRelative.java
===================================================================
---
java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/testRelative.java
(revision 164027)
+++
java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/testRelative.java
(working copy)
@@ -8,32 +8,34 @@
public class testRelative {
- static final String EXPECTED_SQL_STATE = "24000";
- static Connection con;
- static ResultSet rs;
- static PreparedStatement stmt = null;
- static PreparedStatement pStmt = null;
- static Statement stmt1 = null;
- static String returnValue = null;
-
+ static final String NO_CURRENT_ROW_SQL_STATE = "24000";
+
public static void main(String[] args) {
- test1(args);
+ System.out.println("Test testRelative starting");
+ Connection con = null;
+ try {
+ // use the ij utility to read the property file and
+ // make the initial connection.
+ ij.getPropertyArg(args);
+ con = ij.startJBMS();
+ test1(con);
+ } catch (Exception e)
+ {
+ unexpectedException(e);
+ }
}
- public static void test1(String []args) {
- System.out.println("Test testRelative starting");
-
- try
- {
- // use the ij utility to read the property file and
- // make the initial connection.
- ij.getPropertyArg(args);
- con = ij.startJBMS();
-
- con.setAutoCommit(false);
-
- stmt = con.prepareStatement("create table testRelative(name varchar(10), i int)");
- stmt.executeUpdate();
+ public static void test1(Connection con) {
+ ResultSet rs = null;
+ PreparedStatement pStmt = null;
+ Statement stmt1 = null;
+ String returnValue = null;
+
+ try
+ {
+ con.setAutoCommit(false);
+ pStmt = con.prepareStatement("create table testRelative(name varchar(10), i int)");
+ pStmt.executeUpdate();
con.commit();
pStmt = con.prepareStatement("insert into testRelative values (?,?)");
@@ -89,7 +91,7 @@
System.out.println("Value="+returnValue);
} catch(SQLException sqle) {
- dumpSQLExceptions(sqle);
+ expectedException(sqle, NO_CURRENT_ROW_SQL_STATE);
} catch(Throwable e) {
System.out.println("FAIL -- unexpected exception: "+e.getMessage());
e.printStackTrace(System.out);
@@ -98,28 +100,42 @@
}
/**
- * This is to print the expected Exception's details. We are here because we got an Exception
- * when we expected one, but checking to see that we got the right one.
- **/
- static private void dumpSQLExceptions (SQLException se) {
- if( se.getSQLState() != null && (se.getSQLState().equals(EXPECTED_SQL_STATE))) {
+ * Print the expected Exception's details if the SQLException SQLState
+ * matches the expected SQLState. Otherwise fail
+ *
+ * @param se SQLException that was thrown by the test
+ * @param expectedSQLState The SQLState that we expect.
+ *
+ **/
+ static private void expectedException (SQLException se, String expectedSQLState) {
+ if( se.getSQLState() != null && (se.getSQLState().equals(expectedSQLState))) {
System.out.println("PASS -- expected exception");
while (se != null) {
System.out.println("SQLSTATE("+se.getSQLState()+"): "+se.getMessage());
se = se.getNextException();
}
} else {
- System.out.println("FAIL--Unexpected SQLException: "+se.getMessage());
+ System.out.println("FAIL--Unexpected SQLException: " +
+ "SQLSTATE(" +se.getSQLState() + ")" +
+ se.getMessage());
se.printStackTrace(System.out);
}
- }
+ }
/**
* We are here because we got an exception when did not expect one.
* Hence printing the message and stack trace here.
**/
static private void unexpectedSQLException(SQLException se) {
- System.out.println("FAIL -- Unexpected Exception: "+ se.getMessage());
+ System.out.println("FAIL -- Unexpected Exception: "+
+ "SQLSTATE(" +se.getSQLState() +")" +
+ se.getMessage());
se.printStackTrace(System.out);
}
+
+ static private void unexpectedException(Exception e) {
+ System.out.println("FAIL -- Unexpected Exception: "+
+ e.getMessage());
+ }
+
}