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 com.db4o.ObjectSet;
024: import com.db4o.config.Configuration;
025: import com.db4o.query.*;
026:
027: import db4ounit.Assert;
028: import db4ounit.extensions.AbstractDb4oTestCase;
029:
030: public class ComparatorSortTestCase extends AbstractDb4oTestCase {
031: public static class AscendingIdComparator implements
032: QueryComparator<Item> {
033: public int compare(Item first, Item second) {
034: return first._id - second._id;
035: }
036: }
037:
038: public static class DescendingIdComparator implements
039: QueryComparator<Item> {
040: public int compare(Item first, Item second) {
041: return second._id - first._id;
042: }
043: }
044:
045: public static class OddEvenIdComparator implements
046: QueryComparator<Item> {
047: public int compare(Item first, Item second) {
048: int idA = first._id;
049: int idB = second._id;
050: int modA = idA % 2;
051: int modB = idB % 2;
052: if (modA != modB) {
053: return modA - modB;
054: }
055: return idA - idB;
056: }
057: }
058:
059: public static class AscendingNameComparator implements
060: QueryComparator<Item> {
061: public int compare(Item first, Item second) {
062: return first._name.compareTo(second._name);
063: }
064: }
065:
066: public static class SmallerThanThreePredicate extends
067: Predicate<Item> {
068: public boolean match(Item candidate) {
069: return candidate._id < 3;
070: }
071: }
072:
073: public static class Item {
074: public int _id;
075: public String _name;
076:
077: public Item() {
078: this (0, null);
079: }
080:
081: public Item(int id, String name) {
082: this ._id = id;
083: this ._name = name;
084: }
085: }
086:
087: protected void configure(Configuration config) {
088: config.exceptionsOnNotStorable(true);
089: }
090:
091: protected void store() {
092: for (int i = 0; i < 4; i++) {
093: store(new Item(i, String.valueOf(3 - i)));
094: }
095: }
096:
097: public void testByIdAscending() {
098: assertIdOrder(new AscendingIdComparator(), new int[] { 0, 1, 2,
099: 3 });
100: }
101:
102: public void testByIdAscendingConstrained() {
103: Query query = newItemQuery();
104: query.descend("_id").constrain(new Integer(3)).smaller();
105: assertIdOrder(query, new AscendingIdComparator(), new int[] {
106: 0, 1, 2 });
107: }
108:
109: public void testByIdAscendingNQ() {
110: ObjectSet result = db().query(new SmallerThanThreePredicate(),
111: new AscendingIdComparator());
112: assertIdOrder(result, new int[] { 0, 1, 2 });
113: }
114:
115: public void testByIdDescending() {
116: assertIdOrder(new DescendingIdComparator(), new int[] { 3, 2,
117: 1, 0 });
118: }
119:
120: public void testByIdDescendingConstrained() {
121: Query query = newItemQuery();
122: query.descend("_id").constrain(new Integer(3)).smaller();
123: assertIdOrder(query, new DescendingIdComparator(), new int[] {
124: 2, 1, 0 });
125: }
126:
127: public void testByIdDescendingNQ() {
128: ObjectSet result = db().query(new SmallerThanThreePredicate(),
129: new DescendingIdComparator());
130: assertIdOrder(result, new int[] { 2, 1, 0 });
131: }
132:
133: public void testByIdOddEven() {
134: assertIdOrder(new OddEvenIdComparator(),
135: new int[] { 0, 2, 1, 3 });
136: }
137:
138: public void testByIdOddEvenConstrained() {
139: Query query = newItemQuery();
140: query.descend("_id").constrain(new Integer(3)).smaller();
141: assertIdOrder(query, new OddEvenIdComparator(), new int[] { 0,
142: 2, 1 });
143: }
144:
145: public void testByIdOddEvenNQ() {
146: ObjectSet result = db().query(new SmallerThanThreePredicate(),
147: new OddEvenIdComparator());
148: assertIdOrder(result, new int[] { 0, 2, 1 });
149: }
150:
151: public void testByNameAscending() {
152: assertIdOrder(new AscendingNameComparator(), new int[] { 3, 2,
153: 1, 0 });
154: }
155:
156: public void testByNameAscendingConstrained() {
157: Query query = newItemQuery();
158: query.descend("_id").constrain(new Integer(3)).smaller();
159: assertIdOrder(query, new AscendingNameComparator(), new int[] {
160: 2, 1, 0 });
161: }
162:
163: public void testByNameAscendingNQ() {
164: ObjectSet result = db().query(new SmallerThanThreePredicate(),
165: new AscendingNameComparator());
166: assertIdOrder(result, new int[] { 2, 1, 0 });
167: }
168:
169: private void assertIdOrder(QueryComparator comparator, int[] ids) {
170: Query query = newItemQuery();
171: assertIdOrder(query, comparator, ids);
172: }
173:
174: private Query newItemQuery() {
175: return newQuery(Item.class);
176: }
177:
178: private void assertIdOrder(Query query, QueryComparator comparator,
179: int[] ids) {
180: query.sortBy(comparator);
181: ObjectSet result = query.execute();
182: assertIdOrder(result, ids);
183: }
184:
185: private void assertIdOrder(ObjectSet result, int[] ids) {
186: Assert.areEqual(ids.length, result.size());
187: for (int idx = 0; idx < ids.length; idx++) {
188: Assert.areEqual(ids[idx], ((Item) result.next())._id);
189: }
190: }
191: }
|