KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > comparators > TestReverseComparator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.comparators;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Comparator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 /**
31  * Tests for ReverseComparator.
32  *
33  * @version $Revision: 1.9 $ $Date: 2004/02/18 01:20:34 $
34  *
35  * @author Unknown
36  */

37 public class TestReverseComparator extends AbstractTestComparator {
38
39     public TestReverseComparator(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static Test suite() {
44         return new TestSuite(TestReverseComparator.class);
45     }
46
47     /**
48      * For the purposes of this test, return a
49      * ReverseComparator that wraps the java.util.Collections.reverseOrder()
50      * Comparator. The resulting comparator should
51      * sort according to natural Order. (Note: we wrap
52      * a Comparator taken from the JDK so that we can
53      * save a "canonical" form in CVS.
54      *
55      * @return Comparator that returns "natural" order
56      */

57     public Comparator JavaDoc makeComparator() {
58         return new ReverseComparator(Collections.reverseOrder());
59     }
60
61     public List JavaDoc getComparableObjectsOrdered() {
62         List JavaDoc list = new LinkedList JavaDoc();
63         list.add(new Integer JavaDoc(1));
64         list.add(new Integer JavaDoc(2));
65         list.add(new Integer JavaDoc(3));
66         list.add(new Integer JavaDoc(4));
67         list.add(new Integer JavaDoc(5));
68         return list;
69     }
70
71     /**
72      * Override this inherited test since Collections.reverseOrder
73      * doesn't adhere to the "soft" Comparator contract, and we've
74      * already "cannonized" the comparator returned by makeComparator.
75      */

76     public void testSerializeDeserializeThenCompare() throws Exception JavaDoc {
77         Comparator JavaDoc comp = new ReverseComparator(new ComparableComparator());
78
79         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
80         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
81         out.writeObject(comp);
82         out.close();
83             
84         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
85         Object JavaDoc dest = in.readObject();
86         in.close();
87         assertEquals("obj != deserialize(serialize(obj))",comp,dest);
88     }
89
90 }
91
Popular Tags