File size: 7,230 Bytes
2795186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 Copyright  2002-2004 MySQL AB, 2008 Sun Microsystems

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



 */
package testsuite.simple;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;

import testsuite.BaseTestCase;

/**
 * Tests BLOB functionality in the driver.
 * 
 * @author Mark Matthews
 * @version $Id$
 */
public class BlobTest extends BaseTestCase {

	private static File testBlobFile;

	static {
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				for (int i = 0; i < 5; i++) {
					try {
						if (testBlobFile.delete()) {
							break;
						}
					} catch (Throwable t) {
					}
				}
			}
		});
	}
	/**
	 * Creates a new BlobTest object.
	 * 
	 * @param name
	 *            the test to run
	 */
	public BlobTest(String name) {
		super(name);
	}

	/**
	 * Runs all test cases in this test suite
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		junit.textui.TestRunner.run(BlobTest.class);
	}

	/**
	 * Setup the test case
	 * 
	 * @throws Exception
	 *             if an error occurs
	 */
	public void setUp() throws Exception {
		super.setUp();

		if (versionMeetsMinimum(4, 0)) {
			int requiredSize = 32 * 1024 * 1024;

			if (testBlobFile == null || testBlobFile.length() != requiredSize) {
				createBlobFile(requiredSize);
			}

		} else {
			int requiredSize = 8 * 1024 * 1024;

			if (testBlobFile == null || testBlobFile.length() != requiredSize) {
				createBlobFile(requiredSize);
			}
		}

		createTestTable();
	}

	/**
	 * Destroy resources created by test case
	 * 
	 * @throws Exception
	 *             if an error occurs
	 */
	public void tearDown() throws Exception {
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS BLOBTEST");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			super.tearDown();
		}
	}

	public void testByteStreamInsert() throws Exception {
		testByteStreamInsert(this.conn);
	}
	/**
	 * Tests inserting blob data as a stream
	 * 
	 * @throws Exception
	 *             if an error occurs
	 */
	private void testByteStreamInsert(Connection c) throws Exception {
		BufferedInputStream bIn = new BufferedInputStream(new FileInputStream(
				testBlobFile));
		this.pstmt = c
				.prepareStatement("INSERT INTO BLOBTEST(blobdata) VALUES (?)");
		this.pstmt.setBinaryStream(1, bIn, (int) testBlobFile.length());
		this.pstmt.execute();

		this.pstmt.clearParameters();
		doRetrieval();
	}

	private boolean checkBlob(byte[] retrBytes) throws Exception {
		boolean passed = false;
		BufferedInputStream bIn = new BufferedInputStream(new FileInputStream(
				testBlobFile));

		try {
			int fileLength = (int) testBlobFile.length();
			if (retrBytes.length == fileLength) {
				for (int i = 0; i < fileLength; i++) {
					byte fromFile = (byte) (bIn.read() & 0xff);

					if (retrBytes[i] != fromFile) {
						passed = false;
						System.out.println("Byte pattern differed at position "
								+ i + " , " + retrBytes[i] + " != " + fromFile);

						for (int j = 0; (j < (i + 10)) /* && (j < i) */; j++) {
							System.out.print(Integer
									.toHexString(retrBytes[j] & 0xff)
									+ " ");
						}

						break;
					}

					passed = true;
				}
			} else {
				passed = false;
				System.out.println("retrBytes.length(" + retrBytes.length
						+ ") != testBlob.length(" + fileLength + ")");
			}

			return passed;
		} finally {
			if (bIn != null) {
				bIn.close();
			}
		}
	}

	private void createTestTable() throws Exception {
		//
		// Catch the error, the table might exist
		//
		try {
			this.stmt.executeUpdate("DROP TABLE BLOBTEST");
		} catch (SQLException SQLE) {
			;
		}

		this.stmt
				.executeUpdate("CREATE TABLE BLOBTEST (pos int PRIMARY KEY auto_increment, "
						+ "blobdata LONGBLOB)");
	}

	/**
	 * Mark this as deprecated to avoid warnings from compiler...
	 * 
	 * @deprecated
	 * 
	 * @throws Exception
	 *             if an error occurs retrieving the value
	 */
	private void doRetrieval() throws Exception {
		boolean passed = false;
		this.rs = this.stmt
				.executeQuery("SELECT blobdata from BLOBTEST LIMIT 1");
		this.rs.next();

		byte[] retrBytes = this.rs.getBytes(1);
		passed = checkBlob(retrBytes);
		assertTrue(
				"Inserted BLOB data did not match retrieved BLOB data for getBytes().",
				passed);
		retrBytes = this.rs.getBlob(1).getBytes(1L,
				(int) this.rs.getBlob(1).length());
		passed = checkBlob(retrBytes);
		assertTrue(
				"Inserted BLOB data did not match retrieved BLOB data for getBlob().",
				passed);

		InputStream inStr = this.rs.getBinaryStream(1);
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		int b;

		while ((b = inStr.read()) != -1) {
			bOut.write((byte) b);
		}

		retrBytes = bOut.toByteArray();
		passed = checkBlob(retrBytes);
		assertTrue(
				"Inserted BLOB data did not match retrieved BLOB data for getBinaryStream().",
				passed);
		inStr = this.rs.getAsciiStream(1);
		bOut = new ByteArrayOutputStream();

		while ((b = inStr.read()) != -1) {
			bOut.write((byte) b);
		}

		retrBytes = bOut.toByteArray();
		passed = checkBlob(retrBytes);
		assertTrue(
				"Inserted BLOB data did not match retrieved BLOB data for getAsciiStream().",
				passed);
		inStr = this.rs.getUnicodeStream(1);
		bOut = new ByteArrayOutputStream();

		while ((b = inStr.read()) != -1) {
			bOut.write((byte) b);
		}

		retrBytes = bOut.toByteArray();
		passed = checkBlob(retrBytes);
		assertTrue(
				"Inserted BLOB data did not match retrieved BLOB data for getUnicodeStream().",
				passed);
	}

	private final static String TEST_BLOB_FILE_PREFIX = "cmj-testblob";
	
	private void createBlobFile(int size) throws Exception {
		if (testBlobFile != null && testBlobFile.length() != size) {
			testBlobFile.delete();
		}

		testBlobFile = File.createTempFile(TEST_BLOB_FILE_PREFIX, ".dat");
		testBlobFile.deleteOnExit();
		
		cleanupTempFiles(testBlobFile, TEST_BLOB_FILE_PREFIX);

		BufferedOutputStream bOut = new BufferedOutputStream(
				new FileOutputStream(testBlobFile));

		int dataRange = Byte.MAX_VALUE - Byte.MIN_VALUE;

		for (int i = 0; i < size; i++) {
			bOut.write((byte) ((Math.random() * dataRange) + Byte.MIN_VALUE));
		}

		bOut.flush();
		bOut.close();
	}
}