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.classes.simple;
022:
023: import com.db4o.*;
024: import com.db4o.query.*;
025: import com.db4o.test.legacy.soda.*;
026: import com.db4o.test.legacy.soda.arrays.typed.*;
027: import com.db4o.test.legacy.soda.classes.simple.*;
028:
029: public class STString implements STClass1, STInterface {
030:
031: public static transient SodaTest st;
032:
033: public String str;
034:
035: public STString() {
036: }
037:
038: public STString(String str) {
039: this .str = str;
040: }
041:
042: /** needed for STInterface test */
043: public Object returnSomething() {
044: return str;
045: }
046:
047: public Object[] store() {
048: return new Object[] { new STString(null), new STString("aaa"),
049: new STString("bbb"), new STString("dod") };
050: }
051:
052: public void testEquals() {
053: Query q = st.query();
054: q.constrain(store()[2]);
055: st.expectOne(q, store()[2]);
056: }
057:
058: public void testNotEquals() {
059: Query q = st.query();
060: Constraint c = q.constrain(store()[2]);
061: q.descend("str").constraints().not();
062: Object[] r = store();
063: st.expect(q, new Object[] { r[0], r[1], r[3] });
064: }
065:
066: public void testDescendantEquals() {
067: Query q = st.query();
068: q.constrain(new STString());
069: q.descend("str").constrain("bbb");
070: st.expectOne(q, new STString("bbb"));
071: }
072:
073: public void testContains() {
074: Query q = st.query();
075: Constraint c = q.constrain(new STString("od"));
076: q.descend("str").constraints().contains();
077: st.expectOne(q, new STString("dod"));
078: }
079:
080: public void testNotContains() {
081: Query q = st.query();
082: Constraint c = q.constrain(new STString("od"));
083: q.descend("str").constraints().contains().not();
084: st.expect(q, new Object[] { new STString(null),
085: new STString("aaa"), new STString("bbb") });
086: }
087:
088: public void testLike() {
089: Query q = st.query();
090: Constraint c = q.constrain(new STString("do"));
091: q.descend("str").constraints().like();
092: st.expectOne(q, new STString("dod"));
093: q = st.query();
094: c = q.constrain(new STString("od"));
095: q.descend("str").constraints().like();
096:
097: st.expectOne(q, store()[3]);
098: }
099:
100: public void testNotLike() {
101: Query q = st.query();
102: Constraint c = q.constrain(new STString("aaa"));
103: q.descend("str").constraints().like().not();
104: st.expect(q, new Object[] { new STString(null),
105: new STString("bbb"), new STString("dod") });
106: q = st.query();
107: c = q.constrain(new STString("xxx"));
108: q.descend("str").constraints().like();
109: st.expectNone(q);
110: }
111:
112: public void testStartsWith() {
113: Query q = st.query();
114: Constraint c = q.constrain(new STString("do"));
115: q.descend("str").constraints().startsWith(true);
116: st.expectOne(q, new STString("dod"));
117: q = st.query();
118: c = q.constrain(new STString("od"));
119: q.descend("str").constraints().startsWith(true);
120: st.expectNone(q);
121: }
122:
123: public void testEndsWith() {
124: Query q = st.query();
125: Constraint c = q.constrain(new STString("do"));
126: q.descend("str").constraints().endsWith(true);
127: st.expectNone(q);
128: q = st.query();
129: c = q.constrain(new STString("od"));
130: q.descend("str").constraints().endsWith(true);
131: st.expectOne(q, new STString("dod"));
132: q = st.query();
133: c = q.constrain(new STString("D"));
134: q.descend("str").constraints().endsWith(false);
135: st.expectOne(q, new STString("dod"));
136: }
137:
138: public void testIdentity() {
139: Query q = st.query();
140: Constraint c = q.constrain(new STString("aaa"));
141: ObjectSet set = q.execute();
142: STString identityConstraint = (STString) set.next();
143: identityConstraint.str = "hihs";
144: q = st.query();
145: q.constrain(identityConstraint).identity();
146: identityConstraint.str = "aaa";
147: st.expectOne(q, new STString("aaa"));
148: }
149:
150: public void testNotIdentity() {
151: Query q = st.query();
152: Constraint c = q.constrain(new STString("aaa"));
153: ObjectSet set = q.execute();
154: STString identityConstraint = (STString) set.next();
155: identityConstraint.str = null;
156: q = st.query();
157: q.constrain(identityConstraint).identity().not();
158: identityConstraint.str = "aaa";
159: st.expect(q, new Object[] { new STString(null),
160: new STString("bbb"), new STString("dod") });
161: }
162:
163: public void testNull() {
164: Query q = st.query();
165: Constraint c = q.constrain(new STString(null));
166: q.descend("str").constrain(null);
167: st.expectOne(q, new STString(null));
168: }
169:
170: public void testNotNull() {
171: Query q = st.query();
172: Constraint c = q.constrain(new STString(null));
173: q.descend("str").constrain(null).not();
174: st.expect(q, new Object[] { new STString("aaa"),
175: new STString("bbb"), new STString("dod") });
176: }
177:
178: public void testConstraints() {
179: Query q = st.query();
180: q.constrain(new STString("aaa"));
181: q.constrain(new STString("bbb"));
182: Constraints cs = q.constraints();
183: Constraint[] csa = cs.toArray();
184: if (csa.length != 2) {
185: st.error("Constraints not returned");
186: }
187: }
188:
189: public void testEvaluation() {
190: Query q = st.query();
191: q.constrain(new STString(null));
192: q.constrain(new Evaluation() {
193: public void evaluate(Candidate candidate) {
194: STString sts = (STString) candidate.getObject();
195: // FIXME: NPE expected here?
196: candidate.include(sts.str.indexOf("od") == 1);
197: }
198: });
199: st.expectOne(q, new STString("dod"));
200: }
201:
202: public void testCaseInsenstiveContains() {
203: Query q = st.query();
204: q.constrain(STString.class);
205: q.constrain(new Evaluation() {
206: public void evaluate(Candidate candidate) {
207: STString sts = (STString) candidate.getObject();
208: // FIXME: NPE expected?
209: candidate
210: .include(sts.str.toLowerCase().indexOf("od") >= 0);
211: }
212: });
213: st.expectOne(q, new STString("dod"));
214: }
215:
216: }
|