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.db4ounit.common.soda.experiments;
022:
023: // JDK 1.4.x only
024: // import java.util.regex.*;
025:
026: import com.db4o.db4ounit.common.soda.*;
027: import com.db4o.db4ounit.common.soda.classes.simple.*;
028: import com.db4o.db4ounit.common.soda.classes.typedhierarchy.*;
029: import com.db4o.db4ounit.common.soda.wrapper.untyped.*;
030: import com.db4o.query.*;
031:
032: // dependant on the previous run of some other test classes
033: public class STMagicTestCase extends
034: com.db4o.db4ounit.common.soda.util.SodaBaseTestCase implements
035: STInterface {
036:
037: public String str;
038:
039: public STMagicTestCase() {
040: }
041:
042: private STMagicTestCase(String str) {
043: this .str = str;
044: }
045:
046: public String toString() {
047: return "STMagicTestCase: " + str;
048: }
049:
050: /** needed for STInterface test */
051: public Object returnSomething() {
052: return str;
053: }
054:
055: public Object[] createData() {
056: return new Object[] { new STMagicTestCase("aaa"),
057: new STMagicTestCase("aaax") };
058: }
059:
060: /**
061: * Magic:
062: * Query for all objects with a known attribute,
063: * independant of the class or even if you don't
064: * know the class.
065: */
066: public void testUnconstrainedClass() {
067: Query q = newQuery();
068: q.descend("str").constrain("aaa");
069: com.db4o.db4ounit.common.soda.util.SodaTestUtil.expect(q,
070: new Object[] { new STMagicTestCase("aaa"),
071: new STStringTestCase("aaa"),
072: new STStringUTestCase("aaa") });
073: }
074:
075: /**
076: * Magic:
077: * Query for multiple classes.
078: * Every class gets it's own slot in the query graph.
079: */
080: public void testMultiClass() {
081: Query q = newQuery();
082: q.constrain(STDoubleTestCase.class).or(
083: q.constrain(STStringTestCase.class));
084: Object[] stDoubles = new STDoubleTestCase().createData();
085: Object[] stStrings = new STStringTestCase().createData();
086: Object[] res = new Object[stDoubles.length + stStrings.length];
087: System.arraycopy(stDoubles, 0, res, 0, stDoubles.length);
088: System.arraycopy(stStrings, 0, res, stDoubles.length,
089: stStrings.length);
090: com.db4o.db4ounit.common.soda.util.SodaTestUtil.expect(q, res);
091: }
092:
093: /**
094: * Magic:
095: * Execute any node in the query graph.
096: * The data for this example can be found in STTH1.java.
097: */
098: public void testExecuteAnyNode() {
099: Query q = newQuery();
100: q.constrain(new STTH1TestCase().createData()[5]);
101: q = q.descend("h2").descend("h3");
102: // We only get one STTH3 here, because the query is
103: // constrained by the STTH2 with the "str2" member.
104: com.db4o.db4ounit.common.soda.util.SodaTestUtil.expectOne(q,
105: new STTH3("str3"));
106: }
107:
108: /**
109: * Magic:
110: * Querying with regular expression by using an Evaluation callback.
111: *
112: * This test needs JDK 1.4.x java.util.regex.*;
113: * It's uncommented to allow compilation on JDKs 1.2.x and 1.3.x
114: */
115: // public void testRegularExpression() {
116: // Query q = newQuery();
117: // q.constrain(STMagicTestCase.class);
118: // Query qStr = q.descend("str");
119: // final Pattern pattern = Pattern.compile("a*x");
120: // qStr.constrain(new Evaluation() {
121: // public void evaluate(Candidate candidate) {
122: // candidate.include(pattern.matcher(((String) candidate.getObject())).matches());
123: // }
124: // });
125: // com.db4o.db4ounit.common.soda.util.SodaTestUtil.expectOne(q, _array[1]);
126: // }
127: /**
128: * Magic:
129: * Querying for an implemented Interface.
130: * Using an Evaluation allows calls to the interface methods
131: * during the run of the query.s
132: */
133: public void testInterface() {
134: Query q = newQuery();
135: q.constrain(STInterface.class);
136: q.constrain(new Evaluation() {
137: public void evaluate(Candidate candidate) {
138: STInterface sti = (STInterface) candidate.getObject();
139: candidate.include(sti.returnSomething().equals("aaa"));
140: }
141: });
142: com.db4o.db4ounit.common.soda.util.SodaTestUtil.expect(q,
143: new Object[] { new STMagicTestCase("aaa"),
144: new STStringTestCase("aaa") });
145: }
146:
147: }
|