KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > soda > experiments > STMagic


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.test.legacy.soda.experiments;
22
23 // JDK 1.4.x only
24
// import java.util.regex.*;
25

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

67     public void testUnconstrainedClass() {
68         Query q = st.query();
69         q.descend("str").constrain("aaa");
70         st.expect(
71             q,
72             new Object JavaDoc[] { new STMagic("aaa"), new STString("aaa"), new STStringU("aaa")});
73     }
74
75     /**
76      * Magic:
77      * Query for multiple classes.
78      * Every class gets it's own slot in the query graph.
79      */

80     public void testMultiClass() {
81         Query q = st.query();
82         q.constrain(STDouble.class).or(q.constrain(STString.class));
83         Object JavaDoc[] stDoubles = new STDouble().store();
84         Object JavaDoc[] stStrings = new STString().store();
85         Object JavaDoc[] res = new Object JavaDoc[stDoubles.length + stStrings.length];
86         System.arraycopy(stDoubles, 0, res, 0, stDoubles.length);
87         System.arraycopy(stStrings, 0, res, stDoubles.length, stStrings.length);
88         st.expect(q, res);
89     }
90
91     /**
92      * Magic:
93      * Execute any node in the query graph.
94      * The data for this example can be found in STTH1.java.
95      */

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

112 // public void testRegularExpression() {
113
// Query q = st.query();
114
// q.constrain(STMagic.class);
115
// Query qStr = q.descend("str");
116
// final Pattern pattern = Pattern.compile("a*x");
117
// qStr.constrain(new Evaluation() {
118
// public void evaluate(Candidate candidate) {
119
// candidate.include(pattern.matcher(((String) candidate.getObject())).matches());
120
// }
121
// });
122
// st.expectOne(q, store()[1]);
123
// }
124

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

131     public void testInterface() {
132         Query q = st.query();
133         q.constrain(STInterface.class);
134         q.constrain(new Evaluation() {
135             public void evaluate(Candidate candidate) {
136                 STInterface sti = (STInterface) candidate.getObject();
137                 candidate.include(sti.returnSomething().equals("aaa"));
138             }
139         });
140         st.expect(q, new Object JavaDoc[] { new STMagic("aaa"), new STString("aaa")});
141     }
142     
143
144 }
145
Popular Tags