001: package com.mockrunner.test.jdbc;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.sql.SQLException;
007: import java.util.Arrays;
008:
009: import junit.framework.TestCase;
010:
011: import com.mockrunner.mock.jdbc.MockBlob;
012: import com.mockrunner.util.common.StreamUtil;
013:
014: public class MockBlobTest extends TestCase {
015: private MockBlob blob;
016:
017: protected void setUp() throws Exception {
018: super .setUp();
019: blob = new MockBlob(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
020: 11 });
021: }
022:
023: public void testGetBytes() throws Exception {
024: byte[] data = blob.getBytes(1, 3);
025: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3 }));
026: data = blob.getBytes(5, 25);
027: assertTrue(Arrays.equals(data, new byte[] { 5, 6, 7, 8, 9, 10,
028: 11 }));
029: data = blob.getBytes(5, 2);
030: assertTrue(Arrays.equals(data, new byte[] { 5, 6 }));
031: data = blob.getBytes(1, 12);
032: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3, 4, 5, 6,
033: 7, 8, 9, 10, 11 }));
034: assertTrue(blob.getBytes(5, 0).length == 0);
035: assertTrue(new MockBlob(new byte[] {}).getBytes(1, 0).length == 0);
036: try {
037: blob.getBytes(1, -1);
038: fail();
039: } catch (IllegalArgumentException exc) {
040: //expected exception
041: }
042: }
043:
044: public void testGetBinaryStream() throws Exception {
045: InputStream stream = blob.getBinaryStream();
046: assertTrue(StreamUtil.compareStreams(stream,
047: new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5, 6,
048: 7, 8, 9, 10, 11 })));
049: stream = blob.getBinaryStream(1, 3);
050: assertTrue(StreamUtil.compareStreams(stream,
051: new ByteArrayInputStream(new byte[] { 1, 2, 3 })));
052: stream = blob.getBinaryStream(7, 45);
053: assertTrue(StreamUtil
054: .compareStreams(stream, new ByteArrayInputStream(
055: new byte[] { 7, 8, 9, 10, 11 })));
056: blob = new MockBlob(new byte[] {});
057: stream = blob.getBinaryStream();
058: assertTrue(StreamUtil.compareStreams(stream,
059: new ByteArrayInputStream(new byte[0])));
060: try {
061: blob.getBinaryStream(1, -2);
062: fail();
063: } catch (IllegalArgumentException exc) {
064: //expected exception
065: }
066: }
067:
068: public void testPosition() throws Exception {
069: byte[] searchBytes = new byte[] { 8, 9 };
070: assertEquals(8, blob.position(searchBytes, 1));
071: searchBytes = new byte[] { 12 };
072: assertEquals(-1, blob.position(searchBytes, 1));
073: MockBlob searchBlob = new MockBlob(new byte[] { 4, 5, 6 });
074: assertEquals(4, blob.position(searchBlob, 1));
075: }
076:
077: public void testUpdateData() throws Exception {
078: blob.setBytes(11, new byte[] { 12 });
079: byte[] data = blob.getBytes(1, 11);
080: assertEquals(12, data[10]);
081: blob.setBytes(14, new byte[] { 12 });
082: assertTrue(blob.length() == 14);
083: data = blob.getBytes(1, 14);
084: assertEquals(12, data[10]);
085: assertEquals(0, data[11]);
086: assertEquals(0, data[12]);
087: assertEquals(12, data[13]);
088: OutputStream stream = blob.setBinaryStream(2);
089: stream.write(3);
090: data = blob.getBytes(1, 14);
091: assertEquals(1, data[0]);
092: assertEquals(3, data[1]);
093: assertEquals(3, data[2]);
094: stream.write(new byte[] { 4, 5, 6 });
095: data = blob.getBytes(1, 14);
096: assertEquals(1, data[0]);
097: assertEquals(3, data[1]);
098: assertEquals(4, data[2]);
099: assertEquals(5, data[3]);
100: assertEquals(6, data[4]);
101: stream = blob.setBinaryStream(1);
102: OutputStream anotherAtream = blob.setBinaryStream(16);
103: stream.write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
104: 12, 13, 14, 15 });
105: anotherAtream.write(16);
106: data = blob.getBytes(1, 16);
107: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3, 4, 5, 6,
108: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
109: }
110:
111: public void testFree() throws Exception {
112: assertFalse(blob.wasFreeCalled());
113: blob.free();
114: assertTrue(blob.wasFreeCalled());
115: try {
116: blob.getBytes(1, 0);
117: fail();
118: } catch (SQLException exc) {
119: //expected exception
120: }
121: try {
122: blob.truncate(5);
123: fail();
124: } catch (SQLException exc) {
125: //expected exception
126: }
127: try {
128: blob.setBytes(1, new byte[0], 2, 3);
129: fail();
130: } catch (SQLException exc) {
131: //expected exception
132: }
133: MockBlob copy = (MockBlob) blob.clone();
134: assertTrue(copy.wasFreeCalled());
135: }
136:
137: public void testEquals() throws Exception {
138: MockBlob blob1 = new MockBlob(new byte[] { 1, 2, 3 });
139: assertFalse(blob1.equals(null));
140: assertTrue(blob1.equals(blob1));
141: MockBlob blob2 = new MockBlob(new byte[] { 1, 2, 3, 4 });
142: assertFalse(blob1.equals(blob2));
143: assertFalse(blob2.equals(blob1));
144: blob2 = new MockBlob(new byte[] { 1, 2, 3 });
145: assertTrue(blob1.equals(blob2));
146: assertTrue(blob2.equals(blob1));
147: assertEquals(blob1.hashCode(), blob2.hashCode());
148: blob2.free();
149: assertFalse(blob1.equals(blob2));
150: assertFalse(blob2.equals(blob1));
151: }
152:
153: public void testClone() throws Exception {
154: MockBlob cloneBlob = (MockBlob) blob.clone();
155: assertTrue(blob.length() == cloneBlob.length());
156: blob.setBytes(1, new byte[] { 2 });
157: byte[] data = blob.getBytes(1, 11);
158: assertTrue(Arrays.equals(data, new byte[] { 2, 2, 3, 4, 5, 6,
159: 7, 8, 9, 10, 11 }));
160: data = cloneBlob.getBytes(1, 11);
161: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3, 4, 5, 6,
162: 7, 8, 9, 10, 11 }));
163: }
164:
165: public void testToString() throws Exception {
166: MockBlob blob = new MockBlob(new byte[0]);
167: assertEquals("Blob data: []", blob.toString());
168: blob = new MockBlob(new byte[] { 1, 2, 3 });
169: assertEquals("Blob data: [1, 2, 3]", blob.toString());
170: }
171: }
|