01: package net.sourceforge.jaxor.tests;
02:
03: import net.sourceforge.jaxor.JaxorContextImpl;
04: import net.sourceforge.jaxor.MetaRow;
05: import net.sourceforge.jaxor.api.JaxorContext;
06: import net.sourceforge.jaxor.api.JaxorTransaction;
07: import net.sourceforge.jaxor.example.domain.*;
08: import net.sourceforge.jaxor.example.tests.TableTestCase;
09:
10: import java.sql.Connection;
11:
12: /*
13: * User: Mike
14: * Date: Sep 2, 2003
15: * Time: 7:01:26 PM
16: */
17:
18: public class JaxorConnectionImplTest extends TableTestCase {
19:
20: protected MetaRow getRow() {
21: return new AddressMetaRow();
22: }
23:
24: public void testSettingConnectionOnEntity() {
25: AddressEntity entity = ObjectFactory.createAddress(new Long(1));
26: assertTrue(entity.getJaxorContext() == getJaxor());
27: commit();
28: assertTrue(AddressFinder.selectByPrimaryKey(new Long(1))
29: .getJaxorContext() == getJaxor());
30: }
31:
32: public void testNullConnection() {
33: try {
34: JaxorContext context = new JaxorContextImpl(
35: (Connection) null);
36: fail("Should fail");
37: } catch (IllegalArgumentException e) {
38: assertEquals("Cannot pass a null connection", e
39: .getMessage());
40: }
41: }
42:
43: public void testNullConnectionFromTransaction() {
44: JaxorContext context = getNullContext();
45: context.commit();
46: }
47:
48: private JaxorContext getNullContext() {
49: JaxorTransaction trans = new NullJaxorTransaction();
50: JaxorContext context = new JaxorContextImpl(trans);
51: return context;
52: }
53:
54: public void testNullConnectionWithQuery() {
55: JaxorContext context = getNullContext();
56: AddressFinderBase finder = new AddressFinderBase(context);
57: try {
58: finder.findAll().size();
59: } catch (NullPointerException npe) {
60: assertEquals("JaxorTransaction returned a null connection",
61: npe.getMessage());
62: }
63: }
64:
65: public void testNullCommit() {
66: JaxorContext context = getNullContext();
67: AddressFinderBase finder = new AddressFinderBase(context);
68: AddressEntity address = finder.newInstance(new Long(123));
69: ObjectFactory.setDefaultFields(address);
70: try {
71: context.commit();
72: } catch (NullPointerException npe) {
73: assertEquals("JaxorTransaction returned a null connection",
74: npe.getMessage());
75: }
76:
77: }
78:
79: private static class NullJaxorTransaction implements
80: JaxorTransaction {
81: public Connection getConnection() {
82: return null;
83: }
84:
85: public void commit() {
86: }
87:
88: public void end() {
89: }
90:
91: public void rollback(Exception e) {
92: }
93: }
94: }
|