001: package com.mockrunner.test.jdbc;
002:
003: import java.sql.SQLException;
004: import java.util.Arrays;
005:
006: import junit.framework.TestCase;
007:
008: import com.mockrunner.mock.jdbc.MockArray;
009: import com.mockrunner.mock.jdbc.MockResultSet;
010:
011: public class MockArrayTest extends TestCase {
012: private MockArray stringArray;
013: private MockArray byteArray;
014:
015: protected void setUp() throws Exception {
016: super .setUp();
017: stringArray = new MockArray(new String[] { "This", "is", "a",
018: "test", "array" });
019: byteArray = new MockArray(new byte[] { 1, 2, 3, 4, 5, 6, 7 });
020: }
021:
022: public void testGetArray() throws Exception {
023: Object array = stringArray.getArray();
024: assertTrue(array instanceof String[]);
025: assertTrue(Arrays.equals((String[]) array, new String[] {
026: "This", "is", "a", "test", "array" }));
027: array = stringArray.getArray(2, 3);
028: assertTrue(array instanceof String[]);
029: assertTrue(Arrays.equals((String[]) array, new String[] { "is",
030: "a", "test" }));
031: array = byteArray.getArray(1, 7);
032: assertTrue(array instanceof byte[]);
033: assertTrue(Arrays.equals((byte[]) array, new byte[] { 1, 2, 3,
034: 4, 5, 6, 7 }));
035: array = byteArray.getArray(1, 1);
036: assertTrue(array instanceof byte[]);
037: assertTrue(Arrays.equals((byte[]) array, new byte[] { 1 }));
038: }
039:
040: public void testGetResultSet() throws Exception {
041: MockResultSet resultSet = (MockResultSet) stringArray
042: .getResultSet();
043: assertEquals(5, resultSet.getRowCount());
044: resultSet.next();
045: assertEquals(1, resultSet.getInt(1));
046: assertEquals("This", resultSet.getString(2));
047: resultSet.next();
048: assertEquals(2, resultSet.getInt(1));
049: assertEquals("is", resultSet.getString(2));
050: resultSet.next();
051: assertEquals(3, resultSet.getInt(1));
052: assertEquals("a", resultSet.getString(2));
053: resultSet.next();
054: assertEquals(4, resultSet.getInt(1));
055: assertEquals("test", resultSet.getString(2));
056: resultSet.next();
057: assertEquals(5, resultSet.getInt(1));
058: assertEquals("array", resultSet.getString(2));
059: resultSet = (MockResultSet) byteArray.getResultSet(3, 2);
060: assertEquals(2, resultSet.getRowCount());
061: resultSet.next();
062: assertEquals(1, resultSet.getInt(1));
063: assertEquals(3, resultSet.getByte(2));
064: resultSet.next();
065: assertEquals(2, resultSet.getInt(1));
066: assertEquals(4, resultSet.getByte(2));
067: }
068:
069: public void testFree() throws Exception {
070: assertFalse(byteArray.wasFreeCalled());
071: byteArray.free();
072: assertTrue(byteArray.wasFreeCalled());
073: try {
074: byteArray.getArray();
075: fail();
076: } catch (SQLException exc) {
077: //expected exception
078: }
079: try {
080: byteArray.getResultSet(1, 1);
081: fail();
082: } catch (SQLException exc) {
083: //expected exception
084: }
085: try {
086: byteArray.getBaseType();
087: fail();
088: } catch (SQLException exc) {
089: //expected exception
090: }
091: MockArray copy = (MockArray) byteArray.clone();
092: assertTrue(copy.wasFreeCalled());
093: }
094:
095: public void testEquals() throws Exception {
096: MockArray stringArray1 = new MockArray(
097: new String[] { "1", "2" });
098: assertFalse(stringArray1.equals(null));
099: assertTrue(stringArray1.equals(stringArray1));
100: MockArray stringArray2 = new MockArray(new String[] { "1", "2",
101: "3" });
102: assertFalse(stringArray1.equals(stringArray2));
103: assertFalse(stringArray2.equals(stringArray1));
104: stringArray2 = new MockArray(new String[] { "1", "2" });
105: assertTrue(stringArray1.equals(stringArray2));
106: assertTrue(stringArray2.equals(stringArray1));
107: assertEquals(stringArray1.hashCode(), stringArray2.hashCode());
108: stringArray2.setBaseTypeName(null);
109: assertFalse(stringArray1.equals(stringArray2));
110: assertFalse(stringArray2.equals(stringArray1));
111: stringArray1.setBaseTypeName(null);
112: assertTrue(stringArray1.equals(stringArray2));
113: assertTrue(stringArray2.equals(stringArray1));
114: assertEquals(stringArray1.hashCode(), stringArray2.hashCode());
115: stringArray1.setBaseTypeName("xyz");
116: stringArray2.setBaseTypeName("xyz1");
117: assertFalse(stringArray1.equals(stringArray2));
118: assertFalse(stringArray2.equals(stringArray1));
119: stringArray2.setBaseTypeName("xyz");
120: assertTrue(stringArray1.equals(stringArray2));
121: assertTrue(stringArray2.equals(stringArray1));
122: assertEquals(stringArray1.hashCode(), stringArray2.hashCode());
123: stringArray1.setBaseType(5);
124: assertFalse(stringArray1.equals(stringArray2));
125: assertFalse(stringArray2.equals(stringArray1));
126: stringArray2.setBaseType(5);
127: assertTrue(stringArray1.equals(stringArray2));
128: assertTrue(stringArray2.equals(stringArray1));
129: assertEquals(stringArray1.hashCode(), stringArray2.hashCode());
130: stringArray1.free();
131: assertFalse(stringArray1.equals(stringArray2));
132: assertFalse(stringArray2.equals(stringArray1));
133: }
134:
135: public void testClone() throws Exception {
136: MockArray copy = (MockArray) stringArray.clone();
137: assertTrue(Arrays.equals((Object[]) stringArray.getArray(),
138: (Object[]) copy.getArray()));
139: String[] array = (String[]) stringArray.getArray();
140: array[0] = "Test";
141: assertFalse(Arrays.equals((Object[]) stringArray.getArray(),
142: (Object[]) copy.getArray()));
143: }
144:
145: public void testToString() throws Exception {
146: MockArray array = new MockArray(new Object[0]);
147: assertEquals("Array data: []", array.toString());
148: array = new MockArray(new Object[] { "1" });
149: assertEquals("Array data: [1]", array.toString());
150: array = new MockArray(new Object[] { "1", new Integer(1) });
151: assertEquals("Array data: [1, 1]", array.toString());
152: }
153: }
|