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.legacy.soda.experiments;
022:
023: // JDK 1.4.x only
024: // import java.util.regex.*;
025:
026: import com.db4o.query.*;
027: import com.db4o.test.legacy.soda.*;
028: import com.db4o.test.legacy.soda.arrays.typed.*;
029: import com.db4o.test.legacy.soda.classes.simple.*;
030: import com.db4o.test.legacy.soda.classes.typedhierarchy.*;
031: import com.db4o.test.legacy.soda.experiments.*;
032: import com.db4o.test.legacy.soda.wrapper.untyped.*;
033:
034: // dependant on the previous run of some other test classes
035: public class STMagic implements STClass1, STInterface {
036:
037: public static transient SodaTest st;
038:
039: public String str;
040:
041: public STMagic() {
042: }
043:
044: private STMagic(String str) {
045: this .str = str;
046: }
047:
048: public String toString() {
049: return "STMagic: " + str;
050: }
051:
052: /** needed for STInterface test */
053: public Object returnSomething() {
054: return str;
055: }
056:
057: public Object[] store() {
058: return new Object[] { new STMagic("aaa"), new STMagic("aaax") };
059: }
060:
061: /**
062: * Magic:
063: * Query for all objects with a known attribute,
064: * independant of the class or even if you don't
065: * know the class.
066: */
067: public void testUnconstrainedClass() {
068: Query q = st.query();
069: q.descend("str").constrain("aaa");
070: st.expect(q, new Object[] { new STMagic("aaa"),
071: new STString("aaa"), new STStringU("aaa") });
072: }
073:
074: /**
075: * Magic:
076: * Query for multiple classes.
077: * Every class gets it's own slot in the query graph.
078: */
079: public void testMultiClass() {
080: Query q = st.query();
081: q.constrain(STDouble.class).or(q.constrain(STString.class));
082: Object[] stDoubles = new STDouble().store();
083: Object[] stStrings = new STString().store();
084: Object[] res = new Object[stDoubles.length + stStrings.length];
085: System.arraycopy(stDoubles, 0, res, 0, stDoubles.length);
086: System.arraycopy(stStrings, 0, res, stDoubles.length,
087: stStrings.length);
088: st.expect(q, res);
089: }
090:
091: /**
092: * Magic:
093: * Execute any node in the query graph.
094: * The data for this example can be found in STTH1.java.
095: */
096: public void testExecuteAnyNode() {
097: Query q = st.query();
098: q.constrain(new STTH1().store()[5]);
099: 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: * Magic:
126: * Querying for an implemented Interface.
127: * Using an Evaluation allows calls to the interface methods
128: * during the run of the query.s
129: */
130: public void testInterface() {
131: Query q = st.query();
132: q.constrain(STInterface.class);
133: q.constrain(new Evaluation() {
134: public void evaluate(Candidate candidate) {
135: STInterface sti = (STInterface) candidate.getObject();
136: // FIXME: NPE expected?
137: candidate.include(sti.returnSomething().equals("aaa"));
138: }
139: });
140: st.expect(q, new Object[] { new STMagic("aaa"),
141: new STString("aaa") });
142: }
143:
144: }
|