001: /*
002: * Copyright (C) 2005 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: import java.util.*;
020:
021: /**
022: * StringHelper regression test.
023: */
024: class StringHelperTests {
025:
026: private static void equalOrDie(String testName, Object[] a,
027: Object[] b) throws Exception {
028: if (!Arrays.equals(a, b)) {
029: throw new Exception(testName
030: + " failed, arrays are not equal");
031: }
032: }
033:
034: private static void equalOrDie(String testName, String a, String b)
035: throws Exception {
036: if (!a.equals(b)) {
037: throw new Exception(testName
038: + " failed, arrays are not equal");
039: }
040: }
041:
042: private static void trueOrDie(String testName, boolean b)
043: throws Exception {
044: if (!b) {
045: throw new Exception(testName + " failed, condition not met");
046: }
047: }
048:
049: /**
050: * Main method for tests
051: * @param args command line arguments (ignored)
052: */
053: public static void main(String[] args) {
054: try {
055: equalOrDie("Split Test 1",
056: StringHelper.split("1-2-3", "-"), new String[] {
057: "1", "2", "3" });
058: equalOrDie("Split Test 2", StringHelper
059: .split("-1--2-", "-"), new String[] { "", "1", "",
060: "2", "" });
061: equalOrDie("Split Test 3", StringHelper.split("123", ""),
062: new String[] { "123" });
063: equalOrDie("Split Test 4", StringHelper.split(
064: "1-2---3----4", "--"), new String[] { "1-2", "-3",
065: "", "4" });
066: equalOrDie("Split Test 5", StringHelper.split("12345678",
067: "--"), new String[] { "12345678" });
068: equalOrDie("Prepad Test 1", StringHelper.prepad("a", 8),
069: " a");
070: equalOrDie("Prepad Test 2",
071: StringHelper.prepad("AaaAa", 2), "AaaAa");
072: equalOrDie("Prepad Test 3", StringHelper
073: .prepad("a", 8, '-'), "-------a");
074: equalOrDie("Postpad Test 1", StringHelper.postpad("a", 8),
075: "a ");
076: equalOrDie("Postpad Test 2", StringHelper.postpad("AaaAa",
077: 2), "AaaAa");
078: equalOrDie("Postpad Test 3", StringHelper.postpad("a", 8,
079: '-'), "a-------");
080: equalOrDie("Midpad Test 1", StringHelper.midpad("a", 3),
081: " a ");
082: equalOrDie("Midpad Test 2", StringHelper.midpad("a", 4),
083: " a ");
084: equalOrDie("Midpad Test 3", StringHelper
085: .midpad("a", 5, '-'), "--a--");
086: equalOrDie("midpad Test 4",
087: StringHelper.midpad("AaaAa", 2), "AaaAa");
088: equalOrDie("replace Test 1", StringHelper.replace("1-2-3",
089: "-", "|"), "1|2|3");
090: equalOrDie("replace Test 2", StringHelper.replace("-1--2-",
091: "-", "|"), "|1||2|");
092: equalOrDie("replace Test 3", StringHelper.replace("123",
093: "", "|"), "123");
094: equalOrDie("replace Test 4", StringHelper.replace(
095: "1-2---3----4", "--", "|"), "1-2|-3||4");
096: equalOrDie("replace Test 5", StringHelper.replace(
097: "1-2--3---4----", "--", "---"),
098: "1-2---3----4------");
099: equalOrDie("escapeHTML Test 1", StringHelper
100: .escapeHTML("<>&\"'\0\1\2\n\r\f\t" + "hello"),
101: "<>&"'\n\r\f\t" + "hello");
102: equalOrDie("escapeSQL Test 1", StringHelper
103: .escapeSQL("\0\'\"\r\n" + "hello"),
104: "\\0\\'\\\"\r\n" + "hello");
105: equalOrDie("escapeJavaLiteral Test 1", StringHelper
106: .escapeJavaLiteral("\0\'\"\r\n" + "hello"),
107: "\0\\'\\\"\\r\\n" + "hello");
108: equalOrDie("trim Test 1", StringHelper.trim("- -\r\n"
109: + "hello- -\r\n" + "hello- -\r\n", "\r- \n"),
110: "hello- -\r\n" + "hello");
111: equalOrDie("unescapeHTML Test 1", StringHelper
112: .unescapeHTML(">hello" + "&" + "euro" + ";"),
113: ">hello\u20AC");
114: trueOrDie("containsAny Test 1", StringHelper.containsAny(
115: "on" + "two" + "t" + "h" + "r" + "e", new String[] {
116: "one", "two", "three" }));
117: trueOrDie("containsAny Test 2", StringHelper.containsAny(
118: "one" + "t" + "w" + "t" + "h" + "r" + "e",
119: new String[] { "one", "two", "three" }));
120: trueOrDie("containsAny Test 3", StringHelper.containsAny(
121: "on" + "t" + "w" + "three", new String[] { "one",
122: "two", "three" }));
123: trueOrDie("containsAny Test 4", !StringHelper.containsAny(
124: "on" + "t" + "w" + "t" + "h" + "r" + "e",
125: new String[] { "one", "two", "three" }));
126: trueOrDie("equalsAny Test 1", StringHelper.equalsAny("two",
127: new String[] { "one", "two", "three" }));
128: trueOrDie("equalsAny Test 2", !StringHelper.equalsAny("one"
129: + "two" + "three", new String[] { "one", "two",
130: "three" }));
131: trueOrDie("startsWithAny Test 1", StringHelper
132: .startsWithAny("two", new String[] { "one", "two",
133: "three" }));
134: trueOrDie("startsWithAny Test 2", !StringHelper
135: .startsWithAny("on" + "two" + "three",
136: new String[] { "one", "two", "three" }));
137: trueOrDie("endsWithAny Test 1", StringHelper.endsWithAny(
138: "two", new String[] { "one", "two", "three" }));
139: trueOrDie("endsWithAny Test 2", !StringHelper.endsWithAny(
140: "one" + "two" + "t" + "h" + "r" + "e",
141: new String[] { "one", "two", "three" }));
142: trueOrDie("containsAnyIgnoreCase Test 1", StringHelper
143: .containsAnyIgnoreCase("on" + "two" + "t" + "h"
144: + "r" + "e", new String[] { "One", "Two",
145: "Three" }));
146: trueOrDie("containsAnyIgnoreCase Test 2", StringHelper
147: .containsAnyIgnoreCase("one" + "t" + "w" + "t"
148: + "h" + "r" + "e", new String[] { "One",
149: "Two", "Three" }));
150: trueOrDie("containsAnyIgnoreCase Test 3", StringHelper
151: .containsAnyIgnoreCase("on" + "t" + "w" + "three",
152: new String[] { "One", "Two", "Three" }));
153: trueOrDie("containsAnyIgnoreCase Test 4", !StringHelper
154: .containsAnyIgnoreCase("on" + "t" + "w" + "t" + "h"
155: + "r" + "e", new String[] { "One", "Two",
156: "Three" }));
157: trueOrDie("equalsAnyIgnoreCase Test 1", StringHelper
158: .equalsAnyIgnoreCase("Two", new String[] { "One",
159: "Two", "Three" }));
160: trueOrDie("equalsAnyIgnoreCase Test 2", !StringHelper
161: .equalsAnyIgnoreCase("one" + "two" + "three",
162: new String[] { "One", "Two", "Three" }));
163: trueOrDie("startsWithAnyIgnoreCase Test 1", StringHelper
164: .startsWithAnyIgnoreCase("Two", new String[] {
165: "One", "Two", "Three" }));
166: trueOrDie("startsWithAnyIgnoreCase Test 2", !StringHelper
167: .startsWithAnyIgnoreCase("on" + "two" + "three",
168: new String[] { "One", "Two", "Three" }));
169: trueOrDie("endsWithAnyIgnoreCase Test 1", StringHelper
170: .endsWithAnyIgnoreCase("Two", new String[] { "One",
171: "Two", "Three" }));
172: trueOrDie("endsWithAnyIgnoreCase Test 2", !StringHelper
173: .endsWithAnyIgnoreCase("one" + "two" + "t" + "h"
174: + "r" + "e", new String[] { "One", "Two",
175: "Three" }));
176: equalOrDie("splitIncludeDelimiters Test 1", StringHelper
177: .splitIncludeDelimiters("1-2-3", "-"),
178: new String[] { "1", "-", "2", "-", "3" });
179: equalOrDie("splitIncludeDelimiters Test 2", StringHelper
180: .splitIncludeDelimiters("-1--2-", "-"),
181: new String[] { "", "-", "1", "-", "", "-", "2",
182: "-", "" });
183: equalOrDie("splitIncludeDelimiters Test 3", StringHelper
184: .splitIncludeDelimiters("123", ""),
185: new String[] { "123" });
186: equalOrDie("splitIncludeDelimiters Test 4", StringHelper
187: .splitIncludeDelimiters("1-2--3---4----5", "--"),
188: new String[] { "1-2", "--", "3", "--", "-4", "--",
189: "", "--", "5" });
190: equalOrDie("splitIncludeDelimiters Test 5", StringHelper
191: .splitIncludeDelimiters("12345678", "--"),
192: new String[] { "12345678" });
193: equalOrDie("join Test 1", StringHelper
194: .join(new String[] { null }), "");
195: equalOrDie("join Test 2", StringHelper
196: .join(new String[] { "" }), "");
197: equalOrDie("join Test 3", StringHelper.join(new String[] {
198: "", "" }), "");
199: equalOrDie("join Test 4", StringHelper
200: .join(new String[] { "one" }), "one");
201: equalOrDie("join Test 5", StringHelper.join(new String[] {
202: "one", "two", }), "one" + "two");
203: equalOrDie("join Test 6", StringHelper.join(new String[] {
204: "one", null, "two", "", "three" }), "one" + "two"
205: + "three");
206:
207: equalOrDie("join Test 7", StringHelper.join(
208: new String[] { null }, "|"), "");
209: equalOrDie("join Test 8", StringHelper.join(
210: new String[] { "" }, "|"), "");
211: equalOrDie("join Test 9", StringHelper.join(new String[] {
212: "", "" }, "|"), "|");
213: equalOrDie("join Test 10", StringHelper.join(
214: new String[] { "one" }, "|"), "one");
215: equalOrDie("join Test 11", StringHelper.join(new String[] {
216: "one", "two", }, "|"), "one|two");
217: equalOrDie("join Test 12", StringHelper.join(new String[] {
218: "one", null, "two", "", "three" }, "|"),
219: "one||two||three");
220:
221: } catch (Exception x) {
222: x.printStackTrace(System.err);
223: System.exit(1);
224: }
225: System.exit(0);
226: }
227: }
|