Source Code Cross Referenced for CSVTests.java in  » Template-Engine » ostermillerutils » com » Ostermiller » util » 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 » Template Engine » ostermillerutils » com.Ostermiller.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CSV Regression test.
003:         * Copyright (C) 2001-2004 Stephen Ostermiller
004:         * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005:         *
006:         * This program is free software; you can redistribute it and/or modify
007:         * it under the terms of the GNU General Public License as published by
008:         * the Free Software Foundation; either version 2 of the License, or
009:         * (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License for more details.
015:         *
016:         * See COPYING.TXT for details.
017:         */
018:        package com.Ostermiller.util;
019:
020:        import java.io.*;
021:
022:        /**
023:         * Regression test for CSV.
024:         * More information about this class is available from <a target="_top" href=
025:         * "http://ostermiller.org/utils/CSV.html">ostermiller.org</a>.
026:         *
027:         * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
028:         * @since ostermillerutils 1.00.00
029:         */
030:        class CSVTests {
031:
032:            /**
033:             * Main method for running tests
034:             * @param args Command line arguments (ignored)
035:             */
036:            public static void main(String[] args) {
037:                try {
038:                    StringWriter sw = new StringWriter();
039:                    CSVPrinter csvOut = new CSVPrinter(sw, '#');
040:                    csvOut.setLineEnding("\r");
041:
042:                    csvOut.printlnComment("Comma Separated Value Test");
043:                    csvOut.println();
044:                    csvOut.printlnComment("Five Cities");
045:                    csvOut.println(new String[] { "Boston", "San Francisco",
046:                            "New York", "Chicago", "Houston", });
047:                    csvOut.println();
048:                    csvOut.println(""); // an empty value on a line by itself.
049:                    csvOut.println(new String[] { "Two\nTokens",
050:                            "On the\nSame Line" });
051:                    csvOut
052:                            .printlnComment("A two line comment\n just to see that it works");
053:
054:                    CSVParser shredder = new CSVParser(new StringReader(sw
055:                            .toString()));
056:                    shredder.setCommentStart("#;!");
057:                    shredder.setEscapes("nrtf", "\n\r\t\f");
058:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
059:                            "Boston", 4);
060:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
061:                            "San Francisco", 4);
062:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
063:                            "New York", 4);
064:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
065:                            "Chicago", 4);
066:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
067:                            "Houston", 4);
068:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
069:                            "", 6);
070:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
071:                            "Two\nTokens", 7);
072:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
073:                            "On the\nSame Line", 7);
074:                    compare(shredder.nextValue(), shredder.lastLineNumber(),
075:                            null, 9);
076:
077:                    String normalInput = ",\"a\",\",\t'\\\"\"";
078:                    String[][] normalOutput = new String[][] { { "", "a",
079:                            ",\t'\"" } };
080:                    shredder = new CSVParser(new StringReader(normalInput));
081:                    compare("normal", normalOutput, shredder.getAllValues());
082:
083:                    String tabInput = "\t\"a\"\t\",\t'\\\"\"";
084:                    shredder = new CSVParser(new StringReader(tabInput));
085:                    shredder.changeDelimiter('\t');
086:                    compare("tabs", normalOutput, shredder.getAllValues());
087:
088:                    String aposInput = ",'a',',\t\\'\"'";
089:                    shredder = new CSVParser(new StringReader(aposInput));
090:                    shredder.changeQuote('\'');
091:                    compare("apostrophes", normalOutput, shredder
092:                            .getAllValues());
093:
094:                    String swappedInput = "\",a,\",\\,\t'\\\",";
095:                    shredder = new CSVParser(new StringReader(swappedInput));
096:                    shredder.changeDelimiter('\t');
097:                    shredder.changeQuote(',');
098:                    shredder.changeDelimiter('"');
099:                    compare("commas and quotes swapped", normalOutput, shredder
100:                            .getAllValues());
101:
102:                    normalInput = "\"test\\\\\",test";
103:                    normalOutput = new String[][] { { "test\\", "test" } };
104:                    shredder = new CSVParser(new StringReader(normalInput));
105:                    compare("backslash at end of quoted", normalOutput,
106:                            shredder.getAllValues());
107:
108:                    normalInput = "field1,field2 ,    field3,field4   ,  field5   ,field6";
109:                    normalOutput = new String[][] { { "field1", "field2",
110:                            "field3", "field4", "field5", "field6" } };
111:                    shredder = new CSVParser(new StringReader(normalInput));
112:                    compare("white space around fields", normalOutput, shredder
113:                            .getAllValues());
114:
115:                    normalInput = ",field2,, ,field5,";
116:                    normalOutput = new String[][] { { "", "field2", "", "",
117:                            "field5", "" } };
118:                    shredder = new CSVParser(new StringReader(normalInput));
119:                    compare("empty fields", normalOutput, shredder
120:                            .getAllValues());
121:
122:                    normalInput = "1,to,tre,four,five5,sixsix";
123:                    normalOutput = new String[][] { { "1", "to", "tre", "four",
124:                            "five5", "sixsix" } };
125:                    shredder = new CSVParser(new StringReader(normalInput));
126:                    compare("various lengths", normalOutput, shredder
127:                            .getAllValues());
128:
129:                    normalInput = "!comment\n !field1\n;comment\n ;field2\n#comment\n #field3";
130:                    normalOutput = new String[][] { { "!field1" },
131:                            { ";field2" }, { "#field3" } };
132:                    shredder = new CSVParser(new StringReader(normalInput));
133:                    shredder.setCommentStart("#;!");
134:                    compare("comment must start at beginning of line",
135:                            normalOutput, shredder.getAllValues());
136:
137:                } catch (Exception x) {
138:                    System.err.println(x.getMessage());
139:                    x.printStackTrace();
140:                    System.exit(1);
141:                }
142:                System.exit(0);
143:            }
144:
145:            private static void compare(String value, int line,
146:                    String expectedValue, int expectedLine) throws Exception {
147:                if (line != expectedLine) {
148:                    throw new Exception("Line numbers do not match");
149:                }
150:                if (expectedValue == null && value == null) {
151:                    return;
152:                }
153:                if (!value.equals(expectedValue)) {
154:                    throw new Exception("Value and expected value do not match");
155:                }
156:            }
157:
158:            private static void compare(String testName, String[][] a,
159:                    String[][] b) throws Exception {
160:                if (a.length != b.length) {
161:                    throw new Exception(testName
162:                            + ": unexpected number of lines " + a.length
163:                            + " found " + b.length + " expected");
164:                }
165:                for (int i = 0; i < a.length; i++) {
166:                    if (a[i].length != b[i].length) {
167:                        throw new Exception(testName
168:                                + ": unexpected number of values in line: "
169:                                + b[i].length);
170:                    }
171:                    for (int j = 0; j < a[i].length; j++) {
172:                        if (!a[i][j].equals(b[i][j])) {
173:                            System.err.println(a[i][j]);
174:                            System.err.println(b[i][j]);
175:                            throw new Exception(testName
176:                                    + ": values do not match.");
177:                        }
178:                    }
179:                }
180:            }
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.