001: /*
002: * $Id: TestFixedWidthFlatfileTableRowIterator.java,v 1.2 2005/05/02 22:32:04 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 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.engine.tables;
042:
043: import java.io.File;
044: import java.io.FileWriter;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.Properties;
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.Database;
055: import org.axiondb.ExternalTable;
056: import org.axiondb.RowIterator;
057: import org.axiondb.Table;
058: import org.axiondb.engine.Databases;
059: import org.axiondb.engine.rowiterators.AbstractRowIteratorTest;
060: import org.axiondb.engine.rows.SimpleRow;
061: import org.axiondb.io.FileUtil;
062: import org.axiondb.types.IntegerType;
063:
064: /**
065: * @version $Revision: 1.2 $ $Date: 2005/05/02 22:32:04 $
066: * @author Jonathan Giron
067: */
068: public class TestFixedWidthFlatfileTableRowIterator extends
069: AbstractRowIteratorTest {
070:
071: //------------------------------------------------------------ Conventional
072:
073: public TestFixedWidthFlatfileTableRowIterator(String testName) {
074: super (testName);
075: }
076:
077: public static Test suite() {
078: TestSuite suite = new TestSuite(
079: TestFixedWidthFlatfileTableRowIterator.class);
080: return suite;
081: }
082:
083: private static final String FILE_EXTENSION = ".dat";
084:
085: private Table _table;
086: private File _dbDir = new File(new File("."), "TESTDB");
087: private Database _db;
088: private String _dataFileName;
089:
090: protected void setUp() throws Exception {
091: _dbDir.mkdirs();
092: _db = Databases.getOrCreateDatabase("TESTDB", _dbDir);
093: _table = createTable("X");
094: }
095:
096: protected void tearDown() throws Exception {
097: super .tearDown();
098: _table.drop();
099: _db.shutdown();
100:
101: FileUtil.delete(_dbDir);
102: }
103:
104: protected Table createTable(String name) throws Exception {
105: final String eol = System.getProperty("line.separator");
106:
107: File data = new File(_dbDir, name + FILE_EXTENSION);
108: FileWriter out = new FileWriter(data);
109:
110: out.write("1 100 " + eol); // 1
111: out.write("2 22 " + eol); // 2
112: out.write("3 3456 " + eol); // 3
113: out.close();
114:
115: ExternalTableFactory factory = new ExternalTableFactory();
116: ExternalTable t = factory.createTable(_db, name,
117: setProperties(name), buildColumns());
118: // assertEquals("Total line should have correct value", 3, t.getRowCount());
119: return t;
120: }
121:
122: protected Properties setProperties(String name) {
123: Properties props = new Properties();
124:
125: props.setProperty(ExternalTable.PROP_LOADTYPE, "fixedwidth");
126: props.setProperty(
127: FixedWidthFlatfileTable.PROP_HEADERBYTESOFFSET, "0");
128: props.setProperty(BaseFlatfileTable.PROP_ISFIRSTLINEHEADER,
129: "false");
130: props.setProperty(BaseFlatfileTable.PROP_RECORDDELIMITER,
131: System.getProperty("line.separator"));
132:
133: _dataFileName = name + FILE_EXTENSION;
134: props.setProperty(BaseFlatfileTable.PROP_FILENAME,
135: _dataFileName);
136: props.put(BaseFlatfileTable.PROP_ROWSTOSKIP, "0");
137:
138: return props;
139: }
140:
141: private List buildColumns() {
142: List list = new ArrayList(2);
143:
144: Column id = new Column("id", new IntegerType());
145: list.add(id);
146:
147: Column name = new Column("val", new IntegerType());
148: list.add(name);
149: return list;
150: }
151:
152: protected RowIterator makeRowIterator() {
153: try {
154: return _table.getRowIterator(false);
155: } catch (AxionException e) {
156: throw new UnsupportedOperationException(
157: "Could not create RowIterator");
158: }
159: }
160:
161: protected List makeRowList() {
162: List list = new ArrayList();
163: {
164: SimpleRow row = new SimpleRow(2);
165: row.set(0, new Integer(1));
166: row.set(1, new Integer(100));
167: list.add(row);
168: }
169: {
170: SimpleRow row = new SimpleRow(2);
171: row.set(0, new Integer(2));
172: row.set(1, new Integer(22));
173: list.add(row);
174: }
175: {
176: SimpleRow row = new SimpleRow(2);
177: row.set(0, new Integer(3));
178: row.set(1, new Integer(3456));
179: list.add(row);
180: }
181: return list;
182: }
183:
184: protected int getSize() {
185: return 3;
186: }
187: }
|