Source Code Cross Referenced for NQRegressionTestCase.java in  » Database-DBMS » db4o-6.4 » com » db4o » test » nativequery » 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 DBMS » db4o 6.4 » com.db4o.test.nativequery 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright (C) 2004 - 2007  db4objects Inc.  http://www.db4o.com
002:
003:        This file is part of the db4o open source object database.
004:
005:        db4o is free software; you can redistribute it and/or modify it under
006:        the terms of version 2 of the GNU General Public License as published
007:        by the Free Software Foundation and as clarified by db4objects' GPL 
008:        interpretation policy, available at
009:        http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010:        Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011:        Suite 350, San Mateo, CA 94403, USA.
012:
013:        db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014:        WARRANTY; without even the implied warranty of MERCHANTABILITY or
015:        FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
016:        for more details.
017:
018:        You should have received a copy of the GNU General Public License along
019:        with this program; if not, write to the Free Software Foundation, Inc.,
020:        59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */
021:        package com.db4o.test.nativequery;
022:
023:        import java.lang.reflect.*;
024:        import java.util.*;
025:
026:        import com.db4o.*;
027:        import com.db4o.internal.*;
028:        import com.db4o.internal.query.*;
029:        import com.db4o.nativequery.*;
030:        import com.db4o.nativequery.expr.*;
031:        import com.db4o.nativequery.main.*;
032:        import com.db4o.query.*;
033:
034:        import db4ounit.*;
035:        import db4ounit.extensions.*;
036:        import db4ounit.extensions.fixtures.*;
037:
038:        public class NQRegressionTestCase extends AbstractDb4oTestCase {
039:            private final static boolean RUN_LOADTIME = false;
040:
041:            private static final String CSTR = "Cc";
042:            private static final String BSTR = "Ba";
043:            private static final String ASTR = "Aa";
044:            public final static Integer INTWRAPPER = new Integer(1);
045:            public final static Date DATE = new Date(0);
046:            private final static Integer PRIVATE_INTWRAPPER = new Integer(1);
047:
048:            private static Data _prevData = null;
049:
050:            private static abstract class Base {
051:                int id;
052:                Integer idWrap;
053:
054:                public Base(int id) {
055:                    this .id = id;
056:                    idWrap = new Integer(id);
057:                }
058:
059:                public int getId() {
060:                    return id;
061:                }
062:            }
063:
064:            private static class Other extends Base {
065:                public Other() {
066:                    super (1);
067:                }
068:            }
069:
070:            private static class Data extends Base {
071:                boolean bool;
072:                float value;
073:                String name;
074:                Data prev;
075:                int id2;
076:                Boolean boolWrap;
077:                java.util.Date curDate;
078:
079:                public Data(int id, boolean bool, float value, String name,
080:                        Data prev, int id2, java.util.Date curDate) {
081:                    super (id);
082:                    this .bool = bool;
083:                    this .boolWrap = Boolean.valueOf(bool);
084:                    this .value = value;
085:                    this .name = name;
086:                    this .prev = prev;
087:                    this .id2 = id2;
088:                    this .curDate = curDate;
089:                }
090:
091:                public float getValue() {
092:                    return value;
093:                }
094:
095:                public String getName() {
096:                    return name;
097:                }
098:
099:                public boolean getBool() {
100:                    return bool;
101:                }
102:
103:                public Data getPrev() {
104:                    return prev;
105:                }
106:            }
107:
108:            public static void main(String[] args) {
109:                TestSuite suite = new Db4oTestSuiteBuilder(new Db4oSolo(),
110:                        NQRegressionTestCase.class).build();
111:                new TestRunner(suite).run();
112:            }
113:
114:            public void store() {
115:                java.util.Date date1 = new java.util.Date(0);
116:                java.util.Date date2 = new java.util.Date();
117:
118:                Data a = new Data(1, false, 1.1f, ASTR, null, 0, date1);
119:                Data b = new Data(2, false, 1.1f, BSTR, a, Integer.MIN_VALUE,
120:                        date1);
121:                Data c = new Data(3, true, 2.2f, CSTR, b, Integer.MIN_VALUE,
122:                        date1);
123:                Data cc = new Data(3, false, 3.3f, CSTR, null,
124:                        Integer.MIN_VALUE, date2);
125:                ObjectContainer db = db();
126:                db.set(a);
127:                db.set(b);
128:                db.set(c);
129:                db.set(cc);
130:                db.set(new Other());
131:                _prevData = a;
132:            }
133:
134:            private abstract static class ExpectingPredicate extends Predicate {
135:                private String _name;
136:
137:                public ExpectingPredicate(String name) {
138:                    _name = name;
139:                }
140:
141:                public ExpectingPredicate(String name, Class extentType) {
142:                    super (extentType);
143:                    _name = name;
144:                }
145:
146:                public abstract int expected();
147:
148:                public void prepare(ObjectContainer db) {
149:                }
150:
151:                public String toString() {
152:                    return _name;
153:                }
154:            }
155:
156:            private static ExpectingPredicate[] _PREDICATES = {
157:                    // unconditional/untyped
158:                    new ExpectingPredicate("unconditional/untyped") {
159:                        public int expected() {
160:                            return 5;
161:                        }
162:
163:                        public boolean match(Object candidate) {
164:                            return true;
165:                        }
166:                    },
167:                    // unconditional
168:                    new ExpectingPredicate("unconditional: Base") {
169:                        public int expected() {
170:                            return 5;
171:                        }
172:
173:                        public boolean match(Base candidate) {
174:                            return true;
175:                        }
176:                    },
177:                    new ExpectingPredicate("unconditional: Data") {
178:                        public int expected() {
179:                            return 4;
180:                        }
181:
182:                        public boolean match(Data candidate) {
183:                            return true;
184:                        }
185:                    },
186:                    //		new ExpectingPredicate() {
187:                    //			public int expected() { return 0;}
188:                    //			public boolean match(Data candidate) {
189:                    //				return false;
190:                    //			}
191:                    //		},
192:                    // primitive equals
193:                    new ExpectingPredicate("bool") {
194:                        public int expected() {
195:                            return 1;
196:                        }
197:
198:                        public boolean match(Data candidate) {
199:                            return candidate.bool;
200:                        }
201:                    },
202:                    new ExpectingPredicate("!bool") {
203:                        public int expected() {
204:                            return 3;
205:                        }
206:
207:                        public boolean match(Data candidate) {
208:                            return !candidate.bool;
209:                        }
210:                    },
211:                    new ExpectingPredicate("id2==0") {
212:                        public int expected() {
213:                            return 1;
214:                        }
215:
216:                        public boolean match(Data candidate) {
217:                            return candidate.id2 == 0;
218:                        }
219:                    },
220:                    new ExpectingPredicate("id==1") {
221:                        public int expected() {
222:                            return 1;
223:                        }
224:
225:                        public boolean match(Data candidate) {
226:                            return candidate.id == 1;
227:                        }
228:                    },
229:                    new ExpectingPredicate("id==3") {
230:                        public int expected() {
231:                            return 2;
232:                        }
233:
234:                        public boolean match(Data candidate) {
235:                            return candidate.id == 3;
236:                        }
237:                    },
238:                    new ExpectingPredicate("value==1.1") {
239:                        public int expected() {
240:                            return 2;
241:                        }
242:
243:                        public boolean match(Data candidate) {
244:                            return candidate.value == 1.1f;
245:                        }
246:                    },
247:                    new ExpectingPredicate("value==3.3") {
248:                        public int expected() {
249:                            return 1;
250:                        }
251:
252:                        public boolean match(Data candidate) {
253:                            return candidate.value == 3.3f;
254:                        }
255:                    },
256:                    // string equals
257:                    new ExpectingPredicate("name.eq(ASTR)") {
258:                        public int expected() {
259:                            return 1;
260:                        }
261:
262:                        public boolean match(Data candidate) {
263:                            return candidate.name.equals(ASTR);
264:                        }
265:                    },
266:                    new ExpectingPredicate("name.eq(CSTR)") {
267:                        public int expected() {
268:                            return 2;
269:                        }
270:
271:                        public boolean match(Data candidate) {
272:                            return candidate.name.equals(CSTR);
273:                        }
274:                    },
275:                    // string specific comparisons
276:                    new ExpectingPredicate("name.contains('a')") {
277:                        public int expected() {
278:                            return 2;
279:                        }
280:
281:                        public boolean match(Data candidate) {
282:                            return candidate.name.contains("a");
283:                        }
284:                    },
285:                    new ExpectingPredicate("name.contains('A')") {
286:                        public int expected() {
287:                            return 1;
288:                        }
289:
290:                        public boolean match(Data candidate) {
291:                            return candidate.name.contains("A");
292:                        }
293:                    },
294:                    new ExpectingPredicate("name.contains('C')") {
295:                        public int expected() {
296:                            return 2;
297:                        }
298:
299:                        public boolean match(Data candidate) {
300:                            return candidate.name.contains("C");
301:                        }
302:                    },
303:                    new ExpectingPredicate("name.startsWith('C')") {
304:                        public int expected() {
305:                            return 2;
306:                        }
307:
308:                        public boolean match(Data candidate) {
309:                            return candidate.name.startsWith("C");
310:                        }
311:                    },
312:                    new ExpectingPredicate("name.startsWith('a')") {
313:                        public int expected() {
314:                            return 0;
315:                        }
316:
317:                        public boolean match(Data candidate) {
318:                            return candidate.name.startsWith("a");
319:                        }
320:                    },
321:                    new ExpectingPredicate("name.endsWith('A')") {
322:                        public int expected() {
323:                            return 2;
324:                        }
325:
326:                        public boolean match(Data candidate) {
327:                            return candidate.name.endsWith("a");
328:                        }
329:                    },
330:                    new ExpectingPredicate("name.endsWith('A')") {
331:                        public int expected() {
332:                            return 0;
333:                        }
334:
335:                        public boolean match(Data candidate) {
336:                            return candidate.name.endsWith("A");
337:                        }
338:                    },
339:                    new ExpectingPredicate("!(name.contains('A'))") {
340:                        public int expected() {
341:                            return 3;
342:                        }
343:
344:                        public boolean match(Data candidate) {
345:                            return !candidate.name.contains("A");
346:                        }
347:                    },
348:                    new ExpectingPredicate("!(name.startsWith('C'))") {
349:                        public int expected() {
350:                            return 2;
351:                        }
352:
353:                        public boolean match(Data candidate) {
354:                            return !candidate.name.startsWith("C");
355:                        }
356:                    },
357:                    // int field comparison
358:                    new ExpectingPredicate("id<2") {
359:                        public int expected() {
360:                            return 1;
361:                        }
362:
363:                        public boolean match(Data candidate) {
364:                            return candidate.id < 2;
365:                        }
366:                    },
367:                    new ExpectingPredicate("id>2") {
368:                        public int expected() {
369:                            return 2;
370:                        }
371:
372:                        public boolean match(Data candidate) {
373:                            return candidate.id > 2;
374:                        }
375:                    },
376:                    new ExpectingPredicate("id<=2") {
377:                        public int expected() {
378:                            return 2;
379:                        }
380:
381:                        public boolean match(Data candidate) {
382:                            return candidate.id <= 2;
383:                        }
384:                    },
385:                    new ExpectingPredicate("id>=2") {
386:                        public int expected() {
387:                            return 3;
388:                        }
389:
390:                        public boolean match(Data candidate) {
391:                            return candidate.id >= 2;
392:                        }
393:                    },
394:                    // float field comparison
395:                    new ExpectingPredicate("value>2.9") {
396:                        public int expected() {
397:                            return 1;
398:                        }
399:
400:                        public boolean match(Data candidate) {
401:                            return candidate.value > 2.9f;
402:                        }
403:                    },
404:                    new ExpectingPredicate("1.5>=value") {
405:                        public int expected() {
406:                            return 2;
407:                        }
408:
409:                        public boolean match(Data candidate) {
410:                            return 1.5f >= candidate.value;
411:                        }
412:                    },
413:                    // mixed comparison (coercion)
414:                    new ExpectingPredicate("id==1.0") {
415:                        public int expected() {
416:                            return 1;
417:                        }
418:
419:                        public boolean match(Data candidate) {
420:                            return candidate.id == 1.0f;
421:                        }
422:                    },
423:                    new ExpectingPredicate("id!=1.0") {
424:                        public int expected() {
425:                            return 3;
426:                        }
427:
428:                        public boolean match(Data candidate) {
429:                            return candidate.id != 1.0f;
430:                        }
431:                    },
432:                    new ExpectingPredicate("value!=1") {
433:                        public int expected() {
434:                            return 4;
435:                        }
436:
437:                        public boolean match(Data candidate) {
438:                            return candidate.value != 1;
439:                        }
440:                    },
441:                    // won't work: SODA coercion is broken for greater/smaller comparisons
442:                    //		new ExpectingPredicate() {
443:                    //			public int expected() { return 1;}
444:                    //			public boolean match(Data candidate) {
445:                    //				return candidate.value>2.9d;
446:                    //			}
447:                    //		},
448:                    // descend field
449:                    new ExpectingPredicate("getPrev().getId()>=1") {
450:                        public int expected() {
451:                            return 2;
452:                        }
453:
454:                        public boolean match(Data candidate) {
455:                            return candidate.getPrev() != null
456:                                    && candidate.getPrev().getId() >= 1;
457:                        }
458:                    },
459:                    new ExpectingPredicate("BSTR.eq(getPrev().getName()") {
460:                        public int expected() {
461:                            return 1;
462:                        }
463:
464:                        public boolean match(Data candidate) {
465:                            return (candidate.getPrev() != null)
466:                                    && (BSTR.equals(candidate.getPrev()
467:                                            .getName()));
468:                        }
469:                    },
470:                    new ExpectingPredicate("getPrev().getName().eq('')") {
471:                        public int expected() {
472:                            return 0;
473:                        }
474:
475:                        public boolean match(Data candidate) {
476:                            return candidate.getPrev() != null
477:                                    && candidate.getPrev().getName().equals("");
478:                        }
479:                    },
480:                    new ExpectingPredicate("getPrev()==_dataPrev") {
481:                        public int expected() {
482:                            return 1;
483:                        }
484:
485:                        public boolean match(Data candidate) {
486:                            return candidate.getPrev() == _prevData;
487:                        }
488:                    },
489:                    // getter comparisons
490:                    new ExpectingPredicate("getId()==2") {
491:                        public int expected() {
492:                            return 1;
493:                        }
494:
495:                        public boolean match(Data candidate) {
496:                            return candidate.getId() == 2;
497:                        }
498:                    },
499:                    new ExpectingPredicate("getId()<2") {
500:                        public int expected() {
501:                            return 1;
502:                        }
503:
504:                        public boolean match(Data candidate) {
505:                            return candidate.getId() < 2;
506:                        }
507:                    },
508:                    new ExpectingPredicate("getId()>2") {
509:                        public int expected() {
510:                            return 2;
511:                        }
512:
513:                        public boolean match(Data candidate) {
514:                            return candidate.getId() > 2;
515:                        }
516:                    },
517:                    new ExpectingPredicate("getId()<=2") {
518:                        public int expected() {
519:                            return 2;
520:                        }
521:
522:                        public boolean match(Data candidate) {
523:                            return candidate.getId() <= 2;
524:                        }
525:                    },
526:                    new ExpectingPredicate("getId()>=2") {
527:                        public int expected() {
528:                            return 3;
529:                        }
530:
531:                        public boolean match(Data candidate) {
532:                            return candidate.getId() >= 2;
533:                        }
534:                    },
535:                    new ExpectingPredicate("getName().eq(CSTR)") {
536:                        public int expected() {
537:                            return 2;
538:                        }
539:
540:                        public boolean match(Data candidate) {
541:                            return candidate.getName().equals(CSTR);
542:                        }
543:                    },
544:                    // negation
545:                    new ExpectingPredicate("!(id==1)") {
546:                        public int expected() {
547:                            return 3;
548:                        }
549:
550:                        public boolean match(Data candidate) {
551:                            return !(candidate.id == 1);
552:                        }
553:                    },
554:                    new ExpectingPredicate("!(getId()>2)") {
555:                        public int expected() {
556:                            return 2;
557:                        }
558:
559:                        public boolean match(Data candidate) {
560:                            return !(candidate.getId() > 2);
561:                        }
562:                    },
563:                    new ExpectingPredicate("!getName().eq(CSTR)") {
564:                        public int expected() {
565:                            return 2;
566:                        }
567:
568:                        public boolean match(Data candidate) {
569:                            return !(candidate.getName().equals(CSTR));
570:                        }
571:                    },
572:                    // conjunction
573:                    new ExpectingPredicate("bool&&!getBool()") {
574:                        public int expected() {
575:                            return 0;
576:                        }
577:
578:                        public boolean match(Data candidate) {
579:                            return candidate.bool && !candidate.getBool();
580:                        }
581:                    },
582:                    new ExpectingPredicate("id>1&&getName().eq(CSTR)") {
583:                        public int expected() {
584:                            return 2;
585:                        }
586:
587:                        public boolean match(Data candidate) {
588:                            return (candidate.id > 1)
589:                                    && candidate.getName().equals(CSTR);
590:                        }
591:                    },
592:                    new ExpectingPredicate("id>1&&getId()<=2") {
593:                        public int expected() {
594:                            return 1;
595:                        }
596:
597:                        public boolean match(Data candidate) {
598:                            return (candidate.id > 1)
599:                                    && (candidate.getId() <= 2);
600:                        }
601:                    },
602:                    new ExpectingPredicate("id>1&&getId()<1") {
603:                        public int expected() {
604:                            return 0;
605:                        }
606:
607:                        public boolean match(Data candidate) {
608:                            return (candidate.id > 1)
609:                                    && (candidate.getId() < 1);
610:                        }
611:                    },
612:                    // disjunction
613:                    new ExpectingPredicate("bool||getId()==1") {
614:                        public int expected() {
615:                            return 2;
616:                        }
617:
618:                        public boolean match(Data candidate) {
619:                            return candidate.bool || candidate.getId() == 1;
620:                        }
621:                    },
622:                    new ExpectingPredicate("id==1||getName().eq(CSTR)") {
623:                        public int expected() {
624:                            return 3;
625:                        }
626:
627:                        public boolean match(Data candidate) {
628:                            return (candidate.id == 1)
629:                                    || candidate.getName().equals(CSTR);
630:                        }
631:                    },
632:                    new ExpectingPredicate("id>1||getId()<=2") {
633:                        public int expected() {
634:                            return 4;
635:                        }
636:
637:                        public boolean match(Data candidate) {
638:                            return (candidate.id > 1)
639:                                    || (candidate.getId() <= 2);
640:                        }
641:                    },
642:                    new ExpectingPredicate("id<=1||getId()>=3") {
643:                        public int expected() {
644:                            return 3;
645:                        }
646:
647:                        public boolean match(Data candidate) {
648:                            return (candidate.id <= 1)
649:                                    || (candidate.getId() >= 3);
650:                        }
651:                    },
652:                    // nested boolean
653:                    new ExpectingPredicate(
654:                            "id>=1||getName().eq(CSTR)&&getId()<3") {
655:                        public int expected() {
656:                            return 2;
657:                        }
658:
659:                        public boolean match(Data candidate) {
660:                            return ((candidate.id >= 1) || candidate.getName()
661:                                    .equals(CSTR))
662:                                    && candidate.getId() < 3;
663:                        }
664:                    },
665:                    new ExpectingPredicate(
666:                            "(id==2||getId()<=1)&&!(getName().eq(BSTR))") {
667:                        public int expected() {
668:                            return 1;
669:                        }
670:
671:                        public boolean match(Data candidate) {
672:                            return ((candidate.id == 2) || candidate.getId() <= 1)
673:                                    && !candidate.getName().equals(BSTR);
674:                        }
675:                    },
676:                    // predicate member comparison
677:                    new ExpectingPredicate("id>=P.id") {
678:                        private int id = 2;
679:
680:                        public int expected() {
681:                            return 3;
682:                        }
683:
684:                        public boolean match(Data candidate) {
685:                            return candidate.id >= id;
686:                        }
687:                    },
688:                    new ExpectingPredicate("getName().eq(P.name)") {
689:                        private String name = BSTR;
690:
691:                        public int expected() {
692:                            return 1;
693:                        }
694:
695:                        public boolean match(Data candidate) {
696:                            return candidate.getName().equals(name);
697:                        }
698:                    },
699:                    // arithmetic
700:                    new ExpectingPredicate("id>=P.id+1") {
701:                        private int id = 2;
702:
703:                        public int expected() {
704:                            return 2;
705:                        }
706:
707:                        public boolean match(Data candidate) {
708:                            return candidate.id >= id + 1;
709:                        }
710:                    },
711:                    new ExpectingPredicate("id>=P.calc()") {
712:                        private int factor = 2;
713:
714:                        private int calc() {
715:                            return factor + 1;
716:                        }
717:
718:                        public int expected() {
719:                            return 2;
720:                        }
721:
722:                        public boolean match(Data candidate) {
723:                            return candidate.id >= calc();
724:                        }
725:                    },
726:                    new ExpectingPredicate("getValue()==P.calc()") {
727:                        private float predFactor = 2.0f;
728:
729:                        private float calc() {
730:                            return predFactor * 1.1f;
731:                        }
732:
733:                        public int expected() {
734:                            return 1;
735:                        }
736:
737:                        public boolean match(Data candidate) {
738:                            return candidate.getValue() == calc();
739:                        }
740:                    },
741:                    // force extent
742:                    new ExpectingPredicate("force extent", Data.class) {
743:                        public int expected() {
744:                            return 1;
745:                        }
746:
747:                        public boolean match(Object candidate) {
748:                            return ((Data) candidate).getId() == 1;
749:                        }
750:                    },
751:                    // array access
752:                    new ExpectingPredicate("id==P.data[3]") {
753:                        private int[] data = { 0, 1, 2, 3, 4 };
754:
755:                        public int expected() {
756:                            return 2;
757:                        }
758:
759:                        public boolean match(Data candidate) {
760:                            return candidate.id == data[3];
761:                        }
762:                    },
763:                    new ExpectingPredicate("prev==P.data[3]") {
764:                        private Data[] data = { null, null, null, null };
765:
766:                        public int expected() {
767:                            return 2;
768:                        }
769:
770:                        public boolean match(Data candidate) {
771:                            return candidate.prev == data[3];
772:                        }
773:                    },
774:                    // non-candidate method calls
775:                    new ExpectingPredicate("id==Integer.parseInt('3')") {
776:                        public int expected() {
777:                            return 2;
778:                        }
779:
780:                        public boolean match(Data candidate) {
781:                            return candidate.id == Integer.parseInt("3");
782:                        }
783:                    },
784:                    new ExpectingPredicate("id==P.sum(3,0)") {
785:                        public int expected() {
786:                            return 2;
787:                        }
788:
789:                        private int sum(int a, int b) {
790:                            return a + b;
791:                        }
792:
793:                        public boolean match(Data candidate) {
794:                            return candidate.id == sum(3, 0);
795:                        }
796:                    },
797:                    // primitive wrappers
798:                    new ExpectingPredicate("boolWrapper") {
799:                        public int expected() {
800:                            return 1;
801:                        }
802:
803:                        public boolean match(Data candidate) {
804:                            return candidate.boolWrap.booleanValue();
805:                        }
806:                    },
807:                    new ExpectingPredicate("INTWRAPPER.eq(idwrap)") {
808:                        public int expected() {
809:                            return 1;
810:                        }
811:
812:                        public boolean match(Data candidate) {
813:                            return NQRegressionTestCase.INTWRAPPER
814:                                    .equals(candidate.idWrap);
815:                        }
816:                    }, new ExpectingPredicate("idwrap.value==1") {
817:                        public int expected() {
818:                            return 1;
819:                        }
820:
821:                        public boolean match(Data candidate) {
822:                            return candidate.idWrap.intValue() == 1;
823:                        }
824:                    }, new ExpectingPredicate("id==INTWRAPPER.intValue()") {
825:                        public int expected() {
826:                            return 1;
827:                        }
828:
829:                        public boolean match(Data candidate) {
830:                            return candidate.id == INTWRAPPER.intValue();
831:                        }
832:                    },
833:                    new ExpectingPredicate("idwrap.compareTo(INTWRAPPER)<2") {
834:                        public int expected() {
835:                            return 3;
836:                        }
837:
838:                        public boolean match(Data candidate) {
839:                            return candidate.idWrap.compareTo(INTWRAPPER) > 0;
840:                        }
841:                    }, new ExpectingPredicate("curDate.equals(DATE)") {
842:                        public int expected() {
843:                            return 3;
844:                        }
845:
846:                        public boolean match(Data candidate) {
847:                            return candidate.curDate.equals(DATE);
848:                        }
849:                    },
850:                    // Note: We never get to see a static field access here - non-static inner class
851:                    // stuff converts this to NQRegressionTests#access$0()
852:                    new ExpectingPredicate("PRIVATE_INTWRAPPER.eq(idWrap)") {
853:                        public int expected() {
854:                            return 1;
855:                        }
856:
857:                        public boolean match(Data candidate) {
858:                            return NQRegressionTestCase.PRIVATE_INTWRAPPER
859:                                    .equals(candidate.idWrap);
860:                        }
861:                    }, };
862:
863:            private static ExpectingPredicate[] PREDICATES = _PREDICATES;
864:
865:            public void testAll() throws Exception {
866:                _prevData = (Data) db().get(_prevData).next();
867:                for (int predIdx = 0; predIdx < PREDICATES.length; predIdx++) {
868:                    ExpectingPredicate predicate = PREDICATES[predIdx];
869:                    assertNQResult(predicate);
870:                }
871:            }
872:
873:            private void assertNQResult(final ExpectingPredicate predicate)
874:                    throws Exception {
875:                final String predicateId = "PREDICATE: " + predicate;
876:                ObjectContainer db = db();
877:                Db4oQueryExecutionListener listener = new Db4oQueryExecutionListener() {
878:                    private int run = 0;
879:
880:                    public void notifyQueryExecuted(NQOptimizationInfo info) {
881:                        if (run < 2) {
882:                            Assert.areEqual(info.predicate(), predicate,
883:                                    predicateId);
884:                        }
885:                        String expMsg = null;
886:                        switch (run) {
887:                        case 0:
888:                            expMsg = NativeQueryHandler.UNOPTIMIZED;
889:                            Assert.isNull(info.optimized(), predicateId);
890:                            break;
891:                        case 1:
892:                            expMsg = NativeQueryHandler.DYNOPTIMIZED;
893:                            Assert.isTrue(
894:                                    info.optimized() instanceof  Expression,
895:                                    predicateId);
896:                            break;
897:                        case 2:
898:                            expMsg = NativeQueryHandler.PREOPTIMIZED;
899:                            Assert.isNull(info.optimized(), predicateId);
900:                            break;
901:                        }
902:                        Assert.areEqual(expMsg, info.message(), predicateId);
903:                        run++;
904:                    }
905:                };
906:                ((InternalObjectContainer) db).getNativeQueryHandler()
907:                        .addListener(listener);
908:                db.ext().configure().optimizeNativeQueries(false);
909:                ObjectSet raw = db.query(predicate);
910:                db.ext().configure().optimizeNativeQueries(true);
911:                if (NQDebug.LOG) {
912:                    System.err.println("PREDICATE: " + predicate);
913:                }
914:                ObjectSet optimized = db.query(predicate);
915:                if (!raw.equals(optimized)) {
916:                    System.out.println("RAW");
917:                    raw.reset();
918:                    while (raw.hasNext()) {
919:                        System.out.println(raw.next());
920:                    }
921:                    raw.reset();
922:                    System.out.println("OPT");
923:                    optimized.reset();
924:                    while (optimized.hasNext()) {
925:                        System.out.println(optimized.next());
926:                    }
927:                    optimized.reset();
928:                }
929:                Assert.areEqual(raw, optimized, predicateId);
930:                Assert.areEqual(predicate.expected(), raw.size(), predicateId);
931:
932:                if (RUN_LOADTIME) {
933:                    db.ext().configure().optimizeNativeQueries(false);
934:                    NQEnhancingClassloader loader = new NQEnhancingClassloader(
935:                            getClass().getClassLoader());
936:                    Class filterClass = loader.loadClass(predicate.getClass()
937:                            .getName());
938:                    Constructor constr = null;
939:                    Object[] args = null;
940:                    try {
941:                        constr = filterClass
942:                                .getDeclaredConstructor(new Class[] { String.class });
943:                        args = new Object[] { filterClass.getName() };
944:                    } catch (NoSuchMethodException exc) {
945:                        constr = filterClass
946:                                .getDeclaredConstructor(new Class[] {
947:                                        String.class, Class.class });
948:                        args = new Object[] { filterClass.getName(), Data.class };
949:                    }
950:                    constr.setAccessible(true);
951:                    Predicate clPredicate = (Predicate) constr
952:                            .newInstance(args);
953:                    ObjectSet preoptimized = db.query(clPredicate);
954:                    Assert.areEqual(predicate.expected(), preoptimized.size(),
955:                            predicateId);
956:                    Assert.areEqual(raw, preoptimized, predicateId);
957:                    Assert.areEqual(optimized, preoptimized, predicateId);
958:                }
959:                ((InternalObjectContainer) db).getNativeQueryHandler()
960:                        .clearListeners();
961:                db.ext().configure().optimizeNativeQueries(true);
962:            }
963:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.