01: /*
02: * User: Michael Rettig
03: * Date: Aug 11, 2002
04: * Time: 8:35:45 PM
05: */
06: package net.sourceforge.jaxor.example.tests;
07:
08: import net.sourceforge.jaxor.JaxorSession;
09: import net.sourceforge.jaxor.api.JaxorTransaction;
10: import net.sourceforge.jaxor.example.domain.*;
11: import net.sourceforge.jaxor.example.db.AxionContextTestingFactory;
12:
13: import java.sql.Connection;
14: import java.util.ArrayList;
15: import java.util.List;
16:
17: public class CacheTest extends MultiTableTestCase {
18: private static Long id = new Long(1);
19:
20: protected List getRows() {
21: List all = new ArrayList();
22: all.add(new AddressMetaRow());
23: all.add(new CustomerMetaRow());
24: all.add(new OrdersMetaRow());
25: return all;
26: }
27:
28: public void testCaching() {
29: AddressEntity entity = ObjectFactory.createAddress();
30: Long id = entity.getAddressId();
31: entity.insert();
32: assertTrue(AddressFinder.selectByPrimaryKey(id) == AddressFinder
33: .selectByPrimaryKey(id));
34: }
35:
36: public void testPrimkaryKeyCachingWithoutConnection() {
37: Long address = ObjectFactory.createAddress().getAddressId();
38: commit();
39: // We need to use selectAll()
40: // becase all queries are cached. We want to be sure we are hitting
41: // the instance cache and not the query cache.
42: AddressList list = AddressFinder.selectAll();
43: assertEquals(1, list.size());
44: AddressEntity ent = list.get(0);
45: JaxorTransaction originalTrans = disableConnection();
46: // we can find by primary key b/c it will check the cache first, so we should not need the connection.
47: try {
48: assertTrue(ent == AddressFinder.selectByPrimaryKey(address));
49: } finally {
50: JaxorSession.getJaxorContext()
51: .setTransaction(originalTrans);
52: }
53: }
54:
55: public void testWrapperCaching() {
56: ObjectFactory.create(id);
57: commit();
58: assertEquals(CustomerFinder.selectByPrimaryKey(id),
59: CustomerFinder.selectByPrimaryKey(id));
60: }
61:
62: public void testWrapperCachingWithProxies() {
63: ObjectFactory.create(id);
64: commit();
65: assertEquals(CustomerFinder.selectByPrimaryKey(id, true),
66: CustomerFinder.selectByPrimaryKey(id, false));
67: }
68:
69: private JaxorTransaction disableConnection() {
70: JaxorTransaction trans = new JaxorTransaction() {
71: public Connection getConnection() {
72: fail("Should not get a connection. Should use cache.");
73: return null;
74: }
75:
76: public void commit() {
77: }
78:
79: public void end() {
80: }
81:
82: public void rollback(Exception e) {
83: }
84: };
85: JaxorTransaction original = JaxorSession.getJaxorContext()
86: .getTransaction();
87: JaxorSession.getJaxorContext().setTransaction(trans);
88: return original;
89: }
90: }
|