~maria-captains/mariadb-java-client/trunk

« back to all changes in this revision

Viewing changes to src/test/java/org/mariadb/jdbc/DriverTest.java

  • Committer: Vladislav Vaintroub
  • Date: 2013-03-27 17:44:16 UTC
  • Revision ID: wlad@montyprogram.com-20130327174416-wjnqb6o8cuezg6gt
CONJ-31 : fix several issues with Clob and PreparedStatement.setCharacterStream()

- non-ASCII  characters can get lost, if  PreparedStatement.setCharacterStream() is used.
- If PreparedStatement.setClob() was used, CLOB was sent to the server using binary introducer (_BINARY). This can potentially lead to wrongly stored non-ASCII characters.

Small cleanups :
-  remove try/catch around code that can't throw exceptions in MySQLPreparedStatement
-  move all tests related to Clobs, Blobs, and streams to BlobTest.java

Thanks to Rune Bremnes, who contributed parts of this patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
556
556
        assertEquals(sb.toString(),toInsert.substring(0,5));
557
557
    }
558
558
 
559
 
    @Test
560
 
    public void testBlob() throws SQLException, IOException {
561
 
        connection.createStatement().execute("drop table if exists blobtest");
562
 
        connection.createStatement().execute("create table blobtest (id int not null primary key, strm blob)");
563
 
        PreparedStatement stmt = connection.prepareStatement("insert into blobtest (id, strm) values (?,?)");
564
 
        byte [] theBlob = {1,2,3,4,5,6};
565
 
        InputStream stream = new ByteArrayInputStream(theBlob);
566
 
        stmt.setInt(1,1);
567
 
        stmt.setBlob(2,stream);
568
 
        stmt.execute();
569
 
        ResultSet rs = connection.createStatement().executeQuery("select * from blobtest");
570
 
        rs.next();
571
 
        InputStream readStuff = rs.getBlob("strm").getBinaryStream();
572
 
        int ch;
573
 
        int pos=0;
574
 
        while((ch = readStuff.read())!=-1) {
575
 
            assertEquals(theBlob[pos++],ch);
576
 
        }
577
 
 
578
 
        readStuff = rs.getBinaryStream("strm");
579
 
 
580
 
        pos=0;
581
 
        while((ch = readStuff.read())!=-1) {
582
 
            assertEquals(theBlob[pos++],ch);
583
 
        }
584
 
    }
585
 
   @Test
586
 
    public void testBlobWithLength() throws SQLException, IOException {
587
 
        connection.createStatement().execute("drop table if exists blobtest");
588
 
        connection.createStatement().execute("create table blobtest (id int not null primary key, strm blob)");
589
 
        PreparedStatement stmt = connection.prepareStatement("insert into blobtest (id, strm) values (?,?)");
590
 
        byte [] theBlob = {1,2,3,4,5,6};
591
 
        InputStream stream = new ByteArrayInputStream(theBlob);
592
 
        stmt.setInt(1,1);
593
 
        stmt.setBlob(2,stream,4);
594
 
        stmt.execute();
595
 
        ResultSet rs = connection.createStatement().executeQuery("select * from blobtest");
596
 
        rs.next();
597
 
        InputStream readStuff = rs.getBlob("strm").getBinaryStream();
598
 
        int ch;
599
 
        int pos=0;
600
 
        while((ch = readStuff.read())!=-1) {
601
 
            assertEquals(theBlob[pos++],ch);
602
 
        }
603
 
    }
604
 
    @Test
605
 
    public void testClobWithLength() throws SQLException, IOException {
606
 
        connection.createStatement().execute("drop table if exists clobtest");
607
 
        connection.createStatement().execute("create table clobtest (id int not null primary key, strm text)");
608
 
        PreparedStatement stmt = connection.prepareStatement("insert into clobtest (id, strm) values (?,?)");
609
 
        String clob = "clob";
610
 
        stmt.setInt(1,1);
611
 
        stmt.setClob(2, new StringReader(clob));
612
 
        stmt.execute();
613
 
        ResultSet rs = connection.createStatement().executeQuery("select * from clobtest");
614
 
        rs.next();
615
 
        Reader readStuff = rs.getClob("strm").getCharacterStream();
616
 
        char[] a = new char[4];
617
 
        readStuff.read(a);
618
 
        Assert.assertEquals(new String(a), clob);
619
 
    }
620
 
 
621
 
    @Test
622
 
    public void  testClob2() throws SQLException, IOException {
623
 
        connection.createStatement().execute("drop table if exists clobtest");
624
 
        connection.createStatement().execute("create table clobtest (id int not null primary key, strm text)");
625
 
        PreparedStatement stmt = connection.prepareStatement("insert into clobtest (id, strm) values (?,?)");
626
 
        Clob clob = connection.createClob();
627
 
        OutputStream ostream = clob.setAsciiStream(1);
628
 
        byte[] bytes = "hello".getBytes();
629
 
        ostream.write(bytes);
630
 
        stmt.setInt(1,1);
631
 
        stmt.setClob(2,clob);
632
 
        stmt.execute();
633
 
        ResultSet rs = connection.createStatement().executeQuery("select * from clobtest");
634
 
        rs.next();
635
 
        Object o = rs.getObject(2);
636
 
        assertTrue(o instanceof String);
637
 
        String s = rs.getString(2);
638
 
        assertTrue(s.equals("hello"));
639
 
    }
 
559
 
640
560
    @Test
641
561
    public void testEmptyResultSet() throws SQLException {
642
562
        connection.createStatement().execute("drop table if exists emptytest");