001: /*
002: * $Id: TestExternalDatabaseTableRowIterator.java,v 1.6 2005/08/24 00:34:52 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: package org.axiondb.engine.tables;
041:
042: import java.util.ArrayList;
043: import java.util.List;
044: import java.util.Properties;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import org.axiondb.AxionException;
050: import org.axiondb.Column;
051: import org.axiondb.Database;
052: import org.axiondb.DatabaseLink;
053: import org.axiondb.ExternalConnectionProvider;
054: import org.axiondb.ExternalTable;
055: import org.axiondb.RowIterator;
056: import org.axiondb.Table;
057: import org.axiondb.engine.MemoryDatabase;
058: import org.axiondb.engine.rowiterators.AbstractRowIteratorTest;
059: import org.axiondb.engine.rows.SimpleRow;
060: import org.axiondb.types.CharacterVaryingType;
061: import org.axiondb.types.IntegerType;
062:
063: /**
064: *
065: * @author Jonathan Giron
066: * @version $Revision: 1.6 $
067: */
068: public class TestExternalDatabaseTableRowIterator extends
069: AbstractRowIteratorTest {
070:
071: private static final String DATABASE_NAME = "REMOTEDB";
072: private static final String DBLINK_NAME = "AXIONDB";
073:
074: /**
075: * @param testName
076: */
077: public TestExternalDatabaseTableRowIterator(String testName) {
078: super (testName);
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(
083: TestExternalDatabaseTableRowIterator.class);
084: return suite;
085: }
086:
087: protected void setUp() throws Exception {
088: setUpRemote();
089: createExternalTable(getRemoteTableName() + "_EXT");
090: }
091:
092: protected void setUpRemote() throws Exception {
093: _db = new MemoryDatabase(DATABASE_NAME);
094: _table = new MemoryTable(getRemoteTableName());
095: addColumns(_table);
096: _db.addTable(_table);
097: }
098:
099: protected void tearDown() throws Exception {
100: if (_externalTable != null) {
101: _externalTable.shutdown();
102: _externalTable = null;
103: }
104:
105: if (_db.hasDatabaseLink(DBLINK_NAME)) {
106: _db.dropDatabaseLink(DBLINK_NAME);
107: }
108:
109: _db.dropTable(getRemoteTableName());
110: _table.shutdown();
111: _table = null;
112:
113: _db.shutdown();
114: }
115:
116: protected RowIterator makeRowIterator() {
117: try {
118: return _externalTable.getRowIterator();
119: } catch (AxionException e) {
120: throw new UnsupportedOperationException(
121: "Could not get RowIterator from external table.");
122: }
123: }
124:
125: protected String getJdbcDriverName() {
126: return "org.axiondb.jdbc.AxionDriver";
127: }
128:
129: protected String getJdbcURL() {
130: return "jdbc:axiondb:" + DATABASE_NAME;
131: }
132:
133: protected String getUserName() {
134: return "ignored";
135: }
136:
137: protected String getPassword() {
138: return "ignored";
139: }
140:
141: protected String getSchemaName() {
142: return "";
143: }
144:
145: protected String getRemoteTableName() {
146: return "REMOTETABLE";
147: }
148:
149: protected int getSize() {
150: return 3;
151: }
152:
153: protected List makeRowList() {
154: List list = new ArrayList();
155: {
156: SimpleRow row = new SimpleRow(2);
157: row.set(0, new Integer(1));
158: row.set(1, "a");
159: list.add(row);
160: }
161: {
162: SimpleRow row = new SimpleRow(2);
163: row.set(0, new Integer(2));
164: row.set(1, "bb");
165: list.add(row);
166: }
167: {
168: SimpleRow row = new SimpleRow(2);
169: row.set(0, new Integer(3));
170: row.set(1, "ccc");
171: list.add(row);
172: }
173: return list;
174: }
175:
176: private List buildColumns() {
177: List list = new ArrayList(2);
178:
179: Column id = new Column("id", new IntegerType());
180: id.setSqlType("integer");
181: list.add(id);
182:
183: Column name = new Column("name", new CharacterVaryingType(10));
184: name.setSqlType("varchar");
185: list.add(name);
186: return list;
187: }
188:
189: private void addColumns(Table t) throws Exception {
190: Column id = new Column("id", new IntegerType());
191: id.setSqlType("integer");
192: t.addColumn(id);
193:
194: Column name = new Column("name", new CharacterVaryingType(10));
195: name.setSqlType("varchar");
196: t.addColumn(name);
197: }
198:
199: private Properties getDBLinkProperties() {
200: Properties props = new Properties();
201: props.setProperty(ExternalConnectionProvider.PROP_DRIVERCLASS,
202: getJdbcDriverName());
203: props.setProperty(ExternalConnectionProvider.PROP_JDBCURL,
204: getJdbcURL());
205: props.setProperty(ExternalConnectionProvider.PROP_USERNAME,
206: getUserName());
207: props.setProperty(ExternalConnectionProvider.PROP_PASSWORD,
208: getPassword());
209:
210: return props;
211: }
212:
213: private Properties getExternalTableProperties(String name) {
214: Properties props = new Properties();
215: props.setProperty(ExternalTable.PROP_DB, DBLINK_NAME);
216: props.setProperty(ExternalTable.PROP_LOADTYPE, "remote");
217: props.setProperty(ExternalTable.PROP_WHERE, "");
218: props.setProperty(ExternalTable.PROP_ORDERBY, "");
219: props.setProperty(ExternalTable.PROP_CREATE_IF_NOT_EXIST,
220: Boolean.TRUE.toString());
221:
222: String schemaName = getSchemaName();
223: if (null != schemaName) {
224: props.setProperty(ExternalTable.PROP_SCHEMA, schemaName);
225: }
226:
227: props.setProperty(ExternalTable.PROP_REMOTETABLE, name);
228:
229: return props;
230: }
231:
232: private void createExternalTable(String name) throws Exception {
233: _db.createDatabaseLink(new DatabaseLink(DBLINK_NAME,
234: getDBLinkProperties()));
235:
236: ExternalTableFactory factory = new ExternalTableFactory();
237: _externalTable = (ExternalDatabaseTable) factory.createTable(
238: _db, name, getExternalTableProperties(name),
239: buildColumns());
240: addRow();
241: }
242:
243: private void addRow() throws Exception {
244: {
245: SimpleRow row = new SimpleRow(2);
246: row.set(0, new Integer(1));
247: row.set(1, "a");
248: _externalTable.addRow(row);
249: }
250: {
251: SimpleRow row = new SimpleRow(2);
252: row.set(0, new Integer(2));
253: row.set(1, "bb");
254: _externalTable.addRow(row);
255: }
256: {
257: SimpleRow row = new SimpleRow(2);
258: row.set(0, new Integer(3));
259: row.set(1, "ccc");
260: _externalTable.addRow(row);
261: }
262: _externalTable.commit();
263: }
264:
265: private Database _db;
266: private ExternalDatabaseTable _externalTable = null;
267: private Table _table = null;
268: }
|