1 16 package org.apache.commons.lang.mutable; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 29 public class MutableShortTest extends TestCase { 30 31 public MutableShortTest(String testName) { 32 super(testName); 33 } 34 35 public static void main(String [] args) { 36 TestRunner.run(suite()); 37 } 38 39 public static Test suite() { 40 return new TestSuite(MutableShortTest.class); 41 } 42 43 public void testConstructors() { 45 assertEquals((short) 0, new MutableShort().shortValue()); 46 47 assertEquals((short) 1, new MutableShort((short) 1).shortValue()); 48 49 assertEquals((short) 2, new MutableShort(new Short ((short) 2)).shortValue()); 50 assertEquals((short) 3, new MutableShort(new MutableShort((short) 3)).shortValue()); 51 try { 52 new MutableShort(null); 53 fail(); 54 } catch (NullPointerException ex) {} 55 } 56 57 public void testGetSet() { 58 final MutableShort mutNum = new MutableShort((short) 0); 59 assertEquals((short) 0, new MutableShort().shortValue()); 60 assertEquals(new Short ((short) 0), new MutableShort().getValue()); 61 62 mutNum.setValue((short) 1); 63 assertEquals((short) 1, mutNum.shortValue()); 64 assertEquals(new Short ((short) 1), mutNum.getValue()); 65 66 mutNum.setValue(new Short ((short) 2)); 67 assertEquals((short) 2, mutNum.shortValue()); 68 assertEquals(new Short ((short) 2), mutNum.getValue()); 69 70 mutNum.setValue(new MutableShort((short) 3)); 71 assertEquals((short) 3, mutNum.shortValue()); 72 assertEquals(new Short ((short) 3), mutNum.getValue()); 73 try { 74 mutNum.setValue(null); 75 fail(); 76 } catch (NullPointerException ex) {} 77 try { 78 mutNum.setValue("0"); 79 fail(); 80 } catch (ClassCastException ex) {} 81 } 82 83 public void testEquals() { 84 final MutableShort mutNumA = new MutableShort((short) 0); 85 final MutableShort mutNumB = new MutableShort((short) 0); 86 final MutableShort mutNumC = new MutableShort((short) 1); 87 88 assertEquals(true, mutNumA.equals(mutNumA)); 89 assertEquals(true, mutNumA.equals(mutNumB)); 90 assertEquals(true, mutNumB.equals(mutNumA)); 91 assertEquals(true, mutNumB.equals(mutNumB)); 92 assertEquals(false, mutNumA.equals(mutNumC)); 93 assertEquals(false, mutNumB.equals(mutNumC)); 94 assertEquals(true, mutNumC.equals(mutNumC)); 95 assertEquals(false, mutNumA.equals(null)); 96 assertEquals(false, mutNumA.equals(new Short ((short) 0))); 97 assertEquals(false, mutNumA.equals("0")); 98 } 99 100 public void testHashCode() { 101 final MutableShort mutNumA = new MutableShort((short) 0); 102 final MutableShort mutNumB = new MutableShort((short) 0); 103 final MutableShort mutNumC = new MutableShort((short) 1); 104 105 assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode()); 106 assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode()); 107 assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode()); 108 assertEquals(true, mutNumA.hashCode() == new Short ((short) 0).hashCode()); 109 } 110 111 public void testCompareTo() { 112 final MutableShort mutNum = new MutableShort((short) 0); 113 114 assertEquals((short) 0, mutNum.compareTo(new MutableShort((short) 0))); 115 assertEquals((short) +1, mutNum.compareTo(new MutableShort((short) -1))); 116 assertEquals((short) -1, mutNum.compareTo(new MutableShort((short) 1))); 117 try { 118 mutNum.compareTo(null); 119 fail(); 120 } catch (NullPointerException ex) {} 121 try { 122 mutNum.compareTo(new Short ((short) 0)); 123 fail(); 124 } catch (ClassCastException ex) {} 125 try { 126 mutNum.compareTo("0"); 127 fail(); 128 } catch (ClassCastException ex) {} 129 } 130 131 public void testPrimitiveValues() { 132 MutableShort mutNum = new MutableShort( (short) 1 ); 133 134 assertEquals( 1.0F, mutNum.floatValue(), 0 ); 135 assertEquals( 1.0, mutNum.doubleValue(), 0 ); 136 assertEquals( (byte) 1, mutNum.byteValue() ); 137 assertEquals( (short) 1, mutNum.shortValue() ); 138 assertEquals( 1, mutNum.intValue() ); 139 assertEquals( 1L, mutNum.longValue() ); 140 } 141 142 public void testToString() { 143 assertEquals("0", new MutableShort((short) 0).toString()); 144 assertEquals("10", new MutableShort((short) 10).toString()); 145 assertEquals("-123", new MutableShort((short) -123).toString()); 146 } 147 148 } 149 | Popular Tags |