1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 // Some parts of this code is Common Public License - v 1.0 18 // See NOTICE file in junit project. 19 20 package junit.framework; 21 22 public class Assert { Assert()23 protected Assert() {} 24 assertEquals(String message, boolean expected, boolean actual)25 public static void assertEquals(String message, boolean expected, boolean actual) { 26 if (actual != expected) { 27 fail(message, "expected " + expected + " but was " + actual); 28 } 29 } 30 assertEquals(boolean expected, boolean actual)31 public static void assertEquals(boolean expected, boolean actual) { 32 assertEquals("", expected, actual); 33 } 34 assertEquals(String message, byte expected, byte actual)35 public static void assertEquals(String message, byte expected, byte actual) { 36 if (actual != expected) { 37 fail(message, "expected " + expected + " but was " + actual); 38 } 39 } 40 assertEquals(byte expected, byte actual)41 public static void assertEquals(byte expected, byte actual) { 42 assertEquals("", expected, actual); 43 } 44 assertEquals(String message, short expected, short actual)45 public static void assertEquals(String message, short expected, short actual) { 46 if (actual != expected) { 47 fail(message, "expected " + expected + " but was " + actual); 48 } 49 } 50 assertEquals(short expected, short actual)51 public static void assertEquals(short expected, short actual) { 52 assertEquals("", expected, actual); 53 } 54 assertEquals(String message, int expected, int actual)55 public static void assertEquals(String message, int expected, int actual) { 56 if (actual != expected) { 57 fail(message, "expected " + expected + " but was " + actual); 58 } 59 } 60 assertEquals(int expected, int actual)61 public static void assertEquals(int expected, int actual) { 62 assertEquals("", expected, actual); 63 } 64 assertEquals(String message, long expected, long actual)65 public static void assertEquals(String message, long expected, long actual) { 66 if (actual != expected) { 67 fail(message, "expected " + expected + " but was " + actual); 68 } 69 } 70 assertEquals(long expected, long actual)71 public static void assertEquals(long expected, long actual) { 72 assertEquals("", expected, actual); 73 } 74 assertEquals(String message, char expected, char actual)75 public static void assertEquals(String message, char expected, char actual) { 76 if (actual != expected) { 77 fail(message, "expected " + expected + " but was " + actual); 78 } 79 } 80 assertEquals(char expected, char actual)81 public static void assertEquals(char expected, char actual) { 82 assertEquals("", expected, actual); 83 } 84 assertEquals(String message, String expected, String actual)85 public static void assertEquals(String message, String expected, String actual) { 86 assertEquals(message, (Object) expected, (Object) actual); 87 } 88 assertEquals(String expected, String actual)89 public static void assertEquals(String expected, String actual) { 90 assertEquals("", (Object) expected, (Object) actual); 91 } 92 assertEquals(String message, Object expected, Object actual)93 public static void assertEquals(String message, Object expected, Object actual) { 94 if (expected == null && actual == null) { 95 return; 96 } 97 if (expected != null && expected.equals(actual)) { 98 return; 99 } 100 fail(message, "expected " + expected + " but was " + actual); 101 } 102 assertEquals(Object expected, Object actual)103 public static void assertEquals(Object expected, Object actual) { 104 assertEquals("", expected, actual); 105 } 106 107 // assertEquals with delta assertEquals(String message, double expected, double actual, double delta)108 static public void assertEquals(String message, double expected, double actual, double delta) { 109 if (Double.compare(expected, actual) == 0) { 110 return; 111 } 112 if (!(Math.abs(expected-actual) <= delta)) { 113 fail(message, "expected " + expected + " but was " + actual + "; delta=" + delta); 114 } 115 } 116 assertEquals(double expected, double actual, double delta)117 public static void assertEquals(double expected, double actual, double delta) { 118 assertEquals("", expected, actual, delta); 119 } 120 assertEquals(String message, float expected, float actual, float delta)121 static public void assertEquals(String message, float expected, float actual, float delta) { 122 if (Float.compare(expected, actual) == 0) { 123 return; 124 } 125 if (!(Math.abs(expected - actual) <= delta)) { 126 fail(message, "expected " + expected + " but was " + actual + "; delta=" + delta); 127 } 128 } 129 assertEquals(float expected, float actual, float delta)130 public static void assertEquals(float expected, float actual, float delta) { 131 assertEquals("", expected, actual, delta); 132 } 133 134 // other asserts 135 assertTrue(String message, boolean condition)136 public static void assertTrue(String message, boolean condition) { 137 if (!condition) { 138 throw new AssertionFailedError(message); 139 } 140 } 141 assertTrue(boolean condition)142 public static void assertTrue(boolean condition) { 143 assertTrue("", condition); 144 } 145 assertFalse(String message, boolean condition)146 public static void assertFalse(String message, boolean condition) { 147 if (condition) { 148 throw new AssertionFailedError(message); 149 } 150 } 151 assertFalse(boolean condition)152 public static void assertFalse(boolean condition) { 153 assertFalse("", condition); 154 } 155 assertNull(String message, Object reference)156 public static void assertNull(String message, Object reference) { 157 if (reference != null) { 158 throw new AssertionFailedError(message); 159 } 160 } 161 assertNull(Object reference)162 public static void assertNull(Object reference) { 163 assertNull("", reference); 164 } 165 assertNotNull(String message, Object reference)166 public static void assertNotNull(String message, Object reference) { 167 if (reference == null) { 168 throw new AssertionFailedError(message); 169 } 170 } 171 assertNotNull(Object reference)172 public static void assertNotNull(Object reference) { 173 assertNotNull("", reference); 174 } 175 assertSame(String message, Object expected, Object actual)176 public static void assertSame(String message, Object expected, Object actual) { 177 if (expected != actual) { 178 fail(message, "expected same " + expected + ", " + actual); 179 } 180 } 181 assertSame(Object expected, Object actual)182 public static void assertSame(Object expected, Object actual) { 183 assertSame("", expected, actual); 184 } 185 assertNotSame(String message, Object expected, Object actual)186 public static void assertNotSame(String message, Object expected, Object actual) { 187 if (expected == actual) { 188 fail(message, "expected not same " + expected + ", " + actual); 189 } 190 } 191 assertNotSame(Object expected, Object actual)192 public static void assertNotSame(Object expected, Object actual) { 193 assertNotSame("", expected, actual); 194 } 195 196 // fail 197 fail(String message)198 public static void fail(String message) { 199 throw new AssertionFailedError(message); 200 } 201 fail()202 public static void fail() { 203 throw new AssertionFailedError(); 204 } 205 fail(String message, String detail)206 protected static void fail(String message, String detail) { 207 if (message == null || message.isEmpty()) { 208 throw new AssertionFailedError(detail); 209 } else { 210 throw new AssertionFailedError(message + ": " + detail); 211 } 212 } 213 } 214