001: /*
002: * $Id: TestRowDecorator.java,v 1.3 2005/05/02 22:32:04 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004 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.engine.rows;
042:
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: import junit.framework.Test;
047: import junit.framework.TestCase;
048: import junit.framework.TestSuite;
049:
050: import org.axiondb.AxionException;
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.Row;
053: import org.axiondb.RowDecorator;
054: import org.axiondb.types.CharacterVaryingType;
055: import org.axiondb.types.IntegerType;
056:
057: /**
058: * @version $Revision: 1.3 $ $Date: 2005/05/02 22:32:04 $
059: * @author Ahimanikya Satapathy
060: */
061: public class TestRowDecorator extends TestCase {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestRowDecorator(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: TestSuite suite = new TestSuite(TestRowDecorator.class);
071: return suite;
072: }
073:
074: protected Row makeRow(Object[] values) {
075: return new SimpleRow(values);
076: }
077:
078: protected RowDecorator makeRowDecorator() {
079: Map map = new HashMap(4);
080: map.put(makeVarcharCol("one"), new Integer(0));
081: map.put(makeVarcharCol("two"), new Integer(1));
082: map.put(makeVarcharCol("three"), new Integer(2));
083: map.put(makeVarcharCol("four"), new Integer(3));
084: return new RowDecorator(map);
085: }
086:
087: protected RowDecorator makeRowDecoratorInt() {
088: Map map = new HashMap(4);
089: map.put(makeIntCol("one"), new Integer(0));
090: map.put(makeIntCol("two"), new Integer(1));
091: map.put(makeIntCol("three"), new Integer(2));
092: map.put(makeIntCol("four"), new Integer(3));
093: return new RowDecorator(map);
094: }
095:
096: protected RowDecorator makeRowDecoratorWithNoType() {
097: Map map = new HashMap(4);
098: map.put(makeColWithNullType("one"), new Integer(0));
099: map.put(makeColWithNullType("two"), new Integer(1));
100: map.put(makeColWithNullType("three"), new Integer(2));
101: map.put(makeColWithNullType("four"), new Integer(3));
102: return new RowDecorator(map);
103: }
104:
105: private ColumnIdentifier makeVarcharCol(String colName) {
106: return new ColumnIdentifier(null, colName, null,
107: new CharacterVaryingType(10));
108: }
109:
110: private ColumnIdentifier makeIntCol(String colName) {
111: return new ColumnIdentifier(null, colName, null,
112: new IntegerType());
113: }
114:
115: private ColumnIdentifier makeColWithNullType(String colName) {
116: return new ColumnIdentifier(null, colName, null, null);
117: }
118:
119: private void assertRowIndexException(RowDecorator dec) {
120: try {
121: dec.getRowIndex();
122: fail("Expected Exception");
123: } catch (AxionException e) {
124: // expected
125: }
126: }
127:
128: private void assertFieldNotFound1(RowDecorator dec) {
129: try {
130: dec.get(makeVarcharCol("bogus"));
131: fail("Expected Exception");
132: } catch (AxionException e) {
133: // expected
134: }
135: }
136:
137: private void assertFieldNotFound2(RowDecorator dec) {
138: try {
139: dec.get(null);
140: fail("Expected Exception");
141: } catch (AxionException e) {
142: // expected
143: }
144: }
145:
146: private void assertStringRowValue(RowDecorator dec, Object[] val)
147: throws Exception {
148: for (int i = 0; i < 4; i++) {
149: assertEquals(val[i], dec.get(makeVarcharCol(COL_NAMES[i])));
150: }
151: }
152:
153: private void assertRowValueForNullType(RowDecorator dec,
154: Object[] val) throws Exception {
155: for (int i = 0; i < 4; i++) {
156: ColumnIdentifier col = makeColWithNullType(COL_NAMES[i]);
157: col.setDataType(null);
158: assertEquals(val[i], dec.get(col));
159: }
160: }
161:
162: private void assertIntRowValue(RowDecorator dec, Object[] val)
163: throws Exception {
164: for (int i = 0; i < 4; i++) {
165: Integer colVal = new Integer((String) dec
166: .get(makeVarcharCol(COL_NAMES[i])));
167: assertEquals(new Integer((String) val[i]), colVal);
168: }
169: }
170:
171: //------------------------------------------------------------------- Tests
172:
173: public void testSimpleRowDecorator() throws Exception {
174: {
175: Row row = makeRow(STRINGS_ONE);
176:
177: RowDecorator dec = makeRowDecorator();
178:
179: dec.setRow(row);
180: assertEquals(row, dec.getRow());
181: assertStringRowValue(dec, STRINGS_ONE);
182: assertRowIndexException(dec);
183:
184: dec.setRow(50, row);
185: assertStringRowValue(dec, STRINGS_ONE);
186: assertFieldNotFound1(dec);
187: assertFieldNotFound2(dec);
188: }
189: {
190: Row row = makeRow(STRINGS_ONE);
191: RowDecorator dec = makeRowDecoratorInt();
192:
193: dec.setRow(row);
194: assertEquals(row, dec.getRow());
195: assertIntRowValue(dec, STRINGS_ONE);
196: assertRowIndexException(dec);
197:
198: dec.setRow(50, row);
199: assertIntRowValue(dec, STRINGS_ONE);
200: assertFieldNotFound1(dec);
201: assertFieldNotFound2(dec);
202: }
203: {
204: Row row = makeRow(NULLS);
205:
206: RowDecorator dec = makeRowDecorator();
207:
208: dec.setRow(row);
209: assertEquals(row, dec.getRow());
210: assertStringRowValue(dec, NULLS);
211: assertRowIndexException(dec);
212:
213: dec.setRow(50, row);
214: assertStringRowValue(dec, NULLS);
215: assertFieldNotFound1(dec);
216: assertFieldNotFound2(dec);
217: }
218: {
219: Row row = makeRow(STRINGS_ONE);
220:
221: RowDecorator dec = makeRowDecoratorWithNoType();
222:
223: dec.setRow(row);
224: assertEquals(row, dec.getRow());
225: assertRowValueForNullType(dec, STRINGS_ONE);
226: assertRowIndexException(dec);
227:
228: dec.setRow(50, row);
229: assertRowValueForNullType(dec, STRINGS_ONE);
230: assertFieldNotFound1(dec);
231: assertFieldNotFound2(dec);
232: }
233:
234: }
235:
236: private static final String[] COL_NAMES = { "one", "two", "three",
237: "four" };
238: private static final String[] STRINGS_ONE = { "1", "2", "3", "4" };
239: private static final Object[] NULLS = { null, null, null, null };
240: }
|