001: /*
002: * User: Michael Rettig
003: * Date: Sep 20, 2002
004: * Time: 8:13:31 PM
005: */
006: package net.sourceforge.jaxor.example.tests;
007:
008: import net.sourceforge.jaxor.EntityNotFoundException;
009: import net.sourceforge.jaxor.JaxorSession;
010: import net.sourceforge.jaxor.MetaRow;
011: import net.sourceforge.jaxor.api.JaxorTransaction;
012: import net.sourceforge.jaxor.example.simple.*;
013: import net.sourceforge.jaxor.util.DynamicDecorator;
014: import net.sourceforge.jaxor.util.MethodCache;
015:
016: import java.lang.reflect.Method;
017: import java.sql.Connection;
018:
019: public class QueryCacheTest extends TableTestCase {
020:
021: private int count = 0;
022:
023: protected MetaRow getRow() {
024: return new ContactMetaRow();
025: }
026:
027: protected void prep() throws Exception {
028: super .prep();
029: count = 0;
030: // cannot use weak references during testing b/c objects may be garbage collected at any time
031: MethodCache.USE_WEAK_REFERENCES = false;
032: setCountingConnection();
033: }
034:
035: private void setCountingConnection() {
036: final JaxorTransaction old = JaxorSession.getJaxorContext()
037: .getTransaction();
038: JaxorTransaction fact = new JaxorTransaction() {
039: public Connection getConnection() {
040: return (Connection) DynamicDecorator.create(old
041: .getConnection(), new MyOverride());
042: }
043:
044: public void commit() {
045: }
046:
047: public void end() {
048: }
049:
050: public void rollback(Exception e) {
051: }
052: };
053: JaxorSession.getJaxorContext().setTransaction(fact);
054: }
055:
056: protected void cleanUp() throws Exception {
057: super .cleanUp();
058: MethodCache.USE_WEAK_REFERENCES = true;
059: }
060:
061: public void testCache() {
062: assertEquals(0, count);
063: ContactFinder.selectAll().size();
064: assertEquals(1, count);
065: ContactFinder.selectAll().size();
066: assertEquals(1, count);
067: }
068:
069: public void testInsertAndCache() {
070: assertEquals(0, count);
071: ContactFinder.selectAll().size();
072: assertEquals(1, count);
073: ContactFinder.selectAll().size();
074: assertEquals(1, count);
075: ContactFactory.createContact();
076: getJaxor().flush();
077: assertEquals(2, count);
078: assertEquals(1, ContactFinder.selectAll().size());
079: assertEquals(3, count);
080: assertEquals(1, ContactFinder.selectAll().size());
081: assertEquals(3, count);
082: }
083:
084: public void testDefensiveCopyOfResults() {
085: ContactList all = ContactFinder.selectAll();
086: assertEquals(0, all.size());
087: all.add(new ContactBase());
088: assertEquals(0, ContactFinder.selectAll().size());
089: }
090:
091: public void testLazyLoading() {
092: assertEquals(0, count);
093: ContactFinder.selectAll();
094: assertEquals(0, count);
095: ContactEntity contact = ContactFinder.selectByPrimaryKey(
096: new Long(1), true);
097: assertEquals(0, count);
098: try {
099: contact.getEmail();
100: fail("Should not be found");
101: } catch (EntityNotFoundException expected) {
102: assertContains("Parameters: [1]", expected.getMessage());
103: }
104: assertEquals(1, count);
105: }
106:
107: private class MyOverride extends DynamicDecorator.Override {
108: public MyOverride() {
109: super ("prepareStatement", new Class[] { String.class });
110: }
111:
112: protected Object invoke(Object delegate, Object[] args,
113: Method method) throws Throwable {
114: count++;
115: return method.invoke(delegate, args);
116: }
117: }
118: }
|