001: /*
002: * User: Michael Rettig
003: * Date: Aug 21, 2002
004: * Time: 10:59:29 PM
005: */
006: package net.sourceforge.jaxor.example.tests;
007:
008: import net.sourceforge.jaxor.EntityNotFoundException;
009: import net.sourceforge.jaxor.MetaField;
010: import net.sourceforge.jaxor.MetaRow;
011: import net.sourceforge.jaxor.QueryParams;
012: import net.sourceforge.jaxor.api.FieldAdapter;
013: import net.sourceforge.jaxor.example.db.HypersonicContextTestingFactory;
014: import net.sourceforge.jaxor.example.domain.*;
015: import net.sourceforge.jaxor.example.simple.ContactEntity;
016: import net.sourceforge.jaxor.example.simple.ContactFinder;
017: import net.sourceforge.jaxor.example.simple.ContactList;
018: import net.sourceforge.jaxor.example.simple.ContactMetaRow;
019: import net.sourceforge.jaxor.util.SystemException;
020:
021: import java.sql.Connection;
022: import java.sql.ResultSet;
023: import java.sql.Statement;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: public class QueryTest extends MultiTableTestCase {
029: private AddressMetaRow addressMeta;
030: private CustomerMetaRow customerMeta;
031:
032: protected List getRows() {
033: List all = new ArrayList();
034: all.add(new ContactMetaRow());
035: addressMeta = new AddressMetaRow();
036: all.add(addressMeta);
037: customerMeta = new CustomerMetaRow();
038: all.add(customerMeta);
039: return all;
040: }
041:
042: public void testCustomSelectMethodOnCustomerFinder() {
043: assertEquals(0, CustomerFinder.getInstance().selectNothing()
044: .size());
045: }
046:
047: public void testQueryMethodNoResults() {
048: ContactList all = ContactFinder.query("select * from contact");
049: assertEquals(0, all.size());
050: }
051:
052: public void testQueryWithWhereClause() {
053: ContactList all = ContactFinder.find("");
054: assertEquals(0, all.size());
055: }
056:
057: public void testQueryWithWhereClauseWithResult() {
058: ContactFactory.createContact(new Long(1));
059: commit();
060: ContactFinder.getBaseQuerySQL();
061: ContactList all = ContactFinder
062: .find("where contact.contact_ID=1");
063: assertEquals(1, all.size());
064: assertNotNull(ContactFinder.findUnique(
065: "where contact.contact_ID=1", false));
066: }
067:
068: public void testQueryWithWhereClauseWithResultUsingParameter() {
069: ContactFactory.createContact(new Long(1));
070: commit();
071: QueryParams params = new QueryParams();
072: params.addLong(new Long(1));
073: assertNotNull(ContactFinder.findUnique("where contact_id=?",
074: params, false));
075: }
076:
077: public void testQueryMethodOneResult() {
078: ContactFactory.createContact();
079: commit();
080: ContactList all = ContactFinder.query("select * from contact");
081: assertEquals(1, all.size());
082: }
083:
084: public void testQueryMethodWithArgument() {
085: ContactFactory.createContact();
086: QueryParams param1 = new QueryParams();
087: param1.addString("email");
088: ContactList all = ContactFinder.query(
089: "select * from contact where email=?", param1);
090: assertEquals(0, all.size());
091:
092: commit();
093: all = ContactFinder.query(ContactFinder.getBaseQuerySQL()
094: + "where email=?", param1);
095: assertEquals(1, all.size());
096: ContactEntity ent = ContactFactory.createContact();
097: ent.setContactId(new Long(ent.getContactId().intValue() + 1));
098: ent.setEmail("fake@fakey.com");
099: commit();
100:
101: all = ContactFinder.query(ContactFinder.getBaseQuerySQL()
102: + "where email=?", param1);
103: assertEquals(1, all.size());
104: QueryParams params = new QueryParams();
105: params.addString("fake@fakey.com");
106: all = ContactFinder.query(ContactFinder.getBaseQuerySQL()
107: + "where email=?", params);
108: assertEquals(1, all.size());
109: }
110:
111: public void testEntityRow() {
112: Long id = ObjectFactory.createAddress().getAddressId();
113: commit();
114: AddressEntity address = AddressFinder.selectByPrimaryKey(id);
115: FieldAdapter[] row = address.getFields().getList();
116: for (int i = 0; i < row.length; i++) {
117: FieldAdapter fieldAdapter = row[i];
118: Object value = fieldAdapter.getValue();
119: if (!fieldAdapter.getMetaField().canBeNull())
120: assertNotNull(fieldAdapter.getName(), value);
121: }
122: }
123:
124: public void testLoadFromResultSet() throws Exception {
125: ContactEntity ent = ContactFactory.createContact();
126: commit();
127: Connection conn = getConnection();
128: Statement stm = conn.createStatement();
129: ResultSet rs = stm
130: .executeQuery(ContactFinder.getBaseQuerySQL());
131: List arrayList = new ArrayList();
132: while (rs.next())
133: arrayList.add(ContactFinder.load(rs));
134: rs.close();
135: stm.close();
136: conn.close();
137: assertEquals(1, arrayList.size());
138: assertEquals(ent, arrayList.get(0));
139: }
140:
141: public void testUniqueDynamicQuery() {
142: try {
143: ContactFinder.queryUnique(ContactFinder.getBaseQuerySQL(),
144: false);
145: fail("Should not be found");
146: } catch (EntityNotFoundException expected) {
147: }
148: }
149:
150: public void testProxyWithEntityNotFoundFailure() {
151: ContactEntity contactFinder = ContactFinder.queryUnique(
152: ContactFinder.getBaseQuerySQL(), true);
153: try {
154: contactFinder.getNameFirst(); // force resolution
155: fail("Should not be found");
156: } catch (EntityNotFoundException expected) {
157: }
158: }
159:
160: public void testUniqueQueryWithOneResult() {
161: ContactEntity ent = ContactFactory.createContact();
162: commit();
163: QueryParams params = new QueryParams();
164: params.addLong(ent.getContactId());
165: ContactEntity contactEntity = ContactFinder.queryUnique(
166: "select * from contact where contact_id=?", params,
167: false);
168: assertEquals(ent, contactEntity);
169: }
170:
171: public void testFindByEmail() {
172: ContactEntity ent = ContactFactory.createContact();
173: Boolean val = ent.getActive();
174: ent.setActive(new Boolean(!val.booleanValue()));
175: commit();
176: ContactEntity contactEntity = ContactFinder.selectByEmail(ent
177: .getEmail());
178: assertNotNull(contactEntity);
179: assertEquals(!val.booleanValue(), contactEntity.getActive()
180: .booleanValue());
181: }
182:
183: /**
184: * Only works with hypersonic
185: */
186: public void testNullQueryWithResult() {
187: if (JAXOR_Factory.getClass().equals(
188: HypersonicContextTestingFactory.class)) {
189: ContactEntity ent = ContactFactory
190: .createContact(new Long(1));
191: ent.setEmail(null);
192: ent = ContactFactory.createContact(new Long(2));
193: ent.setEmail("email");
194: commit();
195: ContactEntity contactEntity = ContactFinder
196: .selectByEmail(null);
197: assertNotNull(contactEntity);
198: assertNull(contactEntity.getEmail());
199: }
200: }
201:
202: public void testNullQueryWithNoResult() {
203: ContactEntity ent = ContactFactory.createContact(new Long(1));
204: ent.setEmail("email2");
205: ent = ContactFactory.createContact(new Long(2));
206: ent.setEmail("email");
207: commit();
208: try {
209: ContactFinder.selectByEmail(null);
210: fail("Should not be found");
211: } catch (EntityNotFoundException expected) {
212: }
213:
214: }
215:
216: public void testValidationFailure() {
217: ContactEntity entity = ContactFactory.createContact();
218: entity.setPhoneNumber(null);
219: try {
220: commit();
221: fail("should fail validation because phone number is null");
222: } catch (SystemException expected) {
223: }
224: }
225:
226: public void testJoinNoMatches() throws Exception {
227: CustomerEntity ent = ObjectFactory.create(new Long(1));
228: ent.getAddressEntity().registerDelete();
229: commit();
230: String sql = createJoinSQL(ent);
231: Connection conn = getConnection();
232: Statement stmt = conn.createStatement();
233: ResultSet rs = stmt.executeQuery(sql);
234: if (rs.next())
235: fail("should Not have any results");
236: rs.close();
237: stmt.close();
238: conn.close();
239: }
240:
241: private String createJoinSQL(CustomerEntity ent) {
242: String sql = "select " + toFields(customerMeta) + ", "
243: + toFields(addressMeta);
244: String contactTable = customerMeta.getTableName();
245: String addressTable = addressMeta.getTableName();
246: sql += " from " + contactTable + ", " + addressTable;
247: sql += " where " + addressTable + "."
248: + addressMeta.getAddressId().getColumn() + "="
249: + ent.getAddressEntity().getAddressId();
250: sql += " and " + contactTable + "."
251: + customerMeta.getCustomerId().getColumn() + "="
252: + ent.getCustomerId();
253: return sql;
254: }
255:
256: private String createOuterJoinSQL() {
257: String sql = "select " + toFields(customerMeta) + ", "
258: + toFields(addressMeta);
259: String contactTable = customerMeta.getTableName();
260: String addressTable = addressMeta.getTableName();
261: sql += " from " + contactTable + " LEFT OUTER JOIN "
262: + addressTable;
263: sql += " ON " + contactTable + "."
264: + customerMeta.getCustomerId().getColumn() + "="
265: + addressTable + "."
266: + addressMeta.getAddressId().getColumn();
267: return sql;
268: }
269:
270: public void testOuterJoin() throws Exception {
271: CustomerEntity ent = ObjectFactory.create(new Long(1));
272: ent.getAddressEntity().registerDelete();
273: commit();
274: String sql = createOuterJoinSQL();
275: Connection conn = getConnection();
276: Statement stmt = conn.createStatement();
277: ResultSet rs = stmt.executeQuery(sql);
278: if (rs.next()) {
279: assertNotNull(CustomerFinder.load(rs));
280: assertNull(AddressFinder.loadFromOuterJoin(rs));
281: }
282: if (rs.next())
283: fail("should only have one result");
284: rs.close();
285: stmt.close();
286: conn.close();
287: }
288:
289: public void testJoin() throws Exception {
290: CustomerEntity ent = ObjectFactory.create(new Long(1));
291: commit();
292: String sql = createJoinSQL(ent);
293: Connection conn = getConnection();
294: Statement stmt = conn.createStatement();
295: ResultSet rs = stmt.executeQuery(sql);
296: CustomerEntity customer = null;
297: AddressEntity address = null;
298: if (rs.next()) {
299: customer = CustomerFinder.load(rs);
300: address = AddressFinder.load(rs);
301: customer.setAddressEntity(address);
302: }
303: if (rs.next())
304: fail("should only have one result");
305: rs.close();
306: stmt.close();
307: conn.close();
308: assertEquals(ent, customer);
309: assertEquals(ent.getAddressEntity(), customer
310: .getAddressEntity());
311: assertTrue(
312: "This will be false if the customer lazy loads even though we set in the join",
313: customer.getAddressEntity() == address);
314: }
315:
316: private String toFields(MetaRow metaRow) {
317: String table = metaRow.getTableName();
318: Iterator it = metaRow.getColumns().iterator();
319: String sql = "";
320: while (it.hasNext()) {
321: MetaField field = (MetaField) it.next();
322: sql += " " + table + "." + field.getColumn();
323: if (it.hasNext())
324: sql += ",";
325: }
326: return sql;
327: }
328: }
|