001: /*
002: * $Id: TestCharacterType.java,v 1.7 2005/05/02 22:32:03 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 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.util.Arrays;
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/02 22:32:03 $
057: * @author Rodney Waldhoff
058: */
059: public class TestCharacterType extends BaseDataTypeTest {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public TestCharacterType(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() {
068: TestSuite suite = new TestSuite(TestCharacterType.class);
069: return suite;
070: }
071:
072: //--------------------------------------------------------------- Lifecycle
073:
074: private DataType type = null;
075:
076: public void setUp() throws Exception {
077: super .setUp();
078: type = new CharacterType(SIZE);
079: }
080:
081: public void tearDown() throws Exception {
082: super .tearDown();
083: type = null;
084: }
085:
086: //------------------------------------------------------------------- Super
087:
088: protected DataType getDataType() {
089: return type;
090: }
091:
092: //------------------------------------------------------------------- Tests
093:
094: public void testGetPrecision() throws Exception {
095: assertEquals(SIZE, getDataType().getPrecision());
096:
097: DataType myType = new CharacterType();
098: assertEquals(1, myType.getPrecision());
099: }
100:
101: public void testAccepts() throws Exception {
102: assertTrue("Should accept Integer", type
103: .accepts(new Integer(3)));
104: assertTrue("Should accept Character", type
105: .accepts(new Character('c')));
106: assertTrue("Should accept null Character", type
107: .accepts(new Character('\0')));
108: assertTrue("Should accept \"T\"", type.accepts("T"));
109: assertTrue("Should accept \"TRUE\"", type.accepts("TRUE"));
110: }
111:
112: public void testWriteReadNonNull() throws Exception {
113: ByteArrayOutputStream buf = new ByteArrayOutputStream();
114: String value = "c";
115: type.write(type.convert(value), new DataOutputStream(buf));
116: Object read = type.read(new DataInputStream(
117: new ByteArrayInputStream(buf.toByteArray())));
118: assertEquals(padValue(value, SIZE), read);
119: }
120:
121: public void testSuccessor() throws Exception {
122: assertTrue(type.supportsSuccessor());
123: }
124:
125: public void testWriteReadSeveral() throws Exception {
126: Object[] data = { null, "c", null, "C", "1234", "1234567890",
127: null, "1234567890 " };
128: ByteArrayOutputStream out = new ByteArrayOutputStream();
129: for (int i = 0; i < data.length; i++) {
130: type
131: .write(type.convert(data[i]), new DataOutputStream(
132: out));
133: }
134: DataInputStream in = new DataInputStream(
135: new ByteArrayInputStream(out.toByteArray()));
136: for (int i = 0; i < data.length; i++) {
137: Object read = type.read(in);
138: if (null == data[i]) {
139: assertNull(read);
140: } else {
141: assertEquals(type.convert(data[i]), read);
142: assertEquals(SIZE, read.toString().length());
143: }
144: }
145: }
146:
147: public void testImplicitTruncate() throws Exception {
148: final String rawValue = "1234567890 ";
149: final String expectedValue = rawValue.trim(); // should be exactly 10 chars in length
150:
151: final String rawValue2 = "12345 ";
152: final String expectedValue2 = "12345 "; // should be padded to 10 chars
153:
154: final String badValue = "1234567890123"; // plainly exceeds type length of 10
155: final String badValue2 = "12345678901 "; // even after truncating length is exceeded
156:
157: assertEquals(expectedValue, type.convert(rawValue));
158: assertEquals(expectedValue2, type.convert(rawValue2));
159:
160: final String msg = "Expected AxionException (22001) - data exception: string data, right truncation";
161: try {
162: type.convert(badValue);
163: fail(msg);
164: } catch (AxionException expected) {
165: assertEquals(msg, "22001", expected.getSQLState());
166: }
167:
168: try {
169: type.convert(badValue2);
170: fail(msg);
171: } catch (AxionException expected) {
172: assertEquals(msg, "22001", expected.getSQLState());
173: }
174: }
175:
176: public void testToByteArray() throws Exception {
177: Integer rawValue = new Integer(123);
178: byte[] expectedValue = "123 ".getBytes();
179:
180: assertTrue(Arrays.equals(expectedValue, type
181: .toByteArray(rawValue)));
182: }
183:
184: private String padValue(String raw, int fieldSize) {
185: StringBuffer buf = new StringBuffer(raw);
186: for (int i = 0; i < fieldSize - raw.length(); i++) {
187: buf.append(" ");
188: }
189: return buf.toString();
190: }
191:
192: private static final int SIZE = 10;
193: }
|