001: package net.sourceforge.jaxor.example.tests;
002:
003: import junit.textui.TestRunner;
004: import net.sourceforge.jaxor.MetaRow;
005: import net.sourceforge.jaxor.QueryParams;
006: import net.sourceforge.jaxor.api.EntityResultSet;
007: import net.sourceforge.jaxor.api.JaxorContext;
008: import net.sourceforge.jaxor.api.JaxorPreparedStatement;
009: import net.sourceforge.jaxor.example.simple.AccountEntity;
010: import net.sourceforge.jaxor.example.simple.AccountFinderBase;
011: import net.sourceforge.jaxor.example.simple.AccountMetaRow;
012: import net.sourceforge.jaxor.example.simple.AccountQuery;
013: import net.sourceforge.jaxor.impl.NullQueryCacheImpl;
014:
015: import java.sql.Connection;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Statement;
019:
020: /**
021: * Created By: Mike
022: * Date: Nov 14, 2003
023: * Time: 3:21:56 PM
024: *
025: * Last Checkin: $Author: mrettig $
026: * Date: $Date: 2004/01/27 01:26:27 $
027: * Revision: $Revision: 1.12 $
028: */
029: public class JdxStormTest extends TableTestCase {
030: int TOTAL_OBJECTS = 1000;
031:
032: private AccountFinderBase finder;
033: private JaxorContext session;
034:
035: protected MetaRow getRow() {
036: return new AccountMetaRow();
037: }
038:
039: protected void prep() throws Exception {
040: super .prep();
041: String filler = new String(
042: "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678");
043: java.math.BigDecimal amt = new java.math.BigDecimal(1000.25);
044: session = this .getJaxor();
045: session.setQueryCache(new NullQueryCacheImpl());
046: //session.setCache(new NullInstanceCacheImpl());
047: finder = new AccountFinderBase(session);
048: for (int i = 0; i < TOTAL_OBJECTS; i++) {
049: AccountEntity ac = finder.newInstance(new Integer(i));
050: ac.setFiller(filler);
051: ac.setBranchId(new Integer(10));
052: ac.setBalance(amt);
053: }
054: session.flush();
055: }
056:
057: public void testStraightJdbc() throws SQLException {
058: AccountQuery query2 = finder
059: .asFindQuery("where acctId > ? and acctId <= ?");
060: JaxorPreparedStatement stmt = query2.getQueryResult()
061: .preparedStatement();
062: try {
063: QueryParams queryParams = query2.getParams();
064: int high;
065: int low = 0;
066: for (int j = 0; j < TOTAL_OBJECTS; j++) {
067: high = low + 1;
068: if (j > 0)
069: stmt.getStatement().clearParameters();
070:
071: queryParams.add(0, new Integer(low));
072: queryParams.add(1, new Integer(high));
073:
074: ResultSet rs = stmt.execute();
075: int results = 0;
076: while (rs.next()) {
077: AccountEntity account = (AccountEntity) finder
078: .getFinder().load(rs);
079: results++;
080: }
081: assertEquals(1, results);
082: session.flush();
083: }
084: } finally {
085: stmt.close();
086: }
087: }
088:
089: public void testRunning() throws SQLException {
090:
091: int numObjects = 0;
092: Connection conn = session.getConnection();
093: Statement stmt = conn.createStatement();
094: ResultSet rs = stmt
095: .executeQuery("select count(*) from BM_ACCOUNTS");
096: if (rs.next()) {
097: numObjects = rs.getInt(1);
098: }
099: rs.close();
100: stmt.close();
101: assertEquals(TOTAL_OBJECTS, numObjects);
102:
103: //SqlServer cannot handle having 2 prepared statements open at the same time.
104: if (isSqlServerTest())
105: executeAsQueryResult();
106: else
107: executeAsStatement();
108: }
109:
110: private void executeAsQueryResult() {
111: AccountQuery query1 = finder.asFindQuery("where acctId = ?");
112: AccountQuery query2 = finder
113: .asFindQuery("where acctId > ? and acctId <= ?");
114: for (int j = 0; j < TOTAL_OBJECTS; j++) {
115: query1.getParams().add(0, new Integer(j));
116:
117: query2.getParams().add(0, new Integer(j - 1));
118: query2.getParams().add(1, new Integer(j));
119:
120: assertEquals(query1.entity(), query2.entity());
121: session.flush();
122: }
123: }
124:
125: private void executeAsStatement() throws SQLException {
126: JaxorPreparedStatement query1 = finder.asFindQuery(
127: "where acctId = ?").getQueryResult()
128: .preparedStatement();
129: JaxorPreparedStatement query2 = finder.asFindQuery(
130: "where acctId > ? and acctId <= ?").getQueryResult()
131: .preparedStatement();
132: for (int j = 0; j < TOTAL_OBJECTS; j++) {
133: query1.getParams().add(0, new Integer(j));
134:
135: query2.getParams().add(0, new Integer(j - 1));
136: query2.getParams().add(1, new Integer(j));
137:
138: EntityResultSet rs1 = query1.executeAsEntityResultSet();
139: EntityResultSet rs2 = query2.executeAsEntityResultSet();
140: assertTrue(rs1.hasNext());
141: assertTrue(rs2.hasNext());
142: assertEquals(rs1.next(), rs2.next());
143: assertFalse(rs1.hasNext());
144: assertFalse(rs2.hasNext());
145: rs1.close();
146: rs2.close();
147: query1.getStatement().clearParameters();
148: query2.getStatement().clearParameters();
149: session.flush();
150: }
151: query1.close();
152: query2.close();
153: }
154:
155: public static void main(String[] args) {
156: TestRunner.run(JdxStormTest.class);
157: }
158:
159: }
|