1 4 package net.javacoding.jspider.core.util; 5 6 import junit.framework.TestCase; 7 8 public class StringUtilTest extends TestCase { 9 10 public StringUtilTest ( ) { 11 super ( "StringUtilTest" ); 12 } 13 14 public void testDoNothing ( ) { 15 String original = "This is a string"; 16 String expected = new String (original); 17 String pattern="pattern"; 18 String replacement = "[replaced]"; 19 20 String result = StringUtil.replace(original, pattern, replacement); 21 assertEquals ( "replacement failed", expected, result); 22 } 23 24 public void testSimple ( ) { 25 String original = "This is a string with a pattern in it"; 26 String expected = "This is a string with a [replaced] in it"; 27 String pattern="pattern"; 28 String replacement = "[replaced]"; 29 30 String result = StringUtil.replace(original, pattern, replacement); 31 assertEquals ( "replacement failed", expected, result); 32 } 33 34 public void testBeginning ( ) { 35 String original = "pattern at the start of this string must be replaced"; 36 String expected = "[replaced] at the start of this string must be replaced"; 37 String pattern="pattern"; 38 String replacement = "[replaced]"; 39 40 String result = StringUtil.replace(original, pattern, replacement); 41 assertEquals ( "replacement failed", expected, result); 42 } 43 44 public void testEnd ( ) { 45 String original = "At the end of this string, ther's a pattern"; 46 String expected = "At the end of this string, ther's a [replaced]"; 47 String pattern="pattern"; 48 String replacement = "[replaced]"; 49 50 String result = StringUtil.replace(original, pattern, replacement); 51 assertEquals ( "replacement failed", expected, result); 52 } 53 54 public void testMultiple ( ) { 55 String original = "pattern, here, pattern there, hope all this patterns will be replaced by pattern"; 56 String expected = "[replaced], here, [replaced] there, hope all this [replaced]s will be replaced by [replaced]"; 57 String pattern="pattern"; 58 String replacement = "[replaced]"; 59 60 String result = StringUtil.replace(original, pattern, replacement); 61 assertEquals ( "replacement failed", expected, result); 62 } 63 64 public void testSmall ( ) { 65 String original = ".........."; 66 String expected = "xxxxxxxxxx"; 67 String pattern="."; 68 String replacement = "x"; 69 70 String result = StringUtil.replace(original, pattern, replacement); 71 assertEquals ( "replacement failed", expected, result); 72 } 73 74 public void testReplacementIncludesPattern ( ) { 75 String original = ".........."; 76 String expected = "...................."; 77 String pattern="."; 78 String replacement = ".."; 79 80 String result = StringUtil.replace(original, pattern, replacement); 81 assertEquals ( "replacement failed", expected, result); 82 } 83 84 public void testPatternIncludesReplacement ( ) { 85 String original = "...................."; 86 String expected = ".........."; 87 String pattern=".."; 88 String replacement = "."; 89 90 String result = StringUtil.replace(original, pattern, replacement); 91 assertEquals ( "replacement failed", expected, result); 92 } 93 94 public void testReplacementZeroSizeString ( ) { 95 String original = ""; 96 String expected = ""; 97 String pattern="pattern"; 98 String replacement = "replaced"; 99 String result = StringUtil.replace(original, pattern, replacement); 100 assertEquals ( "replacement failed", expected, result); 101 } 102 103 public void testReplacementZeroSizePattern ( ) { 104 String original = "a simple string"; 105 String expected = "a simple string"; 106 String pattern=""; 107 String replacement = "replaced"; 108 String result = StringUtil.replace(original, pattern, replacement); 109 assertEquals ( "replacement failed", expected, result); 110 } 111 112 public void testReplacementNullString ( ) { 113 String original = null; 114 String expected = null; 115 String pattern=""; 116 String replacement = "replaced"; 117 String result = StringUtil.replace(original, pattern, replacement); 118 assertEquals ( "replacement failed", expected, result); 119 } 120 121 public void testReplacementNullPattern ( ) { 122 String original = "a simple string"; 123 String expected = "a simple string"; 124 String pattern=null; 125 String replacement = "replaced"; 126 String result = StringUtil.replace(original, pattern, replacement); 127 assertEquals ( "replacement failed", expected, result); 128 } 129 130 } 131 | Popular Tags |