Source Code Cross Referenced for StringUtilsTest.java in  » Report » datavision-1.1.0 » jimm » datavision » test » 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 » Report » datavision 1.1.0 » jimm.datavision.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package jimm.datavision.test;
002:
003:        import jimm.util.StringUtils;
004:        import jimm.util.Replacer;
005:        import java.util.ArrayList;
006:        import java.util.List;
007:        import junit.framework.TestCase;
008:        import junit.framework.TestSuite;
009:        import junit.framework.Test;
010:
011:        public class StringUtilsTest extends TestCase {
012:
013:            public static Test suite() {
014:                return new TestSuite(StringUtilsTest.class);
015:            }
016:
017:            public StringUtilsTest(String name) {
018:                super (name);
019:            }
020:
021:            public void testEscapeXML() {
022:                assertEquals("", StringUtils.escapeXML(null));
023:                assertEquals("", StringUtils.escapeXML(""));
024:                assertEquals("abc&def", StringUtils.escapeXML("abc&def"));
025:                assertEquals("abc"def'", StringUtils
026:                        .escapeXML("abc\"def'"));
027:                assertEquals("&lt;&gt;", StringUtils.escapeXML("<>"));
028:            }
029:
030:            public void testUnescapeXML() {
031:                assertEquals("", StringUtils.unescapeXML(null));
032:                assertEquals("", StringUtils.unescapeXML(""));
033:                assertEquals("abc&def", StringUtils.unescapeXML("abc&amp;def"));
034:                assertEquals("abc\"def'", StringUtils
035:                        .unescapeXML("abc&quot;def&apos;"));
036:                assertEquals("<>", StringUtils.unescapeXML("&lt;&gt;"));
037:                assertEquals("abc def", StringUtils.unescapeXML("abc&#20;def"));
038:            }
039:
040:            public void testNewlines() {
041:                assertEquals("", StringUtils.newlinesToXHTMLBreaks(null));
042:                assertEquals("", StringUtils.newlinesToXHTMLBreaks(""));
043:                assertEquals("\n<br />", StringUtils
044:                        .newlinesToXHTMLBreaks("\n"));
045:                assertEquals("\n<br />", StringUtils
046:                        .newlinesToXHTMLBreaks("\r"));
047:                assertEquals("\n<br />", StringUtils
048:                        .newlinesToXHTMLBreaks("\r\n"));
049:                assertEquals("hello\n<br />world", StringUtils
050:                        .newlinesToXHTMLBreaks("hello\nworld"));
051:                assertEquals("hello\n<br />world", StringUtils
052:                        .newlinesToXHTMLBreaks("hello\rworld"));
053:                assertEquals("hello\n<br />world", StringUtils
054:                        .newlinesToXHTMLBreaks("hello\r\nworld"));
055:            }
056:
057:            public void testSplit() {
058:                ArrayList typicalAnswer = new ArrayList();
059:                typicalAnswer.add("a");
060:                typicalAnswer.add("b");
061:                typicalAnswer.add("c");
062:
063:                ArrayList origStringAnswer = new ArrayList();
064:                origStringAnswer.add("abc");
065:
066:                // Special cases
067:                assertNull(StringUtils.split(null, null));
068:                assertEquals(origStringAnswer, StringUtils.split("abc", null));
069:                assertEquals(origStringAnswer, StringUtils.split("abc", "x"));
070:
071:                assertEquals(typicalAnswer, StringUtils.split("a.b.c", "."));
072:                assertEquals(typicalAnswer, StringUtils.split("a**&*b**&*c",
073:                        "**&*"));
074:
075:                ArrayList answer = new ArrayList(typicalAnswer);
076:                answer.add(0, "");
077:                assertEquals(answer, StringUtils.split(".a.b.c", "."));
078:                assertEquals(answer, StringUtils.split("**&*a**&*b**&*c",
079:                        "**&*"));
080:
081:                answer = new ArrayList(typicalAnswer);
082:                answer.add(1, "");
083:                assertEquals(answer, StringUtils.split("a..b.c", "."));
084:                assertEquals(answer, StringUtils.split("a**&***&*b**&*c",
085:                        "**&*"));
086:
087:                answer = new ArrayList(typicalAnswer);
088:                answer.add("");
089:                assertEquals(answer, StringUtils.split("a.b.c.", "."));
090:                assertEquals(answer, StringUtils.split("a**&*b**&*c**&*",
091:                        "**&*"));
092:
093:                answer = new ArrayList(typicalAnswer);
094:                answer.add(1, "");
095:                answer.add("");
096:                assertEquals(answer, StringUtils.split("a..b.c.", "."));
097:                assertEquals(answer, StringUtils.split("a**&***&*b**&*c**&*",
098:                        "**&*"));
099:            }
100:
101:            public void testJoin() {
102:                assertEquals("", StringUtils.join(null, null));
103:                assertEquals("", StringUtils.join(null, ","));
104:
105:                ArrayList list = new ArrayList();
106:                assertEquals("", StringUtils.join(list, null));
107:                assertEquals("", StringUtils.join(list, ","));
108:
109:                list.add("a");
110:                assertEquals("a", StringUtils.join(list, null));
111:                assertEquals("a", StringUtils.join(list, ","));
112:
113:                list.add("xyzzy");
114:                assertEquals("axyzzy", StringUtils.join(list, null));
115:                assertEquals("a,xyzzy", StringUtils.join(list, ","));
116:                assertEquals("a-*-xyzzy", StringUtils.join(list, "-*-"));
117:            }
118:
119:            public void testReplace() {
120:                Replacer r = new Replacer() {
121:                    public Object replace(String s) {
122:                        return "x";
123:                    }
124:                };
125:
126:                assertNull(StringUtils.replaceDelimited("", "", null, null));
127:                assertNull(StringUtils.replaceDelimited(null, null, null, null));
128:                assertEquals("abcde", StringUtils.replaceDelimited(null, null,
129:                        r, "abcde"));
130:                assertEquals("abcde", StringUtils.replaceDelimited("", "", r,
131:                        "abcde"));
132:                assertEquals("", StringUtils.replaceDelimited("{%", "}", r, ""));
133:
134:                assertEquals("abxde", StringUtils.replaceDelimited("{%", "}",
135:                        r, "ab{%c}de"));
136:                assertEquals("xabxdex", StringUtils.replaceDelimited("{%", "}",
137:                        r, "{%}ab{%c}de{%   }"));
138:                assertEquals("abcd{%e", StringUtils.replaceDelimited("{%", "}",
139:                        r, "abcd{%e"));
140:                assertEquals(
141:                        "x.nil? ? nil : x / 100.0",
142:                        StringUtils
143:                                .replaceDelimited("{", "}", r,
144:                                        "{jobs.hourly rate}.nil? ? nil : {jobs.hourly rate} / 100.0"));
145:            }
146:
147:            public void testReplaceNotAfter() {
148:                Replacer r = new Replacer() {
149:                    public Object replace(String s) {
150:                        return "x";
151:                    }
152:                };
153:
154:                assertNull(StringUtils
155:                        .replaceDelimited("#", "", "", null, null));
156:                assertNull(StringUtils.replaceDelimited("#", "#", "#", null,
157:                        null));
158:                assertNull(StringUtils.replaceDelimited("#", null, null, null,
159:                        null));
160:                assertEquals("abcde", StringUtils.replaceDelimited("#", null,
161:                        null, r, "abcde"));
162:                assertEquals("abcde", StringUtils.replaceDelimited("#", "", "",
163:                        r, "abcde"));
164:                assertEquals("", StringUtils.replaceDelimited("#", "{%", "}",
165:                        r, ""));
166:
167:                assertEquals("abxde", StringUtils.replaceDelimited("#", "{%",
168:                        "}", r, "ab{%c}de"));
169:                assertEquals("xabxdex", StringUtils.replaceDelimited("#", "{%",
170:                        "}", r, "{%}ab{%c}de{%   }"));
171:                assertEquals("abcd{%e", StringUtils.replaceDelimited("#", "{%",
172:                        "}", r, "abcd{%e"));
173:
174:                assertEquals("ab#{c}de", StringUtils.replaceDelimited("#", "{",
175:                        "}", r, "ab#{c}de"));
176:                assertEquals("ab##{c}de", StringUtils.replaceDelimited("#",
177:                        "{", "}", r, "ab##{c}de"));
178:                assertEquals("ab##{x}dex", StringUtils.replaceDelimited("#",
179:                        "{", "}", r, "ab##{{c}}de{z}"));
180:                assertEquals("ab#{x}de", StringUtils.replaceDelimited("#", "{",
181:                        "}", r, "ab#{{c}}de"));
182:                assertEquals("ab#{x}de", StringUtils.replaceDelimited("#",
183:                        "{%", "}", r, "ab#{{%c}}de"));
184:            }
185:
186:            public void testAvoidInfiniteLoop() {
187:                final int[] count = new int[1];
188:                count[0] = 0;
189:                Replacer r = new Replacer() {
190:                    public Object replace(String s) {
191:                        if (++count[0] == 2)
192:                            fail("caught in an infinite loop");
193:                        return "{" + s + "}";
194:                    }
195:                };
196:                assertEquals("a{}b", StringUtils.replaceDelimited(null, "{",
197:                        "}", r, "a{}b"));
198:            }
199:
200:            public void testLineSplit() {
201:                ArrayList answer = new ArrayList();
202:                answer.add("");
203:
204:                List split = StringUtils.splitIntoLines("");
205:                assertEquals(answer.size(), split.size());
206:                assertEquals(answer, split);
207:
208:                // The three flavors of line endings
209:                String[] endings = { "\n", "\r", "\r\n" };
210:
211:                // We want to see an array with one empty string (the text before the
212:                // newline). There is no string after the newline.
213:                for (int i = 0; i < endings.length; ++i) {
214:                    split = StringUtils.splitIntoLines(endings[i]);
215:                    assertEquals(answer.size(), split.size());
216:                    assertEquals(answer, split);
217:                }
218:
219:                answer.add("x");
220:                for (int i = 0; i < endings.length; ++i) {
221:                    split = StringUtils.splitIntoLines(endings[i] + "x");
222:                    assertEquals(answer.size(), split.size());
223:                    assertEquals(answer, split);
224:                }
225:
226:                answer = new ArrayList();
227:                answer.add("line one");
228:                answer.add("part deux");
229:                answer.add("three's a crowd, eh?");
230:                answer.add("");
231:
232:                split = StringUtils
233:                        .splitIntoLines("line one\npart deux\nthree's a crowd, eh?"
234:                                + "\n\n");
235:                assertEquals(answer.size(), split.size());
236:                assertEquals(answer, split);
237:
238:                // Test two newlines in a row
239:                answer = new ArrayList();
240:                answer.add("two newlines after this line");
241:                answer.add("");
242:                answer.add("part deux");
243:                answer.add("three's a crowd, eh?");
244:                answer.add("");
245:
246:                split = StringUtils
247:                        .splitIntoLines("two newlines after this line\n\npart deux\nthree's a crowd, eh?"
248:                                + "\n\n");
249:                assertEquals(answer.size(), split.size());
250:                assertEquals(answer, split);
251:
252:                split = StringUtils
253:                        .splitIntoLines("two newlines after this line\r\n\r\npart deux\r\nthree's a crowd, eh?"
254:                                + "\n\n");
255:                assertEquals(answer.size(), split.size());
256:                assertEquals(answer, split);
257:
258:                split = StringUtils
259:                        .splitIntoLines("two newlines after this line\r\rpart deux\rthree's a crowd, eh?"
260:                                + "\r\r");
261:                assertEquals(answer.size(), split.size());
262:                assertEquals(answer, split);
263:            }
264:
265:            public void testSplitUp() {
266:                StringBuffer buf = new StringBuffer();
267:                StringUtils.splitUp(buf, null);
268:                assertEquals("", buf.toString());
269:
270:                buf = new StringBuffer();
271:                StringUtils.splitUp(buf, "abcde abcde abcde abcde", 12);
272:                assertEquals("abcde abcde\nabcde abcde", buf.toString());
273:
274:                buf = new StringBuffer();
275:                StringUtils.splitUp(buf, "abcdeabcdeabcdeabcde", 12);
276:                assertEquals("abcdeabcdeabcdeabcde", buf.toString());
277:
278:                buf = new StringBuffer();
279:                StringUtils.splitUp(buf, "  abcde", 12);
280:                assertEquals("abcde", buf.toString());
281:
282:                buf = new StringBuffer();
283:                StringUtils.splitUp(buf,
284:                        "  abcde      abcde     abcde    abcde  ", 12);
285:                assertEquals("abcde\nabcde\nabcde\nabcde", buf.toString());
286:            }
287:
288:            public static void main(String[] args) {
289:                junit.textui.TestRunner.run(suite());
290:                System.exit(0);
291:            }
292:
293:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.