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

« back to all changes in this revision

Viewing changes to src/test/java/org/mariadb/jdbc/BlobTest.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:
4
4
 
5
5
import java.io.ByteArrayInputStream;
6
6
import java.io.ByteArrayOutputStream;
 
7
import java.io.IOException;
 
8
import java.io.InputStream;
7
9
import java.io.ObjectInputStream;
8
10
import java.io.ObjectOutputStream;
 
11
import java.io.OutputStream;
 
12
import java.io.Reader;
 
13
import java.io.StringReader;
 
14
import java.io.Writer;
9
15
import java.sql.Blob;
 
16
import java.sql.Clob;
 
17
import java.sql.PreparedStatement;
10
18
import java.sql.ResultSet;
11
19
import java.sql.SQLException;
12
20
import java.sql.Statement;
13
21
import java.util.logging.Level;
14
22
import java.util.logging.Logger;
 
23
import org.junit.Assert;
15
24
 
16
25
import static org.junit.Assert.assertEquals;
17
26
import static org.junit.Assert.assertTrue;
61
70
        assertEquals(arr.getClass(), rs.getObject(3).getClass());
62
71
        assertEquals(String.class, rs.getObject(4).getClass());
63
72
    }
64
 
 
 
73
    
 
74
 
 
75
    @Test
 
76
    public void testCharacterStreamWithMultibyteCharacterAndLength() throws Exception {
 
77
        connection.createStatement().execute("drop table if exists streamtest2");
 
78
        connection.createStatement().execute("create table streamtest2 (id int primary key not null, strm text)");
 
79
        PreparedStatement stmt = connection.prepareStatement("insert into streamtest2 (id, strm) values (?,?)");
 
80
        stmt.setInt(1,2);
 
81
        String toInsert = "\u00D8abcdefgh\njklmn\"";
 
82
        Reader reader = new StringReader(toInsert);
 
83
        stmt.setCharacterStream(2, reader, 5);
 
84
        stmt.execute();
 
85
        ResultSet rs = connection.createStatement().executeQuery("select * from streamtest2");
 
86
        rs.next();
 
87
        Reader rdr = rs.getCharacterStream("strm");
 
88
        StringBuilder sb = new StringBuilder();
 
89
        int ch;
 
90
        while((ch = rdr.read()) != -1) {
 
91
            sb.append((char)ch);
 
92
        }
 
93
        assertEquals(toInsert.substring(0,5), sb.toString());
 
94
    }
 
95
 
 
96
    @Test
 
97
    public void testCharacterStreamWithMultibyteCharacter() throws Exception {
 
98
        connection.createStatement().execute("drop table if exists streamtest2");
 
99
        connection.createStatement().execute("create table streamtest2 (id int primary key not null, strm text)");
 
100
        PreparedStatement stmt = connection.prepareStatement("insert into streamtest2 (id, strm) values (?,?)");
 
101
        stmt.setInt(1,2);
 
102
        String toInsert = "\u00D8abcdefgh\njklmn\"";
 
103
        Reader reader = new StringReader(toInsert);
 
104
        stmt.setCharacterStream(2, reader);
 
105
        stmt.execute();
 
106
        ResultSet rs = connection.createStatement().executeQuery("select * from streamtest2");
 
107
        rs.next();
 
108
        Reader rdr = rs.getCharacterStream("strm");
 
109
        StringBuilder sb = new StringBuilder();
 
110
        int ch;
 
111
        while((ch = rdr.read()) != -1) {
 
112
            sb.append((char)ch);
 
113
        }
 
114
        assertEquals(toInsert, sb.toString());
 
115
    }
 
116
 
 
117
    @Test
 
118
    public void testClobWithLengthAndMultibyteCharacter() throws SQLException, IOException {
 
119
        connection.createStatement().execute("drop table if exists clobtest");
 
120
        connection.createStatement().execute("create table clobtest (id int not null primary key, strm text)");
 
121
        PreparedStatement stmt = connection.prepareStatement("insert into clobtest (id, strm) values (?,?)");
 
122
        String clob = "\u00D8clob";
 
123
        stmt.setInt(1,1);
 
124
        stmt.setClob(2, new StringReader(clob));
 
125
        stmt.execute();
 
126
        ResultSet rs = connection.createStatement().executeQuery("select * from clobtest");
 
127
        rs.next();
 
128
        Reader readStuff = rs.getClob("strm").getCharacterStream();
 
129
        char[] a = new char[5];
 
130
        readStuff.read(a);
 
131
        assertEquals(new String(a), clob);
 
132
    }
 
133
 
 
134
    @Test
 
135
    public void  testClob3() throws Exception {
 
136
        connection.createStatement().execute("drop table if exists clobtest");
 
137
        connection.createStatement().execute("create table clobtest (strm text)");
 
138
        PreparedStatement stmt = connection.prepareStatement("insert into clobtest (strm) values (?)");
 
139
        Clob clob = connection.createClob();
 
140
        Writer writer = clob.setCharacterStream(1);
 
141
        writer.write("\u00D8hello", 0, 6);
 
142
        writer.flush();
 
143
        stmt.setClob(1,clob);
 
144
        stmt.execute();
 
145
        ResultSet rs = connection.createStatement().executeQuery("select * from clobtest");
 
146
        rs.next();
 
147
        Object o = rs.getObject(1);
 
148
        assertTrue(o instanceof String);
 
149
        String s = rs.getString(1);
 
150
        assertEquals("\u00D8hello", s);
 
151
    }
 
152
    
 
153
       @Test
 
154
    public void testBlob() throws SQLException, IOException {
 
155
        connection.createStatement().execute("drop table if exists blobtest");
 
156
        connection.createStatement().execute("create table blobtest (id int not null primary key, strm blob)");
 
157
        PreparedStatement stmt = connection.prepareStatement("insert into blobtest (id, strm) values (?,?)");
 
158
        byte [] theBlob = {1,2,3,4,5,6};
 
159
        InputStream stream = new ByteArrayInputStream(theBlob);
 
160
        stmt.setInt(1,1);
 
161
        stmt.setBlob(2,stream);
 
162
        stmt.execute();
 
163
        ResultSet rs = connection.createStatement().executeQuery("select * from blobtest");
 
164
        rs.next();
 
165
        InputStream readStuff = rs.getBlob("strm").getBinaryStream();
 
166
        int ch;
 
167
        int pos=0;
 
168
        while((ch = readStuff.read())!=-1) {
 
169
            assertEquals(theBlob[pos++],ch);
 
170
        }
 
171
 
 
172
        readStuff = rs.getBinaryStream("strm");
 
173
 
 
174
        pos=0;
 
175
        while((ch = readStuff.read())!=-1) {
 
176
            assertEquals(theBlob[pos++],ch);
 
177
        }
 
178
    }
 
179
   @Test
 
180
    public void testBlobWithLength() throws SQLException, IOException {
 
181
        connection.createStatement().execute("drop table if exists blobtest");
 
182
        connection.createStatement().execute("create table blobtest (id int not null primary key, strm blob)");
 
183
        PreparedStatement stmt = connection.prepareStatement("insert into blobtest (id, strm) values (?,?)");
 
184
        byte [] theBlob = {1,2,3,4,5,6};
 
185
        InputStream stream = new ByteArrayInputStream(theBlob);
 
186
        stmt.setInt(1,1);
 
187
        stmt.setBlob(2,stream,4);
 
188
        stmt.execute();
 
189
        ResultSet rs = connection.createStatement().executeQuery("select * from blobtest");
 
190
        rs.next();
 
191
        InputStream readStuff = rs.getBlob("strm").getBinaryStream();
 
192
        int ch;
 
193
        int pos=0;
 
194
        while((ch = readStuff.read())!=-1) {
 
195
            assertEquals(theBlob[pos++],ch);
 
196
        }
 
197
    }
 
198
    @Test
 
199
    public void testClobWithLength() throws SQLException, IOException {
 
200
        connection.createStatement().execute("drop table if exists clobtest");
 
201
        connection.createStatement().execute("create table clobtest (id int not null primary key, strm text)");
 
202
        PreparedStatement stmt = connection.prepareStatement("insert into clobtest (id, strm) values (?,?)");
 
203
        String clob = "clob";
 
204
        stmt.setInt(1,1);
 
205
        stmt.setClob(2, new StringReader(clob));
 
206
        stmt.execute();
 
207
        ResultSet rs = connection.createStatement().executeQuery("select * from clobtest");
 
208
        rs.next();
 
209
        Reader readStuff = rs.getClob("strm").getCharacterStream();
 
210
        char[] a = new char[4];
 
211
        readStuff.read(a);
 
212
        Assert.assertEquals(new String(a), clob);
 
213
    }
 
214
 
 
215
    @Test
 
216
    public void  testClob2() throws SQLException, IOException {
 
217
        connection.createStatement().execute("drop table if exists clobtest");
 
218
        connection.createStatement().execute("create table clobtest (id int not null primary key, strm text)");
 
219
        PreparedStatement stmt = connection.prepareStatement("insert into clobtest (id, strm) values (?,?)");
 
220
        Clob clob = connection.createClob();
 
221
        OutputStream ostream = clob.setAsciiStream(1);
 
222
        byte[] bytes = "hello".getBytes();
 
223
        ostream.write(bytes);
 
224
        stmt.setInt(1,1);
 
225
        stmt.setClob(2,clob);
 
226
        stmt.execute();
 
227
        ResultSet rs = connection.createStatement().executeQuery("select * from clobtest");
 
228
        rs.next();
 
229
        Object o = rs.getObject(2);
 
230
        assertTrue(o instanceof String);
 
231
        String s = rs.getString(2);
 
232
        assertTrue(s.equals("hello"));
 
233
    }
65
234
    @Test
66
235
    public void blobSerialization() throws Exception {
67
236
       Blob b = new MySQLBlob(new byte[]{1,2,3});