001: /*
002: * $Id: TestObjectType.java,v 1.3.10.1 2005/04/29 22:08:23 jgiron Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.ByteArrayInputStream;
044: import java.io.ByteArrayOutputStream;
045: import java.io.DataInputStream;
046: import java.io.DataOutputStream;
047: import java.io.ObjectInputStream;
048: import java.io.ObjectOutput;
049: import java.io.ObjectOutputStream;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053:
054: import org.axiondb.DataType;
055: import org.axiondb.Person;
056:
057: /**
058: * @version $Revision: 1.3.10.1 $ $Date: 2005/04/29 22:08:23 $
059: * @author James Strachan
060: */
061: public class TestObjectType extends BaseDataTypeTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestObjectType(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(TestObjectType.class);
071: return suite;
072: }
073:
074: //--------------------------------------------------------------- Lifecycle
075:
076: private DataType type = null;
077:
078: public void setUp() throws Exception {
079: super .setUp();
080: type = new ObjectType();
081: }
082:
083: public void tearDown() throws Exception {
084: super .tearDown();
085: type = null;
086: }
087:
088: //------------------------------------------------------------------- Super
089:
090: protected DataType getDataType() {
091: return type;
092: }
093:
094: //------------------------------------------------------------------- Tests
095:
096: public void testSupportsSuccessor() throws Exception {
097: assertTrue(!type.supportsSuccessor());
098: }
099:
100: public void testAccepts() throws Exception {
101: assertTrue("Should accept Integer", type
102: .accepts(new Integer(3)));
103: assertTrue("Should accept String", type.accepts("3.14159"));
104: assertTrue("Should accept Person", type.accepts(new Person(
105: "James", 1969)));
106: assertTrue("Should not accept non-serializable", !type
107: .accepts(new Object()));
108: }
109:
110: public void testConvertUnacceptable() throws Exception {
111: try {
112: type.convert(new Object());
113: fail("Expected IllegalArgumentException");
114: } catch (IllegalArgumentException e) {
115: // expected
116: }
117: }
118:
119: public void testConvertAccetable() throws Exception {
120: assertNotNull(type.convert(new Person("James", 1969)));
121: }
122:
123: public void testSuccessor() throws Exception {
124: try {
125: type.successor("xyzzy");
126: fail("Expected UnsupportedOperationException");
127: } catch (UnsupportedOperationException e) {
128: // expected
129: }
130: }
131:
132: public void testWriteReadNonNull() throws Exception {
133: Object orig = new Person("James", 1969);
134: ByteArrayOutputStream buf = new ByteArrayOutputStream();
135: type.write(orig, new DataOutputStream(buf));
136: Object read = type.read(new DataInputStream(
137: new ByteArrayInputStream(buf.toByteArray())));
138: assertEquals(orig, read);
139: }
140:
141: public void testWriteReadNonNull2() throws Exception {
142: Object orig = "testing";
143: ByteArrayOutputStream buf = new ByteArrayOutputStream();
144: ObjectOutput out = new ObjectOutputStream(buf);
145: type.write(orig, out);
146: out.flush();
147: out.close();
148: Object read = type.read(new ObjectInputStream(
149: new ByteArrayInputStream(buf.toByteArray())));
150: assertEquals(orig, read);
151: }
152:
153: public void testWriteReadSeveral() throws Exception {
154: Object[] data = { null, new Person("James", 1969), null,
155: new Person("Simon", 1971), new Person("Lisa", 1974),
156: null };
157: ByteArrayOutputStream out = new ByteArrayOutputStream();
158: for (int i = 0; i < data.length; i++) {
159: type.write(data[i], new DataOutputStream(out));
160: }
161: DataInputStream in = new DataInputStream(
162: new ByteArrayInputStream(out.toByteArray()));
163: for (int i = 0; i < data.length; i++) {
164: Object read = type.read(in);
165: if (null == data[i]) {
166: assertNull(read);
167: } else {
168: assertEquals(data[i], read);
169: }
170: }
171: }
172:
173: public void testGetColumnDisplaySize() {
174: assertEquals(0, type.getColumnDisplaySize());
175: }
176: }
|