001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.net.URLDecoder;
009: import java.net.URLEncoder;
010: import java.sql.SQLException;
011: import java.util.Date;
012: import java.util.Random;
013:
014: import org.h2.test.TestBase;
015: import org.h2.util.ByteUtils;
016: import org.h2.util.StringUtils;
017:
018: /**
019: * Tests string utility methods.
020: */
021: public class TestStringUtils extends TestBase {
022:
023: public void test() throws Exception {
024: testHex();
025: testXML();
026: testSplit();
027: testJavaString();
028: testURL();
029: testPad();
030: }
031:
032: private void testHex() throws Exception {
033: check("face", ByteUtils.convertBytesToString(new byte[] {
034: (byte) 0xfa, (byte) 0xce }));
035: check(new byte[] { (byte) 0xfa, (byte) 0xce }, ByteUtils
036: .convertStringToBytes("face"));
037: check(new byte[] { (byte) 0xfa, (byte) 0xce }, ByteUtils
038: .convertStringToBytes("fAcE"));
039: check(new byte[] { (byte) 0xfa, (byte) 0xce }, ByteUtils
040: .convertStringToBytes("FaCe"));
041: try {
042: ByteUtils.convertStringToBytes("120");
043: error();
044: } catch (SQLException e) {
045: checkNotGeneralException(e);
046: }
047: try {
048: ByteUtils.convertStringToBytes("fast");
049: error();
050: } catch (SQLException e) {
051: checkNotGeneralException(e);
052: }
053: }
054:
055: private void testPad() throws Exception {
056: check("large", StringUtils.pad("larger text", 5, null, true));
057: check("large", StringUtils.pad("larger text", 5, null, false));
058: check("short+++++", StringUtils.pad("short", 10, "+", true));
059: check("+++++short", StringUtils.pad("short", 10, "+", false));
060: }
061:
062: private void testXML() throws Exception {
063: check("<!-- - - - - - -abc- - - - - - -->\n", StringUtils
064: .xmlComment("------abc------"));
065: check("<test/>\n", StringUtils.xmlNode("test", null, null));
066: check("<test>Grübel</test>\n", StringUtils.xmlNode("test",
067: null, StringUtils.xmlText("Gr\u00fcbel")));
068: check("Rand&Blue", StringUtils.xmlText("Rand&Blue"));
069: check("<<[[[]]]>>", StringUtils
070: .xmlCData("<<[[[]]]>>"));
071: Date dt = StringUtils.parseDateTime("2001-02-03 04:05:06 GMT",
072: "yyyy-MM-dd HH:mm:ss z", "en", "GMT");
073: String s = StringUtils.xmlStartDoc()
074: + StringUtils.xmlComment("Test Comment")
075: + StringUtils
076: .xmlNode(
077: "rss",
078: StringUtils.xmlAttr("version", "2.0"),
079: StringUtils
080: .xmlComment("Test Comment\nZeile2")
081: + StringUtils
082: .xmlNode(
083: "channel",
084: null,
085: StringUtils
086: .xmlNode(
087: "title",
088: null,
089: "H2 Database Engine")
090: + StringUtils
091: .xmlNode(
092: "link",
093: null,
094: "http://www.h2database.com")
095: + StringUtils
096: .xmlNode(
097: "description",
098: null,
099: "H2 Database Engine")
100: + StringUtils
101: .xmlNode(
102: "language",
103: null,
104: "en-us")
105: + StringUtils
106: .xmlNode(
107: "pubDate",
108: null,
109: StringUtils
110: .formatDateTime(
111: dt,
112: "EEE, d MMM yyyy HH:mm:ss z",
113: "en",
114: "GMT"))
115: + StringUtils
116: .xmlNode(
117: "lastBuildDate",
118: null,
119: StringUtils
120: .formatDateTime(
121: dt,
122: "EEE, d MMM yyyy HH:mm:ss z",
123: "en",
124: "GMT"))
125: + StringUtils
126: .xmlNode(
127: "item",
128: null,
129: StringUtils
130: .xmlNode(
131: "title",
132: null,
133: "New Version 0.9.9.9.9")
134: + StringUtils
135: .xmlNode(
136: "link",
137: null,
138: "http://www.h2database.com")
139: + StringUtils
140: .xmlNode(
141: "description",
142: null,
143: StringUtils
144: .xmlCData("\nNew Features\nTest\n")))));
145: check(
146: s,
147: "<?xml version=\"1.0\"?>\n"
148: + "<!-- Test Comment -->\n"
149: + "<rss version=\"2.0\">\n"
150: + " <!--\n"
151: + " Test Comment\n"
152: + " Zeile2\n"
153: + " -->\n"
154: + " <channel>\n"
155: + " <title>H2 Database Engine</title>\n"
156: + " <link>http://www.h2database.com</link>\n"
157: + " <description>H2 Database Engine</description>\n"
158: + " <language>en-us</language>\n"
159: + " <pubDate>Sat, 3 Feb 2001 04:05:06 GMT</pubDate>\n"
160: + " <lastBuildDate>Sat, 3 Feb 2001 04:05:06 GMT</lastBuildDate>\n"
161: + " <item>\n"
162: + " <title>New Version 0.9.9.9.9</title>\n"
163: + " <link>http://www.h2database.com</link>\n"
164: + " <description>\n"
165: + " <![CDATA[\n"
166: + " New Features\n"
167: + " Test\n"
168: + " ]]>\n"
169: + " </description>\n"
170: + " </item>\n" + " </channel>\n"
171: + "</rss>\n");
172: }
173:
174: private void testURL() throws Exception {
175: Random random = new Random(1);
176: for (int i = 0; i < 100; i++) {
177: int len = random.nextInt(10);
178: StringBuffer buff = new StringBuffer();
179: for (int j = 0; j < len; j++) {
180: if (random.nextBoolean()) {
181: buff.append((char) random.nextInt(0x3000));
182: } else {
183: buff.append((char) random.nextInt(255));
184: }
185: }
186: String a = buff.toString();
187: String b = URLEncoder.encode(a, "UTF-8");
188: String c = URLDecoder.decode(b, "UTF-8");
189: check(a, c);
190: String d = StringUtils.urlDecode(b);
191: check(d, c);
192: }
193: }
194:
195: private void testJavaString() throws Exception {
196: Random random = new Random(1);
197: for (int i = 0; i < 1000; i++) {
198: int len = random.nextInt(10);
199: StringBuffer buff = new StringBuffer();
200: for (int j = 0; j < len; j++) {
201: if (random.nextBoolean()) {
202: buff.append((char) random.nextInt(0x3000));
203: } else {
204: buff.append((char) random.nextInt(255));
205: }
206: }
207: String a = buff.toString();
208: String b = StringUtils.javaEncode(a);
209: String c = StringUtils.javaDecode(b);
210: check(a, c);
211: }
212: }
213:
214: private void testSplit() throws Exception {
215: check(
216: 3,
217: StringUtils.arraySplit("ABC,DEF,G\\,HI", ',', false).length);
218: check(StringUtils.arrayCombine(new String[] { "", " ", "," },
219: ','), ", ,\\,");
220: Random random = new Random(1);
221: for (int i = 0; i < 100; i++) {
222: int len = random.nextInt(10);
223: StringBuffer buff = new StringBuffer();
224: String select = "abcd,";
225: for (int j = 0; j < len; j++) {
226: char c = select.charAt(random.nextInt(select.length()));
227: if (c == 'a') {
228: buff.append("\\\\");
229: } else if (c == 'b') {
230: buff.append("\\,");
231: } else {
232: buff.append(c);
233: }
234: }
235: String a = buff.toString();
236: String[] b = StringUtils.arraySplit(a, ',', false);
237: String c = StringUtils.arrayCombine(b, ',');
238: check(a, c);
239: }
240: }
241:
242: }
|