001: package org.apache.ojb.odmg;
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.junit.ODMGTestCase;
008: import org.odmg.OQLQuery;
009: import org.odmg.Transaction;
010:
011: import java.io.Serializable;
012: import java.math.BigDecimal;
013: import java.util.List;
014:
015: /**
016: * class FieldConversion_ForeigenKeyTest,
017: * check the field conversion behaviour.
018: *
019: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
020: * @version $Id: FieldConversionTest_4.java,v 1.1.2.2 2005/08/08 13:18:49 arminw Exp $
021: */
022: public class FieldConversionTest_4 extends ODMGTestCase {
023: public static void main(String[] args) {
024: String[] arr = { FieldConversionTest_4.class.getName() };
025: junit.textui.TestRunner.main(arr);
026: }
027:
028: public void testSelfReferingParent() throws Exception {
029: String strQuery = "select allNodes from "
030: + Node.class.getName();
031: long id = System.currentTimeMillis();
032: Node node = new Node(id, null, true);
033: node.setParent(node);
034:
035: List result;
036: int before;
037: TransactionExt tx = (TransactionExt) odmg.newTransaction();
038: try {
039: tx.begin();
040:
041: OQLQuery query = odmg.newOQLQuery();
042: query.create(strQuery);
043: result = (List) query.execute();
044: before = result.size();
045:
046: database.makePersistent(node);
047: tx.commit();
048:
049: tx.begin();
050: tx.getBroker().clearCache();
051: query = odmg.newOQLQuery();
052: query.create(strQuery);
053: result = (List) query.execute();
054: tx.commit();
055: } finally {
056: if (tx != null && tx.isOpen()) {
057: tx.abort();
058: }
059: }
060: int after = result.size();
061: assertFalse(after == 0);
062: assertEquals(before + 1, after);
063:
064: tx.begin();
065: database.deletePersistent(node);
066: tx.commit();
067:
068: OQLQuery query = odmg.newOQLQuery();
069: query.create(strQuery);
070: result = (List) query.execute();
071: after = result.size();
072: assertEquals(before, after);
073: }
074:
075: public void testMakePersistentNode() throws Exception {
076: String strQuery = "select allNodes from "
077: + Node.class.getName();
078: long id = System.currentTimeMillis();
079: Node node = new Node(id, null, true);
080: Node child = new Node(id + 1, node, false);
081:
082: List result;
083: int before;
084: Transaction tx = odmg.newTransaction();
085: try {
086: tx.begin();
087:
088: OQLQuery query = odmg.newOQLQuery();
089: query.create(strQuery);
090: result = (List) query.execute();
091: before = result.size();
092:
093: database.makePersistent(child);
094:
095: tx.commit();
096:
097: tx.begin();
098: query = odmg.newOQLQuery();
099: query.create(strQuery);
100: result = (List) query.execute();
101: tx.commit();
102: } finally {
103: if (tx != null && tx.isOpen()) {
104: tx.abort();
105: }
106: }
107:
108: int after = result.size();
109:
110: assertFalse(after == 0);
111: assertEquals(before + 2, after);
112:
113: }
114:
115: public void testLockNode() throws Exception {
116: String strQuery = "select allNodes from "
117: + Node.class.getName();
118: long id = System.currentTimeMillis();
119: Node node = new Node(id, null, true);
120: Node child = new Node(id + 1, node, false);
121:
122: Transaction tx = odmg.newTransaction();
123: List result;
124: int before;
125:
126: try {
127: tx.begin();
128: OQLQuery query = odmg.newOQLQuery();
129: query.create(strQuery);
130: result = (List) query.execute();
131: before = result.size();
132:
133: tx.lock(child, Transaction.WRITE);
134: tx.commit();
135:
136: tx.begin();
137: query = odmg.newOQLQuery();
138: query.create(strQuery);
139: result = (List) query.execute();
140: tx.commit();
141: } finally {
142: if (tx != null && tx.isOpen()) {
143: tx.abort();
144: }
145: }
146:
147: int after = result.size();
148:
149: assertFalse(after == 0);
150: assertEquals(before + 2, after);
151: }
152:
153: //****************************************************************************
154: // inner class
155: //****************************************************************************
156: public static class LongToBigDecimalConversion implements
157: FieldConversion {
158: public LongToBigDecimalConversion() {
159: }
160:
161: public Object javaToSql(Object source)
162: throws ConversionException {
163: Object ret;
164: if (source == null) {
165: ret = null;
166: } else if (source instanceof Long) {
167: ret = new BigDecimal(((Long) source).doubleValue());
168: } else {
169: throw new ConversionException(
170: "java-->sql, expected type was"
171: + Long.class.getClass()
172: + ", found type " + source.getClass());
173: }
174: return ret;
175: }
176:
177: public Object sqlToJava(Object source)
178: throws ConversionException {
179: Object ret;
180: if (source == null) {
181: ret = null;
182: } else if (source instanceof BigDecimal) {
183: ret = new Long(((BigDecimal) source).longValue());
184: } else {
185: throw new ConversionException(
186: "sql-->java, expected type was"
187: + BigDecimal.class.getClass()
188: + ", found type " + source.getClass());
189: }
190: return ret;
191: }
192: }
193:
194: //****************************************************************************
195: // inner class
196: //****************************************************************************
197: public static class Node implements Serializable {
198: private long uid; // primary key
199: private long refId;
200: private boolean nodeState;
201: Node parent;
202:
203: public Node() {
204: }
205:
206: public Node(long uid, Node parent, boolean nodeState) {
207: this .uid = uid;
208: this .parent = parent;
209: this .nodeState = nodeState;
210: }
211:
212: public String toString() {
213: return ToStringBuilder.reflectionToString(this ,
214: ToStringStyle.MULTI_LINE_STYLE);
215: }
216:
217: public long getUid() {
218: return uid;
219: }
220:
221: public void setUid(long uid) {
222: this .uid = uid;
223: }
224:
225: public boolean isState() {
226: return nodeState;
227: }
228:
229: public void setState(boolean state) {
230: this .nodeState = state;
231: }
232:
233: public long getRefId() {
234: return refId;
235: }
236:
237: public void setRefId(long refId) {
238: this .refId = refId;
239: }
240:
241: public Node getParent() {
242: return parent;
243: }
244:
245: public void setParent(Node parent) {
246: this.parent = parent;
247: }
248: }
249: }
|