| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.commons.lang.mutable; |
| |
| import junit.framework.TestCase; |
| |
| /** |
| * JUnit tests. |
| * |
| * @version $Id$ |
| * @see MutableLong |
| */ |
| public class MutableLongTest extends TestCase { |
| |
| public MutableLongTest(String testName) { |
| super(testName); |
| } |
| |
| // ---------------------------------------------------------------- |
| public void testConstructors() { |
| assertEquals(0, new MutableLong().longValue()); |
| |
| assertEquals(1, new MutableLong(1).longValue()); |
| |
| assertEquals(2, new MutableLong(new Long(2)).longValue()); |
| assertEquals(3, new MutableLong(new MutableLong(3)).longValue()); |
| |
| assertEquals(2, new MutableLong("2").longValue()); |
| |
| try { |
| new MutableLong((Number)null); |
| fail(); |
| } catch (NullPointerException ex) {} |
| } |
| |
| public void testGetSet() { |
| final MutableLong mutNum = new MutableLong(0); |
| assertEquals(0, new MutableLong().longValue()); |
| assertEquals(new Long(0), new MutableLong().getValue()); |
| |
| mutNum.setValue(1); |
| assertEquals(1, mutNum.longValue()); |
| assertEquals(new Long(1), mutNum.getValue()); |
| |
| mutNum.setValue(new Long(2)); |
| assertEquals(2, mutNum.longValue()); |
| assertEquals(new Long(2), mutNum.getValue()); |
| |
| mutNum.setValue(new MutableLong(3)); |
| assertEquals(3, mutNum.longValue()); |
| assertEquals(new Long(3), mutNum.getValue()); |
| try { |
| mutNum.setValue(null); |
| fail(); |
| } catch (NullPointerException ex) {} |
| try { |
| mutNum.setValue("0"); |
| fail(); |
| } catch (ClassCastException ex) {} |
| } |
| |
| public void testEquals() { |
| final MutableLong mutNumA = new MutableLong(0); |
| final MutableLong mutNumB = new MutableLong(0); |
| final MutableLong mutNumC = new MutableLong(1); |
| |
| assertEquals(true, mutNumA.equals(mutNumA)); |
| assertEquals(true, mutNumA.equals(mutNumB)); |
| assertEquals(true, mutNumB.equals(mutNumA)); |
| assertEquals(true, mutNumB.equals(mutNumB)); |
| assertEquals(false, mutNumA.equals(mutNumC)); |
| assertEquals(false, mutNumB.equals(mutNumC)); |
| assertEquals(true, mutNumC.equals(mutNumC)); |
| assertEquals(false, mutNumA.equals(null)); |
| assertEquals(false, mutNumA.equals(new Long(0))); |
| assertEquals(false, mutNumA.equals("0")); |
| } |
| |
| public void testHashCode() { |
| final MutableLong mutNumA = new MutableLong(0); |
| final MutableLong mutNumB = new MutableLong(0); |
| final MutableLong mutNumC = new MutableLong(1); |
| |
| assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode()); |
| assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode()); |
| assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode()); |
| assertEquals(true, mutNumA.hashCode() == new Long(0).hashCode()); |
| } |
| |
| public void testCompareTo() { |
| final MutableLong mutNum = new MutableLong(0); |
| |
| assertEquals(0, mutNum.compareTo(new MutableLong(0))); |
| assertEquals(+1, mutNum.compareTo(new MutableLong(-1))); |
| assertEquals(-1, mutNum.compareTo(new MutableLong(1))); |
| try { |
| mutNum.compareTo(null); |
| fail(); |
| } catch (NullPointerException ex) {} |
| try { |
| mutNum.compareTo(new Long(0)); |
| fail(); |
| } catch (ClassCastException ex) {} |
| try { |
| mutNum.compareTo("0"); |
| fail(); |
| } catch (ClassCastException ex) {} |
| } |
| |
| public void testPrimitiveValues() { |
| MutableLong mutNum = new MutableLong(1L); |
| |
| assertEquals( 1.0F, mutNum.floatValue(), 0 ); |
| assertEquals( 1.0, mutNum.doubleValue(), 0 ); |
| assertEquals( (byte) 1, mutNum.byteValue() ); |
| assertEquals( (short) 1, mutNum.shortValue() ); |
| assertEquals( 1, mutNum.intValue() ); |
| assertEquals( 1L, mutNum.longValue() ); |
| } |
| |
| public void testToLong() { |
| assertEquals(new Long(0L), new MutableLong(0L).toLong()); |
| assertEquals(new Long(123L), new MutableLong(123L).toLong()); |
| } |
| |
| public void testIncrement() { |
| MutableLong mutNum = new MutableLong(1); |
| mutNum.increment(); |
| |
| assertEquals(2, mutNum.intValue()); |
| assertEquals(2L, mutNum.longValue()); |
| } |
| |
| public void testDecrement() { |
| MutableLong mutNum = new MutableLong(1); |
| mutNum.decrement(); |
| |
| assertEquals(0, mutNum.intValue()); |
| assertEquals(0L, mutNum.longValue()); |
| } |
| |
| public void testAddValuePrimitive() { |
| MutableLong mutNum = new MutableLong(1); |
| mutNum.add(1); |
| |
| assertEquals(2, mutNum.intValue()); |
| assertEquals(2L, mutNum.longValue()); |
| } |
| |
| public void testAddValueObject() { |
| MutableLong mutNum = new MutableLong(1); |
| mutNum.add(new Long(1)); |
| |
| assertEquals(2, mutNum.intValue()); |
| assertEquals(2L, mutNum.longValue()); |
| } |
| |
| public void testSubtractValuePrimitive() { |
| MutableLong mutNum = new MutableLong(1); |
| mutNum.subtract(1); |
| |
| assertEquals(0, mutNum.intValue()); |
| assertEquals(0L, mutNum.longValue()); |
| } |
| |
| public void testSubtractValueObject() { |
| MutableLong mutNum = new MutableLong(1); |
| mutNum.subtract(new Long(1)); |
| |
| assertEquals(0, mutNum.intValue()); |
| assertEquals(0L, mutNum.longValue()); |
| } |
| |
| public void testToString() { |
| assertEquals("0", new MutableLong(0).toString()); |
| assertEquals("10", new MutableLong(10).toString()); |
| assertEquals("-123", new MutableLong(-123).toString()); |
| } |
| |
| } |