001: /*
002: * $Id: TestTaggedEBCDICFlatfileTable.java,v 1.14 2005/12/20 18:32:42 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.engine.tables;
042:
043: import java.io.File;
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.Properties;
047: import java.util.Random;
048:
049: import junit.framework.Test;
050: import junit.framework.TestSuite;
051:
052: import org.axiondb.AxionException;
053: import org.axiondb.Column;
054: import org.axiondb.ColumnIdentifier;
055: import org.axiondb.Database;
056: import org.axiondb.ExternalTable;
057: import org.axiondb.ExternalTableLoader;
058: import org.axiondb.Index;
059: import org.axiondb.Row;
060: import org.axiondb.RowIterator;
061: import org.axiondb.Table;
062: import org.axiondb.TableIdentifier;
063: import org.axiondb.constraints.NotNullConstraint;
064: import org.axiondb.constraints.NullConstraint;
065: import org.axiondb.constraints.PrimaryKeyConstraint;
066: import org.axiondb.engine.DiskDatabase;
067: import org.axiondb.engine.rows.SimpleRow;
068: import org.axiondb.types.BigDecimalType;
069: import org.axiondb.types.BooleanType;
070: import org.axiondb.types.CharacterVaryingType;
071: import org.axiondb.types.IntegerType;
072:
073: /**
074: * @version $Revision: 1.14 $ $Date: 2005/12/20 18:32:42 $
075: * @author Ahimanikya Satapathy
076: */
077: public class TestTaggedEBCDICFlatfileTable extends AbstractTableTest {
078:
079: //------------------------------------------------------------ Conventional
080:
081: public TestTaggedEBCDICFlatfileTable(String testName) {
082: super (testName);
083: }
084:
085: public static Test suite() {
086: TestSuite suite = new TestSuite(
087: TestTaggedEBCDICFlatfileTable.class);
088: return suite;
089: }
090:
091: //--------------------------------------------------------------- Lifecycle
092:
093: //--------------------------------------------------------------- Lifecycle
094:
095: protected DiskDatabase _db = null;
096: protected String tableName = null;
097: protected String dataFileName = null;
098:
099: protected Table createTable(String name) throws Exception {
100: name = name.toUpperCase();
101: tableName = name;
102: ExternalTableLoader loader = new TaggedEBCDICTableLoader();
103: ExternalTable t = (ExternalTable) loader.createTable(_db, name);
104: t.loadExternalTable(setProperties(name));
105: return t;
106: }
107:
108: protected Database getDatabase() throws Exception {
109: return _db;
110: }
111:
112: public void testAddThenDropColumn() throws Exception {
113: // TODO: make this test case pass
114: }
115:
116: protected File getDataFile() throws Exception {
117: return new File(getDbdir(), dataFileName);
118: }
119:
120: protected Properties setProperties(String name) {
121: Properties props = new Properties();
122:
123: props.setProperty(ExternalTable.PROP_LOADTYPE, "taggedebcdic");
124: props.setProperty(TaggedEBCDICTable.PROP_RECORDLENGTH, "213");
125: props.setProperty(TaggedEBCDICTable.PROP_HEADERBYTESOFFSET,
126: "24");
127: props.setProperty(TaggedEBCDICTable.PROP_TAGLENGTH, "4");
128: props.setProperty(TaggedEBCDICTable.PROP_MINTAGCOUNT, "1");
129: props.setProperty(TaggedEBCDICTable.PROP_MAXTAGCOUNT, "48");
130: props.setProperty(TaggedEBCDICTable.PROP_TAGBYTECOUNT, "2");
131: props.setProperty(TaggedEBCDICTable.PROP_ENCODING, "cp037");
132:
133: dataFileName = name.toUpperCase() + ".txt";
134: props
135: .setProperty(BaseFlatfileTable.PROP_FILENAME,
136: dataFileName);
137:
138: return props;
139: }
140:
141: protected String getTableName() {
142: return tableName != null ? tableName : "INPUT";
143: }
144:
145: public void setUp() throws Exception {
146: getDbdir().mkdirs();
147: _db = new DiskDatabase(getDbdir());
148: super .setUp();
149: }
150:
151: public void tearDown() throws Exception {
152: super .tearDown();
153: _db.shutdown();
154: File data = new File(getDbdir(), tableName + ".txt");
155: data.delete();
156: }
157:
158: //------------------------------------------------------------------- Tests
159:
160: public void testObjectTable() throws Exception {
161: // TODO: Make this test pass, define a interface MarshallableObject for method
162: // toString and toObject, or or MarshallableXMLObject toXMLString , toObject , If
163: // the Object implement this then we can use them in flat file.
164: }
165:
166: public void testInvalidPropertyKey() throws Exception {
167: try {
168: Properties badProps = new Properties();
169: badProps.put(ExternalTable.PROP_LOADTYPE, "taggedebcdic");
170: badProps.put("UNKNOWN_PROPERTY", Boolean.TRUE);
171: ExternalTableFactory factory = new ExternalTableFactory();
172: factory.createTable(_db, "BadTable", badProps,
173: buildColumns());
174: fail("Expected AxionException due to unrecognized property name 'UNKNOWN_PROPERTY'");
175: } catch (AxionException expected) {
176: // Expected AxionException due to unrecognized property name.
177: }
178: }
179:
180: public void testDataTypes() throws Exception {
181: Table typeTable = createTable("TYPETABLE");
182:
183: typeTable.addColumn(new Column("STRCOL",
184: new CharacterVaryingType(30)));
185: typeTable.addColumn(new Column("INTCOL", new IntegerType()));
186: typeTable.addColumn(new Column("BOOLCOL", new BooleanType()));
187:
188: // TODO: Make null value test pass
189: Object[][] values = new Object[][] {
190: new Object[] { "", "A String", "Another String",
191: "Yet Another String" },
192: new Object[] { new Integer(17), new Integer(0),
193: new Integer(5575), new Integer(22) },
194: new Object[] { Boolean.TRUE, Boolean.TRUE,
195: Boolean.FALSE, Boolean.TRUE } };
196:
197: Random random = new Random();
198: int numRows = 7;
199:
200: for (int i = 0; i < numRows; i++) {
201: Row row = new SimpleRow(typeTable.getColumnCount());
202: for (int j = 0; j < typeTable.getColumnCount(); j++) {
203: row.set(j, values[j][random.nextInt(values[j].length)]);
204: }
205: typeTable.addRow(row);
206: }
207:
208: RowIterator iter = typeTable.getRowIterator(true);
209: assertNotNull(iter);
210: for (int i = 0; i < numRows; i++) {
211: assertTrue(iter.hasNext());
212: assertNotNull(iter.next());
213: }
214: assertTrue(!iter.hasNext());
215: typeTable.shutdown();
216: }
217:
218: public void testDiskInsert() throws Exception {
219: testAddRow();
220: table.shutdown();
221: File data = new File(getDbdir(), "INPUT.txt");
222: assertTrue("Should have data file", data.exists());
223: assertTrue("Should have some data in data file",
224: data.length() > 41);
225: }
226:
227: public void testAddPrimaryKeyConstraintOnPopulatedTable()
228: throws Exception {
229: // TODO: Make this test pass
230: }
231:
232: public void testAddThenDropConstraint() throws Exception {
233: addColumns();
234: Database db = getDatabase();
235: db.addTable(table);
236: PrimaryKeyConstraint pk = new PrimaryKeyConstraint("PK_FOO");
237: pk.addSelectable(new ColumnIdentifier(new TableIdentifier(table
238: .getName()), "ID"));
239: table.addConstraint(pk);
240: addRows();
241: Column column = table.getColumn("ID");
242: if (!table.isColumnIndexed(column)) {
243: assertFalse(table.hasIndex("BOGUS"));
244: Index index1 = db.getIndexFactory("btree")
245: .makeNewSystemInstance(table, column,
246: db.getDBDirectory() == null);
247: Index index2 = db.getIndexFactory("array").makeNewInstance(
248: "INDEX_FOO", column, true,
249: db.getDBDirectory() == null);
250: db.addIndex(index1, table, true);
251: assertTrue(table.hasIndex(index1.getName()));
252: db.addIndex(index2, table, true);
253: assertTrue(table.hasIndex(index2.getName()));
254: }
255:
256: try {
257: table.addConstraint(new PrimaryKeyConstraint("PK_BAR"));
258: fail("Expected AxionException");
259: } catch (AxionException e) {
260: // expected
261: }
262: try {
263: table.addConstraint(new NotNullConstraint("PK_FOO"));
264: fail("Expected AxionException");
265: } catch (AxionException e) {
266: // expected
267: }
268:
269: table.removeConstraint("this constraint does not exist");
270: table.removeConstraint("PRIMARYKEY");
271: //table.addConstraint(pk);
272: table.removeConstraint("pk_foo");
273: table.removeConstraint("PRIMARYKEY"); // shd be silent
274:
275: //table.addConstraint(pk);
276: table.addConstraint(new NotNullConstraint("NN_FOO"));
277: table.removeConstraint("NN_FOO");
278: table.addConstraint(new NullConstraint("N_FOO"));
279: table.removeConstraint("N_FOO");
280: table.removeConstraint(null);
281: db.dropTable(table.getName());
282: table = null;
283: }
284:
285: public void testDiskDrop() throws Exception {
286: testAddRow();
287: File tabledir = new File(getDbdir(), "INPUT");
288: File meta = new File(tabledir, "INPUT.META");
289: assertTrue("Table directory should exist", tabledir.exists());
290: assertTrue("Meta file should exist", meta.exists());
291: table.drop();
292: assertTrue("Meta file should not exist", !meta.exists());
293: assertTrue("Table directory should not exist", !tabledir
294: .exists());
295: }
296:
297: // private void writeBytes(RandomAccessFile out, String str) throws Exception{
298: // byte[] ascii = str.getBytes();
299: // AsciiEbcdicEncoder.convertAsciiToEbcdic(ascii);
300: // out.write(ascii);
301: // }
302:
303: // public void testFileRead() throws Exception {
304: // File data = new File(getDbdir(), "FFTest.txt");
305: // RandomAccessFile out = new BufferedRandomAccessFile(data, "rw");
306: //
307: // writeBytes(out,"ID NAME " ); // Header
308: // writeBytes(out,"1 aa " ); // 1
309: // writeBytes(out,"2.00bbb " ); // 2
310: // writeBytes(out,"3.00ccc " ); // 3
311: // writeBytes(out,"4.00ddd " ); // 4
312: // writeBytes(out,"" ); // skip
313: // writeBytes(out,"we " ); // bad 1
314: // writeBytes(out,"7 dfdf " ); // 5
315: // writeBytes(out,"7.0fccc " ); // 6
316: // writeBytes(out,"xx xx " ); // bad 2
317: // writeBytes(out,"5 test " ); // 7
318: // writeBytes(out,"10-1hhhh " ); // bad 3
319: // writeBytes(out," " ); // bad 4
320: // out.close();
321: //
322: // Properties props = new Properties();
323: //
324: // props.setProperty(ExternalTable.PROP_LOADTYPE, "taggedebcdic");
325: // props.setProperty(TaggedEBCDICTable.PROP_RECORDLENGTH, "9");
326: // props.setProperty(TaggedEBCDICTable.PROP_HEADERBYTESOFFSET, "0");
327: // props.setProperty(TaggedEBCDICTable.PROP_TAGLENGTH, "4");
328: // props.setProperty(TaggedEBCDICTable.PROP_MINTAGCOUNT, "1");
329: // props.setProperty(TaggedEBCDICTable.PROP_MAXTAGCOUNT, "48");
330: // props.setProperty(TaggedEBCDICTable.PROP_TAGBYTECOUNT, "2");
331: // props.setProperty(TaggedEBCDICTable.PROP_ENCODING, "cp037");
332: // props.setProperty(BaseFlatfileTable.PROP_FILENAME, "FFTest.txt");
333: //
334: // ExternalTableFactory factory = new ExternalTableFactory();
335: //
336: // Table table2 = factory.createTable(_db, "FFTest", props, buildColumns());
337: // RowIterator itr = table2.getRowIterator(false);
338: //
339: // // the intial load should ignore empty line
340: // assertEquals("Total line Should have correct value", 11, table2.getRowCount());
341: //
342: // int rowCount = 0;
343: // while (itr.hasNext()) {
344: // itr.next();
345: // rowCount++;
346: // }
347: //
348: // assertEquals("Valid row Count Should have correct value", 7, rowCount);
349: // table2.drop();
350: //
351: // }
352:
353: public void testDefrag() throws Exception {
354:
355: }
356:
357: private List buildColumns() {
358: List list = new ArrayList(2);
359: Column id = new Column("ID", new BigDecimalType(4));
360: list.add(id);
361:
362: Column name = new Column("NAME", new CharacterVaryingType(5));
363: list.add(name);
364: return list;
365: }
366: }
|