001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.functor.range; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import org.junit.Test; 024 025/** 026 * Tests for endpoint. 027 * 028 * @since 1.0 029 * @version $Revision: $ $Date: $ 030 */ 031public class TestEndpoint { 032 033 private final Endpoint<Integer> openEndpoint = new Endpoint<Integer>(1, BoundType.OPEN); 034 private final Endpoint<Integer> closedEndpoint = new Endpoint<Integer>(2, BoundType.CLOSED); 035 036 @Test 037 public void testValue() { 038 assertEquals(Integer.valueOf(1), openEndpoint.getValue()); 039 assertEquals(Integer.valueOf(2), closedEndpoint.getValue()); 040 } 041 042 @Test 043 public void testBoundType() { 044 assertEquals(BoundType.OPEN, openEndpoint.getBoundType()); 045 assertEquals(BoundType.CLOSED, closedEndpoint.getBoundType()); 046 } 047 048 @Test 049 public void testToString() { 050 assertEquals("Endpoint<1, OPEN>", openEndpoint.toString()); 051 assertEquals("Endpoint<2, CLOSED>", closedEndpoint.toString()); 052 assertEquals("(1", openEndpoint.toLeftString()); 053 assertEquals("[2", closedEndpoint.toLeftString()); 054 assertEquals("1)", openEndpoint.toRightString()); 055 assertEquals("2]", closedEndpoint.toRightString()); 056 } 057 058 @Test 059 public void testEquals() 060 throws Exception { 061 // equals basic properties 062 Endpoint<Integer> endpoint = new Endpoint<Integer>(1, BoundType.OPEN); 063 assertEquals("equals must be reflexive", endpoint, endpoint); 064 assertEquals("hashCode must be reflexive", endpoint.hashCode(), 065 endpoint.hashCode()); 066 assertTrue(!endpoint.equals(null)); // should be able to compare to null 067 068 Object endpoint2 = new Endpoint<Integer>(1, BoundType.OPEN); 069 if (endpoint.equals(endpoint2)) { 070 assertEquals("equals implies hash equals", endpoint.hashCode(), 071 endpoint2.hashCode()); 072 assertEquals("equals must be symmetric", endpoint2, endpoint); 073 } else { 074 assertTrue("equals must be symmetric", !endpoint2.equals(endpoint)); 075 } 076 077 Object differentEndpoint = new Endpoint<Integer>(1, BoundType.CLOSED); 078 assertTrue(!differentEndpoint.equals(endpoint)); 079 assertTrue(differentEndpoint.hashCode() != endpoint.hashCode()); 080 } 081 082}