KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > util > StringUtilities


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/util/StringUtilities.java,v 1.4 2004/02/14 03:34:30 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.util;
20
21 import junit.framework.TestCase;
22
23 /**
24  * @version $Revision: 1.4 $
25  */

26 public final class StringUtilities
27 {
28     public static String JavaDoc substitute(String JavaDoc input, String JavaDoc pattern, String JavaDoc sub)
29     {
30         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
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     /**
44      * Private constructor to prevent instantiation.
45      */

46     private StringUtilities()
47     {
48     }
49     
50     public static class Test extends TestCase
51     {
52         public Test(String JavaDoc name)
53         {
54             super(name);
55         }
56
57         public void testSub1() throws Exception JavaDoc
58         {
59             String JavaDoc input = "http://jakarta.apache.org/jmeter/index.html";
60             String JavaDoc pattern = "jakarta.apache.org";
61             String JavaDoc sub = "${server}";
62             assertEquals(
63                 "http://${server}/jmeter/index.html",
64                 StringUtilities.substitute(input, pattern, sub));
65         }
66
67         public void testSub2() throws Exception JavaDoc
68         {
69             String JavaDoc input = "arg1=param1;param1";
70             String JavaDoc pattern = "param1";
71             String JavaDoc sub = "${value}";
72             assertEquals(
73                 "arg1=${value};${value}",
74                 StringUtilities.substitute(input, pattern, sub));
75         }
76
77         public void testSub3() throws Exception JavaDoc
78         {
79             String JavaDoc input = "jakarta.apache.org";
80             String JavaDoc pattern = "jakarta.apache.org";
81             String JavaDoc sub = "${server}";
82             assertEquals(
83                 "${server}",
84                 StringUtilities.substitute(input, pattern, sub));
85         }
86
87     }
88 }
89
Popular Tags