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;
022:
023: import com.db4o.*;
024: import com.db4o.query.*;
025:
026: import db4ounit.*;
027: import db4ounit.extensions.*;
028:
029: // COR-18
030: public class SortMultipleTestCase extends AbstractDb4oTestCase {
031:
032: public static void main(String[] arguments) {
033: new SortMultipleTestCase().runSolo();
034: }
035:
036: public static class IntHolder {
037: public int _value;
038:
039: public IntHolder(int value) {
040: this ._value = value;
041: }
042:
043: public boolean equals(Object obj) {
044: if (this == obj) {
045: return true;
046: }
047: if (obj == null || getClass() != obj.getClass()) {
048: return false;
049: }
050: IntHolder intHolder = (IntHolder) obj;
051: return _value == intHolder._value;
052: }
053:
054: public int hashCode() {
055: return _value;
056: }
057:
058: public String toString() {
059: return String.valueOf(_value);
060: }
061: }
062:
063: public static class Data {
064: public int _first;
065: public int _second;
066: public IntHolder _third;
067:
068: public Data(int first, int second, int third) {
069: this ._first = first;
070: this ._second = second;
071: this ._third = new IntHolder(third);
072: }
073:
074: public boolean equals(Object obj) {
075: if (this == obj) {
076: return true;
077: }
078: if (obj == null || getClass() != obj.getClass()) {
079: return false;
080: }
081: Data data = (Data) obj;
082: return _first == data._first && _second == data._second
083: && _third.equals(data._third);
084: }
085:
086: public int hashCode() {
087: int hc = _first;
088: hc *= 29 + _second;
089: hc *= 29 + _third.hashCode();
090: return hc;
091: }
092:
093: public String toString() {
094: return _first + "/" + _second + "/" + _third;
095: }
096: }
097:
098: private final static Data[] DATA = { new Data(1, 2, 4), // 0
099: new Data(1, 4, 3), // 1
100: new Data(2, 4, 2), // 2
101: new Data(3, 1, 4), // 3
102: new Data(4, 3, 1), // 4
103: new Data(4, 1, 3) // 5
104: };
105:
106: protected void store() throws Exception {
107: for (int dataIdx = 0; dataIdx < DATA.length; dataIdx++) {
108: store(DATA[dataIdx]);
109: }
110: }
111:
112: public void testSortFirstThenSecond() {
113: Query query = newQuery(Data.class);
114: query.descend("_first").orderAscending();
115: query.descend("_second").orderAscending();
116: assertSortOrder(query, new int[] { 0, 1, 2, 3, 5, 4 });
117: }
118:
119: public void testSortSecondThenFirst() {
120: Query query = newQuery(Data.class);
121: query.descend("_second").orderAscending();
122: query.descend("_first").orderAscending();
123: assertSortOrder(query, new int[] { 3, 5, 0, 4, 1, 2 });
124: }
125:
126: public void testSortThirdThenFirst() {
127: Query query = newQuery(Data.class);
128: query.descend("_third").descend("_value").orderAscending();
129: query.descend("_first").orderAscending();
130: assertSortOrder(query, new int[] { 4, 2, 1, 5, 0, 3 });
131: }
132:
133: public void testSortThirdThenSecond() {
134: Query query = newQuery(Data.class);
135: query.descend("_third").descend("_value").orderAscending();
136: query.descend("_second").orderAscending();
137: assertSortOrder(query, new int[] { 4, 2, 5, 1, 3, 0 });
138: }
139:
140: public void testSortSecondThenThird() {
141: Query query = newQuery(Data.class);
142: query.descend("_second").orderAscending();
143: query.descend("_third").descend("_value").orderAscending();
144: assertSortOrder(query, new int[] { 5, 3, 0, 4, 2, 1 });
145: }
146:
147: private void assertSortOrder(Query query, int[] expectedIndexes) {
148: ObjectSet result = query.execute();
149: Assert.areEqual(expectedIndexes.length, result.size());
150: for (int i = 0; i < expectedIndexes.length; i++) {
151: Assert.areEqual(DATA[expectedIndexes[i]], result.next());
152: }
153: }
154: }
|