1 54 55 package junitx.framework; 56 57 63 public class StringAssert { 64 65 68 private StringAssert() { 69 } 70 71 75 public static void assertContains(String substring, 76 String actual) { 77 assertContains(null, substring, actual); 78 } 79 80 84 public static void assertContains(String message, 85 String substring, 86 String actual) { 87 Assert.assertNotNull(substring); 88 if (actual != null && actual.indexOf(substring) >= 0) { 89 return; 90 } 91 failContains(message, substring, actual); 92 } 93 94 98 public static void assertNotContains(String substring, 99 String actual) { 100 assertNotContains(null, substring, actual); 101 } 102 103 107 public static void assertNotContains(String message, 108 String substring, 109 String actual) { 110 if ((actual == null && substring != null) || 111 (actual != null && substring == null) || 112 (actual != null && actual.indexOf(substring) < 0)) { 113 return; 114 } 115 failNotContains(message, substring, actual); 116 } 117 118 122 public static void assertStartsWith(String substring, String actual) { 123 assertStartsWith(null, substring, actual); 124 } 125 126 130 public static void assertStartsWith(String message, String substring, String actual) { 131 Assert.assertNotNull(substring); 132 if (actual != null && actual.startsWith(substring)) { 133 return; 134 } 135 failStartsWith(message, substring, actual); 136 } 137 138 142 public static void assertNotStartsWith(String substring, String actual) { 143 assertNotStartsWith(null, substring, actual); 144 } 145 146 150 public static void assertNotStartsWith(String message, String substring, String actual) { 151 if ((actual == null && substring != null) || 152 (actual != null && substring == null) || 153 (actual != null && !actual.startsWith(substring))) { 154 return; 155 } 156 failNotStartsWith(message, substring); 157 } 158 159 163 public static void assertEndsWith(String substring, String actual) { 164 assertEndsWith(null, substring, actual); 165 } 166 167 171 public static void assertEndsWith(String message, String substring, String actual) { 172 Assert.assertNotNull(substring); 173 if (actual != null && actual.endsWith(substring)) { 174 return; 175 } 176 failEndsWith(message, substring, actual); 177 } 178 179 183 public static void assertNotEndsWith(String substring, String actual) { 184 assertNotEndsWith(null, substring, actual); 185 } 186 187 191 public static void assertNotEndsWith(String message, String substring, String actual) { 192 if ((actual == null && substring != null) || 193 (actual != null && substring == null) || 194 (actual != null && !actual.endsWith(substring))) { 195 return; 196 } 197 failNotEndsWith(message, substring); 198 } 199 200 private static void failContains(String message, 201 String substring, 202 String actual) { 203 String formatted = ""; 204 if (message != null) { 205 formatted = message + " "; 206 } 207 208 Assert.fail(formatted + "expected containing: <" + substring + "> but was: <" + actual + ">"); 209 } 210 211 private static void failNotContains(String message, 212 String substring, 213 String actual) { 214 String formatted = ""; 215 if (message != null) { 216 formatted = message + " "; 217 } 218 219 Assert.fail(formatted + "expected not containing: <" + substring + "> but was: <" + actual + ">"); 220 } 221 222 private static void failStartsWith(String message, String substring, String actual) { 223 String formatted = ""; 224 if (message != null) { 225 formatted = message + " "; 226 } 227 228 Assert.fail(formatted + "expected starting with: <" + substring + "> but was: <" + actual + ">"); 229 } 230 231 private static void failNotStartsWith(String message, String substring) { 232 String formatted = ""; 233 if (message != null) { 234 formatted = message + " "; 235 } 236 237 Assert.fail(formatted + "expected not starting with: <" + substring + ">"); 238 } 239 240 private static void failEndsWith(String message, String substring, String actual) { 241 String formatted = ""; 242 if (message != null) { 243 formatted = message + " "; 244 } 245 246 Assert.fail(formatted + "expected ending with: <" + substring + "> but was: <" + actual + ">"); 247 } 248 249 private static void failNotEndsWith(String message, String substring) { 250 String formatted = ""; 251 if (message != null) { 252 formatted = message + " "; 253 } 254 255 Assert.fail(formatted + "expected not ending with: <" + substring + ">"); 256 } 257 258 } 259 | Popular Tags |