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 17 package org.apache.harmony.tests.java.lang; 18 19 import junit.framework.TestCase; 20 21 public class BooleanTest extends TestCase { 22 23 /** 24 * java.lang.Boolean#hashCode() 25 */ 26 public void test_hashCode() { 27 assertEquals(1231, Boolean.TRUE.hashCode()); 28 assertEquals(1237, Boolean.FALSE.hashCode()); 29 } 30 31 /** 32 * java.lang.Boolean#Boolean(String) 33 */ 34 public void test_ConstructorLjava_lang_String() { 35 assertEquals(Boolean.TRUE, new Boolean("TRUE")); 36 assertEquals(Boolean.TRUE, new Boolean("true")); 37 assertEquals(Boolean.TRUE, new Boolean("True")); 38 39 assertEquals(Boolean.FALSE, new Boolean("yes")); 40 assertEquals(Boolean.FALSE, new Boolean("false")); 41 } 42 43 /** 44 * java.lang.Boolean#Boolean(boolean) 45 */ 46 public void test_ConstructorZ() { 47 assertEquals(Boolean.TRUE, new Boolean(true)); 48 assertEquals(Boolean.FALSE, new Boolean(false)); 49 } 50 51 /** 52 * java.lang.Boolean#booleanValue() 53 */ 54 public void test_booleanValue() { 55 assertTrue(Boolean.TRUE.booleanValue()); 56 assertFalse(Boolean.FALSE.booleanValue()); 57 } 58 59 /** 60 * java.lang.Boolean#equals(Object) 61 */ 62 public void test_equalsLjava_lang_Object() { 63 assertTrue(Boolean.TRUE.equals(Boolean.TRUE)); 64 assertTrue(Boolean.TRUE.equals(new Boolean(true))); 65 assertFalse(Boolean.TRUE.equals("true")); 66 assertFalse(Boolean.TRUE.equals(null)); 67 assertFalse(Boolean.FALSE.equals(Boolean.TRUE)); 68 assertTrue(Boolean.FALSE.equals(Boolean.FALSE)); 69 assertTrue(Boolean.FALSE.equals(new Boolean(false))); 70 } 71 72 /** 73 * java.lang.Boolean#getBoolean(String) 74 */ 75 public void test_getBooleanLjava_lang_String() { 76 System.setProperty(getClass().getName(), "true"); 77 assertTrue(Boolean.getBoolean(getClass().getName())); 78 79 System.setProperty(getClass().getName(), "TRUE"); 80 assertTrue(Boolean.getBoolean(getClass().getName())); 81 82 System.setProperty(getClass().getName(), "false"); 83 assertFalse(Boolean.getBoolean(getClass().getName())); 84 } 85 86 /** 87 * java.lang.Boolean#toString() 88 */ 89 public void test_toString() { 90 assertEquals("true", Boolean.TRUE.toString()); 91 assertEquals("false", Boolean.FALSE.toString()); 92 } 93 94 /** 95 * java.lang.Boolean#toString(boolean) 96 */ 97 public void test_toStringZ() { 98 assertEquals("true", Boolean.toString(true)); 99 assertEquals("false", Boolean.toString(false)); 100 } 101 102 /** 103 * java.lang.Boolean#valueOf(String) 104 */ 105 public void test_valueOfLjava_lang_String() { 106 assertEquals(Boolean.TRUE, Boolean.valueOf("true")); 107 assertEquals(Boolean.FALSE, Boolean.valueOf("false")); 108 109 assertEquals(Boolean.TRUE, Boolean.valueOf("TRUE")); 110 assertEquals(Boolean.FALSE, Boolean.valueOf("false")); 111 112 assertEquals(Boolean.FALSE, Boolean.valueOf(null)); 113 assertEquals(Boolean.FALSE, Boolean.valueOf("")); 114 assertEquals(Boolean.FALSE, Boolean.valueOf("invalid")); 115 116 assertTrue("Failed to parse true to true", Boolean.valueOf("true").booleanValue()); 117 assertTrue("Failed to parse mixed case true to true", Boolean.valueOf("TrUe") 118 .booleanValue()); 119 assertTrue("parsed non-true to true", !Boolean.valueOf("ddddd").booleanValue()); 120 } 121 122 /** 123 * java.lang.Boolean#valueOf(boolean) 124 */ 125 public void test_valueOfZ() { 126 assertEquals(Boolean.TRUE, Boolean.valueOf(true)); 127 assertEquals(Boolean.FALSE, Boolean.valueOf(false)); 128 } 129 130 /** 131 * java.lang.Boolean#parseBoolean(String) 132 */ 133 public void test_parseBooleanLjava_lang_String() { 134 assertTrue(Boolean.parseBoolean("true")); 135 assertTrue(Boolean.parseBoolean("TRUE")); 136 assertFalse(Boolean.parseBoolean("false")); 137 assertFalse(Boolean.parseBoolean(null)); 138 assertFalse(Boolean.parseBoolean("")); 139 assertFalse(Boolean.parseBoolean("invalid")); 140 } 141 142 /** 143 * java.lang.Boolean#compareTo(Boolean) 144 */ 145 public void test_compareToLjava_lang_Boolean() { 146 assertTrue(Boolean.TRUE.compareTo(Boolean.TRUE) == 0); 147 assertTrue(Boolean.FALSE.compareTo(Boolean.FALSE) == 0); 148 assertTrue(Boolean.TRUE.compareTo(Boolean.FALSE) > 0); 149 assertTrue(Boolean.FALSE.compareTo(Boolean.TRUE) < 0); 150 151 try { 152 Boolean.TRUE.compareTo(null); 153 fail("No NPE"); 154 } catch (NullPointerException e) { 155 } 156 } 157 } 158