From: Steve F. <ste...@m3...> - 2002-11-10 10:42:55
|
Thanks for your example. I'm assuming that you're doing the getString() so that you can check for null values, which getInt() won't tell you. It's true that our current implementation cannot handle different returns for the same index, we haven't had to so far. Looking at the JDBC spec, there's a wasNull() call on a result set which tells you if the last retrieved value was null. Would that be a better solution? You could then override wasNull() on the MockResultSet you're using and return a boolean as appropriate. Alternatively, if you feel like changing the code in MockResultSet, go ahead. We don't believe we can cover all the bases in something as large as the JDBC library and that's what CVS merge is for. Steve Mark Harper wrote: >Hi there, > >Sanjay Chakravarty told me that that you might be interested in a problem >I've run into using com.mockobjects.sql.MockSingleRowResultSet. >See the attached test class for a full description... > >I realize that the code in countSomething() is redundant, and probably >should be simplified. But the question is, should this be considered a >bug in the MockResultSet implementation? >If I am constrained not to change this code, how might I work around this? > >Many Thanks, >_Mark Harper > > > > >------------------------------------------------------------------------ > >package mockobjectstest; > >import com.mockobjects.sql.MockConnection; >import com.mockobjects.sql.MockPreparedStatement; >import com.mockobjects.sql.MockSingleRowResultSet; >import com.mockobjects.sql.MockStatement; >import java.lang.Exception; >import java.sql.Connection; >import java.sql.ResultSet; >import java.sql.PreparedStatement; > >public class ResultSetBugTest >{ > /* > * The following code works on a java.sql.ResultSet. > * With a MockResultSet, however, I can't get it to work. > */ > public static int countSomething(Connection conn) > { > PreparedStatement pstmt = null; > int resultCount = 0; > > try { > pstmt = conn.prepareStatement("SELECT COUNT(*) FROM A_TABLE"); > > ResultSet rs = pstmt.executeQuery(); > > if (rs.next()) > { > if (rs.getString(1) != null) //line 1 > { > resultCount = rs.getInt(1); //line 2 > } > } > } > catch (java.sql.SQLException e) > { > e.printStackTrace(); > } > return resultCount; > } > > /* > * If I set up a Mock Result set with an Integer value for the count... > */ > public static void testSetupOne() > { > MockConnection mockConnection = new MockConnection(); > MockPreparedStatement mockPreparedStatement = new MockPreparedStatement(); > mockConnection.setupStatement( mockPreparedStatement ); > mockConnection.addExpectedPreparedStatement( mockPreparedStatement ); > MockSingleRowResultSet mockResultSet = new MockSingleRowResultSet(); > > Integer countValue = new Integer(1); > Integer[] resultValues = new Integer[] {countValue}; > mockResultSet.addExpectedIndexedValues( resultValues ); > mockPreparedStatement.addResultSet( mockResultSet ); > countSomething( mockConnection ); > /* > * I get a... > * java.lang.ClassCastException: java.lang.Integer > * at com.mockobjects.sql.MockResultSet.getString(MockResultSet.java:306) > * at //line 1 above. > */ > } > > > /* > * If I set up the Mock Result set with a String value... > */ > public static void testSetupTwo() > { > MockConnection mockConnection = new MockConnection(); > MockPreparedStatement mockPreparedStatement = new MockPreparedStatement(); > mockConnection.setupStatement( mockPreparedStatement ); > mockConnection.addExpectedPreparedStatement( mockPreparedStatement ); > MockSingleRowResultSet mockResultSet = new MockSingleRowResultSet(); > > String countValue = "1"; > String[] resultValues = new String[] {countValue}; > mockResultSet.addExpectedIndexedValues( resultValues ); > mockPreparedStatement.addResultSet( mockResultSet ); > countSomething( mockConnection ); > /* > * I get a... > * java.lang.ClassCastException: java.lang.String > * at com.mockobjects.sql.MockResultSet.getInt(MockResultSet.java:266) > * at //line 2 above. > */ > } > > public static void main (String args[]) > { > System.out.println("Trying Test One"); > try > { > testSetupOne(); > } > catch (Exception e) > { > e.printStackTrace(); > } > > System.out.println("Trying Test Two"); > try > { > testSetupTwo(); > } > catch (Exception e) > { > e.printStackTrace(); > } > > > } >} > > > |