001: /*
002: * $Id: BaseDataTypeTest.java,v 1.6 2005/05/02 22:32:03 ahimanikya 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.IOException;
048: import java.io.Serializable;
049:
050: import org.axiondb.BaseSerializableTest;
051: import org.axiondb.DataType;
052:
053: /**
054: * @version $Revision: 1.6 $ $Date: 2005/05/02 22:32:03 $
055: * @author Rodney Waldhoff
056: */
057: public abstract class BaseDataTypeTest extends BaseSerializableTest {
058:
059: //------------------------------------------------------------ Conventional
060:
061: public BaseDataTypeTest(String testName) {
062: super (testName);
063: }
064:
065: //---------------------------------------------------------------- Framework
066:
067: protected abstract DataType getDataType();
068:
069: protected final Serializable makeSerializable() {
070: return getDataType();
071: }
072:
073: protected void compareEm(Object[] values) throws Exception {
074: for (int i = 0; i < values.length; i++) {
075: for (int j = 0; j < values.length; j++) {
076: int result = getDataType()
077: .compare(values[i], values[j]);
078: if (i < j) {
079: assertTrue(values[i] + " should be less than "
080: + values[j] + " found " + result,
081: result < 0);
082: } else if (i == j) {
083: assertTrue(values[i] + " should be equal to "
084: + values[j] + " found " + result,
085: result == 0);
086: } else {
087: assertTrue(values[i] + " should be greater than "
088: + values[j] + " found " + result,
089: result > 0);
090: }
091: }
092: }
093: }
094:
095: //------------------------------------------------------------------- Tests
096:
097: public final void testBaseToString() throws Exception {
098: assertNotNull(getDataType().toString());
099: assertNull(getDataType().toString(null));
100: }
101:
102: public final void testAcceptsNull() throws Exception {
103: assertTrue("Should accept null", getDataType().accepts(null));
104: }
105:
106: public final void testConvertNullToNull() throws Exception {
107: assertTrue(null == getDataType().convert(null));
108: }
109:
110: public final void testHasComparator() throws Exception {
111: DataType type = getDataType();
112: if (type instanceof BaseDataType) {
113: assertNotNull(((BaseDataType) type).getComparator());
114: }
115: }
116:
117: public final void testWriteReadNull() throws Exception {
118: ByteArrayOutputStream buf = new ByteArrayOutputStream();
119: getDataType().write(null, new DataOutputStream(buf));
120: Object read = getDataType().read(
121: new DataInputStream(new ByteArrayInputStream(buf
122: .toByteArray())));
123: assertNull(read);
124: }
125:
126: public final void testWriteReadSeveralNulls() throws Exception {
127: ByteArrayOutputStream buf = new ByteArrayOutputStream();
128: getDataType().write(null, new DataOutputStream(buf));
129: getDataType().write(null, new DataOutputStream(buf));
130: getDataType().write(null, new DataOutputStream(buf));
131: DataInputStream in = new DataInputStream(
132: new ByteArrayInputStream(buf.toByteArray()));
133: assertNull(getDataType().read(in));
134: assertNull(getDataType().read(in));
135: assertNull(getDataType().read(in));
136: }
137:
138: public final void testPreferredValueClassNameIsNotNull()
139: throws Exception {
140: assertNotNull(getDataType().getPreferredValueClassName());
141: }
142:
143: public void testGetPrecision() throws Exception {
144: assertEquals(0, getDataType().getPrecision());
145: }
146:
147: protected void assertInvalidWriteFails(Object badValue) {
148: ByteArrayOutputStream bos = new ByteArrayOutputStream();
149: try {
150: getDataType().write(badValue, new DataOutputStream(bos));
151: fail("Expected IOException due to invalid write data");
152: } catch (IOException expected) {
153: // expected
154: }
155: }
156: }
|