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