1 18 19 package org.apache.jmeter.util; 20 21 import junit.framework.TestCase; 22 23 26 public final class StringUtilities 27 { 28 public static String substitute(String input, String pattern, String sub) 29 { 30 StringBuffer ret = new StringBuffer (); 31 int start = 0; 32 int index = -1; 33 while ((index = input.indexOf(pattern, start)) >= start) 34 { 35 ret.append(input.substring(start, index)); 36 ret.append(sub); 37 start = index + pattern.length(); 38 } 39 ret.append(input.substring(start)); 40 return ret.toString(); 41 } 42 43 46 private StringUtilities() 47 { 48 } 49 50 public static class Test extends TestCase 51 { 52 public Test(String name) 53 { 54 super(name); 55 } 56 57 public void testSub1() throws Exception 58 { 59 String input = "http://jakarta.apache.org/jmeter/index.html"; 60 String pattern = "jakarta.apache.org"; 61 String sub = "${server}"; 62 assertEquals( 63 "http://${server}/jmeter/index.html", 64 StringUtilities.substitute(input, pattern, sub)); 65 } 66 67 public void testSub2() throws Exception 68 { 69 String input = "arg1=param1;param1"; 70 String pattern = "param1"; 71 String sub = "${value}"; 72 assertEquals( 73 "arg1=${value};${value}", 74 StringUtilities.substitute(input, pattern, sub)); 75 } 76 77 public void testSub3() throws Exception 78 { 79 String input = "jakarta.apache.org"; 80 String pattern = "jakarta.apache.org"; 81 String sub = "${server}"; 82 assertEquals( 83 "${server}", 84 StringUtilities.substitute(input, pattern, sub)); 85 } 86 87 } 88 } 89 | Popular Tags |