Source Code Cross Referenced for TestQueries.java in  » Database-ORM » openjpa » org » apache » openjpa » persistence » kernel » 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 » openjpa » org.apache.openjpa.persistence.kernel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * TestQueries.java
003:         *
004:         * Created on October 13, 2006, 4:27 PM
005:         *
006:         * To change this template, choose Tools | Template Manager
007:         * and open the template in the editor.
008:         */
009:
010:        /*
011:         * Licensed to the Apache Software Foundation (ASF) under one
012:         * or more contributor license agreements.  See the NOTICE file
013:         * distributed with this work for additional information
014:         * regarding copyright ownership.  The ASF licenses this file
015:         * to you under the Apache License, Version 2.0 (the
016:         * "License"); you may not use this file except in compliance
017:         * with the License.  You may obtain a copy of the License at
018:         *
019:         * http://www.apache.org/licenses/LICENSE-2.0
020:         *
021:         * Unless required by applicable law or agreed to in writing,
022:         * software distributed under the License is distributed on an
023:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024:         * KIND, either express or implied.  See the License for the
025:         * specific language governing permissions and limitations
026:         * under the License.    
027:         */
028:        package org.apache.openjpa.persistence.kernel;
029:
030:        import java.util.Collection;
031:        import java.util.Iterator;
032:
033:        import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
034:        import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest2;
035:
036:        import org.apache.openjpa.persistence.Extent;
037:        import org.apache.openjpa.persistence.OpenJPAEntityManager;
038:        import org.apache.openjpa.persistence.OpenJPAQuery;
039:
040:        public class TestQueries extends BaseKernelTest {
041:
042:            private static final int CHILD_COUNT = 3;
043:            private int id = 10000;
044:
045:            public TestQueries(String name) {
046:                super (name);
047:            }
048:
049:            public TestQueries() {
050:            }
051:
052:            public void setUp() throws Exception {
053:                super .setUp();
054:                deleteAll(RuntimeTest1.class);
055:            }
056:
057:            public void testSimpleQuery() {
058:                OpenJPAEntityManager pm = getPM();
059:
060:                persist(newRuntimeTest1("SimpleQueryA", 50), pm);
061:                persist(newRuntimeTest2("SimpleQueryB", 50), pm);
062:
063:                Collection results = runQuery(RuntimeTest1.class, false,
064:                        "stringField = \'SimpleQueryA\'", pm);
065:                assertEquals(1, results.size());
066:
067:                results = runQuery(RuntimeTest1.class, true, "intField1 = 50",
068:                        pm);
069:                assertEquals(2, results.size());
070:
071:                results = runQuery(RuntimeTest2.class, true, "intField1 = 50",
072:                        pm);
073:                assertEquals(1, results.size());
074:
075:                results = runQuery(RuntimeTest2.class, false, "intField1 = 50",
076:                        pm);
077:                assertEquals(1, results.size());
078:
079:                endEm(pm);
080:            }
081:
082:            public void testAndQuery() {
083:                String query = "intField1 > 10 AND o.intField1 < 50";
084:
085:                for (int currentCount = 1; currentCount < 15; currentCount++) {
086:                    OpenJPAEntityManager pm = getPM();
087:
088:                    // make sure that none exist
089:                    deleteByQuery(RuntimeTest1.class, false, query, pm);
090:
091:                    startTx(pm);
092:                    for (int i = 1; i <= currentCount; i++)
093:                        pm.persist(newRuntimeTest1("AndQueryTest", 30 + i));
094:                    endTx(pm);
095:
096:                    // verify that the query works.
097:                    Collection results = runQuery(RuntimeTest1.class, false,
098:                            query, pm);
099:                    assertEquals(currentCount, results.size());
100:                    endEm(pm);
101:                }
102:            }
103:
104:            public void testRelationQuery() {
105:                relationQuery(10, "JOE", 20);
106:                relationQuery(99, "BOB", 2);
107:                relationQuery(3, "MARTHA", 1);
108:                relationQuery(5, "brenda", 43);
109:                relationQuery(43, "SarA", 55);
110:            }
111:
112:            public void relationQuery(int intField, String stringField,
113:                    int count) {
114:                OpenJPAEntityManager pm = getPM();
115:
116:                String query = "selfOneOne.intField1 = " + intField
117:                        + " AND o.selfOneOne.stringField = '" + stringField
118:                        + "'";
119:                deleteByQuery(RuntimeTest1.class, true, query, pm);
120:
121:                // we go up the to max count, adding to the people, and validate
122:                // with a query each time.
123:                for (int currentcount = 0; currentcount <= count; currentcount++) {
124:                    if (currentcount != 0)
125:                        persist(newRuntimeTest2(stringField, intField), pm);
126:
127:                    Collection results = runQuery(RuntimeTest1.class, true,
128:                            query, pm);
129:                    assertEquals("query (" + query + ") failed to yield "
130:                            + currentcount + " instances", currentcount
131:                            * CHILD_COUNT, results.size());
132:                }
133:
134:                endEm(pm);
135:            }
136:
137:            public void testQueryExecuteThrowsExceptionWhenNoNTR() {
138:                OpenJPAEntityManager pm = getPM();
139:                startTx(pm);
140:                pm.persist(newRuntimeTest1("Query", 10));
141:                endTx(pm);
142:
143:                pm.setNontransactionalRead(false);
144:                //        OpenJPAQuery q = pm.createNativeQuery("",RuntimeTest1.class);
145:                OpenJPAQuery q = pm.createQuery("SELECT o FROM RuntimeTest1 o");
146:                try {
147:                    Collection c = (Collection) q.getResultList();
148:                    fail("Query.execute() should have thrown a JDOException when "
149:                            + "PM is outside a Transaction and NTR==false");
150:                } catch (Exception jdoe) {
151:                    // good
152:                    startTx(pm);
153:                    q.getResultList();
154:                    rollbackTx(pm);
155:                }
156:            }
157:
158:            /**
159:             * Delete the results of a query, so we can reinsert and test.
160:             */
161:            private void deleteByQuery(Class type, boolean subs, String filter,
162:                    OpenJPAEntityManager pm) {
163:                startTx(pm);
164:                Extent extent = pm.createExtent(type, subs);
165:                //FIXME jthomas
166:                //        OpenJPAQuery query = pm.newQuery(extent, filter);
167:                //        Collection items = (Collection) query.execute();
168:                String cstrng = type.getSimpleName();
169:
170:                OpenJPAQuery query = pm.createQuery("SELECT o FROM " + cstrng
171:                        + " o WHERE o." + filter);
172:                query.setSubclasses(subs);
173:                Collection items = (Collection) query.getResultList();
174:                for (Iterator i = items.iterator(); i.hasNext();)
175:                    pm.remove(i.next());
176:
177:                //FIXME jthomas
178:                //query = pm.newQuery(extent, filter);
179:
180:                query = pm.createQuery("SELECT o FROM " + cstrng
181:                        + " o WHERE o." + filter);
182:                query.setSubclasses(subs);
183:                items = query.getResultList();
184:                endTx(pm);
185:                assertEquals("after deleting from query (" + query
186:                        + "), there should have been zero items", 0, items
187:                        .size());
188:            }
189:
190:            private Collection runQuery(Class type, boolean subs,
191:                    String filter, OpenJPAEntityManager pm) {
192:                Extent extent = pm.createExtent(type, subs);
193:                //FIXME jthomas
194:                //Query query = pm.newQuery(extent, filter);
195:                String cstrng = type.getName();
196:                OpenJPAQuery query = pm.createQuery("SELECT o FROM " + cstrng
197:                        + " o WHERE o." + filter);
198:                query.setSubclasses(subs);
199:                Collection results = (Collection) query.getResultList();
200:                return results;
201:            }
202:
203:            private void persist(RuntimeTest1 parent, OpenJPAEntityManager pm) {
204:                RuntimeTest1 child;
205:                for (int i = 0; i < CHILD_COUNT; i++) {
206:                    child = newRuntimeTest1("CHILD" + i, i * 10);
207:                    child.setSelfOneOne(parent);
208:                    parent.getSelfOneMany().add(child);
209:                }
210:
211:                startTx(pm);
212:                pm.persist(parent);
213:                endTx(pm);
214:            }
215:
216:            private RuntimeTest1 newRuntimeTest1(String stringField,
217:                    int intField) {
218:                RuntimeTest1 pc = new RuntimeTest1(stringField, intField);
219:                pc.setIntField(id++);
220:                return pc;
221:            }
222:
223:            private RuntimeTest2 newRuntimeTest2(String stringField,
224:                    int intField) {
225:                RuntimeTest2 pc = new RuntimeTest2(stringField, intField);
226:                pc.setIntField(id++);
227:                return pc;
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.