001: /*
002: * $Id: TestBooleanType.java,v 1.8 2005/06/18 01:03:45 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 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.util.Arrays;
049: import java.util.Comparator;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053:
054: import org.axiondb.AxionException;
055: import org.axiondb.DataType;
056:
057: /**
058: * @version $Revision: 1.8 $ $Date: 2005/06/18 01:03:45 $
059: * @author Rodney Waldhoff
060: * @author Jonathan Giron
061: */
062: public class TestBooleanType extends BaseDataTypeTest {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestBooleanType(String testName) {
067: super (testName);
068: }
069:
070: public static Test suite() {
071: TestSuite suite = new TestSuite(TestBooleanType.class);
072: return suite;
073: }
074:
075: //--------------------------------------------------------------- Lifecycle
076:
077: private DataType type = null;
078:
079: public void setUp() throws Exception {
080: super .setUp();
081: type = new BooleanType();
082: }
083:
084: public void tearDown() throws Exception {
085: super .tearDown();
086: type = null;
087: }
088:
089: //------------------------------------------------------------------- Super
090:
091: protected DataType getDataType() {
092: return type;
093: }
094:
095: //------------------------------------------------------------------- Tests
096:
097: public void testGetPrecision() throws Exception {
098: assertEquals(1, getDataType().getPrecision());
099: }
100:
101: public void testAccepts() throws Exception {
102: assertTrue("Should not accept Integer", !type
103: .accepts(new Integer(3)));
104: assertTrue("Should accept Boolean.TRUE", type
105: .accepts(Boolean.TRUE));
106: assertTrue("Should accept Boolean.FALSE", type
107: .accepts(Boolean.FALSE));
108: assertTrue("Should accept \"TRUE\"", type.accepts("TRUE"));
109: assertTrue("Should accept \"true\"", type.accepts("true"));
110: assertTrue("Should accept \"FALSE\"", type.accepts("FALSE"));
111: assertTrue("Should accept \"false\"", type.accepts("false"));
112: assertTrue("Should not accept unknown object", !type
113: .accepts(new Object()));
114: }
115:
116: public void testToBoolean() throws Exception {
117: assertEquals(true, type.toBoolean(Boolean.TRUE));
118: assertEquals(false, type.toBoolean(Boolean.FALSE));
119:
120: try {
121: type.toBoolean(null);
122: fail("Expected AxionException");
123: } catch (AxionException expected) {
124: // Expected.
125: }
126:
127: try {
128: type.toBoolean(new Object());
129: fail("Expected AxionException");
130: } catch (AxionException expected) {
131: // Expected.
132: }
133: }
134:
135: public void testWriteReadNonNull() throws Exception {
136: ByteArrayOutputStream buf = new ByteArrayOutputStream();
137: type.write(Boolean.TRUE, new DataOutputStream(buf));
138: Object read = type.read(new DataInputStream(
139: new ByteArrayInputStream(buf.toByteArray())));
140: assertEquals(Boolean.TRUE, read);
141: }
142:
143: public void testWriteReadSeveral() throws Exception {
144: Object[] data = { null, Boolean.TRUE, null, Boolean.FALSE,
145: Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, null };
146: ByteArrayOutputStream out = new ByteArrayOutputStream();
147: for (int i = 0; i < data.length; i++) {
148: type.write(data[i], new DataOutputStream(out));
149: }
150: DataInputStream in = new DataInputStream(
151: new ByteArrayInputStream(out.toByteArray()));
152: for (int i = 0; i < data.length; i++) {
153: Object read = type.read(in);
154: if (null == data[i]) {
155: assertNull(read);
156: } else {
157: assertEquals(type.convert(data[i]), read);
158: }
159: }
160: }
161:
162: public void testReadInvalid() throws Exception {
163: ByteArrayOutputStream out = new ByteArrayOutputStream();
164: new DataOutputStream(out).writeByte((byte) 127);
165:
166: DataInputStream in = new DataInputStream(
167: new ByteArrayInputStream(out.toByteArray()));
168: try {
169: type.read(in);
170: fail("Expected IOException due to invalid byte input value.");
171: } catch (IOException expected) {
172: // Expected.
173: }
174: }
175:
176: public void testComparator() throws Exception {
177: Comparator comp = ((BaseDataType) type).getComparator();
178: Object[] ascending = new Object[] { Boolean.FALSE,
179: Boolean.TRUE, null };
180:
181: for (int i = 0; i < ascending.length - 1; i++) {
182: assertEquals(-1, comp.compare(ascending[i],
183: ascending[i + 1]));
184: }
185:
186: for (int i = ascending.length - 1; i >= 1; i--) {
187: assertEquals(1, comp
188: .compare(ascending[i], ascending[i - 1]));
189: }
190:
191: Object[] itemsToSort = new Object[] { Boolean.TRUE, null, null,
192: Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, null,
193: Boolean.FALSE };
194: final Object[] expected = new Object[] { Boolean.FALSE,
195: Boolean.FALSE, Boolean.FALSE, Boolean.TRUE,
196: Boolean.TRUE, null, null, null };
197: Arrays.sort(itemsToSort, comp);
198:
199: for (int i = 0; i < itemsToSort.length; i++) {
200: assertEquals(
201: "Did not find expected item in comparator sorted list",
202: expected[i], itemsToSort[i]);
203: }
204: }
205: }
|