001: package com.mockrunner.test.jdbc;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.OutputStream;
005: import java.io.StringReader;
006: import java.io.Writer;
007: import java.sql.SQLException;
008:
009: import junit.framework.TestCase;
010:
011: import com.mockrunner.mock.jdbc.MockClob;
012: import com.mockrunner.util.common.StreamUtil;
013:
014: public class MockClobTest extends TestCase {
015: private MockClob clob;
016:
017: protected void setUp() throws Exception {
018: super .setUp();
019: clob = new MockClob("This is a Test Clob");
020: }
021:
022: public void testSubString() throws Exception {
023: assertEquals(" is a Test Clob", clob.getSubString(5, 15));
024: assertEquals("This is a Test Clob", clob.getSubString(1, 19));
025: assertEquals("This is a Test Clob", clob.getSubString(1, 25));
026: assertEquals("Th", clob.getSubString(1, 2));
027: assertEquals("C", clob.getSubString(16, 1));
028: assertEquals("", clob.getSubString(16, 0));
029: try {
030: clob.getSubString(5, -1);
031: fail();
032: } catch (IllegalArgumentException exc) {
033: //expected exception
034: }
035: }
036:
037: public void testGetStream() throws Exception {
038: assertTrue(StreamUtil.compareReaders(clob.getCharacterStream(),
039: new StringReader("This is a Test Clob")));
040: assertTrue(StreamUtil.compareStreams(clob.getAsciiStream(),
041: new ByteArrayInputStream("This is a Test Clob"
042: .getBytes("ISO-8859-1"))));
043: assertTrue(StreamUtil.compareReaders(clob.getCharacterStream(6,
044: 15), new StringReader("is a Test Clob")));
045: assertTrue(StreamUtil.compareReaders(clob.getCharacterStream(6,
046: 16), new StringReader("is a Test Clob")));
047: assertTrue(StreamUtil.compareReaders(clob.getCharacterStream(1,
048: 4), new StringReader("This")));
049: assertTrue(StreamUtil.compareReaders(clob.getCharacterStream(1,
050: 0), new StringReader("")));
051: try {
052: clob.getSubString(1, -1);
053: fail();
054: } catch (IllegalArgumentException exc) {
055: //expected exception
056: }
057: }
058:
059: public void testPosition() throws Exception {
060: assertEquals(16, clob.position("Clob", 0));
061: assertEquals(16, clob.position(new MockClob("Clob"), 5));
062: assertEquals(-1, clob.position(new MockClob("XYZ"), 5));
063: assertEquals(1, clob.position("T", 1));
064: assertEquals(11, clob.position("T", 2));
065: assertEquals(1, clob.position(clob, 1));
066: }
067:
068: public void testUpdateData() throws Exception {
069: clob.setString(11, "XYZZ");
070: assertEquals("This is a XYZZ Clob", clob.getSubString(1, 19));
071: clob.setString(11, "Test Mock Clob");
072: assertEquals("This is a Test Mock Clob", clob.getSubString(1,
073: 24));
074: clob.setString(1, "XYZ This", 4, 4);
075: assertEquals("This is a Test Mock Clob", clob.getSubString(1,
076: 24));
077: OutputStream stream = clob.setAsciiStream(1);
078: stream.write(new byte[] { 65, 66, 67, 68 });
079: assertEquals("ABCD is a Test Mock Clob", clob.getSubString(1,
080: 24));
081: Writer writer = clob.setCharacterStream(5);
082: writer.write("FFG");
083: stream.write(69);
084: assertEquals("ABCDEFG a Test Mock Clob", clob.getSubString(1,
085: 24));
086: writer = clob.setCharacterStream(1);
087: writer.write("This is a Test ClobThis is a Test Clob");
088: assertEquals("This is a Test ClobThis is a Test Clob", clob
089: .getSubString(1, 38));
090: }
091:
092: public void testFree() throws Exception {
093: assertFalse(clob.wasFreeCalled());
094: clob.free();
095: assertTrue(clob.wasFreeCalled());
096: try {
097: clob.getSubString(1, 2);
098: fail();
099: } catch (SQLException exc) {
100: //expected exception
101: }
102: try {
103: clob.getAsciiStream();
104: fail();
105: } catch (SQLException exc) {
106: //expected exception
107: }
108: try {
109: clob.position("", 1);
110: fail();
111: } catch (SQLException exc) {
112: //expected exception
113: }
114: MockClob copy = (MockClob) clob.clone();
115: assertTrue(copy.wasFreeCalled());
116: }
117:
118: public void testEquals() throws Exception {
119: MockClob clob1 = new MockClob("This is a Test Clob");
120: assertFalse(clob1.equals(null));
121: assertTrue(clob1.equals(clob1));
122: MockClob clob2 = new MockClob("This is another Test Clob");
123: assertFalse(clob1.equals(clob2));
124: assertFalse(clob2.equals(clob1));
125: clob2 = new MockClob("This is a Test Clob");
126: assertTrue(clob1.equals(clob2));
127: assertTrue(clob2.equals(clob1));
128: assertEquals(clob1.hashCode(), clob2.hashCode());
129: clob1.free();
130: assertFalse(clob1.equals(clob2));
131: assertFalse(clob2.equals(clob1));
132: }
133:
134: public void testClone() throws Exception {
135: MockClob cloneClob = (MockClob) clob.clone();
136: clob.setString(1, "Test");
137: assertEquals("Test is a Test Clob", clob.getSubString(1, 19));
138: assertEquals("This is a Test Clob", cloneClob.getSubString(1,
139: 19));
140: }
141:
142: public void testToString() throws Exception {
143: MockClob clob = new MockClob("");
144: assertEquals("Clob data: ", clob.toString());
145: clob = new MockClob("test");
146: assertEquals("Clob data: test", clob.toString());
147: }
148: }
|