001: /*
002: * $Id: TestBaseDataType.java,v 1.7 2005/05/03 17:55:16 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.DataInput;
044: import java.io.DataOutput;
045: import java.io.IOException;
046: import java.io.Serializable;
047: import java.sql.DatabaseMetaData;
048:
049: import junit.framework.Test;
050: import junit.framework.TestSuite;
051:
052: import org.axiondb.AxionException;
053: import org.axiondb.DataType;
054:
055: /**
056: * @version $Revision: 1.7 $ $Date: 2005/05/03 17:55:16 $
057: * @author Rodney Waldhoff
058: */
059: public class TestBaseDataType extends BaseDataTypeTest {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public TestBaseDataType(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() {
068: TestSuite suite = new TestSuite(TestBaseDataType.class);
069: return suite;
070: }
071:
072: //--------------------------------------------------------------- Framework
073:
074: protected DataType getDataType() {
075: return getBaseDataType();
076: }
077:
078: protected BaseDataType getBaseDataType() {
079: return new MockBaseDataTypeImpl();
080: }
081:
082: //------------------------------------------------------------------- Tests
083:
084: public void testGetPreferredValueClassName() {
085: assertEquals("java.lang.Object", getDataType()
086: .getPreferredValueClassName());
087: }
088:
089: public void testSupportsSuccessor() {
090: assertTrue(!getDataType().supportsSuccessor());
091: }
092:
093: public void testSuccessorIsUnsupported() {
094: try {
095: getDataType().successor(null);
096: fail("Expected UnsupportedOperationException");
097: } catch (UnsupportedOperationException e) {
098: // expected
099: }
100: }
101:
102: public void testNumberToNumber() throws Exception {
103: assertEquals(new Integer(1), getBaseDataType().toNumber(
104: new Integer(1)));
105: }
106:
107: public void testNullToBigDecimal() throws Exception {
108: assertNull(getDataType().toBigDecimal(null));
109: }
110:
111: public void testNullToBigInteger() throws Exception {
112: assertNull(getDataType().toBigInteger(null));
113: }
114:
115: public void testToByte() throws Exception {
116: assertEquals((byte) 3, getDataType().toByte(new Byte((byte) 3)));
117: }
118:
119: public void testToDate() throws Exception {
120: try {
121: getDataType().toDate(null);
122: fail("Expected AxionException");
123: } catch (AxionException e) {
124: // expected
125: }
126: }
127:
128: public void testToDouble() throws Exception {
129: assertEquals(3.14d, getDataType().toDouble(new Double(3.14d)),
130: 0.0d);
131: }
132:
133: public void testToFloat() throws Exception {
134: assertEquals(3.14f, getDataType().toFloat(new Double(3.14f)),
135: 0.0f);
136: }
137:
138: public void testToInt() throws Exception {
139: assertEquals(3, getDataType().toInt(new Integer(3)));
140: }
141:
142: public void testToLong() throws Exception {
143: assertEquals(3L, getDataType().toLong(new Long(3L)));
144: }
145:
146: public void testNullToString() throws Exception {
147: assertNull(getDataType().toString(null));
148: }
149:
150: public void testNonNullToString() throws Exception {
151: assertEquals("xyzzy", getDataType().toString("xyzzy"));
152: }
153:
154: public void testToTime() throws Exception {
155: try {
156: getDataType().toTime(null);
157: fail("Expected AxionException");
158: } catch (AxionException e) {
159: // expected
160: }
161: }
162:
163: public void testToTimestamp() throws Exception {
164: try {
165: getDataType().toTimestamp(null);
166: fail("Expected AxionException");
167: } catch (AxionException e) {
168: // expected
169: }
170: }
171:
172: public void testToClob() throws Exception {
173: assertNotNull(getDataType().toClob("xyzzy"));
174: }
175:
176: public void testToBlob() throws Exception {
177: assertNull(getDataType().toBlob(null));
178: byte[] xyzzy = "xyzzy".getBytes();
179: byte[] xyzzy1 = getDataType().toBlob(xyzzy).getBytes(0,
180: xyzzy.length);
181: for (int i = 0; i < xyzzy.length; i++) {
182: assertEquals(xyzzy[i], xyzzy1[i]);
183: }
184: }
185:
186: public void testGetLiteralPrefix() throws Exception {
187: assertNull(getDataType().getLiteralPrefix());
188: }
189:
190: public void testGetLiteralSuffix() throws Exception {
191: assertNull(getDataType().getLiteralSuffix());
192: }
193:
194: public void testGetNullableCode() throws Exception {
195: assertEquals(DatabaseMetaData.typeNullable, getDataType()
196: .getNullableCode());
197: }
198:
199: public void testIsCaseSensitive() throws Exception {
200: assertTrue(!getDataType().isCaseSensitive());
201: }
202: }
203:
204: class MockBaseDataTypeImpl extends BaseDataType implements Serializable {
205: public boolean accepts(Object value) {
206: return true;
207: }
208:
209: public Object convert(Object value) {
210: return value;
211: }
212:
213: public int getJdbcType() {
214: return 0;
215: }
216:
217: public DataType makeNewInstance() {
218: return new MockBaseDataTypeImpl();
219: }
220:
221: public Object read(DataInput in) throws IOException {
222: if (in.readBoolean()) {
223: return new Integer(1);
224: }
225: return null;
226: }
227:
228: public void write(Object value, DataOutput out) throws IOException {
229: if (null == value) {
230: out.writeBoolean(false);
231: } else {
232: out.writeBoolean(true);
233: }
234: }
235: }
|