View Javadoc
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  
18  package org.apache.commons.functor.range;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.junit.Test;
24  
25  /**
26   * Tests for endpoint.
27   *
28   * @since 1.0
29   * @version $Revision: $ $Date: $
30   */
31  public class TestEndpoint {
32  
33      private final Endpoint<Integer> openEndpoint = new Endpoint<Integer>(1, BoundType.OPEN);
34      private final Endpoint<Integer> closedEndpoint = new Endpoint<Integer>(2, BoundType.CLOSED);
35  
36      @Test
37      public void testValue() {
38          assertEquals(Integer.valueOf(1), openEndpoint.getValue());
39          assertEquals(Integer.valueOf(2), closedEndpoint.getValue());
40      }
41  
42      @Test
43      public void testBoundType() {
44          assertEquals(BoundType.OPEN, openEndpoint.getBoundType());
45          assertEquals(BoundType.CLOSED, closedEndpoint.getBoundType());
46      }
47  
48      @Test
49      public void testToString() {
50          assertEquals("Endpoint<1, OPEN>", openEndpoint.toString());
51          assertEquals("Endpoint<2, CLOSED>", closedEndpoint.toString());
52          assertEquals("(1", openEndpoint.toLeftString());
53          assertEquals("[2", closedEndpoint.toLeftString());
54          assertEquals("1)", openEndpoint.toRightString());
55          assertEquals("2]", closedEndpoint.toRightString());
56      }
57  
58      @Test
59      public void testEquals()
60          throws Exception {
61          // equals basic properties
62          Endpoint<Integer> endpoint = new Endpoint<Integer>(1, BoundType.OPEN);
63          assertEquals("equals must be reflexive", endpoint, endpoint);
64          assertEquals("hashCode must be reflexive", endpoint.hashCode(),
65                       endpoint.hashCode());
66          assertTrue(!endpoint.equals(null)); // should be able to compare to null
67  
68          Object endpoint2 = new Endpoint<Integer>(1, BoundType.OPEN);
69          if (endpoint.equals(endpoint2)) {
70              assertEquals("equals implies hash equals", endpoint.hashCode(),
71                           endpoint2.hashCode());
72              assertEquals("equals must be symmetric", endpoint2, endpoint);
73          } else {
74              assertTrue("equals must be symmetric", !endpoint2.equals(endpoint));
75          }
76  
77          Object differentEndpoint = new Endpoint<Integer>(1, BoundType.CLOSED);
78          assertTrue(!differentEndpoint.equals(endpoint));
79          assertTrue(differentEndpoint.hashCode() != endpoint.hashCode());
80      }
81  
82  }