Source Code Cross Referenced for TestStringUtils.java in  » Database-DBMS » h2database » org » h2 » test » unit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » h2database » org.h2.test.unit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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&#xfc;bel</test>\n", StringUtils.xmlNode("test",
067:                        null, StringUtils.xmlText("Gr\u00fcbel")));
068:                check("Rand&amp;Blue", StringUtils.xmlText("Rand&Blue"));
069:                check("&lt;&lt;[[[]]]&gt;&gt;", 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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.