KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > TestStringUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestStringUtil.java,v 1.4 2004/02/01 05:16:33 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton;
21
22 import java.util.*;
23 // jrk_2002-12-01.1 - the sql libraries are unused. No sense in importing them.
24
//import java.sql.*;
25
//import javax.sql.*;
26

27 import junit.framework.*;
28 //import org.enhydra.barracuda.plankton.data.*;
29
import org.apache.log4j.*;
30 import org.enhydra.barracuda.testbed.*;
31
32
33 /**
34  * Test the StringUtil class
35  */

36 public class TestStringUtil extends DefaultTestCase {
37     //common vars (customize for every test class)
38
private static String JavaDoc testClass = TestStringUtil.class.getName();
39     private static final Logger logger = Logger.getLogger("test."+testClass);
40
41     //variables
42
protected String JavaDoc sourceStr = null;
43     protected String JavaDoc oldPattern = null;
44     protected String JavaDoc newPattern = null;
45     protected String JavaDoc expectedResult = null;
46
47     //-------------------- Basics --------------------------------
48
/**
49      * Public Constructor
50      */

51     public TestStringUtil(String JavaDoc name) {
52         super(name);
53 //logger.setPriority(org.apache.log4j.Priority.DEBUG);
54
//com.atmr.kilimanjaro.nxgenw.sql.RepSQL.logger.setPriority(org.apache.log4j.Priority.DEBUG);
55
}
56
57     /**
58      * Public Constructor
59      */

60     public TestStringUtil(String JavaDoc name, String JavaDoc isourceStr, String JavaDoc ioldPattern, String JavaDoc inewPattern, String JavaDoc iexpectedResult) {
61         super(name);
62         sourceStr = isourceStr;
63         oldPattern = ioldPattern;
64         newPattern = inewPattern;
65         expectedResult = iexpectedResult;
66     }
67
68     /**
69      * Every test class should have a main method so it can be run
70      * directly (when debugging tests you often will not want to run
71      * the whole suite)
72      *
73      * @param args defined in test.util.TestUtil
74      */

75     public static void main(String JavaDoc args[]) {
76         //check for standard runtime parameters
77
TestUtil.parseParams(args);
78
79         //launch the test
80
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String JavaDoc[] {testClass});
81         else junit.swingui.TestRunner.main(new String JavaDoc[] {testClass});
82     }
83
84     /**
85      * Return a suite of tests...
86      */

87     public static Test suite() {
88         //create the test suite
89
TestSuite suite = new TestSuite();
90
91         //add the tests
92
suite.addTest(new TestStringUtil("testReplace", null, null, "Blah", "Blah"));
93         suite.addTest(new TestStringUtil("testReplace", null, "a", "Blah", null));
94         suite.addTest(new TestStringUtil("testReplace", "Foo Blah", null, "_", "Foo Blah"));
95         suite.addTest(new TestStringUtil("testReplace", "Foo Blah", " ", "_", "Foo_Blah"));
96         suite.addTest(new TestStringUtil("testReplace", "Foo Blah Blah", " ", "_", "Foo_Blah_Blah"));
97         suite.addTest(new TestStringUtil("testReplace", "Foo Blah", " ", "_", "Foo__Blah"));
98         suite.addTest(new TestStringUtil("testReplace", "~Some Text", "~", null, "Some Text"));
99         suite.addTest(new TestStringUtil("testReplace", "Some Text~", "~", null, "Some Text"));
100         suite.addTest(new TestStringUtil("testReplace", "~Some~Text~", "~", null, "SomeText"));
101         suite.addTest(new TestStringUtil("testReplace", "~~~", "~", null, null));
102         suite.addTest(new TestStringUtil("testReplace", "~~~", "~", "", ""));
103         suite.addTest(new TestStringUtil("testReplace", "", " ", null, ""));
104
105         //finally return the suite
106
return suite;
107     }
108
109     //-------------------- Actual Tests --------------------------
110
/**
111      * Test a table to make sure it contains the expected columns
112      */

113     // jrk_2002-12-01.2 - the sql libraries are unused. No sense in referencing them.
114
//public void testReplace() throws SQLException {
115
public void testReplace() {
116         logger.info("running testReplace()");
117
118         String JavaDoc actualResult = StringUtil.replace(sourceStr, oldPattern, newPattern);
119         assertTrue("Replace err on source:'"+sourceStr+"' ('"+oldPattern+"'-->'"+newPattern+"') got:'"+actualResult+"' expected:'"+expectedResult+"'", actualResult==expectedResult || (actualResult!=null && actualResult.equals(expectedResult)));
120     }
121 }
122
Popular Tags