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.jre11.concurrency.staging;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.ext.*;
026: import com.db4o.query.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030:
031: public class ComparatorSortTestCase extends Db4oClientServerTestCase {
032:
033: public static void main(String[] args) {
034: new ComparatorSortTestCase().runConcurrency();
035: }
036:
037: public static class AscendingIdComparator implements
038: QueryComparator {
039: public int compare(Object first, Object second) {
040: return ((ComparatorSortTestCase) first)._id
041: - ((ComparatorSortTestCase) second)._id;
042: }
043: }
044:
045: public static class DescendingIdComparator implements
046: QueryComparator {
047: public int compare(Object first, Object second) {
048: return ((ComparatorSortTestCase) second)._id
049: - ((ComparatorSortTestCase) first)._id;
050: }
051: }
052:
053: public static class OddEvenIdComparator implements QueryComparator {
054: public int compare(Object first, Object second) {
055: int idA = ((ComparatorSortTestCase) first)._id;
056: int idB = ((ComparatorSortTestCase) second)._id;
057: int modA = idA % 2;
058: int modB = idB % 2;
059: if (modA != modB) {
060: return modA - modB;
061: }
062: return idA - idB;
063: }
064: }
065:
066: public static class AscendingNameComparator implements
067: QueryComparator {
068: public int compare(Object first, Object second) {
069: return ((ComparatorSortTestCase) first)._name
070: .compareTo(((ComparatorSortTestCase) second)._name);
071: }
072: }
073:
074: public static class SmallerThanThirtyPredicate extends Predicate {
075: public boolean match(Object candidate) {
076: return ((ComparatorSortTestCase) candidate)._id < 30;
077: }
078: }
079:
080: public int _id;
081:
082: public String _name;
083:
084: public ComparatorSortTestCase() {
085: this (0, null);
086: }
087:
088: public ComparatorSortTestCase(int id, String name) {
089: this ._id = id;
090: this ._name = name;
091: }
092:
093: protected void configure(Configuration config) {
094: config.exceptionsOnNotStorable(true);
095: }
096:
097: protected void store() {
098: for (int i = 30; i >= 0; --i) {
099: String name = i < 10 ? "0" + i : String.valueOf(i);
100: store(new ComparatorSortTestCase(i, name));
101: }
102: }
103:
104: public void conc(ExtObjectContainer oc) {
105: assertByIdAscending(oc);
106: assertByIdAscendingConstrained(oc);
107: assertByIdAscendingNQ(oc);
108:
109: assertByIdDescending(oc);
110: asertByIdDescendingConstrained(oc);
111: assertByIdDescendingNQ(oc);
112:
113: assertByIdOddEven(oc);
114: assertByIdOddEvenConstrained(oc);
115: assertByIdOddEvenNQ(oc);
116:
117: assertByNameAscending(oc);
118: assertByNameAscendingConstrained(oc);
119: assertByNameAscendingNQ(oc);
120: }
121:
122: public void assertByIdAscending(ExtObjectContainer oc) {
123: assertIdOrder(oc, new AscendingIdComparator(), range(0, 31));
124: }
125:
126: public void assertByIdAscendingConstrained(ExtObjectContainer oc) {
127: Query query = oc.query();
128: query.constrain(getClass());
129: query.descend("_id").constrain(new Integer(30)).smaller();
130: assertIdOrder(query, new AscendingIdComparator(), range(0, 30));
131: }
132:
133: public void assertByIdAscendingNQ(ExtObjectContainer oc) {
134: ObjectSet result = oc.query(new SmallerThanThirtyPredicate(),
135: new AscendingIdComparator());
136: assertIdOrder(result, range(0, 30));
137: }
138:
139: public void assertByIdDescending(ExtObjectContainer oc) {
140: int[] expected = descendingRange(30);
141: assertIdOrder(oc, new DescendingIdComparator(), expected);
142: }
143:
144: private int[] range(final int begin, final int end) {
145: int[] expected = new int[end];
146: for (int i = begin; i < end; ++i) {
147: expected[i] = i;
148: }
149: return expected;
150: }
151:
152: private int[] descendingRange(int begin) {
153: int[] range = new int[begin + 1];
154: for (int i = 0; i <= begin; ++i) {
155: range[i] = begin - i;
156: }
157: return range;
158: }
159:
160: public void asertByIdDescendingConstrained(ExtObjectContainer oc) {
161: Query query = oc.query();
162: query.constrain(getClass());
163: query.descend("_id").constrain(new Integer(30)).smaller();
164: assertIdOrder(query, new DescendingIdComparator(),
165: descendingRange(29));
166: }
167:
168: public void assertByIdDescendingNQ(ExtObjectContainer oc) {
169: int[] expected = descendingRange(29);
170: ObjectSet result = oc.query(new SmallerThanThirtyPredicate(),
171: new DescendingIdComparator());
172: assertIdOrder(result, expected);
173: }
174:
175: public void assertByIdOddEven(ExtObjectContainer oc) {
176: int[] expected = new int[31];
177: int i = 0;
178: for (; i <= 30 / 2; i++) {
179: expected[i] = 2 * i;
180: }
181: for (int j = 0; j <= (30 - 1) / 2; j++) {
182: expected[i++] = 2 * j + 1;
183: }
184: assertIdOrder(oc, new OddEvenIdComparator(), expected);
185: }
186:
187: public void assertByIdOddEvenConstrained(ExtObjectContainer oc) {
188: int[] expected = new int[30];
189: int i = 0;
190: for (; i < 30 / 2; i++) {
191: expected[i] = 2 * i;
192: }
193: for (int j = 0; j <= (30 - 1) / 2; j++) {
194: expected[i++] = 2 * j + 1;
195: }
196: Query query = oc.query();
197: query.constrain(getClass());
198: query.descend("_id").constrain(new Integer(30)).smaller();
199: assertIdOrder(query, new OddEvenIdComparator(), expected);
200: }
201:
202: public void assertByIdOddEvenNQ(ExtObjectContainer oc) {
203: int[] expected = new int[30];
204: int i = 0;
205: for (; i < 30 / 2; i++) {
206: expected[i] = 2 * i;
207: }
208: for (int j = 0; j <= (30 - 1) / 2; j++) {
209: expected[i++] = 2 * j + 1;
210: }
211: ObjectSet result = oc.query(new SmallerThanThirtyPredicate(),
212: new OddEvenIdComparator());
213: assertIdOrder(result, expected);
214: }
215:
216: public void assertByNameAscending(ExtObjectContainer oc) {
217: assertIdOrder(oc, new AscendingNameComparator(), range(0, 31));
218: }
219:
220: public void assertByNameAscendingConstrained(ExtObjectContainer oc) {
221: Query query = oc.query();
222: query.constrain(getClass());
223: query.descend("_id").constrain(new Integer(30)).smaller();
224: assertIdOrder(query, new AscendingNameComparator(),
225: range(0, 30));
226: }
227:
228: public void assertByNameAscendingNQ(ExtObjectContainer oc) {
229: ObjectSet result = oc.query(new SmallerThanThirtyPredicate(),
230: new AscendingNameComparator());
231: assertIdOrder(result, range(0, 30));
232: }
233:
234: private void assertIdOrder(ExtObjectContainer oc,
235: QueryComparator comparator, int[] ids) {
236: Query query = oc.query();
237: query.constrain(ComparatorSortTestCase.class);
238: assertIdOrder(query, comparator, ids);
239: }
240:
241: private void assertIdOrder(Query query, QueryComparator comparator,
242: int[] ids) {
243: query.sortBy(comparator);
244: ObjectSet result = query.execute();
245: assertIdOrder(result, ids);
246: }
247:
248: private void assertIdOrder(ObjectSet result, int[] ids) {
249: Assert.areEqual(ids.length, result.size());
250: for (int idx = 0; idx < ids.length; idx++) {
251: Assert.areEqual(ids[idx], ((ComparatorSortTestCase) result
252: .next())._id);
253: }
254: }
255: }
|