001: package org.apache.ojb.broker;
002:
003: import junit.framework.TestCase;
004: import org.apache.commons.lang.builder.ToStringBuilder;
005: import org.apache.commons.lang.builder.ToStringStyle;
006: import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
007: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
008:
009: import java.io.Serializable;
010:
011: /**
012: * Test case to check the field conversion.
013: *
014: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
015: * @version $Id: FieldConversionTest.java,v 1.2.4.1 2005/12/13 18:18:25 arminw Exp $
016: */
017: public class FieldConversionTest extends TestCase {
018: private PersistenceBroker broker;
019:
020: public static void main(String[] args) {
021: String[] arr = { FieldConversionTest.class.getName() };
022: junit.textui.TestRunner.main(arr);
023: }
024:
025: /**
026: * Insert the method's description here.
027: * Creation date: (06.12.2000 21:58:53)
028: */
029: public void setUp() throws PBFactoryException {
030: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
031: }
032:
033: /**
034: * Insert the method's description here.
035: * Creation date: (06.12.2000 21:59:14)
036: */
037: public void tearDown() {
038: try {
039: broker.clearCache();
040: broker.close();
041: } catch (PersistenceBrokerException e) {
042: }
043: }
044:
045: public void testConversion() {
046: int id = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
047: ConversionVO vo = new ConversionVO(null, new ConversionId(
048: new Integer(id)), null, new ConversionId(new Integer(
049: id + 1)));
050: broker.beginTransaction();
051: broker.store(vo);
052: broker.commitTransaction();
053:
054: Identity identity = new Identity(vo, broker);
055: broker.clearCache();
056: ConversionVO find_vo = (ConversionVO) broker
057: .getObjectByIdentity(identity);
058: }
059:
060: public static class FieldConversionConversionIdToInteger implements
061: FieldConversion {
062: public Object javaToSql(Object source)
063: throws ConversionException {
064: if (!(source instanceof ConversionId)) {
065: throw new ConversionException(
066: "Wrong java field type when java-->sql, expected "
067: + ConversionId.class.getClass()
068: + ", found " + source.getClass());
069: }
070: return ((ConversionId) source).getConversionId();
071: }
072:
073: public Object sqlToJava(Object source)
074: throws ConversionException {
075: if (!(source instanceof Integer)) {
076: throw new ConversionException(
077: "Wrong java field type when java-->sql, expected java.lang.Integer, found "
078: + source.getClass());
079: }
080: return new ConversionId((Integer) source);
081: }
082: }
083:
084: public static class FieldConversionLongToInteger implements
085: FieldConversion {
086: public Object javaToSql(Object source)
087: throws ConversionException {
088: if (source == null)
089: return null;
090: if (!(source instanceof Long)) {
091: throw new ConversionException(
092: "Wrong java field type when java-->sql, expected java.lang.Long, found "
093: + source.getClass());
094: }
095: return new Integer(((Long) source).intValue());
096: }
097:
098: public Object sqlToJava(Object source)
099: throws ConversionException {
100: if (source == null)
101: return null;
102: if (!(source instanceof Integer)) {
103: throw new ConversionException(
104: "Wrong java field type when java-->sql, expected java.lang.Integer, found "
105: + source.getClass());
106: }
107: return new Long(((Integer) source).longValue());
108: }
109: }
110:
111: public static class ConversionId implements Serializable {
112: private Integer conversionId;
113:
114: public ConversionId(Integer conversionId) {
115: this .conversionId = conversionId;
116: }
117:
118: public Integer getConversionId() {
119: return conversionId;
120: }
121:
122: public void setConversionId(Integer conversionId) {
123: this .conversionId = conversionId;
124: }
125:
126: public String toString() {
127: return new ToStringBuilder(this ,
128: ToStringStyle.DEFAULT_STYLE).append("conversionId",
129: conversionId).toString();
130: }
131: }
132:
133: public static class ConversionVO implements Serializable {
134: Long pkWithAutoIncrement;
135: ConversionId pkWithoutAutoIncrement;
136: Long normalWithAutoIncrement;
137: ConversionId normalWithoutAutoIncrement;
138:
139: public ConversionVO() {
140: }
141:
142: public ConversionVO(Long pkWithAutoIncrement,
143: ConversionId pkWithoutAutoIncrement,
144: Long normalWithAutoIncrement,
145: ConversionId normalWithoutAutoIncrement) {
146: this .pkWithAutoIncrement = pkWithAutoIncrement;
147: this .pkWithoutAutoIncrement = pkWithoutAutoIncrement;
148: this .normalWithAutoIncrement = normalWithAutoIncrement;
149: this .normalWithoutAutoIncrement = normalWithoutAutoIncrement;
150: }
151:
152: public String toString() {
153: return new ToStringBuilder(this ,
154: ToStringStyle.MULTI_LINE_STYLE).append(
155: "pkWithAutoIncrement", pkWithAutoIncrement).append(
156: "pkWithoutAutoIncrement", pkWithoutAutoIncrement)
157: .append("normalWithAutoIncrement",
158: normalWithAutoIncrement).append(
159: "normalWithoutAutoIncrement",
160: normalWithoutAutoIncrement).toString();
161: }
162:
163: public Long getPkWithAutoIncrement() {
164: return pkWithAutoIncrement;
165: }
166:
167: public void setPkWithAutoIncrement(Long pkWithAutoIncrement) {
168: this .pkWithAutoIncrement = pkWithAutoIncrement;
169: }
170:
171: public ConversionId getPkWithoutAutoIncrement() {
172: return pkWithoutAutoIncrement;
173: }
174:
175: public void setPkWithoutAutoIncrement(
176: ConversionId pkWithoutAutoIncrement) {
177: this .pkWithoutAutoIncrement = pkWithoutAutoIncrement;
178: }
179:
180: public Long getNormalWithAutoIncrement() {
181: return normalWithAutoIncrement;
182: }
183:
184: public void setNormalWithAutoIncrement(
185: Long normalWithAutoIncrement) {
186: this .normalWithAutoIncrement = normalWithAutoIncrement;
187: }
188:
189: public ConversionId getNormalWithoutAutoIncrement() {
190: return normalWithoutAutoIncrement;
191: }
192:
193: public void setNormalWithoutAutoIncrement(
194: ConversionId normalWithoutAutoIncrement) {
195: this.normalWithoutAutoIncrement = normalWithoutAutoIncrement;
196: }
197: }
198: }
|