001: /*
002: * $Id: TestBLOBType.java,v 1.6 2005/11/24 20:47:31 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.File;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import java.sql.Blob;
048:
049: import junit.framework.Test;
050: import junit.framework.TestCase;
051: import junit.framework.TestSuite;
052:
053: import org.axiondb.jdbc.AxionBlob;
054:
055: /**
056: * @version $Revision: 1.6 $ $Date: 2005/11/24 20:47:31 $
057: * @author James Burke
058: * @author Rodney Waldhoff
059: */
060: public class TestBLOBType extends TestCase {
061:
062: public TestBLOBType(String name) {
063: super (name);
064: }
065:
066: public static void main(String args[]) {
067: String[] testCaseName = { TestBLOBType.class.getName() };
068: junit.textui.TestRunner.main(testCaseName);
069: }
070:
071: public static Test suite() {
072: return new TestSuite(TestBLOBType.class);
073: }
074:
075: private LOBType _blobtype = null;
076: private String _dataAsString = null;
077: private byte[] _data = null;
078: private String _origLobDirProp = null;
079:
080: public void setUp() throws Exception {
081: super .setUp();
082: _origLobDirProp = System.getProperty("axoindb.lobdir");
083: System.setProperty("axiondb.lobdir", "testlobdir");
084: File file = new File("testlobdir");
085: file.deleteOnExit();
086: file.mkdirs();
087: _blobtype = new BLOBType();
088: _blobtype.setLobDir(file);
089: _dataAsString = "Four score and seven years ago.";
090: _data = _dataAsString.getBytes();
091: }
092:
093: public void tearDown() throws Exception {
094: deleteFile((new File("testlobdir")));
095: if (null == _origLobDirProp) {
096: System.getProperties().remove("axiondb.lobdir");
097: } else {
098: System.setProperty("axiondb.lobdir", _origLobDirProp);
099: }
100: super .tearDown();
101: }
102:
103: private boolean deleteFile(File file) throws Exception {
104: if (file.exists()) {
105: if (file.isDirectory()) {
106: File[] files = file.listFiles();
107: for (int i = 0; i < files.length; i++) {
108: deleteFile(files[i]);
109: }
110: }
111: return file.delete();
112: }
113: return true;
114: }
115:
116: public void testSupportsSuccessor() throws Exception {
117: assertTrue(!_blobtype.supportsSuccessor());
118: }
119:
120: public void testAcceptsNewLob() throws Exception {
121: assertTrue(_blobtype.accepts("newlob()"));
122: }
123:
124: public void testCreateNewBlob() throws Exception {
125: Blob blob = _blobtype.toBlob("newlob()");
126: assertNotNull(blob);
127: AxionBlob ablob = (AxionBlob) (blob);
128:
129: OutputStream out = ablob.setBinaryStream(0L);
130: out.write(_data);
131: out.close();
132:
133: InputStream in = ablob.getBinaryStream();
134: ByteArrayOutputStream buf = new ByteArrayOutputStream();
135: for (int i = in.read(); i != -1; i = in.read()) {
136: buf.write(i);
137: }
138: in.close();
139: assertEquals(_dataAsString, new String(buf.toByteArray()));
140: }
141: }
|