KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > soda > experiments > STMagicTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.soda.experiments;
22 // JDK 1.4.x only
23
// import java.util.regex.*;
24

25 import com.db4o.db4ounit.common.soda.*;
26 import com.db4o.db4ounit.common.soda.classes.simple.*;
27 import com.db4o.db4ounit.common.soda.classes.typedhierarchy.*;
28 import com.db4o.db4ounit.common.soda.wrapper.untyped.*;
29 import com.db4o.query.*;
30
31
32
33
34
35
36
37 // dependant on the previous run of some other test classes
38
public class STMagicTestCase extends com.db4o.db4ounit.common.soda.util.SodaBaseTestCase implements STInterface {
39
40     public String JavaDoc str;
41
42     public STMagicTestCase() {
43     }
44
45     private STMagicTestCase(String JavaDoc str) {
46         this.str = str;
47     }
48
49     public String JavaDoc toString() {
50         return "STMagicTestCase: " + str;
51     }
52
53     /** needed for STInterface test */
54     public Object JavaDoc returnSomething() {
55         return str;
56     }
57
58     public Object JavaDoc[] createData() {
59         return new Object JavaDoc[] { new STMagicTestCase("aaa"), new STMagicTestCase("aaax")};
60     }
61
62     /**
63      * Magic:
64      * Query for all objects with a known attribute,
65      * independant of the class or even if you don't
66      * know the class.
67      */

68     public void testUnconstrainedClass() {
69         Query q = newQuery();
70         q.descend("str").constrain("aaa");
71         com.db4o.db4ounit.common.soda.util.SodaTestUtil.expect(
72             q,
73             new Object JavaDoc[] { new STMagicTestCase("aaa"), new STStringTestCase("aaa"), new STStringUTestCase("aaa")});
74     }
75
76     /**
77      * Magic:
78      * Query for multiple classes.
79      * Every class gets it's own slot in the query graph.
80      */

81     public void testMultiClass() {
82         Query q = newQuery();
83         q.constrain(STDoubleTestCase.class).or(q.constrain(STStringTestCase.class));
84         Object JavaDoc[] stDoubles = new STDoubleTestCase().createData();
85         Object JavaDoc[] stStrings = new STStringTestCase().createData();
86         Object JavaDoc[] res = new Object JavaDoc[stDoubles.length + stStrings.length];
87         System.arraycopy(stDoubles, 0, res, 0, stDoubles.length);
88         System.arraycopy(stStrings, 0, res, stDoubles.length, stStrings.length);
89         com.db4o.db4ounit.common.soda.util.SodaTestUtil.expect(q, res);
90     }
91
92     /**
93      * Magic:
94      * Execute any node in the query graph.
95      * The data for this example can be found in STTH1.java.
96      */

97     public void testExecuteAnyNode() {
98         Query q = newQuery();
99         q.constrain(new STTH1TestCase().createData()[5]);
100         q = q.descend("h2").descend("h3");
101         // We only get one STTH3 here, because the query is
102
// constrained by the STTH2 with the "str2" member.
103
com.db4o.db4ounit.common.soda.util.SodaTestUtil.expectOne(q, new STTH3("str3"));
104     }
105
106     /**
107      * Magic:
108      * Querying with regular expression by using an Evaluation callback.
109      *
110      * This test needs JDK 1.4.x java.util.regex.*;
111      * It's uncommented to allow compilation on JDKs 1.2.x and 1.3.x
112      */

113 // public void testRegularExpression() {
114
// Query q = newQuery();
115
// q.constrain(STMagicTestCase.class);
116
// Query qStr = q.descend("str");
117
// final Pattern pattern = Pattern.compile("a*x");
118
// qStr.constrain(new Evaluation() {
119
// public void evaluate(Candidate candidate) {
120
// candidate.include(pattern.matcher(((String) candidate.getObject())).matches());
121
// }
122
// });
123
// com.db4o.db4ounit.common.soda.util.SodaTestUtil.expectOne(q, _array[1]);
124
// }
125

126     /**
127      * Magic:
128      * Querying for an implemented Interface.
129      * Using an Evaluation allows calls to the interface methods
130      * during the run of the query.s
131      */

132     public void testInterface() {
133         Query q = newQuery();
134         q.constrain(STInterface.class);
135         q.constrain(new Evaluation() {
136             public void evaluate(Candidate candidate) {
137                 STInterface sti = (STInterface) candidate.getObject();
138                 candidate.include(sti.returnSomething().equals("aaa"));
139             }
140         });
141         com.db4o.db4ounit.common.soda.util.SodaTestUtil.expect(q, new Object JavaDoc[] { new STMagicTestCase("aaa"), new STStringTestCase("aaa")});
142     }
143     
144
145 }
146
Popular Tags