001: package net.sourceforge.jaxor.tests;
002:
003: import junit.framework.TestCase;
004: import net.sourceforge.jaxor.JaxorContextImpl;
005: import net.sourceforge.jaxor.MetaRow;
006: import net.sourceforge.jaxor.QueryParams;
007: import net.sourceforge.jaxor.ReferenceResolver;
008: import net.sourceforge.jaxor.EntityRow;
009: import net.sourceforge.jaxor.MetaField;
010: import net.sourceforge.jaxor.mappers.FieldAdapterImpl;
011: import net.sourceforge.jaxor.mappers.StringMapper;
012: import net.sourceforge.jaxor.api.*;
013: import net.sourceforge.jaxor.example.db.HyperConnection;
014: import net.sourceforge.jaxor.example.domain.*;
015:
016: import java.sql.ResultSet;
017: import java.util.List;
018:
019: /*
020: * User: Mike
021: * Date: Sep 2, 2003
022: * Time: 9:15:39 PM
023: */
024:
025: public class ReferenceResolverTest extends TestCase {
026:
027: private static final AddressEntity address = new AddressImpl();
028: private static final AddressMetaRow meta = new AddressMetaRow();
029: private static final Long ADDRESS_ID = new Long(1);
030: private MockCustomer customer;
031: private static final String BASE_SQL = "where address_ID=?";
032:
033: protected void setUp() throws Exception {
034: customer = new MockCustomer("table");
035: }
036:
037: public void testGetSql() {
038: ReferenceResolver resolver = createResolver();
039: resolver.add(meta.getCity(), meta.getCity());
040: assertEquals("where address_ID=? AND city=?", resolver
041: .createFindSql());
042: }
043:
044: public void testResolvingWithNoContext() {
045: CustomerEntity cust = new CustomerBase();
046: assertNull(cust.getAddressEntity());
047: }
048:
049: private ReferenceResolver createResolver() {
050: JaxorContextImpl conn = new JaxorContextImpl(
051: HyperConnection.INSTANCE);
052: customer.setJaxorContext(conn);
053: ReferenceResolver resolver = new ReferenceResolver(
054: MockAddressFinder.class, customer, true);
055: resolver.add(meta.getAddressId(), meta.getAddressId());
056: assertEquals(BASE_SQL, resolver.createFindSql());
057: return resolver;
058: }
059:
060: public void testGettingBeanValues() {
061: ReferenceResolver resolver = createResolver();
062: assertEquals(resolver.getValue("AddressId"), ADDRESS_ID);
063: }
064:
065: public void testResolving() {
066: ReferenceResolver resolver = createResolver();
067: assertTrue(address == resolver.getObject());
068: }
069:
070: private class MockCustomer extends EntityRow {
071:
072: public MockCustomer(String table) {
073: super (table);
074: MetaField metaData = new MetaField("AddressId",
075: "AddressId", false, true, false, false,
076: StringMapper.class);
077: FieldAdapter field = new FieldAdapterImpl(metaData, null);
078: field.setValue(ADDRESS_ID);
079: setFields(new FieldAdapter[] { field });
080: }
081: }
082:
083: public static class MockAddressFinder implements FinderAdapter,
084: Finder {
085:
086: public MockAddressFinder(JaxorContext conn) {
087:
088: }
089:
090: public EntityInterface newInstance() {
091: return null;
092: }
093:
094: public MetaRow getMetaRow() {
095: return null;
096: }
097:
098: public Finder getFinder() {
099: return this ;
100: }
101:
102: public String getBaseQuerySQL() {
103: return null;
104: }
105:
106: public EntityInterface load(ResultSet rs) {
107: return null;
108: }
109:
110: public void registerNew(EntityInterface entity) {
111: }
112:
113: public boolean entityExists(QueryParams ps) {
114: return false;
115: }
116:
117: public List selectAll() {
118: return null;
119: }
120:
121: public QueryResult query(String sql) {
122: return null;
123: }
124:
125: public QueryResult query(String sql, QueryParams args) {
126: return null;
127: }
128:
129: public QueryResult find(String whereClause) {
130: return null;
131: }
132:
133: public QueryResult find(String whereClause, QueryParams p) {
134: assertEquals(BASE_SQL, whereClause);
135: return new QueryResult() {
136: public List list() {
137: return null;
138: }
139:
140: public String getSQL() {
141: return "";
142: }
143:
144: public EntityResultSet resultSet() {
145: return null;
146: }
147:
148: public EntityInterface entity() {
149: return address;
150: }
151:
152: public EntityInterface entityProxy() {
153: return null;
154: }
155:
156: public QueryParams getParams() {
157: return new QueryParams();
158: }
159:
160: public JaxorPreparedStatement preparedStatement() {
161: return null;
162: }
163:
164: };
165: }
166:
167: public EntityInterface selectByPrimaryKey(QueryParams key,
168: boolean lazy) {
169: return null;
170: }
171: }
172: }
|