Source Code Cross Referenced for BlobTest.java in  » Database-JDBC-Connection-Pool » mysql » testsuite » simple » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » mysql » testsuite.simple 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         Copyright (C) 2002-2004 MySQL AB
003:
004:         This program is free software; you can redistribute it and/or modify
005:         it under the terms of version 2 of the GNU General Public License as 
006:         published by the Free Software Foundation.
007:
008:         There are special exceptions to the terms and conditions of the GPL 
009:         as it is applied to this software. View the full text of the 
010:         exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
011:         software distribution.
012:
013:         This program is distributed in the hope that it will be useful,
014:         but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         GNU General Public License for more details.
017:
018:         You should have received a copy of the GNU General Public License
019:         along with this program; if not, write to the Free Software
020:         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
021:
022:
023:
024:         */
025:        package testsuite.simple;
026:
027:        import testsuite.BaseTestCase;
028:
029:        import java.io.BufferedInputStream;
030:        import java.io.BufferedOutputStream;
031:        import java.io.ByteArrayOutputStream;
032:        import java.io.File;
033:        import java.io.FileInputStream;
034:        import java.io.FileOutputStream;
035:        import java.io.InputStream;
036:
037:        import java.sql.Connection;
038:        import java.sql.SQLException;
039:
040:        /**
041:         * Tests BLOB functionality in the driver.
042:         * 
043:         * @author Mark Matthews
044:         * @version $Id: BlobTest.java 6437 2007-05-24 20:17:01Z mmatthews $
045:         */
046:        public class BlobTest extends BaseTestCase {
047:
048:            private static File testBlobFile;
049:
050:            static {
051:                Runtime.getRuntime().addShutdownHook(new Thread() {
052:                    public void run() {
053:                        for (int i = 0; i < 5; i++) {
054:                            try {
055:                                if (testBlobFile.delete()) {
056:                                    break;
057:                                }
058:                            } catch (Throwable t) {
059:                            }
060:                        }
061:                    }
062:                });
063:            }
064:
065:            /**
066:             * Creates a new BlobTest object.
067:             * 
068:             * @param name
069:             *            the test to run
070:             */
071:            public BlobTest(String name) {
072:                super (name);
073:            }
074:
075:            /**
076:             * Runs all test cases in this test suite
077:             * 
078:             * @param args
079:             */
080:            public static void main(String[] args) {
081:                junit.textui.TestRunner.run(BlobTest.class);
082:            }
083:
084:            /**
085:             * Setup the test case
086:             * 
087:             * @throws Exception
088:             *             if an error occurs
089:             */
090:            public void setUp() throws Exception {
091:                super .setUp();
092:
093:                if (versionMeetsMinimum(4, 0)) {
094:                    int requiredSize = 32 * 1024 * 1024;
095:
096:                    if (testBlobFile == null
097:                            || testBlobFile.length() != requiredSize) {
098:                        createBlobFile(requiredSize);
099:                    }
100:
101:                } else {
102:                    int requiredSize = 8 * 1024 * 1024;
103:
104:                    if (testBlobFile == null
105:                            || testBlobFile.length() != requiredSize) {
106:                        createBlobFile(requiredSize);
107:                    }
108:                }
109:
110:                createTestTable();
111:            }
112:
113:            /**
114:             * Destroy resources created by test case
115:             * 
116:             * @throws Exception
117:             *             if an error occurs
118:             */
119:            public void tearDown() throws Exception {
120:                try {
121:                    this .stmt.executeUpdate("DROP TABLE IF EXISTS BLOBTEST");
122:                } catch (Exception e) {
123:                    e.printStackTrace();
124:                } finally {
125:                    super .tearDown();
126:                }
127:            }
128:
129:            public void testByteStreamInsert() throws Exception {
130:                testByteStreamInsert(this .conn);
131:            }
132:
133:            /**
134:             * Tests inserting blob data as a stream
135:             * 
136:             * @throws Exception
137:             *             if an error occurs
138:             */
139:            private void testByteStreamInsert(Connection c) throws Exception {
140:                BufferedInputStream bIn = new BufferedInputStream(
141:                        new FileInputStream(testBlobFile));
142:                this .pstmt = c
143:                        .prepareStatement("INSERT INTO BLOBTEST(blobdata) VALUES (?)");
144:                this .pstmt.setBinaryStream(1, bIn, (int) testBlobFile.length());
145:                this .pstmt.execute();
146:
147:                this .pstmt.clearParameters();
148:                doRetrieval();
149:            }
150:
151:            private boolean checkBlob(byte[] retrBytes) throws Exception {
152:                boolean passed = false;
153:                BufferedInputStream bIn = new BufferedInputStream(
154:                        new FileInputStream(testBlobFile));
155:
156:                try {
157:                    int fileLength = (int) testBlobFile.length();
158:                    if (retrBytes.length == fileLength) {
159:                        for (int i = 0; i < fileLength; i++) {
160:                            byte fromFile = (byte) (bIn.read() & 0xff);
161:
162:                            if (retrBytes[i] != fromFile) {
163:                                passed = false;
164:                                System.out
165:                                        .println("Byte pattern differed at position "
166:                                                + i
167:                                                + " , "
168:                                                + retrBytes[i]
169:                                                + " != " + fromFile);
170:
171:                                for (int j = 0; (j < (i + 10)) /* && (j < i) */; j++) {
172:                                    System.out.print(Integer
173:                                            .toHexString(retrBytes[j] & 0xff)
174:                                            + " ");
175:                                }
176:
177:                                break;
178:                            }
179:
180:                            passed = true;
181:                        }
182:                    } else {
183:                        passed = false;
184:                        System.out.println("retrBytes.length("
185:                                + retrBytes.length + ") != testBlob.length("
186:                                + fileLength + ")");
187:                    }
188:
189:                    return passed;
190:                } finally {
191:                    if (bIn != null) {
192:                        bIn.close();
193:                    }
194:                }
195:            }
196:
197:            private void createTestTable() throws Exception {
198:                //
199:                // Catch the error, the table might exist
200:                //
201:                try {
202:                    this .stmt.executeUpdate("DROP TABLE BLOBTEST");
203:                } catch (SQLException SQLE) {
204:                    ;
205:                }
206:
207:                this .stmt
208:                        .executeUpdate("CREATE TABLE BLOBTEST (pos int PRIMARY KEY auto_increment, "
209:                                + "blobdata LONGBLOB)");
210:            }
211:
212:            /**
213:             * Mark this as deprecated to avoid warnings from compiler...
214:             * 
215:             * @deprecated
216:             * 
217:             * @throws Exception
218:             *             if an error occurs retrieving the value
219:             */
220:            private void doRetrieval() throws Exception {
221:                boolean passed = false;
222:                this .rs = this .stmt
223:                        .executeQuery("SELECT blobdata from BLOBTEST LIMIT 1");
224:                this .rs.next();
225:
226:                byte[] retrBytes = this .rs.getBytes(1);
227:                passed = checkBlob(retrBytes);
228:                assertTrue(
229:                        "Inserted BLOB data did not match retrieved BLOB data for getBytes().",
230:                        passed);
231:                retrBytes = this .rs.getBlob(1).getBytes(1L,
232:                        (int) this .rs.getBlob(1).length());
233:                passed = checkBlob(retrBytes);
234:                assertTrue(
235:                        "Inserted BLOB data did not match retrieved BLOB data for getBlob().",
236:                        passed);
237:
238:                InputStream inStr = this .rs.getBinaryStream(1);
239:                ByteArrayOutputStream bOut = new ByteArrayOutputStream();
240:                int b;
241:
242:                while ((b = inStr.read()) != -1) {
243:                    bOut.write((byte) b);
244:                }
245:
246:                retrBytes = bOut.toByteArray();
247:                passed = checkBlob(retrBytes);
248:                assertTrue(
249:                        "Inserted BLOB data did not match retrieved BLOB data for getBinaryStream().",
250:                        passed);
251:                inStr = this .rs.getAsciiStream(1);
252:                bOut = new ByteArrayOutputStream();
253:
254:                while ((b = inStr.read()) != -1) {
255:                    bOut.write((byte) b);
256:                }
257:
258:                retrBytes = bOut.toByteArray();
259:                passed = checkBlob(retrBytes);
260:                assertTrue(
261:                        "Inserted BLOB data did not match retrieved BLOB data for getAsciiStream().",
262:                        passed);
263:                inStr = this .rs.getUnicodeStream(1);
264:                bOut = new ByteArrayOutputStream();
265:
266:                while ((b = inStr.read()) != -1) {
267:                    bOut.write((byte) b);
268:                }
269:
270:                retrBytes = bOut.toByteArray();
271:                passed = checkBlob(retrBytes);
272:                assertTrue(
273:                        "Inserted BLOB data did not match retrieved BLOB data for getUnicodeStream().",
274:                        passed);
275:            }
276:
277:            private final static String TEST_BLOB_FILE_PREFIX = "cmj-testblob";
278:
279:            private void createBlobFile(int size) throws Exception {
280:                if (testBlobFile != null && testBlobFile.length() != size) {
281:                    testBlobFile.delete();
282:                }
283:
284:                testBlobFile = File.createTempFile(TEST_BLOB_FILE_PREFIX,
285:                        ".dat");
286:                testBlobFile.deleteOnExit();
287:
288:                cleanupTempFiles(testBlobFile, TEST_BLOB_FILE_PREFIX);
289:
290:                BufferedOutputStream bOut = new BufferedOutputStream(
291:                        new FileOutputStream(testBlobFile));
292:
293:                int dataRange = Byte.MAX_VALUE - Byte.MIN_VALUE;
294:
295:                for (int i = 0; i < size; i++) {
296:                    bOut
297:                            .write((byte) ((Math.random() * dataRange) + Byte.MIN_VALUE));
298:                }
299:
300:                bOut.flush();
301:                bOut.close();
302:            }
303:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.