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;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.query.*;
028:
029: public class IndexCreateDrop {
030:
031: public int myInt;
032: public String myString;
033: public Date myDate;
034:
035: public void store() {
036: Test.deleteAllInstances(this );
037: store(4);
038: store(7);
039: store(6);
040: store(6);
041: store(5);
042: store(4);
043: store(0);
044: store(0);
045: }
046:
047: public void test() {
048: queries();
049: indexed(true);
050: Test.reOpenServer();
051: queries();
052: indexed(false);
053: Test.reOpenServer();
054: queries();
055: indexed(true);
056: Test.reOpenServer();
057: queries();
058: }
059:
060: private void indexed(boolean flag) {
061: ObjectClass oc = Db4o.configure().objectClass(this .getClass());
062: oc.objectField("myInt").indexed(flag);
063: oc.objectField("myString").indexed(flag);
064: oc.objectField("myDate").indexed(flag);
065: }
066:
067: private void store(int val) {
068: IndexCreateDrop icd = new IndexCreateDrop();
069: icd.myInt = val;
070: if (val != 0) {
071: icd.myString = "" + val;
072: icd.myDate = new Date(val);
073: }
074:
075: Test.store(icd);
076: }
077:
078: private void queries() {
079: Query q = Test.query();
080: q.constrain(IndexCreateDrop.class);
081: q.descend("myInt").constrain(new Integer(6));
082: Test.ensure(q.execute().size() == 2);
083:
084: q = Test.query();
085: q.constrain(IndexCreateDrop.class);
086: q.descend("myInt").constrain(new Integer(4)).greater();
087: Test.ensure(q.execute().size() == 4);
088:
089: q = Test.query();
090: q.constrain(IndexCreateDrop.class);
091: q.descend("myInt").constrain(new Integer(4)).greater().equal();
092: Test.ensure(q.execute().size() == 6);
093:
094: q = Test.query();
095: q.constrain(IndexCreateDrop.class);
096: q.descend("myInt").constrain(new Integer(7)).smaller().equal();
097: Test.ensure(q.execute().size() == 8);
098:
099: q = Test.query();
100: q.constrain(IndexCreateDrop.class);
101: q.descend("myInt").constrain(new Integer(7)).smaller();
102: Test.ensure(q.execute().size() == 7);
103:
104: q = Test.query();
105: q.constrain(IndexCreateDrop.class);
106: q.descend("myString").constrain("6");
107: Test.ensure(q.execute().size() == 2);
108:
109: q = Test.query();
110: q.constrain(IndexCreateDrop.class);
111: q.descend("myString").constrain("7");
112: Test.ensure(q.execute().size() == 1);
113:
114: q = Test.query();
115: q.constrain(IndexCreateDrop.class);
116: q.descend("myString").constrain("4");
117: Test.ensure(q.execute().size() == 2);
118:
119: q = Test.query();
120: q.constrain(IndexCreateDrop.class);
121: q.descend("myString").constrain(null);
122: Test.ensure(q.execute().size() == 2);
123:
124: q = Test.query();
125: q.constrain(IndexCreateDrop.class);
126: q.descend("myDate").constrain(new Date(4)).greater();
127: Test.ensure(q.execute().size() == 4);
128:
129: q = Test.query();
130: q.constrain(IndexCreateDrop.class);
131: q.descend("myDate").constrain(new Date(4)).greater().equal();
132: Test.ensure(q.execute().size() == 6);
133:
134: q = Test.query();
135: q.constrain(IndexCreateDrop.class);
136: q.descend("myDate").constrain(new Date(7)).smaller().equal();
137:
138: Test.ensure(q.execute().size() == 6);
139:
140: q = Test.query();
141: q.constrain(IndexCreateDrop.class);
142: q.descend("myDate").constrain(new Date(7)).smaller();
143:
144: Test.ensure(q.execute().size() == 5);
145:
146: q = Test.query();
147: q.constrain(IndexCreateDrop.class);
148: q.descend("myDate").constrain(null);
149: Test.ensureEquals(2, q.execute().size());
150: }
151:
152: }
|