1 5 package org.h2.test.unit; 6 7 import java.net.URLDecoder ; 8 import java.net.URLEncoder ; 9 import java.util.Date ; 10 import java.util.Random ; 11 12 import org.h2.test.TestBase; 13 import org.h2.util.StringUtils; 14 15 public class TestStringUtils extends TestBase { 16 17 public void test() throws Exception { 18 testXML(); 19 testSplit(); 20 testJavaString(); 21 testURL(); 22 } 23 24 private void testXML() throws Exception { 25 check("<!-- - - - - - -abc- - - - - - -->\n", StringUtils.xmlComment("------abc------")); 26 check("<test/>\n", StringUtils.xmlNode("test", null, null)); 27 check("<test>Grübel</test>\n", StringUtils.xmlNode("test", null, StringUtils.xmlText("Gr\u00fcbel"))); 28 check("Rand&Blue", StringUtils.xmlText("Rand&Blue")); 29 check("<<[[[]]]>>", StringUtils.xmlCData("<<[[[]]]>>")); 30 Date dt = StringUtils.parseDateTime("2001-02-03 04:05:06 GMT", "yyyy-MM-dd HH:mm:ss z", "en", "GMT"); 31 String s = StringUtils.xmlStartDoc() + 32 StringUtils.xmlComment("Test Comment") + 33 StringUtils.xmlNode("rss", StringUtils.xmlAttr("version", "2.0"), 34 StringUtils.xmlComment("Test Comment\nZeile2") + 35 StringUtils.xmlNode("channel", null, 36 StringUtils.xmlNode("title", null, "H2 Database Engine") + 37 StringUtils.xmlNode("link", null, "http://www.h2database.com") + 38 StringUtils.xmlNode("description", null, "H2 Database Engine") + 39 StringUtils.xmlNode("language", null, "en-us") + 40 StringUtils.xmlNode("pubDate", null, StringUtils.formatDateTime(dt, "EEE, d MMM yyyy HH:mm:ss z", "en", "GMT")) + 41 StringUtils.xmlNode("lastBuildDate", null, StringUtils.formatDateTime(dt, "EEE, d MMM yyyy HH:mm:ss z", "en", "GMT")) + 42 StringUtils.xmlNode("item", null, 43 StringUtils.xmlNode("title", null, "New Version 0.9.9.9.9") + 44 StringUtils.xmlNode("link", null, "http://www.h2database.com") + 45 StringUtils.xmlNode("description", null, 46 StringUtils.xmlCData("\nNew Features\nTest\n") 47 ) 48 ) 49 ) 50 ); 51 check(s, "<?xml version=\"1.0\"?>\n" 52 +"<!-- Test Comment -->\n" 53 + "<rss version=\"2.0\">\n" 54 +" <!--\n" 55 +" Test Comment\n" 56 +" Zeile2\n" 57 +" -->\n" 58 +" <channel>\n" 59 +" <title>H2 Database Engine</title>\n" 60 +" <link>http://www.h2database.com</link>\n" 61 +" <description>H2 Database Engine</description>\n" 62 +" <language>en-us</language>\n" 63 +" <pubDate>Sat, 3 Feb 2001 04:05:06 GMT</pubDate>\n" 64 +" <lastBuildDate>Sat, 3 Feb 2001 04:05:06 GMT</lastBuildDate>\n" 65 +" <item>\n" 66 +" <title>New Version 0.9.9.9.9</title>\n" 67 +" <link>http://www.h2database.com</link>\n" 68 +" <description>\n" 69 +" <![CDATA[\n" 70 +" New Features\n" 71 +" Test\n" 72 +" ]]>\n" 73 +" </description>\n" 74 +" </item>\n" 75 +" </channel>\n" 76 +"</rss>\n"); 77 } 78 79 private void testURL() throws Exception { 80 Random random = new Random (1); 81 for(int i=0; i<100; i++) { 82 int len = random.nextInt(10); 83 StringBuffer buff = new StringBuffer (); 84 for(int j=0; j<len; j++) { 85 if(random.nextBoolean()) { 86 buff.append((char)random.nextInt(0x3000)); 87 } else { 88 buff.append((char)random.nextInt(255)); 89 } 90 } 91 String a = buff.toString(); 92 String b = URLEncoder.encode(a, "UTF-8"); 93 String c = URLDecoder.decode(b, "UTF-8"); 94 check(a, c); 95 String d = StringUtils.urlDecode(b); 96 check(d, c); 97 } 98 } 99 100 private void testJavaString() throws Exception { 101 Random random = new Random (1); 102 for(int i=0; i<1000; i++) { 103 int len = random.nextInt(10); 104 StringBuffer buff = new StringBuffer (); 105 for(int j=0; j<len; j++) { 106 if(random.nextBoolean()) { 107 buff.append((char)random.nextInt(0x3000)); 108 } else { 109 buff.append((char)random.nextInt(255)); 110 } 111 } 112 String a = buff.toString(); 113 String b = StringUtils.javaEncode(a); 114 String c = StringUtils.javaDecode(b); 115 check(a, c); 116 } 117 } 118 119 private void testSplit() throws Exception { 120 check(3, StringUtils.arraySplit("ABC,DEF,G\\,HI", ',', false).length); 121 check(StringUtils.arrayCombine(new String []{"", " ", ","}, ','), ", ,\\,"); 122 Random random = new Random (1); 123 for(int i=0; i<100; i++) { 124 int len = random.nextInt(10); 125 StringBuffer buff = new StringBuffer (); 126 String select = "abcd,"; 127 for(int j=0; j<len; j++) { 128 char c = select.charAt(random.nextInt(select.length())); 129 if(c == 'a') { 130 buff.append("\\\\"); 131 } else if(c=='b') { 132 buff.append("\\,"); 133 } else { 134 buff.append(c); 135 } 136 } 137 String a = buff.toString(); 138 String [] b = StringUtils.arraySplit(a, ',', false); 139 String c = StringUtils.arrayCombine(b, ','); 140 check(a, c); 141 } 142 } 143 144 } 145 | Popular Tags |