#6 preparedStatement.setBytes() broken

closed-fixed
Jeff Martin
None
5
2002-09-05
2002-08-27
Anonymous
No

In the Mock implementation of PreparedStatement, if you
call addExpectedSetParameter(int, byte[]) and in the
production code you have
preparedStatement.setBytes(int, byte[]), this always
fails with:
CommonMockPreparedStatement.setParameters did not
receive an expected item

Discussion

  • Jeff Martin
    Jeff Martin
    2002-09-03

    Test case for setBytes

     
  • Jeff Martin
    Jeff Martin
    2002-09-03

    • status: open --> pending-postponed
     
  • Jeff Martin
    Jeff Martin
    2002-09-03

    Logged In: YES
    user_id=157613

    The attached test code passes. Can you provide a test that
    fails?

     
  • Cynick Young
    Cynick Young
    2002-09-03

    Logged In: YES
    user_id=605185

    I am attaching the source files where I am encountering a
    problem. This is the failure that occurs.
    testInsertIntoDB(FooTest)junit.framework.AssertionFailedError:
    CommonMockPreparedStatement.setParameters did not receive an
    expected itemUnexpected:1=[B@c21495

    Foo.java
    --------

    import java.sql.*;

    public class Foo {

    public static final String SQL_STATEMENT = "INSERT INTO
    foo(bar) values(?)";
    private String bar = "some value";

    public byte[] getBarBytes() {
    return bar.getBytes();
    }

    public void insertIntoDB(Connection conn) throws
    SQLException {
    PreparedStatement preparedStatement =
    conn.prepareStatement(SQL_STATEMENT);
    try {
    preparedStatement.clearParameters();
    preparedStatement.setBytes(1, this.getBarBytes());
    preparedStatement.executeUpdate();
    }
    finally {
    preparedStatement.close();
    }
    }
    }

    FooTest.java
    ------------

    import junit.framework.*;

    import java.sql.SQLException;

    import com.mockobjects.sql.*;

    public class FooTest extends TestCase {

    private MockConnection mockConnection;
    private MockPreparedStatement mockPreparedStatement;

    public FooTest(String aName) {
    super(aName);
    }

    public static void main(java.lang.String[] args) {
    junit.textui.TestRunner.run(suite());
    }

    public static Test suite() {
    TestSuite suite = new TestSuite(FooTest.class);
    return suite;
    }

    protected void setUp() throws Exception {
    mockConnection = new MockConnection();
    mockPreparedStatement = new MockPreparedStatement();

    mockConnection.setupAddPreparedStatement(mockPreparedStatement);
    }

    protected void tearDown() throws Exception {
    mockConnection = null;
    mockPreparedStatement = null;
    }

    public void testInsertIntoDB() throws Exception
    {
    Foo foo = new Foo();

    mockConnection.addExpectedPreparedStatementString(foo.SQL_STATEMENT);
    mockPreparedStatement.addExpectedSetParameter(1, "some
    value".getBytes());

    mockPreparedStatement.setExpectedExecuteCalls(1);
    mockPreparedStatement.setExpectedCloseCalls(1);

    foo.insertIntoDB(mockConnection);

    mockPreparedStatement.verify();
    mockConnection.verify();
    }
    }

     
  • Jeff Martin
    Jeff Martin
    2002-09-03

    • assigned_to: nobody --> custommonkey
    • status: pending-postponed --> open-accepted
     
  • Jeff Martin
    Jeff Martin
    2002-09-03

    Logged In: YES
    user_id=157613

    This would seem to be a problem with com.mockobjects.MapEntry

    At the moment this fails.
    assertEquals(
    "Should be equal byte arrays",
    new MapEntry("A", "A".getBytes()),
    new MapEntry("A", "A".getBytes()));

    A work around would be to only call getBytes() once and
    store the result.

    I'll run this past the mailing list to check that the above
    test should actually pass. Then knock out a patch.

     
  • Jeff Martin
    Jeff Martin
    2002-09-05

    Logged In: YES
    user_id=157613

    MapEntry fix is in CVS problem should be solved.

     
  • Jeff Martin
    Jeff Martin
    2002-09-05

    • status: open-accepted --> closed-fixed
     
  • Cynick Young
    Cynick Young
    2002-09-20

    Logged In: YES
    user_id=605185

    Works! Thanks for the great work!