Return-Path: Delivered-To: apmail-db-derby-dev-archive@www.apache.org Received: (qmail 50384 invoked from network); 27 Apr 2005 03:28:47 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 27 Apr 2005 03:28:47 -0000 Received: (qmail 42091 invoked by uid 500); 27 Apr 2005 03:29:34 -0000 Delivered-To: apmail-db-derby-dev-archive@db.apache.org Received: (qmail 42044 invoked by uid 500); 27 Apr 2005 03:29:33 -0000 Mailing-List: contact derby-dev-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Development" Delivered-To: mailing list derby-dev@db.apache.org Received: (qmail 42029 invoked by uid 99); 27 Apr 2005 03:29:33 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from brmea-mail-4.Sun.COM (HELO brmea-mail-4.sun.com) (192.18.98.36) by apache.org (qpsmtpd/0.28) with ESMTP; Tue, 26 Apr 2005 20:29:32 -0700 Received: from phys-biff-1 ([129.158.227.36]) by brmea-mail-4.sun.com (8.12.10/8.12.9) with ESMTP id j3R3Sfi7013688 for ; Tue, 26 Apr 2005 21:28:41 -0600 (MDT) Received: from conversion-daemon.biff-mail1.india.sun.com by biff-mail1.india.sun.com (iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003)) id <0IFL00J014YV8H@biff-mail1.india.sun.com> (original mail from Shreyas.Kaushik@Sun.COM) for derby-dev@db.apache.org; Wed, 27 Apr 2005 08:58:40 +0530 (IST) Received: from [192.168.1.100] (vpn-129-150-156-10.India.Sun.COM [129.150.156.10]) by biff-mail1.india.sun.com (iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003)) with ESMTP id <0IFL00EG65NRT9@biff-mail1.india.sun.com> for derby-dev@db.apache.org; Wed, 27 Apr 2005 08:58:40 +0530 (IST) Date: Wed, 27 Apr 2005 08:59:39 +0530 From: Shreyas Kaushik Subject: Re: jdbcapi/testRelative intermittent diff In-reply-to: <4269746D.1020601@sbcglobal.net> To: Derby Development Message-id: <426F0723.3070004@sun.com> MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7BIT X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) References: <425C13F4.10107@sbcglobal.net> <425F8710.7030600@Sun.com> <425FF8A8.4090506@sbcglobal.net> <42632DC8.4000907@sun.com> <42633AB4.8010209@sun.com> <42641DD7.2070909@sbcglobal.net> <42649A92.2070808@Sun.com> <42678752.3010507@sbcglobal.net> <42678D40.4000307@Sun.com> <4269746D.1020601@sbcglobal.net> X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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()); >+ } >+ > } > > >