1 8 package org.apache.avalon.excalibur.util.test; 9 10 import junit.framework.TestCase; 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.avalon.excalibur.util.StringUtil; 14 15 20 public final class StringUtilTestCase 21 extends TestCase 22 { 23 private final static String S1 = "TestMe"; 24 private final static String S2 = "TestMeTestMe"; 25 private final static String S3 = "TestMeTestMeTestMe"; 26 private final static String S4 = "MeeeMer"; 27 private final static String S5 = "MeeeeMer"; 28 29 private final static String P11 = "eT"; 30 private final static String P21 = "eT"; 31 private final static String P31 = "eT"; 32 private final static String P41 = "ee"; 33 private final static String P51 = "ee"; 34 35 private final static String P12 = "Te"; 36 private final static String P22 = "Te"; 37 private final static String P32 = "Te"; 38 private final static String P42 = "ff"; 39 private final static String P52 = "ff"; 40 41 private final static String SR1 = "TestMe"; 42 private final static String SR2 = "TestMTeestMe"; 43 private final static String SR3 = "TestMTeestMTeestMe"; 44 private final static String SR4 = "MffeMer"; 45 private final static String SR5 = "MffffMer"; 46 47 private final static String ST1 = "TestMe"; 48 private final static String ST2 = "TestMeT"; 49 private final static String ST3 = "TestMeT"; 50 private final static String ST4 = "MeeeMer"; 51 private final static String ST5 = "MeeeeMe"; 52 53 private final static String STN1 = "TestMe"; 54 private final static String STN2 = "Test..."; 55 private final static String STN3 = "Test..."; 56 private final static String STN4 = "MeeeMer"; 57 private final static String STN5 = "Meee..."; 58 59 private final static String [] SS1 = new String [] { "T", "stM" }; 60 private final static String [] SS2 = new String [] { "T", "stM", "T", "stM" }; 61 private final static String [] SS3 = new String [] { "T", "stM", "T", "stM", "T", "stM" }; 62 private final static String [] SS4 = new String [] { "M", "M", "r" }; 63 private final static String [] SS5 = new String [] { "M", "M", "r" }; 64 65 private final static String SP = " "; 66 67 private final static String WU1 = S1 + SP + S2 + SP + S3 + SP + S4 + SP + S5; 68 private final static String WW1 = 69 "TestMe\n" + 70 "TestMeTe\n" + 71 "stMe\n" + 72 "TestMeTe\n" + 73 "stMeTest\n" + 74 "Me\n" + 75 "MeeeMer\n" + 76 "MeeeeMer"; 77 private final static String WW2 = 78 "TestMe\n" + 79 "TestMeT\n" + 80 "estMe\n" + 81 "TestMeT\n" + 82 "estMeTe\n" + 83 "stMe\n" + 84 "MeeeMer\n" + 85 "MeeeeMe\n" + 86 "r"; 87 88 public StringUtilTestCase( final String name ) 89 { 90 super( name ); 91 } 92 93 public void testReplaceSubString() 94 throws Exception 95 { 96 final String result1 = StringUtil.replaceSubString( S1, P11, P12 ); 97 final String result2 = StringUtil.replaceSubString( S2, P21, P22 ); 98 final String result3 = StringUtil.replaceSubString( S3, P31, P32 ); 99 final String result4 = StringUtil.replaceSubString( S4, P41, P42 ); 100 final String result5 = StringUtil.replaceSubString( S5, P51, P52 ); 101 102 assertEquals( "ReplaceSubString SR1", SR1, result1 ); 103 assertEquals( "ReplaceSubString SR2", SR2, result2 ); 104 assertEquals( "ReplaceSubString SR3", SR3, result3 ); 105 assertEquals( "ReplaceSubString SR4", SR4, result4 ); 106 assertEquals( "ReplaceSubString SR5", SR5, result5 ); 107 } 108 109 public void testWordWrap() 110 throws Exception 111 { 112 assertEquals( "WordWrap S1", WW1, StringUtil.wordWrap( WU1, 8, true ) ); 113 assertEquals( "WordWrap S1", WW2, StringUtil.wordWrap( WU1, 7, true ) ); 114 } 115 116 public void testTruncate() 117 throws Exception 118 { 119 assertEquals( "Truncate S1", ST1, StringUtil.truncate( S1, 7 ) ); 120 assertEquals( "Truncate S2", ST2, StringUtil.truncate( S2, 7 ) ); 121 assertEquals( "Truncate S3", ST3, StringUtil.truncate( S3, 7 ) ); 122 assertEquals( "Truncate S4", ST4, StringUtil.truncate( S4, 7 ) ); 123 assertEquals( "Truncate S5", ST5, StringUtil.truncate( S5, 7 ) ); 124 } 125 126 public void testTruncateNicely() 127 throws Exception 128 { 129 assertEquals( "Truncate Nicely S1", STN1, StringUtil.truncateNicely( S1, 7 ) ); 130 assertEquals( "Truncate Nicely S2", STN2, StringUtil.truncateNicely( S2, 7 ) ); 131 assertEquals( "Truncate Nicely S3", STN3, StringUtil.truncateNicely( S3, 7 ) ); 132 assertEquals( "Truncate Nicely S4", STN4, StringUtil.truncateNicely( S4, 7 ) ); 133 assertEquals( "Truncate Nicely S5", STN5, StringUtil.truncateNicely( S5, 7 ) ); 134 135 assertEquals( "Truncate Nicely 1", ".", StringUtil.truncateNicely( S5, 1 ) ); 136 assertEquals( "Truncate Nicely 2", "..", StringUtil.truncateNicely( S5, 2 ) ); 137 assertEquals( "Truncate Nicely 3", "...", StringUtil.truncateNicely( S5, 3 ) ); 138 } 139 140 public void testSplitString() 141 throws Exception 142 { 143 assertEqualArrays( SS1, StringUtil.split( S1, "e" ) ); 144 assertEqualArrays( SS2, StringUtil.split( S2, "e" ) ); 145 assertEqualArrays( SS3, StringUtil.split( S3, "e" ) ); 146 assertEqualArrays( SS4, StringUtil.split( S4, "e" ) ); 147 assertEqualArrays( SS5, StringUtil.split( S5, "e" ) ); 148 } 149 150 public void testConcatStrings() 151 throws Exception 152 { 153 assertEquals( "TestMeTruly", StringUtil.concat("Test", "Me", "Truly")); 154 assertEquals( "<prefix:name/>", StringUtil.concat(new String [] { 155 "<", 156 "prefix", 157 ":", 158 "name", 159 "/>" 160 })); 161 } 162 163 private void assertEqualArrays( final String [] s1, final String [] s2 ) 164 { 165 assertEquals( "Array Length Equality", s1.length, s2.length ); 166 assertEquals( "Array Type Equality", 167 s1.getClass().getComponentType(), 168 s2.getClass().getComponentType() ); 169 170 for( int i = 0; i < s1.length; i++ ) 171 { 172 assertEquals( "Array Element " + i, s1[ i ], s2[ i ] ); 173 } 174 } 175 } 176 | Popular Tags |