001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
004: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
005: import org.apache.ojb.broker.query.Criteria;
006: import org.apache.ojb.broker.query.QueryFactory;
007: import org.apache.ojb.junit.PBTestCase;
008:
009: import java.io.Serializable;
010: import java.math.BigDecimal;
011:
012: /**
013: * Test using field conversions for PK values.
014: *
015: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
016: * @version $Id: FieldConversionTest_3.java,v 1.4.2.2 2005/03/03 17:18:17 arminw Exp $
017: */
018: public class FieldConversionTest_3 extends PBTestCase {
019:
020: public static void main(String[] args) {
021: String[] arr = { FieldConversionTest_3.class.getName() };
022: junit.textui.TestRunner.main(arr);
023: }
024:
025: public void tearDown() {
026: try {
027: broker.clearCache();
028: super .tearDown();
029: } catch (Exception e) {
030: //ignored
031: }
032: }
033:
034: /**
035: * store nested classes needed field conversion of a primary key field.
036: */
037: public void testStoreNestedNodes() throws Exception {
038: //String strQuery = "select allNodes from " + Node.class.getName();
039: long id = System.currentTimeMillis();
040: Node node = new Node(id, null, true);
041: Node child = new Node(id + 1, node, false);
042:
043: int before;
044:
045: broker.beginTransaction();
046: before = broker.getCount(QueryFactory.newQuery(Node.class,
047: (Criteria) null));
048:
049: broker.store(child);
050:
051: broker.commitTransaction();
052:
053: broker.beginTransaction();
054: int after = broker.getCount(QueryFactory.newQuery(Node.class,
055: (Criteria) null));
056: broker.commitTransaction();
057:
058: assertFalse(after == 0);
059: assertEquals(before + 2, after);
060: }
061:
062: /**
063: * store class needed field conversion of a primary key field.
064: */
065: public void testStoreNode() throws Exception {
066: //String strQuery = "select allNodes from " + Node.class.getName();
067: long id = System.currentTimeMillis();
068: Node node = new Node(id, null, false);
069:
070: int before;
071:
072: broker.beginTransaction();
073: before = broker.getCount(QueryFactory.newQuery(Node.class,
074: (Criteria) null));
075:
076: broker.store(node);
077:
078: broker.commitTransaction();
079:
080: broker.beginTransaction();
081: int after = broker.getCount(QueryFactory.newQuery(Node.class,
082: (Criteria) null));
083: broker.commitTransaction();
084:
085: assertFalse(after == 0);
086: assertEquals(before + 1, after);
087: }
088:
089: /**
090: * Assert that StatementManager handles NULL-values correct when binding deletions.
091: */
092: public void testDeleteNode() throws Exception {
093: NodeWoAutoInc node = new NodeWoAutoInc(0);
094:
095: try {
096: broker.beginTransaction();
097: // mkalen: Try to issue delete with numeric field=NULL after field conversion,
098: // which will make eg Oracle JDBC throw SQLException if not using stmt.setNull()
099: broker.delete(node);
100: broker.commitTransaction();
101: } finally {
102: if (broker.isInTransaction()) {
103: try {
104: broker.abortTransaction();
105: } catch (Throwable ignore) {
106: //ignore
107: }
108: }
109: }
110: }
111:
112: //****************************************************************************
113: // inner class
114: //****************************************************************************
115: public static class LongToBigDecimalConversion implements
116: FieldConversion {
117: private static final Long NULL_BIG_DECIMAL = null;
118: private static final Long ZERO = new Long(0);
119:
120: public LongToBigDecimalConversion() {
121: }
122:
123: public Object javaToSql(Object source)
124: throws ConversionException {
125: if (source == null)
126: return null;
127: Object ret;
128: if (source instanceof Long) {
129: if (ZERO.equals(source)) {
130: ret = NULL_BIG_DECIMAL;
131: } else {
132: ret = new BigDecimal(((Long) source).doubleValue());
133: }
134: } else {
135: throw new ConversionException(
136: "java-->sql, expected type was"
137: + Long.class.getClass()
138: + ", found type " + source.getClass());
139: }
140: return ret;
141: }
142:
143: public Object sqlToJava(Object source)
144: throws ConversionException {
145: if (source == null)
146: return null;
147: Object ret;
148: if (source instanceof BigDecimal) {
149: ret = new Long(((BigDecimal) source).longValue());
150: } else {
151: throw new ConversionException(
152: "sql-->java, expected type was"
153: + BigDecimal.class.getClass()
154: + ", found type " + source.getClass());
155: }
156: return ret;
157: }
158: }
159:
160: //****************************************************************************
161: // inner class
162: //****************************************************************************
163: public static class Node implements Serializable {
164: private long uid; // primary key
165: private long refId;
166: private boolean nodeState;
167: Node parent;
168:
169: public Node() {
170: }
171:
172: public Node(long uid, Node parent, boolean nodeState) {
173: this .uid = uid;
174: this .parent = parent;
175: this .nodeState = nodeState;
176: }
177:
178: public long getUid() {
179: return uid;
180: }
181:
182: public void setUid(long uid) {
183: this .uid = uid;
184: }
185:
186: public boolean isNodeState() {
187: return nodeState;
188: }
189:
190: public void setNodeState(boolean nodeState) {
191: this .nodeState = nodeState;
192: }
193:
194: public long getRefId() {
195: return refId;
196: }
197:
198: public void setRefId(long refId) {
199: this .refId = refId;
200: }
201:
202: public Node getParent() {
203: return parent;
204: }
205:
206: public void setParent(Node parent) {
207: this .parent = parent;
208: }
209: }
210:
211: public static class NodeWoAutoInc implements Serializable {
212: private long uid; // primary key, no auto increment
213:
214: public NodeWoAutoInc() {
215: }
216:
217: public NodeWoAutoInc(long uid) {
218: this .uid = uid;
219: }
220:
221: public long getUid() {
222: return uid;
223: }
224:
225: public void setUid(long uid) {
226: this.uid = uid;
227: }
228: }
229:
230: }
|