Source Code Cross Referenced for FieldConversionTest_4.java in  » Database-ORM » db-ojb » org » apache » ojb » odmg » 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.odmg 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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