001: package com.mockrunner.test.jdbc;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005: import java.io.Reader;
006: import java.io.StringReader;
007: import java.util.Arrays;
008: import java.util.Date;
009:
010: import junit.framework.TestCase;
011:
012: import com.mockrunner.jdbc.ParameterUtil;
013: import com.mockrunner.mock.jdbc.MockArray;
014: import com.mockrunner.mock.jdbc.MockBlob;
015: import com.mockrunner.mock.jdbc.MockClob;
016: import com.mockrunner.mock.jdbc.MockRef;
017: import com.mockrunner.mock.jdbc.MockResultSet;
018: import com.mockrunner.mock.jdbc.MockStruct;
019: import com.mockrunner.util.common.StreamUtil;
020:
021: public class ParameterUtilTest extends TestCase {
022: public void testCompareParameter() {
023: assertTrue(ParameterUtil.compareParameter(null, null));
024: assertFalse(ParameterUtil.compareParameter("test", null));
025: assertTrue(ParameterUtil.compareParameter("test", "test"));
026: assertFalse(ParameterUtil.compareParameter(new Double(1),
027: new Double(2)));
028: assertTrue(ParameterUtil.compareParameter(
029: new byte[] { 1, 2, 3 }, new byte[] { 1, 2, 3 }));
030: assertFalse(ParameterUtil.compareParameter(
031: new byte[] { 1, 2, 4 }, new byte[] { 1, 2, 3 }));
032: assertTrue(ParameterUtil.compareParameter(
033: new ByteArrayInputStream(new byte[] { 1, 2, 3 }),
034: new ByteArrayInputStream(new byte[] { 1, 2, 3 })));
035: assertTrue(ParameterUtil.compareParameter(new StringReader(
036: "xyz"), new StringReader("xyz")));
037: assertFalse(ParameterUtil.compareParameter(new StringReader(
038: "xyz"), new StringReader("")));
039: assertFalse(ParameterUtil.compareParameter(new Object(),
040: new Object()));
041: assertTrue(ParameterUtil.compareParameter(new MockRef("test"),
042: new MockRef("test")));
043: assertFalse(ParameterUtil.compareParameter(new MockRef(""),
044: new MockRef("xyz")));
045: assertTrue(ParameterUtil.compareParameter(new MockArray(
046: new String[] { "", "" }), new MockArray(new String[] {
047: "", "" })));
048: assertFalse(ParameterUtil.compareParameter(new MockArray(
049: new String[] { "", "" }), new MockArray(new int[] { 1,
050: 1 })));
051: assertTrue(ParameterUtil.compareParameter(new MockArray(
052: new Object[] { "1", new Integer(2) }), new MockArray(
053: new Object[] { "1", new Integer(2) })));
054: assertFalse(ParameterUtil.compareParameter(new MockArray(
055: new Object[] { new Integer(2), "1" }), new MockArray(
056: new Object[] { "1", new Integer(2) })));
057: assertTrue(ParameterUtil.compareParameter(new MockBlob(
058: new byte[] { 1, 1, 1 }), new MockBlob(new byte[] { 1,
059: 1, 1 })));
060: assertFalse(ParameterUtil.compareParameter(new MockBlob(
061: new byte[] { 1, 1, 1 }), new MockBlob(
062: new byte[] { 1, 1 })));
063: assertTrue(ParameterUtil.compareParameter(new MockBlob(
064: new byte[] {}), new MockBlob(new byte[] {})));
065: assertFalse(ParameterUtil.compareParameter(new MockBlob(
066: new byte[] {}), new MockBlob(new byte[] { 2 })));
067: assertFalse(ParameterUtil.compareParameter(new MockBlob(
068: new byte[] { 1, 2, 3 }), new MockBlob(new byte[] { 1,
069: 2, 4 })));
070: assertTrue(ParameterUtil.compareParameter(new MockClob("123"),
071: new MockClob("123")));
072: assertTrue(ParameterUtil.compareParameter(new MockClob(""),
073: new MockClob("")));
074: assertFalse(ParameterUtil.compareParameter(new MockClob("1"),
075: new MockClob("")));
076: assertTrue(ParameterUtil.compareParameter(new MockStruct(""),
077: new MockStruct("")));
078: assertTrue(ParameterUtil.compareParameter(
079: new MockStruct("123"), new MockStruct("123")));
080: assertFalse(ParameterUtil.compareParameter(
081: new MockStruct("123"), new MockStruct("")));
082: MockStruct struct = new MockStruct("123");
083: struct.addAttribute("test");
084: assertTrue(ParameterUtil.compareParameter(struct, struct));
085: assertFalse(ParameterUtil.compareParameter(struct,
086: new MockStruct("123")));
087: MockStruct anotherStruct = new MockStruct("123");
088: anotherStruct.addAttribute("test");
089: assertTrue(ParameterUtil
090: .compareParameter(struct, anotherStruct));
091: anotherStruct.addAttribute(new Integer(2));
092: assertFalse(ParameterUtil.compareParameter(struct,
093: anotherStruct));
094: struct.addAttribute(new Integer(2));
095: assertTrue(ParameterUtil
096: .compareParameter(struct, anotherStruct));
097: MockResultSet resultSet1 = new MockResultSet("id");
098: resultSet1.addRow(new Object[] { "1", new Integer(1) });
099: resultSet1.addRow(new Object[] { "2", new Integer(2) });
100: resultSet1.addRow(new Object[] { "3", new Integer(3) });
101: MockResultSet resultSet2 = new MockResultSet("id");
102: resultSet2.addRow(new Object[] { "1", new Integer(1) });
103: resultSet2.addRow(new Object[] { "2", new Integer(2) });
104: resultSet2.addRow(new Object[] { "3", new Integer(3) });
105: assertTrue(ParameterUtil.compareParameter(resultSet1,
106: resultSet1));
107: assertTrue(ParameterUtil.compareParameter(resultSet1,
108: resultSet2));
109: resultSet2.addRow(new Object[] { "4", new Integer(4) });
110: assertFalse(ParameterUtil.compareParameter(resultSet1,
111: resultSet2));
112: }
113:
114: public void testCopyParameter() {
115: String testString = new String("Test");
116: String copyString = (String) ParameterUtil
117: .copyParameter(testString);
118: assertTrue(testString == copyString);
119: byte[] testArray = new byte[] { 1, 2, 3 };
120: byte[] copyArray = (byte[]) ParameterUtil
121: .copyParameter(testArray);
122: assertFalse(testArray == copyArray);
123: assertTrue(Arrays.equals(testArray, copyArray));
124: Reader testReader = new StringReader("Test");
125: Reader copyReader = (Reader) ParameterUtil
126: .copyParameter(testReader);
127: assertFalse(testReader == copyReader);
128: assertTrue(StreamUtil.compareReaders(testReader, copyReader));
129: InputStream testStream = new ByteArrayInputStream(new byte[] {
130: 1, 2, 3 });
131: InputStream copyStream = (InputStream) ParameterUtil
132: .copyParameter(testStream);
133: assertFalse(testStream == copyStream);
134: assertTrue(StreamUtil.compareStreams(testStream, copyStream));
135: MockClob testClob = new MockClob("Test");
136: MockClob copyClob = (MockClob) ParameterUtil
137: .copyParameter(testClob);
138: assertFalse(testClob == copyClob);
139: assertTrue(ParameterUtil.compareParameter(testClob, copyClob));
140: MockArray testMockArray = new MockArray(new String[] { "", "" });
141: MockArray copyMockArray = (MockArray) ParameterUtil
142: .copyParameter(testMockArray);
143: assertFalse(testMockArray == copyMockArray);
144: assertTrue(ParameterUtil.compareParameter(testMockArray,
145: copyMockArray));
146: Date testDate = new Date();
147: Date copyDate = (Date) ParameterUtil.copyParameter(testDate);
148: assertNotSame(testDate, copyDate);
149: assertEquals(testDate, copyDate);
150: TestParameter parameter = new TestParameter();
151: TestParameter copyParameter = (TestParameter) ParameterUtil
152: .copyParameter(parameter);
153: assertSame(parameter, copyParameter);
154: }
155:
156: public static class TestParameter implements Cloneable {
157: public Object clone() throws CloneNotSupportedException {
158: throw new RuntimeException("OOPPSS");
159: }
160: }
161: }
|