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.assorted;
022:
023: import java.util.Date;
024:
025: import com.db4o.config.*;
026: import com.db4o.internal.handlers.*;
027: import com.db4o.query.Query;
028:
029: import db4ounit.Assert;
030: import db4ounit.extensions.AbstractDb4oTestCase;
031: import db4ounit.extensions.fixtures.*;
032: import db4ounit.util.*;
033:
034: public class IndexCreateDropTestCase extends AbstractDb4oTestCase
035: implements OptOutDefragSolo {
036:
037: public static class IndexCreateDropItem {
038:
039: public int _int;
040:
041: public String _string;
042:
043: public Date _date;
044:
045: public IndexCreateDropItem(int int_, String string_, Date date_) {
046: _int = int_;
047: _string = string_;
048: _date = date_;
049: }
050:
051: public IndexCreateDropItem(int int_) {
052: this (int_, int_ == 0 ? null : "" + int_,
053: int_ == 0 ? nullDate() : new Date(int_));
054: }
055:
056: /**
057: * java.util.Date gets translated to System.DateTime on .net which is
058: * a value type thus no null.
059: *
060: * We ask the DateHandler the proper 'null' representation for the
061: * current platform.
062: */
063: private static Date nullDate() {
064: return (Date) new DateHandler(null).primitiveNull();
065: }
066: }
067:
068: private final int[] VALUES = new int[] { 4, 7, 6, 6, 5, 4, 0, 0 };
069:
070: public static void main(String[] arguments) {
071: new IndexCreateDropTestCase().runSolo();
072: }
073:
074: protected void configure(Configuration config) throws Exception {
075: // TODO
076: super .configure(config);
077: }
078:
079: protected void store() {
080: for (int i = 0; i < VALUES.length; i++) {
081: db().set(new IndexCreateDropItem(VALUES[i]));
082: }
083: }
084:
085: public void test() throws Exception {
086: assertQueryResults();
087: assertQueryResults(true);
088: assertQueryResults(false);
089: assertQueryResults(true);
090: }
091:
092: private void assertQueryResults(boolean indexed) throws Exception {
093: indexed(indexed);
094: reopen();
095: assertQueryResults();
096: }
097:
098: private void indexed(boolean flag) {
099: ObjectClass oc = fixture().config().objectClass(
100: IndexCreateDropItem.class);
101: oc.objectField("_int").indexed(flag);
102: oc.objectField("_string").indexed(flag);
103: oc.objectField("_date").indexed(flag);
104: }
105:
106: protected Query newQuery() {
107: Query q = super .newQuery();
108: q.constrain(IndexCreateDropItem.class);
109: return q;
110: }
111:
112: private void assertQueryResults() {
113: Query q = newQuery();
114: q.descend("_int").constrain(new Integer(6));
115: assertQuerySize(2, q);
116:
117: q = newQuery();
118: q.descend("_int").constrain(new Integer(4)).greater();
119: assertQuerySize(4, q);
120:
121: q = newQuery();
122: q.descend("_int").constrain(new Integer(4)).greater().equal();
123: assertQuerySize(6, q);
124:
125: q = newQuery();
126: q.descend("_int").constrain(new Integer(7)).smaller().equal();
127: assertQuerySize(8, q);
128:
129: q = newQuery();
130: q.descend("_int").constrain(new Integer(7)).smaller();
131: assertQuerySize(7, q);
132:
133: q = newQuery();
134: q.descend("_string").constrain("6");
135: assertQuerySize(2, q);
136:
137: q = newQuery();
138: q.descend("_string").constrain("7");
139: assertQuerySize(1, q);
140:
141: q = newQuery();
142: q.descend("_string").constrain("4");
143: assertQuerySize(2, q);
144:
145: q = newQuery();
146: q.descend("_string").constrain(null);
147: assertQuerySize(2, q);
148:
149: q = newQuery();
150: q.descend("_date").constrain(new Date(4)).greater();
151: assertQuerySize(4, q);
152:
153: q = newQuery();
154: q.descend("_date").constrain(new Date(4)).greater().equal();
155: assertQuerySize(6, q);
156:
157: q = newQuery();
158: q.descend("_date").constrain(new Date(7)).smaller().equal();
159: assertQuerySize(PlatformInformation.isJava() ? 6 : 8, q);
160:
161: q = newQuery();
162: q.descend("_date").constrain(new Date(7)).smaller();
163: assertQuerySize(PlatformInformation.isJava() ? 5 : 7, q);
164:
165: q = newQuery();
166: q.descend("_date").constrain(null);
167: assertQuerySize(PlatformInformation.isJava() ? 2 : 0, q);
168:
169: }
170:
171: private void assertQuerySize(int size, Query q) {
172: Assert.areEqual(size, q.execute().size());
173: }
174:
175: }
|