001: package com.mockrunner.test.connector;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.util.Arrays;
006:
007: import junit.framework.TestCase;
008:
009: import com.mockrunner.mock.connector.cci.MockStreamableByteArrayRecord;
010:
011: public class MockStreamableByteArrayRecordTest extends TestCase {
012: private MockStreamableByteArrayRecord record;
013:
014: protected void setUp() throws Exception {
015: record = new MockStreamableByteArrayRecord();
016: }
017:
018: protected void tearDown() throws Exception {
019: record = null;
020: }
021:
022: public void testSetAndGetContent() {
023: assertNull(record.getContent());
024: record.setContent(null);
025: assertNull(record.getContent());
026: record.setContent(new byte[0]);
027: assertTrue(Arrays.equals(new byte[0], record.getContent()));
028: byte[] content = new byte[] { 1, 2, 3 };
029: record.setContent(content);
030: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, record
031: .getContent()));
032: content[0] = 3;
033: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, record
034: .getContent()));
035: content = record.getContent();
036: content[0] = 3;
037: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, record
038: .getContent()));
039: record.setContent(null);
040: assertNull(record.getContent());
041: }
042:
043: public void testRead() throws Exception {
044: record.read(null);
045: assertNull(record.getContent());
046: record.read(new ByteArrayInputStream(new byte[0]));
047: assertTrue(Arrays.equals(new byte[0], record.getContent()));
048: record.read(new ByteArrayInputStream(new byte[] { 1, 2, 3 }));
049: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, record
050: .getContent()));
051: record.read(null);
052: assertNull(record.getContent());
053: }
054:
055: public void testWrite() throws Exception {
056: ByteArrayOutputStream output = new ByteArrayOutputStream();
057: record.write(output);
058: assertEquals(0, output.size());
059: record.setContent(new byte[0]);
060: record.write(output);
061: assertEquals(0, output.size());
062: record.setContent(new byte[] { 1, 2, 3 });
063: record.write(output);
064: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, output
065: .toByteArray()));
066: }
067:
068: public void testEquals() throws Exception {
069: assertFalse(record.equals(null));
070: assertFalse(record.equals("abc"));
071: assertTrue(record.equals(record));
072: record.setRecordName("1");
073: record.setRecordShortDescription("2");
074: MockStreamableByteArrayRecord other = new MockStreamableByteArrayRecord();
075: assertFalse(record.equals(other));
076: other.setRecordName("1");
077: assertFalse(record.equals(other));
078: other.setRecordShortDescription("2");
079: assertTrue(record.equals(other));
080: record.setContent(new byte[] { 1, 2, 3 });
081: assertFalse(record.equals(other));
082: other.read(new ByteArrayInputStream(new byte[] { 1, 2, 3 }));
083: assertTrue(record.equals(other));
084: assertTrue(other.equals(record));
085: assertEquals(other.hashCode(), record.hashCode());
086: other.setContent(null);
087: assertFalse(record.equals(other));
088: assertFalse(other.equals(record));
089: record.setContent(null);
090: assertTrue(record.equals(other));
091: assertTrue(other.equals(record));
092: assertEquals(other.hashCode(), record.hashCode());
093: }
094:
095: public void testClone() {
096: record.setRecordName("1");
097: record.setRecordShortDescription("2");
098: MockStreamableByteArrayRecord clone = (MockStreamableByteArrayRecord) record
099: .clone();
100: assertEquals("1", clone.getRecordName());
101: assertEquals("2", clone.getRecordShortDescription());
102: assertNull(clone.getContent());
103: record.setContent(new byte[0]);
104: clone = (MockStreamableByteArrayRecord) record.clone();
105: assertEquals("1", clone.getRecordName());
106: assertEquals("2", clone.getRecordShortDescription());
107: assertTrue(Arrays.equals(new byte[0], clone.getContent()));
108: record.setContent(new byte[] { 1, 2, 3 });
109: clone = (MockStreamableByteArrayRecord) record.clone();
110: assertEquals("1", clone.getRecordName());
111: assertEquals("2", clone.getRecordShortDescription());
112: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, clone
113: .getContent()));
114: }
115: }
|