01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.io.ByteArrayOutputStream;
20: import java.io.ObjectInputStream;
21: import java.io.ObjectOutputStream;
22: import java.util.Collections;
23: import java.util.Comparator;
24: import java.util.LinkedList;
25: import java.util.List;
26:
27: import junit.framework.Test;
28: import junit.framework.TestSuite;
29:
30: /**
31: * Tests for ReverseComparator.
32: *
33: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
34: *
35: * @author Unknown
36: */
37: public class TestReverseComparator extends AbstractTestComparator {
38:
39: public TestReverseComparator(String 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 makeComparator() {
58: return new ReverseComparator(Collections.reverseOrder());
59: }
60:
61: public List getComparableObjectsOrdered() {
62: List list = new LinkedList();
63: list.add(new Integer(1));
64: list.add(new Integer(2));
65: list.add(new Integer(3));
66: list.add(new Integer(4));
67: list.add(new Integer(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 {
77: Comparator comp = new ReverseComparator(
78: new ComparableComparator());
79:
80: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
81: ObjectOutputStream out = new ObjectOutputStream(buffer);
82: out.writeObject(comp);
83: out.close();
84:
85: ObjectInputStream in = new ObjectInputStream(
86: new ByteArrayInputStream(buffer.toByteArray()));
87: Object dest = in.readObject();
88: in.close();
89: assertEquals("obj != deserialize(serialize(obj))", comp, dest);
90: }
91:
92: }
|