001: package com.xoetrope.sql.test;
002:
003: import java.io.FileReader;
004:
005: import junit.framework.TestCase;
006: import net.xoetrope.optional.data.XOptionalDataSource;
007: import net.xoetrope.optional.data.sql.ConnectionManager;
008: import net.xoetrope.optional.data.sql.DatabaseRowModel;
009: import net.xoetrope.optional.data.sql.DatabaseTable;
010: import net.xoetrope.optional.data.sql.DatabaseTableModel;
011: import net.xoetrope.xui.data.XModel;
012:
013: /**
014: * <p>Copyright (c) Xoetrope Ltd. 2001-2004</p>
015: * $Revision: 1.2 $
016: */
017: public class XTableTest extends TestCase {
018:
019: private static final double EPSILON = 0.1E-15;
020: private static final double KELVIN_OFFSET = 273.15;
021: private static final double GRAVITY = 9.8;
022: private static final double PI = 3.141592654;
023: private static final double RHO_REF_FLUID = 1000.0;
024:
025: public XTableTest() {
026: }
027:
028: // public void testHsqlConnection()
029: // {
030: // createDataSource();
031: // String fields = "ID, NAME, C_GOODS_BF, C_GOODS_AF, DHSL";
032: // ConnectionManager.getInstance().reset( "org.hsqldb.jdbcDriver", "jdbc:hsqldb:hsql://localhost", "sa", "" );
033: // DatabaseTable xtable = new DatabaseTable( "CS_FROZEN_GOODS", fields, null, null, false );
034: // xtable.retrieve( null );
035: //
036: // DatabaseTableModel tableModel = new DatabaseTableModel();
037: // tableModel.setName( "FrozenGoods" );
038: // tableModel.setupTable( "CS_FROZEN_GOODS", fields, null );
039: //
040: // String value1 = xtable.getValue( 1 );
041: // assertTrue( value1.compareTo( "Fruit" ) == 0 );
042: // assertTrue( value1.compareTo( tableModel.getAttribValue( 1 ).toString() ) == 0 );
043: // assertTrue( value1.compareTo( tableModel.getFieldValue( 0, 1 ) ) == 0 );
044: // }
045: //
046: // public void testConnection()
047: // {
048: //// String idfields[] { "id", "type_code" }
049: // String fields = "ID, NAME, C_GOODS_BF, C_GOODS_AF, DHSL";
050: //
051: // ConnectionManager.getInstance().reset( "sun.jdbc.odbc.JdbcOdbcDriver",
052: // "jdbc:odbc:XLibTest", "sa", "" );
053: //// ConnectionManager.reset("com.microsoft.jdbc.sqlserver.SQLServerDriver",
054: //// "jdbc:microsoft:sqlserver://miranda:1433;DatabaseName=CoolSelector;User=sa;Password=", "sa", "");
055: // DatabaseTable xtable = new DatabaseTable( "CS_FROZEN_GOODS_OLD", fields, null, null, false );
056: // xtable.retrieve( null );
057: //
058: // DatabaseTableModel tableModel = new DatabaseTableModel();
059: // tableModel.setName( "FrozenGoods" );
060: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD", fields, null );
061: //
062: // String value1 = xtable.getValue( 1 );
063: // assertTrue( value1.compareTo( "Fruit" ) == 0 );
064: // assertTrue( value1.compareTo( tableModel.getAttribValue( 1 ).toString() ) == 0 );
065: // assertTrue( value1.compareTo( tableModel.getFieldValue( 0, 1 ).toString() ) == 0 );
066: // }
067: //
068: // public void testMetaData()
069: // {
070: // DatabaseTableModel tableModel = new DatabaseTableModel();
071: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD" );
072: //
073: //// String value1 = tableModel.getFieldValue(1);
074: // String value1 = null;
075: // // No data has been retrieved yet
076: // assertTrue( value1 == null );
077: // tableModel.retrieve();
078: // // Now it should succeed
079: // value1 = tableModel.getFieldValue( 1 );
080: // assertTrue( value1.compareTo( "Fruit" ) == 0 );
081: // assertTrue( value1.compareTo( tableModel.getAttribValue( 1 ) ) == 0 );
082: // assertTrue( value1.compareTo( tableModel.getFieldValue( 0, 1 ) ) == 0 );
083: //
084: // assertTrue( tableModel.next() );
085: // assertTrue( ( tableModel.getFieldValue( 1 ).compareTo( "Vegetables" ) == 0 ) || ( tableModel.getFieldValue( 1 ).compareTo( "New Data" ) == 0 ) );
086: // assertTrue( tableModel.getFieldValue( 1 ).compareTo( tableModel.getFieldValue( 1, 1 ) ) == 0 );
087: // }
088: //
089: // public void testUpdate()
090: // {
091: // DatabaseTableModel tableModel = new DatabaseTableModel();
092: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD", null, null, true );
093: // tableModel.retrieve();
094: // // Now it should succeed
095: // String value1 = tableModel.getFieldValue( 1 );
096: // assertTrue( value1.compareTo( "Fruit" ) == 0 );
097: // tableModel.next();
098: // tableModel.setFieldValue( 1, "New Data" );
099: // }
100: //
101: // public void testServiceAccessViaTableModel()
102: // {
103: // String goodsName = "Pork";
104: // String fieldName = "NAME";
105: //
106: // DatabaseTableModel tableModel = new DatabaseTableModel();
107: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD", null, "NAME='Pork'" );
108: // int nameFieldIdx = tableModel.getAttribute( fieldName );
109: // assertTrue( tableModel.getFieldValue( 0, nameFieldIdx ).compareTo( goodsName ) == 0 );
110: //
111: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD" );
112: // DatabaseTableModel filteredTableModel = tableModel.findRows( nameFieldIdx, goodsName );
113: // assertTrue( filteredTableModel.getFieldValue( 0, nameFieldIdx ).compareTo( goodsName ) == 0 );
114: //
115: // String where = fieldName + "='" + goodsName + "'";
116: // DatabaseTableModel filteredTableModel2 = tableModel.findRows( where );
117: // assertTrue( filteredTableModel2.getFieldValue( 0, nameFieldIdx ).compareTo( goodsName ) == 0 );
118: //
119: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD" );
120: // DatabaseRowModel rowModel = tableModel.findRow( nameFieldIdx, goodsName );
121: // assertTrue( rowModel.getFieldValue( nameFieldIdx ).compareTo( goodsName ) == 0 );
122: //
123: // DatabaseRowModel rowModel2 = tableModel.findRow( where );
124: // assertTrue( rowModel.getFieldValue( nameFieldIdx ).compareTo( goodsName ) == 0 );
125: //
126: // double troom = 4;
127: // double tgoods_in = 16;
128: // double mgoods = 500;
129: // double Qgoods = 0.0;
130: //
131: // // Eqn 1.29
132: // double tfp = rowModel.getDouble( "TFP" );
133: // if ( ( troom > tfp ) && ( tgoods_in > tfp ) )
134: // Qgoods = mgoods * rowModel.getDouble( "C_GOODS_AF" ) * ( tgoods_in - troom );
135: //
136: // // Eqn 1.31
137: // if ( ( troom < ( tfp + EPSILON ) ) && ( tgoods_in > ( tfp - EPSILON ) ) )
138: // Qgoods = mgoods * rowModel.getDouble( "C_GOODS_AF" ) * ( tgoods_in - tfp ) +
139: // mgoods * rowModel.getDouble( "DHSL" ) +
140: // mgoods * rowModel.getDouble( "C_GOODS_BF" ) * ( tfp - troom );
141: //
142: // // Eqn 1.32
143: // if ( ( troom < tfp ) && ( tgoods_in < tfp ) )
144: // Qgoods = mgoods * rowModel.getDouble( "C_GOODS_BF" ) * ( tgoods_in - troom );
145: // }
146: //
147: // public void testServiceAccessViaXModel()
148: // {
149: // String goodsName = "Pork";
150: // String fieldName = "NAME";
151: //
152: // DatabaseTableModel tableModel = new DatabaseTableModel();
153: // tableModel.setupTable( "CS_FROZEN_GOODS_OLD", null, null );
154: // int nameFieldIdx = tableModel.getAttribute( fieldName );
155: // int numRows = tableModel.getNumChildren();
156: // XModel rowNode = null;
157: // for ( int i = 0; i < numRows; i++ ) {
158: // rowNode = tableModel.get( i );
159: // if ( rowNode.getAttribValueAsString( 1 ).compareTo( goodsName ) == 0 )
160: // break;
161: // else
162: // rowNode = null;
163: // }
164: //
165: // if ( rowNode != null ) {
166: // assertTrue( rowNode.getAttribValueAsString( nameFieldIdx ).compareTo( goodsName ) == 0 );
167: //
168: // double troom = 4;
169: // double tgoods_in = 16;
170: // double mgoods = 500;
171: // double Qgoods = 0.0;
172: //
173: // // Eqn 1.29
174: // double tfp = rowNode.getAttribValueAsDouble( tableModel.getAttribute( "TFP" ) );
175: // if ( ( troom > tfp ) && ( tgoods_in > tfp ) )
176: // Qgoods = mgoods * rowNode.getAttribValueAsDouble( tableModel.getAttribute( "C_GOODS_AF" ) ) * ( tgoods_in - troom );
177: //
178: // // Eqn 1.31
179: // if ( ( troom < ( tfp + EPSILON ) ) && ( tgoods_in > ( tfp - EPSILON ) ) )
180: // Qgoods = mgoods * rowNode.getAttribValueAsDouble( tableModel.getAttribute( "C_GOODS_AF" ) ) * ( tgoods_in - tfp ) +
181: // mgoods * rowNode.getAttribValueAsDouble( tableModel.getAttribute( "DHSL" ) ) +
182: // mgoods * rowNode.getAttribValueAsDouble( tableModel.getAttribute( "C_GOODS_BF" ) ) * ( tfp - troom );
183: //
184: // // Eqn 1.32
185: // if ( ( troom < tfp ) && ( tgoods_in < tfp ) )
186: // Qgoods = mgoods * rowNode.getAttribValueAsDouble( tableModel.getAttribute( "C_GOODS_BF" ) ) * ( tgoods_in - troom );
187: // }
188: // }
189: //
190: // public void testSave()
191: // {
192: // // Setup a writable table.
193: // DatabaseTableModel tableModel = new DatabaseTableModel();
194: // tableModel.setupTable( "tempTable", null, "Refrigerant_code=2", true );
195: //
196: // // Force a field value
197: // int numRows = tableModel.getNumChildren();
198: // DatabaseRowModel rowNode = null;
199: // double d = 0.0;
200: // for ( int i = 0; i < numRows; i++ ) {
201: // rowNode = ( DatabaseRowModel )tableModel.get( i );
202: // d = new Double( rowNode.getFieldValue( 3 ) ).doubleValue();
203: // rowNode.setFieldValue( 3, new Double( d + 1.0 ).toString() );
204: // }
205: //
206: // // Reset the table and get its value
207: // tableModel.setupTable( "tempTable", null, "Refrigerant_code=2", false );
208: // rowNode = ( DatabaseRowModel )tableModel.get( 0 );
209: // double d2 = new Double( rowNode.getFieldValue( 3 ) ).doubleValue();
210: //
211: // // The values should now be the same
212: // assertTrue( ( d + 1.0 ) == d2 );
213: // }
214: //
215: // public void createDataSource()
216: // {
217: // try {
218: // FileReader fr = new FileReader( "C:\\cvs\\xlib\\resources\\xlib_datasets.xml" );
219: // XOptionalDataSource ds = new XOptionalDataSource();
220: // ds.read( fr );
221: // fr.close();
222: // }
223: // catch ( Exception ex ) {
224: // ex.printStackTrace();
225: // }
226: //
227: // }
228: }
|