BooleanTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.lang;
18
19import dalvik.annotation.TestInfo;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTarget;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26@TestTargetClass(Boolean.class)
27public class BooleanTest extends TestCase {
28
29    /**
30     * @tests java.lang.Boolean#hashCode()
31     */
32    @TestInfo(
33      level = TestLevel.COMPLETE,
34      purpose = "",
35      targets = {
36        @TestTarget(
37          methodName = "hashCode",
38          methodArgs = {}
39        )
40    })
41    public void test_hashCode() {
42        assertEquals(1231, Boolean.TRUE.hashCode());
43        assertEquals(1237, Boolean.FALSE.hashCode());
44    }
45
46    /**
47     * @tests java.lang.Boolean#Boolean(String)
48     */
49    @TestInfo(
50      level = TestLevel.COMPLETE,
51      purpose = "",
52      targets = {
53        @TestTarget(
54          methodName = "Boolean",
55          methodArgs = {java.lang.String.class}
56        )
57    })
58    public void test_ConstructorLjava_lang_String() {
59        assertEquals(Boolean.TRUE, new Boolean("TRUE"));
60        assertEquals(Boolean.TRUE, new Boolean("true"));
61        assertEquals(Boolean.TRUE, new Boolean("True"));
62
63        assertEquals(Boolean.FALSE, new Boolean("yes"));
64        assertEquals(Boolean.FALSE, new Boolean("false"));
65    }
66
67    /**
68     * @tests java.lang.Boolean#Boolean(boolean)
69     */
70    @TestInfo(
71      level = TestLevel.COMPLETE,
72      purpose = "",
73      targets = {
74        @TestTarget(
75          methodName = "Boolean",
76          methodArgs = {boolean.class}
77        )
78    })
79    public void test_ConstructorZ() {
80        assertEquals(Boolean.TRUE, new Boolean(true));
81        assertEquals(Boolean.FALSE, new Boolean(false));
82    }
83
84    /**
85     * @tests java.lang.Boolean#booleanValue()
86     */
87    @TestInfo(
88      level = TestLevel.COMPLETE,
89      purpose = "",
90      targets = {
91        @TestTarget(
92          methodName = "booleanValue",
93          methodArgs = {}
94        )
95    })
96    public void test_booleanValue() {
97        assertTrue(Boolean.TRUE.booleanValue());
98        assertFalse(Boolean.FALSE.booleanValue());
99    }
100
101    /**
102     * @tests java.lang.Boolean#equals(Object)
103     */
104    @TestInfo(
105      level = TestLevel.COMPLETE,
106      purpose = "",
107      targets = {
108        @TestTarget(
109          methodName = "equals",
110          methodArgs = {java.lang.Object.class}
111        )
112    })
113    public void test_equalsLjava_lang_Object() {
114        assertTrue(Boolean.TRUE.equals(Boolean.TRUE));
115        assertTrue(Boolean.TRUE.equals(new Boolean(true)));
116        assertFalse(Boolean.TRUE.equals("true"));
117        assertFalse(Boolean.TRUE.equals(null));
118        assertFalse(Boolean.FALSE.equals(Boolean.TRUE));
119        assertTrue(Boolean.FALSE.equals(Boolean.FALSE));
120        assertTrue(Boolean.FALSE.equals(new Boolean(false)));
121    }
122
123    /**
124     * @tests java.lang.Boolean#getBoolean(String)
125     */
126    @TestInfo(
127      level = TestLevel.COMPLETE,
128      purpose = "",
129      targets = {
130        @TestTarget(
131          methodName = "getBoolean",
132          methodArgs = {java.lang.String.class}
133        )
134    })
135    public void test_getBooleanLjava_lang_String() {
136        System.setProperty(getClass().getName(), "true");
137        assertTrue(Boolean.getBoolean(getClass().getName()));
138
139        System.setProperty(getClass().getName(), "TRUE");
140        assertTrue(Boolean.getBoolean(getClass().getName()));
141
142        System.setProperty(getClass().getName(), "false");
143        assertFalse(Boolean.getBoolean(getClass().getName()));
144    }
145
146    /**
147     * @tests java.lang.Boolean#toString()
148     */
149    @TestInfo(
150      level = TestLevel.COMPLETE,
151      purpose = "",
152      targets = {
153        @TestTarget(
154          methodName = "toString",
155          methodArgs = {}
156        )
157    })
158    public void test_toString() {
159        assertEquals("true", Boolean.TRUE.toString());
160        assertEquals("false", Boolean.FALSE.toString());
161    }
162
163    /**
164     * @tests java.lang.Boolean#toString(boolean)
165     */
166    @TestInfo(
167      level = TestLevel.COMPLETE,
168      purpose = "",
169      targets = {
170        @TestTarget(
171          methodName = "toString",
172          methodArgs = {boolean.class}
173        )
174    })
175    public void test_toStringZ() {
176        assertEquals("true", Boolean.toString(true));
177        assertEquals("false", Boolean.toString(false));
178    }
179
180    /**
181     * @tests java.lang.Boolean#valueOf(String)
182     */
183    @TestInfo(
184      level = TestLevel.COMPLETE,
185      purpose = "",
186      targets = {
187        @TestTarget(
188          methodName = "valueOf",
189          methodArgs = {java.lang.String.class}
190        )
191    })
192    public void test_valueOfLjava_lang_String() {
193        assertEquals(Boolean.TRUE, Boolean.valueOf("true"));
194        assertEquals(Boolean.FALSE, Boolean.valueOf("false"));
195
196        assertEquals(Boolean.TRUE, Boolean.valueOf("TRUE"));
197        assertEquals(Boolean.FALSE, Boolean.valueOf("false"));
198
199        assertEquals(Boolean.FALSE, Boolean.valueOf(null));
200        assertEquals(Boolean.FALSE, Boolean.valueOf(""));
201        assertEquals(Boolean.FALSE, Boolean.valueOf("invalid"));
202
203        assertTrue("Failed to parse true to true", Boolean.valueOf("true").booleanValue());
204        assertTrue("Failed to parse mixed case true to true", Boolean.valueOf("TrUe")
205                .booleanValue());
206        assertTrue("parsed non-true to true", !Boolean.valueOf("ddddd").booleanValue());
207    }
208
209    /**
210     * @tests java.lang.Boolean#valueOf(boolean)
211     */
212    @TestInfo(
213      level = TestLevel.COMPLETE,
214      purpose = "",
215      targets = {
216        @TestTarget(
217          methodName = "valueOf",
218          methodArgs = {boolean.class}
219        )
220    })
221    public void test_valueOfZ() {
222        assertEquals(Boolean.TRUE, Boolean.valueOf(true));
223        assertEquals(Boolean.FALSE, Boolean.valueOf(false));
224    }
225
226    /**
227     * @tests java.lang.Boolean#parseBoolean(String)
228     */
229    @TestInfo(
230      level = TestLevel.COMPLETE,
231      purpose = "",
232      targets = {
233        @TestTarget(
234          methodName = "parseBoolean",
235          methodArgs = {java.lang.String.class}
236        )
237    })
238    public void test_parseBooleanLjava_lang_String() {
239        assertTrue(Boolean.parseBoolean("true"));
240        assertTrue(Boolean.parseBoolean("TRUE"));
241        assertFalse(Boolean.parseBoolean("false"));
242        assertFalse(Boolean.parseBoolean(null));
243        assertFalse(Boolean.parseBoolean(""));
244        assertFalse(Boolean.parseBoolean("invalid"));
245    }
246
247    /**
248     * @tests java.lang.Boolean#compareTo(Boolean)
249     */
250    @TestInfo(
251      level = TestLevel.COMPLETE,
252      purpose = "",
253      targets = {
254        @TestTarget(
255          methodName = "compareTo",
256          methodArgs = {java.lang.Boolean.class}
257        )
258    })
259    public void test_compareToLjava_lang_Boolean() {
260        assertTrue(Boolean.TRUE.compareTo(Boolean.TRUE) == 0);
261        assertTrue(Boolean.FALSE.compareTo(Boolean.FALSE) == 0);
262        assertTrue(Boolean.TRUE.compareTo(Boolean.FALSE) > 0);
263        assertTrue(Boolean.FALSE.compareTo(Boolean.TRUE) < 0);
264
265        try {
266            Boolean.TRUE.compareTo(null);
267            fail("No NPE");
268        } catch (NullPointerException e) {
269        }
270    }
271}
272