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.lang.mutable;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  
24  /**
25   * JUnit tests.
26   * 
27   * @version $Id: MutableLongTest.java 618693 2008-02-05 16:33:29Z sebb $
28   * @see MutableLong
29   */
30  public class MutableLongTest extends TestCase {
31  
32      public MutableLongTest(String testName) {
33          super(testName);
34      }
35  
36      public static void main(String[] args) {
37          TestRunner.run(suite());
38      }
39  
40      public static Test suite() {
41          return new TestSuite(MutableLongTest.class);
42      }
43  
44      // ----------------------------------------------------------------
45      public void testConstructors() {
46          assertEquals(0, new MutableLong().longValue());
47          
48          assertEquals(1, new MutableLong(1).longValue());
49          
50          assertEquals(2, new MutableLong(new Long(2)).longValue());
51          assertEquals(3, new MutableLong(new MutableLong(3)).longValue());
52          try {
53              new MutableLong(null);
54              fail();
55          } catch (NullPointerException ex) {}
56      }
57  
58      public void testGetSet() {
59          final MutableLong mutNum = new MutableLong(0);
60          assertEquals(0, new MutableLong().longValue());
61          assertEquals(new Long(0), new MutableLong().getValue());
62          
63          mutNum.setValue(1);
64          assertEquals(1, mutNum.longValue());
65          assertEquals(new Long(1), mutNum.getValue());
66          
67          mutNum.setValue(new Long(2));
68          assertEquals(2, mutNum.longValue());
69          assertEquals(new Long(2), mutNum.getValue());
70          
71          mutNum.setValue(new MutableLong(3));
72          assertEquals(3, mutNum.longValue());
73          assertEquals(new Long(3), mutNum.getValue());
74          try {
75              mutNum.setValue(null);
76              fail();
77          } catch (NullPointerException ex) {}
78          try {
79              mutNum.setValue("0");
80              fail();
81          } catch (ClassCastException ex) {}
82      }
83  
84      public void testEquals() {
85          final MutableLong mutNumA = new MutableLong(0);
86          final MutableLong mutNumB = new MutableLong(0);
87          final MutableLong mutNumC = new MutableLong(1);
88  
89          assertEquals(true, mutNumA.equals(mutNumA));
90          assertEquals(true, mutNumA.equals(mutNumB));
91          assertEquals(true, mutNumB.equals(mutNumA));
92          assertEquals(true, mutNumB.equals(mutNumB));
93          assertEquals(false, mutNumA.equals(mutNumC));
94          assertEquals(false, mutNumB.equals(mutNumC));
95          assertEquals(true, mutNumC.equals(mutNumC));
96          assertEquals(false, mutNumA.equals(null));
97          assertEquals(false, mutNumA.equals(new Long(0)));
98          assertEquals(false, mutNumA.equals("0"));
99      }
100 
101     public void testHashCode() {
102         final MutableLong mutNumA = new MutableLong(0);
103         final MutableLong mutNumB = new MutableLong(0);
104         final MutableLong mutNumC = new MutableLong(1);
105 
106         assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
107         assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
108         assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
109         assertEquals(true, mutNumA.hashCode() == new Long(0).hashCode());
110     }
111 
112     public void testCompareTo() {
113         final MutableLong mutNum = new MutableLong(0);
114 
115         assertEquals(0, mutNum.compareTo(new MutableLong(0)));
116         assertEquals(+1, mutNum.compareTo(new MutableLong(-1)));
117         assertEquals(-1, mutNum.compareTo(new MutableLong(1)));
118         try {
119             mutNum.compareTo(null);
120             fail();
121         } catch (NullPointerException ex) {}
122         try {
123             mutNum.compareTo(new Long(0));
124             fail();
125         } catch (ClassCastException ex) {}
126         try {
127             mutNum.compareTo("0");
128             fail();
129         } catch (ClassCastException ex) {}
130     }
131 
132     public void testPrimitiveValues() {
133         MutableLong mutNum = new MutableLong(1L);
134 
135         assertEquals( 1.0F, mutNum.floatValue(), 0 );
136         assertEquals( 1.0, mutNum.doubleValue(), 0 );
137         assertEquals( (byte) 1, mutNum.byteValue() );
138         assertEquals( (short) 1, mutNum.shortValue() );
139         assertEquals( 1, mutNum.intValue() );
140         assertEquals( 1L, mutNum.longValue() );
141     }
142 
143     public void testToLong() {
144         assertEquals(new Long(0L), new MutableLong(0L).toLong());
145         assertEquals(new Long(123L), new MutableLong(123L).toLong());
146     }
147 
148     public void testIncrement() {
149         MutableLong mutNum = new MutableLong(1);
150         mutNum.increment();
151         
152         assertEquals(2, mutNum.intValue());
153         assertEquals(2L, mutNum.longValue());
154     }
155 
156     public void testDecrement() {
157         MutableLong mutNum = new MutableLong(1);
158         mutNum.decrement();
159         
160         assertEquals(0, mutNum.intValue());
161         assertEquals(0L, mutNum.longValue());
162     }
163 
164     public void testAddValuePrimitive() {
165         MutableLong mutNum = new MutableLong(1);
166         mutNum.add(1);
167         
168         assertEquals(2, mutNum.intValue());
169         assertEquals(2L, mutNum.longValue());
170     }
171 
172     public void testAddValueObject() {
173         MutableLong mutNum = new MutableLong(1);
174         mutNum.add(new Long(1));
175         
176         assertEquals(2, mutNum.intValue());
177         assertEquals(2L, mutNum.longValue());
178     }
179 
180     public void testSubtractValuePrimitive() {
181         MutableLong mutNum = new MutableLong(1);
182         mutNum.subtract(1);
183         
184         assertEquals(0, mutNum.intValue());
185         assertEquals(0L, mutNum.longValue());
186     }
187 
188     public void testSubtractValueObject() {
189         MutableLong mutNum = new MutableLong(1);
190         mutNum.subtract(new Long(1));
191         
192         assertEquals(0, mutNum.intValue());
193         assertEquals(0L, mutNum.longValue());
194     }
195 
196     public void testToString() {
197         assertEquals("0", new MutableLong(0).toString());
198         assertEquals("10", new MutableLong(10).toString());
199         assertEquals("-123", new MutableLong(-123).toString());
200     }
201 
202 }