001: /*
002: * $Id: TestDelimitedFlatfileTableRowIterator.java,v 1.3 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.CharacterVaryingType;
063: import org.axiondb.types.IntegerType;
064:
065: /**
066: * Tests internal RowIterator implementation of
067: * {@link org.axiondb.engine.tables.DelimitedFlatfileTable}.
068: *
069: * @version $Revision: 1.3 $ $Date: 2005/05/02 22:32:04 $
070: * @author Jonathan Giron
071: */
072: public class TestDelimitedFlatfileTableRowIterator extends
073: AbstractRowIteratorTest {
074:
075: protected static final String TABLE_NAME = "X_DELIMITED";
076:
077: private static final String DATABASE_NAME = "TESTDB";
078: private static final String FILE_EXTENSION = ".csv";
079:
080: //------------------------------------------------------------ Conventional
081:
082: public TestDelimitedFlatfileTableRowIterator(String testName) {
083: super (testName);
084: }
085:
086: public static Test suite() {
087: TestSuite suite = new TestSuite(
088: TestDelimitedFlatfileTableRowIterator.class);
089: return suite;
090: }
091:
092: private Table _table;
093: private File _dbDir = new File(new File("."), DATABASE_NAME);
094: private Database _db;
095: private String _dataFileName;
096:
097: protected void setUp() throws Exception {
098: _dbDir.mkdirs();
099: _db = Databases.getOrCreateDatabase(DATABASE_NAME, _dbDir);
100: _table = createTable(TABLE_NAME);
101: }
102:
103: protected void tearDown() throws Exception {
104: super .tearDown();
105: _table.truncate();
106: _table.drop();
107: _db.shutdown();
108:
109: FileUtil.delete(_dbDir);
110: }
111:
112: protected Table createTable(String name) throws Exception {
113: File data = new File(_dbDir, name + FILE_EXTENSION);
114: FileWriter out = new FileWriter(data);
115:
116: String eol = System.getProperty("line.separator");
117:
118: out.write("ID,NAME" + eol); // Header
119: out.write("1,a" + eol); // 1
120: out.write("2,bb" + eol); // 2
121: out.write("3,ccc" + eol); // 3
122: out.close();
123:
124: ExternalTableFactory factory = new ExternalTableFactory();
125: ExternalTable t = factory.createTable(_db, name,
126: setProperties(name), buildColumns());
127: return t;
128: }
129:
130: protected Properties setProperties(String name) {
131: Properties props = new Properties();
132:
133: props.setProperty(ExternalTable.PROP_LOADTYPE, "delimited");
134: props.setProperty(DelimitedFlatfileTable.PROP_FIELDDELIMITER,
135: ",");
136:
137: String eol = System.getProperty("line.separator");
138: props.setProperty(BaseFlatfileTable.PROP_RECORDDELIMITER, eol);
139: props.setProperty(BaseFlatfileTable.PROP_ISFIRSTLINEHEADER,
140: "true");
141:
142: _dataFileName = name + FILE_EXTENSION;
143: props.setProperty(BaseFlatfileTable.PROP_FILENAME,
144: _dataFileName);
145:
146: return props;
147: }
148:
149: protected RowIterator makeRowIterator() {
150: try {
151: return _table.getRowIterator(false);
152: } catch (AxionException e) {
153: throw new UnsupportedOperationException(
154: "Could not create RowIterator");
155: }
156: }
157:
158: protected int getSize() {
159: return 3;
160: }
161:
162: protected List makeRowList() {
163: List list = new ArrayList();
164: {
165: SimpleRow row = new SimpleRow(2);
166: row.set(0, new Integer(1));
167: row.set(1, "a");
168: list.add(row);
169: }
170: {
171: SimpleRow row = new SimpleRow(2);
172: row.set(0, new Integer(2));
173: row.set(1, "bb");
174: list.add(row);
175: }
176: {
177: SimpleRow row = new SimpleRow(2);
178: row.set(0, new Integer(3));
179: row.set(1, "ccc");
180: list.add(row);
181: }
182: return list;
183: }
184:
185: private List buildColumns() {
186: List list = new ArrayList(2);
187:
188: Column id = new Column("id", new IntegerType());
189: id.setSqlType("integer");
190: list.add(id);
191:
192: Column name = new Column("name", new CharacterVaryingType(10));
193: name.setSqlType("varchar");
194: list.add(name);
195: return list;
196: }
197:
198: }
|