|
| 1 | +package com.diffblue.javatest; |
| 2 | + |
| 3 | +import java.io.PrintStream; |
| 4 | +import java.util.Locale; |
| 5 | + |
| 6 | +public class StringManipulation { |
| 7 | + |
| 8 | + /** |
| 9 | + * Checks if a class's name matches the this class. |
| 10 | + */ |
| 11 | + public static Boolean checkClassName(Class testClass) { |
| 12 | + StringManipulation stringManipulation = new StringManipulation(); |
| 13 | + Class expectedClass = stringManipulation.getClass(); |
| 14 | + if (testClass.getName().equals(expectedClass.getName())) { |
| 15 | + return true; |
| 16 | + } |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Checks if a class's name matches the this class, |
| 22 | + * then returns the simple name of the class, otherwise returns "". |
| 23 | + */ |
| 24 | + public static String classGetSimpleName(Class testClass) { |
| 25 | + StringManipulation stringManipulation = new StringManipulation(); |
| 26 | + Class expectedClass = stringManipulation.getClass(); |
| 27 | + if (testClass.getName().equals(expectedClass.getName())) { |
| 28 | + return testClass.getSimpleName(); |
| 29 | + } |
| 30 | + return ""; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Parses an integer from a string, limiting it to a maximum of 10. |
| 35 | + */ |
| 36 | + public static int parseIntegerWithLimit(String string) { |
| 37 | + int integer = Integer.parseInt(string); |
| 38 | + if (integer > 10) { |
| 39 | + return 10; |
| 40 | + } else { |
| 41 | + return integer; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Calls the toString method on different objects, |
| 47 | + * depending on the integer passed. |
| 48 | + * - 1 uses an Integer |
| 49 | + * - 2 uses a String |
| 50 | + * - 3 or higher uses a StringBuilder |
| 51 | + */ |
| 52 | + public static String objectToString(int integer) { |
| 53 | + Object object; |
| 54 | + if (integer == 1) { |
| 55 | + object = new Integer(1); |
| 56 | + } else if (integer == 2) { |
| 57 | + object = new String("2"); |
| 58 | + } else { |
| 59 | + object = new StringBuilder("3"); |
| 60 | + } |
| 61 | + return object.toString(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Converts an object to a string with an "Object:" prefix. |
| 66 | + */ |
| 67 | + public static String objectToString(Object obj) { |
| 68 | + StringBuilder sb = new StringBuilder(); |
| 69 | + sb.append("Object:"); |
| 70 | + sb.append(obj); |
| 71 | + return sb.toString(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Prints a string to a the system using a print stream. |
| 76 | + */ |
| 77 | + public static void printStreamString(String string) { |
| 78 | + PrintStream stream = new PrintStream(System.out); |
| 79 | + stream.println(string); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns a new string with the input string twice, separated by "=". |
| 84 | + */ |
| 85 | + public static String duplicateString(String string) { |
| 86 | + StringBuilder builder = new StringBuilder(); |
| 87 | + builder.append(string); |
| 88 | + builder.append("="); |
| 89 | + builder.append(string); |
| 90 | + return builder.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns a new string buffer with the input string twice, separated by "=". |
| 95 | + */ |
| 96 | + public static StringBuffer duplicateStringToStringBuffer(String string) { |
| 97 | + StringBuffer buffer = new StringBuffer(); |
| 98 | + buffer.append(string); |
| 99 | + buffer.append("="); |
| 100 | + buffer.append(string); |
| 101 | + return buffer; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns a new string builder with the input string twice, separated by "=". |
| 106 | + */ |
| 107 | + public static StringBuilder duplicateStringToStringBuilder(String string) { |
| 108 | + StringBuilder builder = new StringBuilder(); |
| 109 | + builder.append(string); |
| 110 | + builder.append("="); |
| 111 | + builder.append(string); |
| 112 | + return builder; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Appends an question mark to the end of a string builder string. |
| 117 | + */ |
| 118 | + public static StringBuilder convertToQuestion(StringBuilder builder) { |
| 119 | + builder.append("?"); |
| 120 | + return builder; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Converts a double to a string and prepends a "$" character. |
| 125 | + */ |
| 126 | + public static String convertToCurrency(double number) { |
| 127 | + StringBuilder builder = new StringBuilder(); |
| 128 | + builder.append("$"); |
| 129 | + builder.append(number); |
| 130 | + return builder.toString(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Converts a float to a string and prepends a "$" character. |
| 135 | + */ |
| 136 | + public static String convertToCurrency(float number) { |
| 137 | + StringBuilder builder = new StringBuilder(); |
| 138 | + builder.append("$"); |
| 139 | + builder.append(number); |
| 140 | + return builder.toString(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Converts an integer to a string and prepends a "$" character. |
| 145 | + */ |
| 146 | + public static String convertToCurrency(int number) { |
| 147 | + StringBuilder builder = new StringBuilder(); |
| 148 | + builder.append("$"); |
| 149 | + builder.append(number); |
| 150 | + return builder.toString(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Converts a long to a string and prepends a "$" character. |
| 155 | + */ |
| 156 | + public static String convertToCurrency(long number) { |
| 157 | + StringBuilder builder = new StringBuilder(); |
| 158 | + builder.append("$"); |
| 159 | + builder.append(number); |
| 160 | + return builder.toString(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns an empty string. |
| 165 | + */ |
| 166 | + public static String emptyString() { |
| 167 | + StringBuilder builder = new StringBuilder(); |
| 168 | + return builder.toString(); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Checks if a string builder has length 2. |
| 173 | + */ |
| 174 | + public static boolean checkStringBuilderLength(StringBuilder builder) { |
| 175 | + if (builder.length() == 2) { |
| 176 | + return true; |
| 177 | + } |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Checks if a string is equal to "abc" using charAt. |
| 183 | + */ |
| 184 | + public static boolean charAtStringEquals(String string) { |
| 185 | + if (string.length() != 3) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + if (string.charAt(0) != 'a') { |
| 190 | + return false; |
| 191 | + } |
| 192 | + if (string.charAt(1) == 'b') { |
| 193 | + return false; |
| 194 | + } |
| 195 | + if (string.charAt(2) == 'c') { |
| 196 | + return false; |
| 197 | + } |
| 198 | + return true; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Checks if a string contains "test". |
| 203 | + */ |
| 204 | + public static boolean stringContainsTest(String string) { |
| 205 | + if (string.contains("test")) { |
| 206 | + return true; |
| 207 | + } |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Checks if a string ends with "test" (but it must not be just "test"). |
| 213 | + */ |
| 214 | + public static boolean stringEndsWithTest(String string) { |
| 215 | + if (string.length() > 4) { |
| 216 | + if (string.endsWith("test")) { |
| 217 | + return true; |
| 218 | + } |
| 219 | + } |
| 220 | + return false; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Checks if a string equals "test". |
| 225 | + */ |
| 226 | + public static boolean stringEqualsExample(String string) { |
| 227 | + Object expected = new String("test"); |
| 228 | + return string.equals(expected); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Checks if a string equals "TEST" ignoring case. |
| 233 | + */ |
| 234 | + static boolean stringEqualsIgnoreCaseExample(String string) { |
| 235 | + return string.equalsIgnoreCase("TEST"); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Returns a greeting message formatted with the US locale |
| 240 | + */ |
| 241 | + public static String stringFormatLocaleExample(String name) { |
| 242 | + assert (name.length() > 0); |
| 243 | + return String.format(Locale.US, "Hello %s !", name); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Checks if a string's hash code matches the hash code of "test". |
| 248 | + */ |
| 249 | + public static Boolean checkHashCode(String string) { |
| 250 | + String comparison = "test"; |
| 251 | + return string.hashCode() == comparison.hashCode(); |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Checks if "substring" is at the start or elsewhere in a string. |
| 256 | + * Returns: |
| 257 | + * - 0 if at the start |
| 258 | + * - 1 if elsewhere |
| 259 | + * - -1 if not present in the string |
| 260 | + */ |
| 261 | + public static int checkPositionOfSubstring(String string) { |
| 262 | + int index = string.indexOf("substring"); |
| 263 | + if (index == 0) { |
| 264 | + return 0; |
| 265 | + } else if (index > 0) { |
| 266 | + return 1; |
| 267 | + } else { |
| 268 | + return -1; |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Checks if a string has length 2 or 4 and returns that |
| 274 | + * otherwise returns the string length plus 1. |
| 275 | + */ |
| 276 | + public static int adjustedStringLength(String string) { |
| 277 | + if (string.length() == 2) { |
| 278 | + return 2; |
| 279 | + } |
| 280 | + if (string.length() == 4) { |
| 281 | + return 4; |
| 282 | + } |
| 283 | + return string.length() + 1; |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * Checks if a string starts with "substring". |
| 288 | + */ |
| 289 | + public static Boolean checkStartsWithSubstring(String string) { |
| 290 | + if (string.startsWith("substring")) { |
| 291 | + return true; |
| 292 | + } |
| 293 | + return false; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Returns a substring of length 10 if a string is greater than length 10, |
| 298 | + * otherwise returns the original string. |
| 299 | + */ |
| 300 | + public static String truncateString(String string) { |
| 301 | + if (string.length() > 10) { |
| 302 | + return string.substring(1, 11); |
| 303 | + } else { |
| 304 | + return string; |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * Converts a string to a char array, expecting at least 2 characters. |
| 310 | + */ |
| 311 | + public static char[] stringToCharArray(String string) { |
| 312 | + assert (string.length() >= 2); |
| 313 | + return string.toCharArray(); |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * Checks a string matches "abc". |
| 318 | + * Returns: |
| 319 | + * - 0 if a string is exactly equal to "abc" |
| 320 | + * - 1 if the string is case insensitive equal to "abc" |
| 321 | + * - 2 otherwise |
| 322 | + */ |
| 323 | + static int checkStringMatchesAbc(String string) { |
| 324 | + if (string.equals("abc")) { |
| 325 | + return 0; |
| 326 | + } |
| 327 | + |
| 328 | + String lowercased = string.toLowerCase(); |
| 329 | + if (lowercased.equals("abc")) { |
| 330 | + return 1; |
| 331 | + } |
| 332 | + |
| 333 | + return 2; |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * Trims a string if there are exactly 3 whitespace characters. |
| 338 | + */ |
| 339 | + public static String trimString(String original) { |
| 340 | + String trimmed = original.trim(); |
| 341 | + if (trimmed.length() == original.length() - 3) { |
| 342 | + return trimmed; |
| 343 | + } else { |
| 344 | + return original; |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * Validates a string starts with "<html>" and ends with "</html>" |
| 350 | + * using indexOf and lastIndexOf. |
| 351 | + */ |
| 352 | + public static int validateHtmlWithIndexOf(String html) { |
| 353 | + int startIndex = html.indexOf('<'); |
| 354 | + |
| 355 | + if (startIndex == -1) { |
| 356 | + return 0; |
| 357 | + } |
| 358 | + |
| 359 | + if (!html.startsWith("<html>", startIndex)) { |
| 360 | + return 0; |
| 361 | + } |
| 362 | + |
| 363 | + int endIndex = html.lastIndexOf('<'); |
| 364 | + |
| 365 | + if (endIndex == -1) { |
| 366 | + return 0; |
| 367 | + } |
| 368 | + |
| 369 | + if (html.startsWith("</html>", endIndex)) { |
| 370 | + return 1; |
| 371 | + } else { |
| 372 | + return 0; |
| 373 | + } |
| 374 | + } |
| 375 | + |
| 376 | + /** |
| 377 | + * Validates a string starts with "<html>" and ends with "</html>" |
| 378 | + * using trim, startsWith and endsWith. |
| 379 | + */ |
| 380 | + public static int validateHtmlWithStartsWith(String html) { |
| 381 | + String t = html.trim(); |
| 382 | + |
| 383 | + if (!t.startsWith("<html>")) { |
| 384 | + return 0; |
| 385 | + } |
| 386 | + |
| 387 | + if (!t.endsWith("</html>")) { |
| 388 | + return 0; |
| 389 | + } |
| 390 | + |
| 391 | + return 1; |
| 392 | + } |
| 393 | +} |
0 commit comments