1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.comparator;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import org.junit.Test;
24  
25  /**
26   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
27   * @author Rodney Waldhoff
28   */
29  public class TestComparableComparator {
30  
31      // Tests
32      // ------------------------------------------------------------------------
33  
34      @Test
35      public void testCompareIntegers() {
36          assertTrue(ComparableComparator.instance().compare(new Integer(Integer.MIN_VALUE),new Integer(Integer.MIN_VALUE)) == 0);
37          assertTrue(ComparableComparator.instance().compare(new Integer(-1),new Integer(-1)) == 0);
38          assertTrue(ComparableComparator.instance().compare(new Integer(0),new Integer(0)) == 0);
39          assertTrue(ComparableComparator.instance().compare(new Integer(Integer.MAX_VALUE),new Integer(Integer.MAX_VALUE)) == 0);
40          assertTrue(ComparableComparator.instance().compare(new Integer(1),new Integer(1)) == 0);
41      }
42  
43      @Test
44      public void testCompareIncomparable() {
45          try {
46              ComparableComparator.instance().compare(new Object(),new Integer(2));
47              fail("Expected ClassCastException");
48          } catch(ClassCastException e) {
49              // expected
50          }
51      }
52  
53      @Test
54      public void testCompareNull() {
55          try {
56              ComparableComparator.instance().compare(null,new Integer(2));
57              fail("Expected NullPointerException");
58          } catch(NullPointerException e) {
59              // expected
60          }
61      }
62  
63      @Test
64      public void testEqualsAndHashCode() {
65          assertEquals(new ComparableComparator(),new ComparableComparator());
66          assertEquals(new ComparableComparator().hashCode(),new ComparableComparator().hashCode());
67          assertTrue(!new ComparableComparator().equals(null));
68      }
69  }