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.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetNew; 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 @TestTargetNew( 33 level = TestLevel.COMPLETE, 34 notes = "", 35 method = "hashCode", 36 args = {} 37 ) 38 public void test_hashCode() { 39 assertEquals(1231, Boolean.TRUE.hashCode()); 40 assertEquals(1237, Boolean.FALSE.hashCode()); 41 } 42 43 /** 44 * @tests java.lang.Boolean#Boolean(String) 45 */ 46 @TestTargetNew( 47 level = TestLevel.COMPLETE, 48 notes = "", 49 method = "Boolean", 50 args = {java.lang.String.class} 51 ) 52 public void test_ConstructorLjava_lang_String() { 53 assertEquals(Boolean.TRUE, new Boolean("TRUE")); 54 assertEquals(Boolean.TRUE, new Boolean("true")); 55 assertEquals(Boolean.TRUE, new Boolean("True")); 56 57 assertEquals(Boolean.FALSE, new Boolean("yes")); 58 assertEquals(Boolean.FALSE, new Boolean("false")); 59 } 60 61 /** 62 * @tests java.lang.Boolean#Boolean(boolean) 63 */ 64 @TestTargetNew( 65 level = TestLevel.COMPLETE, 66 notes = "", 67 method = "Boolean", 68 args = {boolean.class} 69 ) 70 public void test_ConstructorZ() { 71 assertEquals(Boolean.TRUE, new Boolean(true)); 72 assertEquals(Boolean.FALSE, new Boolean(false)); 73 } 74 75 /** 76 * @tests java.lang.Boolean#booleanValue() 77 */ 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 notes = "", 81 method = "booleanValue", 82 args = {} 83 ) 84 public void test_booleanValue() { 85 assertTrue(Boolean.TRUE.booleanValue()); 86 assertFalse(Boolean.FALSE.booleanValue()); 87 } 88 89 /** 90 * @tests java.lang.Boolean#equals(Object) 91 */ 92 @TestTargetNew( 93 level = TestLevel.COMPLETE, 94 notes = "", 95 method = "equals", 96 args = {java.lang.Object.class} 97 ) 98 public void test_equalsLjava_lang_Object() { 99 assertTrue(Boolean.TRUE.equals(Boolean.TRUE)); 100 assertTrue(Boolean.TRUE.equals(new Boolean(true))); 101 assertFalse(Boolean.TRUE.equals("true")); 102 assertFalse(Boolean.TRUE.equals(null)); 103 assertFalse(Boolean.FALSE.equals(Boolean.TRUE)); 104 assertTrue(Boolean.FALSE.equals(Boolean.FALSE)); 105 assertTrue(Boolean.FALSE.equals(new Boolean(false))); 106 } 107 108 /** 109 * @tests java.lang.Boolean#getBoolean(String) 110 */ 111 @TestTargetNew( 112 level = TestLevel.COMPLETE, 113 notes = "", 114 method = "getBoolean", 115 args = {java.lang.String.class} 116 ) 117 public void test_getBooleanLjava_lang_String() { 118 System.setProperty(getClass().getName(), "true"); 119 assertTrue(Boolean.getBoolean(getClass().getName())); 120 121 System.setProperty(getClass().getName(), "TRUE"); 122 assertTrue(Boolean.getBoolean(getClass().getName())); 123 124 System.setProperty(getClass().getName(), "false"); 125 assertFalse(Boolean.getBoolean(getClass().getName())); 126 } 127 128 /** 129 * @tests java.lang.Boolean#toString() 130 */ 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 notes = "", 134 method = "toString", 135 args = {} 136 ) 137 public void test_toString() { 138 assertEquals("true", Boolean.TRUE.toString()); 139 assertEquals("false", Boolean.FALSE.toString()); 140 } 141 142 /** 143 * @tests java.lang.Boolean#toString(boolean) 144 */ 145 @TestTargetNew( 146 level = TestLevel.COMPLETE, 147 notes = "", 148 method = "toString", 149 args = {boolean.class} 150 ) 151 public void test_toStringZ() { 152 assertEquals("true", Boolean.toString(true)); 153 assertEquals("false", Boolean.toString(false)); 154 } 155 156 /** 157 * @tests java.lang.Boolean#valueOf(String) 158 */ 159 @TestTargetNew( 160 level = TestLevel.COMPLETE, 161 notes = "", 162 method = "valueOf", 163 args = {java.lang.String.class} 164 ) 165 public void test_valueOfLjava_lang_String() { 166 assertEquals(Boolean.TRUE, Boolean.valueOf("true")); 167 assertEquals(Boolean.FALSE, Boolean.valueOf("false")); 168 169 assertEquals(Boolean.TRUE, Boolean.valueOf("TRUE")); 170 assertEquals(Boolean.FALSE, Boolean.valueOf("false")); 171 172 assertEquals(Boolean.FALSE, Boolean.valueOf(null)); 173 assertEquals(Boolean.FALSE, Boolean.valueOf("")); 174 assertEquals(Boolean.FALSE, Boolean.valueOf("invalid")); 175 176 assertTrue("Failed to parse true to true", Boolean.valueOf("true").booleanValue()); 177 assertTrue("Failed to parse mixed case true to true", Boolean.valueOf("TrUe") 178 .booleanValue()); 179 assertTrue("parsed non-true to true", !Boolean.valueOf("ddddd").booleanValue()); 180 } 181 182 /** 183 * @tests java.lang.Boolean#valueOf(boolean) 184 */ 185 @TestTargetNew( 186 level = TestLevel.COMPLETE, 187 notes = "", 188 method = "valueOf", 189 args = {boolean.class} 190 ) 191 public void test_valueOfZ() { 192 assertEquals(Boolean.TRUE, Boolean.valueOf(true)); 193 assertEquals(Boolean.FALSE, Boolean.valueOf(false)); 194 } 195 196 /** 197 * @tests java.lang.Boolean#parseBoolean(String) 198 */ 199 @TestTargetNew( 200 level = TestLevel.COMPLETE, 201 notes = "", 202 method = "parseBoolean", 203 args = {java.lang.String.class} 204 ) 205 public void test_parseBooleanLjava_lang_String() { 206 assertTrue(Boolean.parseBoolean("true")); 207 assertTrue(Boolean.parseBoolean("TRUE")); 208 assertFalse(Boolean.parseBoolean("false")); 209 assertFalse(Boolean.parseBoolean(null)); 210 assertFalse(Boolean.parseBoolean("")); 211 assertFalse(Boolean.parseBoolean("invalid")); 212 } 213 214 /** 215 * @tests java.lang.Boolean#compareTo(Boolean) 216 */ 217 @TestTargetNew( 218 level = TestLevel.COMPLETE, 219 notes = "", 220 method = "compareTo", 221 args = {java.lang.Boolean.class} 222 ) 223 public void test_compareToLjava_lang_Boolean() { 224 assertTrue(Boolean.TRUE.compareTo(Boolean.TRUE) == 0); 225 assertTrue(Boolean.FALSE.compareTo(Boolean.FALSE) == 0); 226 assertTrue(Boolean.TRUE.compareTo(Boolean.FALSE) > 0); 227 assertTrue(Boolean.FALSE.compareTo(Boolean.TRUE) < 0); 228 229 try { 230 Boolean.TRUE.compareTo(null); 231 fail("No NPE"); 232 } catch (NullPointerException e) { 233 } 234 } 235} 236