001: /*
002: * $Id: TestVarBinaryType.java,v 1.5 2005/05/02 22:32:03 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 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.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.5 $ $Date: 2005/05/02 22:32:03 $
057: * @author Rodney Waldhoff
058: */
059: public class TestVarBinaryType extends BaseDataTypeTest {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public TestVarBinaryType(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() {
068: TestSuite suite = new TestSuite(TestVarBinaryType.class);
069: return suite;
070: }
071:
072: //--------------------------------------------------------------- Lifecycle
073:
074: public void setUp() throws Exception {
075: super .setUp();
076: _type = new VarBinaryType(SIZE);
077: }
078:
079: public void tearDown() throws Exception {
080: super .tearDown();
081: _type = null;
082: }
083:
084: //------------------------------------------------------------------- Super
085:
086: protected DataType getDataType() {
087: return _type;
088: }
089:
090: //------------------------------------------------------------------- Tests
091:
092: public void testGetPrecision() throws Exception {
093: assertEquals(SIZE, getDataType().getPrecision());
094: assertEquals(10, getDataType().getPrecisionRadix());
095: }
096:
097: public void testAccepts() throws Exception {
098: // currently VarBinaryType accepts everything, because it drops back to toString().getBytes()
099: assertTrue(
100: "Should accept byte[]",
101: _type
102: .accepts("the quick brown fox jumped over the lazy dogs"
103: .getBytes()));
104: assertTrue(
105: "Should accept String",
106: _type
107: .accepts("the quick brown fox jumped over the lazy dogs"));
108: assertTrue("accepts null", _type.accepts(null));
109: }
110:
111: public void testConvert() throws Exception {
112: assertNull(_type.convert(null));
113: assertTrue(Arrays.equals("The quick brown fox".getBytes(),
114: (byte[]) _type.convert("The quick brown fox")));
115: assertTrue(Arrays.equals("17".getBytes(), (byte[]) _type
116: .convert(new Integer(17))));
117: assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) -1 },
118: (byte[]) _type
119: .convert(new byte[] { (byte) 1, (byte) -1 })));
120: }
121:
122: public void testWriteReadNonNull() throws Exception {
123: byte[] orig = "The quick brown fox jumped over the lazy dogs."
124: .getBytes();
125: ByteArrayOutputStream buf = new ByteArrayOutputStream();
126: _type.write(orig, new DataOutputStream(buf));
127: Object read = _type.read(new DataInputStream(
128: new ByteArrayInputStream(buf.toByteArray())));
129: assertTrue(Arrays.equals(orig, (byte[]) read));
130: }
131:
132: public void testCompare() throws Exception {
133: byte[][] list = new byte[][] { new byte[0],
134: new byte[] { Byte.MIN_VALUE },
135: new byte[] { Byte.MIN_VALUE, Byte.MIN_VALUE },
136: new byte[] { Byte.MIN_VALUE, -1 },
137: new byte[] { Byte.MIN_VALUE, 0 },
138: new byte[] { Byte.MIN_VALUE, 1 },
139: new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE },
140: new byte[] { -1 }, new byte[] { -1, Byte.MIN_VALUE },
141: new byte[] { -1, -1 }, new byte[] { -1, 0 },
142: new byte[] { -1, 1 },
143: new byte[] { -1, Byte.MAX_VALUE }, new byte[] { 0 },
144: new byte[] { 0, Byte.MIN_VALUE }, new byte[] { 0, -1 },
145: new byte[] { 0, 0 }, new byte[] { 0, 1 },
146: new byte[] { 0, Byte.MAX_VALUE }, new byte[] { 1 },
147: new byte[] { 1, Byte.MIN_VALUE }, new byte[] { 1, -1 },
148: new byte[] { 1, 0 }, new byte[] { 1, 1 },
149: new byte[] { 1, Byte.MAX_VALUE },
150: new byte[] { Byte.MAX_VALUE },
151: new byte[] { Byte.MAX_VALUE, Byte.MIN_VALUE },
152: new byte[] { Byte.MAX_VALUE, -1 },
153: new byte[] { Byte.MAX_VALUE, 0 },
154: new byte[] { Byte.MAX_VALUE, 1 },
155: new byte[] { Byte.MAX_VALUE, Byte.MAX_VALUE }, };
156: for (int i = 0; i < list.length; i++) {
157: for (int j = 0; j < list.length; j++) {
158: if (i == j) {
159: assertEquals(tos(list[i]) + " = " + tos(list[j]),
160: 0, _type.compare(list[i], list[j]));
161: } else if (i < j) {
162: assertTrue(tos(list[i]) + " < " + tos(list[j]),
163: _type.compare(list[i], list[j]) < 0);
164: } else if (i > j) {
165: assertTrue(tos(list[i]) + " > " + tos(list[j]),
166: _type.compare(list[i], list[j]) > 0);
167: }
168: }
169: }
170: }
171:
172: public void testSuccessor() throws Exception {
173: {
174: byte[] v = new byte[] { (byte) 0 };
175: for (int i = 0; i < 10; i++) {
176: assertEquals(i, v[0]);
177: byte[] w = (byte[]) _type.successor(v);
178: assertTrue(_type.compare(v, w) < 0);
179: assertEquals(i + 1, w[0]);
180: v = w;
181: }
182: }
183: {
184: byte[] v = new byte[] { (byte) 0, (byte) 0 };
185: for (int i = 0; i < 10; i++) {
186: assertEquals(0, v[0]);
187: assertEquals(i, v[1]);
188: byte[] w = (byte[]) _type.successor(v);
189: assertTrue(_type.compare(v, w) < 0);
190: assertEquals(0, w[0]);
191: assertEquals(i + 1, w[1]);
192: v = w;
193: }
194: }
195:
196: {
197: byte[] v = new byte[] { (byte) 0, Byte.MAX_VALUE };
198: byte[] expected = new byte[] { (byte) 0, Byte.MAX_VALUE,
199: Byte.MIN_VALUE };
200: Arrays.equals(expected, (byte[]) _type.successor(v));
201: assertTrue(_type.compare(v, expected) < 0);
202: }
203:
204: Arrays.equals(new byte[] { Byte.MIN_VALUE }, (byte[]) _type
205: .successor(new byte[0]));
206: }
207:
208: public void testSupportsSuccessor() throws Exception {
209: assertTrue(_type.supportsSuccessor());
210: }
211:
212: public void testRangeCheck() throws Exception {
213: byte[] badArray = new byte[SIZE + 1];
214: for (int i = 0; i < badArray.length; i++) {
215: badArray[i] = (byte) (i % 10);
216: }
217:
218: final String msg = "Expected AxionException (22001) - string data, right truncation";
219: try {
220: _type.convert(badArray);
221: fail(msg);
222: } catch (AxionException expected) {
223: assertEquals(msg, "22001", expected.getSQLState());
224: }
225: }
226:
227: public void testWriteReadSeveral() throws Exception {
228: Object[] data = { null, new byte[0], null, "quick".getBytes(),
229: new byte[10],
230: new byte[] { (byte) 1, (byte) 2, (byte) -1 },
231: "brown ".getBytes(), " fox".getBytes(), null };
232: ByteArrayOutputStream out = new ByteArrayOutputStream();
233: for (int i = 0; i < data.length; i++) {
234: _type.write(data[i], new DataOutputStream(out));
235: }
236: DataInputStream in = new DataInputStream(
237: new ByteArrayInputStream(out.toByteArray()));
238: for (int i = 0; i < data.length; i++) {
239: Object read = _type.read(in);
240: if (null == data[i]) {
241: assertNull(read);
242: } else {
243: Arrays.equals((byte[]) data[i], (byte[]) read);
244: }
245: }
246: }
247:
248: public void testInvalidWriteFails() throws Exception {
249: byte[] badArray = new byte[SIZE + 1];
250: for (int i = 0; i < badArray.length; i++) {
251: badArray[i] = (byte) (i % 10);
252: }
253:
254: assertInvalidWriteFails(badArray);
255: }
256:
257: public void testToString() throws Exception {
258: assertEquals("the quick brown fox", _type
259: .toString("the quick brown fox"));
260: assertEquals("the quick brown fox", _type
261: .toString("the quick brown fox".getBytes()));
262: }
263:
264: public void testGetColumnDisplaySize() {
265: assertEquals(Integer.MAX_VALUE, _type.getColumnDisplaySize());
266: }
267:
268: private String tos(byte[] val) {
269: StringBuffer buf = new StringBuffer();
270: buf.append("[");
271: for (int i = 0; i < val.length; i++) {
272: if (i != 0) {
273: buf.append(",");
274: }
275: buf.append(i);
276: }
277: buf.append("]");
278: return buf.toString();
279: }
280:
281: private static final int SIZE = 46;
282: private DataType _type = null;
283: }
|