KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > assorted > ComparatorSortTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.assorted;
22
23 import com.db4o.*;
24 import com.db4o.config.Configuration;
25 import com.db4o.query.*;
26
27 import db4ounit.Assert;
28 import db4ounit.extensions.AbstractDb4oTestCase;
29
30 public class ComparatorSortTestCase extends AbstractDb4oTestCase {
31     public static class AscendingIdComparator implements QueryComparator {
32         public int compare(Object JavaDoc first, Object JavaDoc second) {
33             return ((Item)first)._id-((Item)second)._id;
34         }
35     }
36
37     public static class DescendingIdComparator implements QueryComparator {
38         public int compare(Object JavaDoc first, Object JavaDoc second) {
39             return ((Item)second)._id-((Item)first)._id;
40         }
41     }
42
43     public static class OddEvenIdComparator implements QueryComparator {
44         public int compare(Object JavaDoc first, Object JavaDoc second) {
45             int idA=((Item)first)._id;
46             int idB=((Item)second)._id;
47             int modA=idA%2;
48             int modB=idB%2;
49             if(modA!=modB) {
50                 return modA-modB;
51             }
52             return idA-idB;
53         }
54     }
55
56     public static class AscendingNameComparator implements QueryComparator {
57         public int compare(Object JavaDoc first, Object JavaDoc second) {
58             return ((Item)first)._name.compareTo(((Item)second)._name);
59         }
60     }
61
62     public static class SmallerThanThreePredicate extends Predicate {
63         public boolean match(Item candidate) {
64             return candidate._id<3;
65         }
66     }
67     
68     public static class Item {
69         public int _id;
70         public String JavaDoc _name;
71     
72         public Item() {
73             this(0,null);
74         }
75         
76         public Item(int id, String JavaDoc name) {
77             this._id = id;
78             this._name = name;
79         }
80     }
81     
82     protected void configure(Configuration config) {
83         config.exceptionsOnNotStorable(true);
84     }
85     
86     protected void store() {
87         for(int i=0;i<4;i++) {
88             store(new Item(i,String.valueOf(3-i)));
89         }
90     }
91     
92     public void testByIdAscending() {
93         assertIdOrder(new AscendingIdComparator(),new int[]{0,1,2,3});
94     }
95
96     public void testByIdAscendingConstrained() {
97         Query query=newItemQuery();
98         query.descend("_id").constrain(new Integer JavaDoc(3)).smaller();
99         assertIdOrder(query,new AscendingIdComparator(),new int[]{0,1,2});
100     }
101
102     public void testByIdAscendingNQ() {
103         ObjectSet result=db().query(new SmallerThanThreePredicate(),new AscendingIdComparator());
104         assertIdOrder(result,new int[]{0,1,2});
105     }
106
107     public void testByIdDescending() {
108         assertIdOrder(new DescendingIdComparator(),new int[]{3,2,1,0});
109     }
110
111     public void testByIdDescendingConstrained() {
112         Query query=newItemQuery();
113         query.descend("_id").constrain(new Integer JavaDoc(3)).smaller();
114         assertIdOrder(query,new DescendingIdComparator(),new int[]{2,1,0});
115     }
116
117     public void testByIdDescendingNQ() {
118         ObjectSet result=db().query(new SmallerThanThreePredicate(),new DescendingIdComparator());
119         assertIdOrder(result,new int[]{2,1,0});
120     }
121
122     public void testByIdOddEven() {
123         assertIdOrder(new OddEvenIdComparator(),new int[]{0,2,1,3});
124     }
125
126     public void testByIdOddEvenConstrained() {
127         Query query=newItemQuery();
128         query.descend("_id").constrain(new Integer JavaDoc(3)).smaller();
129         assertIdOrder(query,new OddEvenIdComparator(),new int[]{0,2,1});
130     }
131
132     public void testByIdOddEvenNQ() {
133         ObjectSet result=db().query(new SmallerThanThreePredicate(),new OddEvenIdComparator());
134         assertIdOrder(result,new int[]{0,2,1});
135     }
136
137     public void testByNameAscending() {
138         assertIdOrder(new AscendingNameComparator(),new int[]{3,2,1,0});
139     }
140
141     public void testByNameAscendingConstrained() {
142         Query query=newItemQuery();
143         query.descend("_id").constrain(new Integer JavaDoc(3)).smaller();
144         assertIdOrder(query,new AscendingNameComparator(),new int[]{2,1,0});
145     }
146     
147     public void testByNameAscendingNQ() {
148         ObjectSet result=db().query(new SmallerThanThreePredicate(),new AscendingNameComparator());
149         assertIdOrder(result,new int[]{2,1,0});
150     }
151
152     private void assertIdOrder(QueryComparator comparator,int[] ids) {
153         Query query=newItemQuery();
154         assertIdOrder(query,comparator,ids);
155     }
156
157     private Query newItemQuery() {
158         return newQuery(Item.class);
159     }
160
161     private void assertIdOrder(Query query,QueryComparator comparator,int[] ids) {
162         query.sortBy(comparator);
163         ObjectSet result=query.execute();
164         assertIdOrder(result,ids);
165     }
166
167     private void assertIdOrder(ObjectSet result,int[] ids) {
168         Assert.areEqual(ids.length,result.size());
169         for (int idx = 0; idx < ids.length; idx++) {
170             Assert.areEqual(ids[idx], ((Item)result.next())._id);
171         }
172     }
173 }
174
Popular Tags