001: package com.ecyrd.jspwiki;
002:
003: import junit.framework.*;
004: import java.util.*;
005:
006: public class TextUtilTest extends TestCase {
007:
008: public TextUtilTest(String s) {
009: super (s);
010: }
011:
012: public void setUp() throws Exception {
013: }
014:
015: public void tearDown() {
016: }
017:
018: public void testEncodeName_1() {
019: String name = "Hello/World";
020:
021: assertEquals("Hello/World", TextUtil.urlEncode(name,
022: "ISO-8859-1"));
023: }
024:
025: public void testEncodeName_2() {
026: String name = "Hello~World";
027:
028: assertEquals("Hello%7EWorld", TextUtil.urlEncode(name,
029: "ISO-8859-1"));
030: }
031:
032: public void testEncodeName_3() {
033: String name = "Hello/World ~";
034:
035: assertEquals("Hello/World+%7E", TextUtil.urlEncode(name,
036: "ISO-8859-1"));
037: }
038:
039: public void testDecodeName_1() throws Exception {
040: String name = "Hello/World+%7E+%2F";
041:
042: assertEquals("Hello/World ~ /", TextUtil.urlDecode(name,
043: "ISO-8859-1"));
044: }
045:
046: public void testEncodeNameUTF8_1() {
047: String name = "\u0041\u2262\u0391\u002E";
048:
049: assertEquals("A%E2%89%A2%CE%91.", TextUtil.urlEncodeUTF8(name));
050: }
051:
052: public void testEncodeNameUTF8_2() {
053: String name = "\uD55C\uAD6D\uC5B4";
054:
055: assertEquals("%ED%95%9C%EA%B5%AD%EC%96%B4", TextUtil
056: .urlEncodeUTF8(name));
057: }
058:
059: public void testEncodeNameUTF8_3() {
060: String name = "\u65E5\u672C\u8A9E";
061:
062: assertEquals("%E6%97%A5%E6%9C%AC%E8%AA%9E", TextUtil
063: .urlEncodeUTF8(name));
064: }
065:
066: public void testEncodeNameUTF8_4() {
067: String name = "Hello World";
068:
069: assertEquals("Hello+World", TextUtil.urlEncodeUTF8(name));
070: }
071:
072: public void testDecodeNameUTF8_1() {
073: String name = "A%E2%89%A2%CE%91.";
074:
075: assertEquals("\u0041\u2262\u0391\u002E", TextUtil
076: .urlDecodeUTF8(name));
077: }
078:
079: public void testDecodeNameUTF8_2() {
080: String name = "%ED%95%9C%EA%B5%AD%EC%96%B4";
081:
082: assertEquals("\uD55C\uAD6D\uC5B4", TextUtil.urlDecodeUTF8(name));
083: }
084:
085: public void testDecodeNameUTF8_3() {
086: String name = "%E6%97%A5%E6%9C%AC%E8%AA%9E";
087:
088: assertEquals("\u65E5\u672C\u8A9E", TextUtil.urlDecodeUTF8(name));
089: }
090:
091: public void testReplaceString1() {
092: String text = "aabacaa";
093:
094: assertEquals("ddbacdd", TextUtil
095: .replaceString(text, "aa", "dd"));
096: }
097:
098: public void testReplaceString4() {
099: String text = "aabacaafaa";
100:
101: assertEquals("ddbacddfdd", TextUtil.replaceString(text, "aa",
102: "dd"));
103: }
104:
105: public void testReplaceString5() {
106: String text = "aaabacaaafaa";
107:
108: assertEquals("dbacdfaa", TextUtil.replaceString(text, "aaa",
109: "d"));
110: }
111:
112: public void testReplaceString2() {
113: String text = "abcde";
114:
115: assertEquals("fbcde", TextUtil.replaceString(text, "a", "f"));
116: }
117:
118: public void testReplaceString3() {
119: String text = "ababab";
120:
121: assertEquals("afafaf", TextUtil.replaceString(text, "b", "f"));
122: }
123:
124: // Pure UNIX.
125: public void testNormalizePostdata1() {
126: String text = "ab\ncd";
127:
128: assertEquals("ab\r\ncd\r\n", TextUtil.normalizePostData(text));
129: }
130:
131: // Pure MSDOS.
132: public void testNormalizePostdata2() {
133: String text = "ab\r\ncd";
134:
135: assertEquals("ab\r\ncd\r\n", TextUtil.normalizePostData(text));
136: }
137:
138: // Pure Mac
139: public void testNormalizePostdata3() {
140: String text = "ab\rcd";
141:
142: assertEquals("ab\r\ncd\r\n", TextUtil.normalizePostData(text));
143: }
144:
145: // Mixed, ending correct.
146: public void testNormalizePostdata4() {
147: String text = "ab\ncd\r\n\r\n\r";
148:
149: assertEquals("ab\r\ncd\r\n\r\n\r\n", TextUtil
150: .normalizePostData(text));
151: }
152:
153: // Multiple newlines
154: public void testNormalizePostdata5() {
155: String text = "ab\ncd\n\n\n\n";
156:
157: assertEquals("ab\r\ncd\r\n\r\n\r\n\r\n", TextUtil
158: .normalizePostData(text));
159: }
160:
161: // Empty.
162: public void testNormalizePostdata6() {
163: String text = "";
164:
165: assertEquals("\r\n", TextUtil.normalizePostData(text));
166: }
167:
168: // Just a newline.
169: public void testNormalizePostdata7() {
170: String text = "\n";
171:
172: assertEquals("\r\n", TextUtil.normalizePostData(text));
173: }
174:
175: public void testGetBooleanProperty() {
176: Properties props = new Properties();
177:
178: props.setProperty("foobar.0", "YES");
179: props.setProperty("foobar.1", "true");
180: props.setProperty("foobar.2", "false");
181: props.setProperty("foobar.3", "no");
182: props.setProperty("foobar.4", "on");
183: props.setProperty("foobar.5", "OFF");
184: props.setProperty("foobar.6", "gewkjoigew");
185:
186: assertTrue("foobar.0", TextUtil.getBooleanProperty(props,
187: "foobar.0", false));
188: assertTrue("foobar.1", TextUtil.getBooleanProperty(props,
189: "foobar.1", false));
190:
191: assertFalse("foobar.2", TextUtil.getBooleanProperty(props,
192: "foobar.2", true));
193: assertFalse("foobar.3", TextUtil.getBooleanProperty(props,
194: "foobar.3", true));
195: assertTrue("foobar.4", TextUtil.getBooleanProperty(props,
196: "foobar.4", false));
197:
198: assertFalse("foobar.5", TextUtil.getBooleanProperty(props,
199: "foobar.5", true));
200:
201: assertFalse("foobar.6", TextUtil.getBooleanProperty(props,
202: "foobar.6", true));
203:
204: }
205:
206: public void testGetSection1() throws Exception {
207: String src = "Single page.";
208:
209: assertEquals("section 1", src, TextUtil.getSection(src, 1));
210:
211: try {
212: TextUtil.getSection(src, 5);
213: fail("Did not get exception for 2");
214: } catch (IllegalArgumentException e) {
215: }
216:
217: try {
218: TextUtil.getSection(src, -1);
219: fail("Did not get exception for -1");
220: } catch (IllegalArgumentException e) {
221: }
222: }
223:
224: public void testGetSection2() throws Exception {
225: String src = "First section\n----\nSecond section\n\n----\n\nThird section";
226:
227: assertEquals("section 1", "First section\n", TextUtil
228: .getSection(src, 1));
229: assertEquals("section 2", "\nSecond section\n\n", TextUtil
230: .getSection(src, 2));
231: assertEquals("section 3", "\n\nThird section", TextUtil
232: .getSection(src, 3));
233:
234: try {
235: TextUtil.getSection(src, 4);
236: fail("Did not get exception for section 4");
237: } catch (IllegalArgumentException e) {
238: }
239: }
240:
241: public void testGetSection3() throws Exception {
242: String src = "----\nSecond section\n----";
243:
244: assertEquals("section 1", "", TextUtil.getSection(src, 1));
245: assertEquals("section 2", "\nSecond section\n", TextUtil
246: .getSection(src, 2));
247: assertEquals("section 3", "", TextUtil.getSection(src, 3));
248:
249: try {
250: TextUtil.getSection(src, 4);
251: fail("Did not get exception for section 4");
252: } catch (IllegalArgumentException e) {
253: }
254: }
255:
256: public void testBooleanParameter() throws Exception {
257: assertEquals("1", true, TextUtil.isPositive(" true "));
258: assertEquals("2", false, TextUtil
259: .isPositive(" fewqkfow kfpokwe "));
260: assertEquals("3", true, TextUtil.isPositive("on"));
261: assertEquals("4", true, TextUtil.isPositive("\t\ton"));
262: }
263:
264: public void testTrimmedProperty() {
265: String[] vals = { "foo", " this is a property ", "bar", "60" };
266:
267: Properties props = TextUtil.createProperties(vals);
268:
269: assertEquals("foo", "this is a property", TextUtil
270: .getStringProperty(props, "foo", ""));
271: assertEquals("bar", 60, TextUtil.getIntegerProperty(props,
272: "bar", 0));
273: }
274:
275: public static Test suite() {
276: return new TestSuite(TextUtilTest.class);
277: }
278: }
|