Source Code Cross Referenced for FieldConversionTest_3.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.broker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.