001: package org.apache.ojb.broker;
002:
003: import org.apache.commons.lang.builder.ToStringBuilder;
004: import org.apache.commons.lang.builder.ToStringStyle;
005: import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
006: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
007: import org.apache.ojb.broker.query.Criteria;
008: import org.apache.ojb.broker.query.Query;
009: import org.apache.ojb.broker.query.QueryFactory;
010: import org.apache.ojb.junit.PBTestCase;
011:
012: import java.io.Serializable;
013: import java.util.Collection;
014: import java.util.Iterator;
015:
016: /**
017: *
018: * @author J. Russell Smyth
019: * @version $Id: FieldConversionTest_2.java,v 1.3 2004/05/31 22:57:21 arminw Exp $
020: */
021: public class FieldConversionTest_2 extends PBTestCase {
022: public FieldConversionTest_2(String testName) {
023: super (testName);
024: }
025:
026: public static void main(String[] args) {
027: String[] arr = { FieldConversionTest_2.class.getName() };
028: junit.textui.TestRunner.main(arr);
029: }
030:
031: public void testConvertedReferenceLookup() {
032: Collection coll = null;
033: Criteria c = null;
034: Query q = QueryFactory.newQuery(ConversionReferrer.class, c);
035: coll = broker.getCollectionByQuery(q);
036: assertTrue("There should be more than 0 matching items", coll
037: .size() > 0);
038:
039: Iterator i = coll.iterator();
040: while (i.hasNext()) {
041: ConversionReferrer cref = (ConversionReferrer) i.next();
042: assertTrue(
043: "PK value should not be converted, id should be < 1000, found "
044: + cref, cref.getPk1() < 1000);
045: assertTrue("Reference id should be > 1000, found " + cref,
046: cref.getRef1() > 1000);
047: assertTrue("Reference should be non-null, found " + cref,
048: cref.getReferred() != null);
049: assertEquals("Reference does not select correct item", cref
050: .getRef1(), cref.getReferred().getPk1());
051: }
052: }
053:
054: public void testMultipleConverted() {
055: String error = "Indicate that the field conversion was not/or multiple times called for a value, expected > 100 - found ";
056: Collection coll = null;
057: Criteria c = null;
058: Query q = QueryFactory.newQuery(ConversionReferrer.class, c);
059: coll = broker.getCollectionByQuery(q);
060: assertTrue("There should be more than 0 matching items", coll
061: .size() > 0);
062:
063: Iterator i = coll.iterator();
064: while (i.hasNext()) {
065: ConversionReferrer cref = (ConversionReferrer) i.next();
066: assertTrue(
067: "PK value should not be converted, id should be < 1000, found "
068: + cref, cref.getPk1() < 1000);
069: assertTrue("Reference should be non-null, found " + cref,
070: cref.getReferred() != null);
071: assertEquals("Reference selected incorrect item", cref
072: .getRef1(), cref.getReferred().getPk1());
073:
074: /*
075: The used conversion does the following
076: val = Integer.MAX_VALUE - val;
077: for both conversion directions.
078: The result was e.g.
079: sqlToJava: 10 --> 2147483637
080: javaToSql: 2147483637 --> 10
081:
082: */
083:
084: int value = 0;
085: value = cref.getRef1();
086: assertTrue(error + cref, value > 1000);
087:
088: value = cref.getTestId();
089: assertTrue(error + cref, value > 1000);
090:
091: value = cref.getReferred().getPk1();
092: assertTrue(error + cref, value > 1000);
093:
094: value = cref.getReferred().getTestId();
095: assertTrue(error + cref, value > 1000);
096: }
097: }
098:
099: public void testConvertedReferenceInsert() {
100: String error = "Maybe field conversion was not called or multiple times";
101: int no = 110;
102: int noRef = Integer.MAX_VALUE - 109;
103: int noTest = Integer.MAX_VALUE - 108;
104: int noTestRef = Integer.MAX_VALUE - 107;
105:
106: ConversionReferrer cref = new ConversionReferrer();
107: cref.setPk1(no);
108: cref.setTestId(noTest);
109:
110: ConversionReferred crefed = new ConversionReferred();
111: crefed.setPk1(noRef);
112: crefed.setTestId(noTestRef);
113: // set reference
114: cref.setReferred(crefed);
115:
116: broker.beginTransaction();
117: broker.store(crefed);
118: broker.store(cref);
119: broker.commitTransaction();
120:
121: broker.clearCache();
122:
123: // save id for recapturing object
124: Identity id = new Identity(cref, broker);
125: broker.clearCache();
126:
127: ConversionReferrer referrer = (ConversionReferrer) broker
128: .getObjectByIdentity(id);
129: assertNotNull(cref.getReferred());
130: assertNotNull("We should found a reference, found " + referrer,
131: referrer.getReferred());
132: assertEquals(
133: "Stored reference ID should match refed object pk",
134: referrer.getRef1(), crefed.getPk1());
135: assertEquals(error, cref.getPk1(), referrer.getPk1());
136: assertEquals(error, cref.getTestId(), referrer.getTestId());
137: assertEquals(error, cref.getReferred().getPk1(), referrer
138: .getReferred().getPk1());
139:
140: assertEquals(error, cref.getReferred().getTestId(), referrer
141: .getReferred().getTestId());
142:
143: broker.beginTransaction();
144: // delete objects
145: broker.delete(crefed);
146: broker.delete(cref);
147: broker.commitTransaction();
148: }
149:
150: //****************************************************************************
151: // inner class
152: //****************************************************************************
153: /**
154: * A conversion class for unit testing. The conversion is nonsensical - java
155: * field is difference of Integer.MAX_VALUE and db value.
156: * @author drfish
157: */
158: public static class TestInt2IntConverter implements FieldConversion {
159:
160: /** Creates a new instance of FromMaxInt2IntConversion */
161: public TestInt2IntConverter() {
162: }
163:
164: /** convert a Java object to its SQL pendant, used for insert & update */
165: public Object javaToSql(Object source)
166: throws ConversionException {
167:
168: int val = ((Integer) source).intValue();
169: val = Integer.MAX_VALUE - val;
170: return new Integer(val);
171: }
172:
173: /** convert a SQL value to a Java Object, used for SELECT */
174: public Object sqlToJava(Object source)
175: throws ConversionException {
176: int val = ((Integer) source).intValue();
177: val = Integer.MAX_VALUE - val;
178: return new Integer(val);
179: }
180: }
181:
182: //****************************************************************************
183: // inner class
184: //****************************************************************************
185: public static class ConversionReferrer implements Serializable {
186:
187: private int pk1;
188: private int testId;
189: private int ref1;
190: private ConversionReferred referred;
191:
192: /** Creates a new instance of ConversionParent */
193: public ConversionReferrer() {
194: }
195:
196: public String toString() {
197: ToStringBuilder buf = new ToStringBuilder(this ,
198: ToStringStyle.MULTI_LINE_STYLE);
199: buf.append("pk1", pk1);
200: buf.append("ref1", ref1);
201: buf.append("testId", testId);
202: buf.append("referred", referred);
203: return buf.toString();
204: }
205:
206: public int getTestId() {
207: return testId;
208: }
209:
210: public void setTestId(int testId) {
211: this .testId = testId;
212: }
213:
214: /** Getter for property pk1.
215: * @return Value of property pk1.
216: *
217: */
218: public int getPk1() {
219: return pk1;
220: }
221:
222: /** Setter for property pk1.
223: * @param pk1 New value of property pk1.
224: *
225: */
226: public void setPk1(int pk1) {
227: this .pk1 = pk1;
228: }
229:
230: /** Getter for property ref1.
231: * @return Value of property ref1.
232: *
233: */
234: public int getRef1() {
235: return ref1;
236: }
237:
238: /** Setter for property ref1.
239: * @param ref1 New value of property ref1.
240: *
241: */
242: public void setRef1(int ref1) {
243: this .ref1 = ref1;
244: }
245:
246: /** Getter for property referred.
247: * @return Value of property referred.
248: *
249: */
250: public ConversionReferred getReferred() {
251: return referred;
252: }
253:
254: /** Setter for property referred.
255: * @param referred New value of property referred.
256: *
257: */
258: public void setReferred(ConversionReferred referred) {
259: this .referred = referred;
260: }
261: }
262:
263: //****************************************************************************
264: // inner class
265: //****************************************************************************
266: public static class ConversionReferred implements Serializable {
267:
268: private int pk1;
269: private int testId;
270:
271: /** Creates a new instance of ConversionReferred */
272: public ConversionReferred() {
273: }
274:
275: public String toString() {
276: ToStringBuilder buf = new ToStringBuilder(this ,
277: ToStringStyle.MULTI_LINE_STYLE);
278: buf.append("pk1", pk1);
279: buf.append("testId", testId);
280: return buf.toString();
281: }
282:
283: public int getTestId() {
284: return testId;
285: }
286:
287: public void setTestId(int testId) {
288: this .testId = testId;
289: }
290:
291: /** Getter for property pk1.
292: * @return Value of property pk1.
293: *
294: */
295: public int getPk1() {
296: return pk1;
297: }
298:
299: /** Setter for property pk1.
300: * @param pk1 New value of property pk1.
301: *
302: */
303: public void setPk1(int pk1) {
304: this.pk1 = pk1;
305: }
306: }
307: }
|