Source Code Cross Referenced for FileUtilTest.java in  » Database-Client » SQL-Workbench » workbench » 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 » Database Client » SQL Workbench » workbench.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * FileUtilTest.java
003:         *
004:         * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005:         *
006:         * Copyright 2002-2008, Thomas Kellerer
007:         * No part of this code maybe reused without the permission of the author
008:         *
009:         * To contact the author please send an email to: support@sql-workbench.net
010:         *
011:         */
012:        package workbench.util;
013:
014:        import java.io.BufferedReader;
015:        import java.io.File;
016:        import java.io.FileInputStream;
017:        import java.io.FileOutputStream;
018:        import java.io.FileReader;
019:        import java.io.FileWriter;
020:        import java.io.InputStream;
021:        import java.io.OutputStream;
022:        import java.io.OutputStreamWriter;
023:        import java.io.PrintWriter;
024:        import java.io.Reader;
025:        import java.io.Writer;
026:        import java.util.Collection;
027:        import junit.framework.*;
028:        import workbench.TestUtil;
029:        import workbench.WbTestCase;
030:
031:        /**
032:         *
033:         * @author support@sql-workbench.net
034:         */
035:        public class FileUtilTest extends WbTestCase {
036:            private TestUtil testUtil;
037:
038:            public FileUtilTest(String testName) {
039:                super (testName);
040:                testUtil = getTestUtil();
041:            }
042:
043:            public void testGetLines() {
044:                try {
045:                    File f = new File(testUtil.getBaseDir(), "somedata.txt");
046:                    String encoding = "ISO-8859-1";
047:                    Writer w = EncodingUtil.createWriter(
048:                            new FileOutputStream(f), encoding);
049:                    for (int i = 0; i < 100; i++) {
050:                        w.write("line_" + i + "\n");
051:                    }
052:                    w.close();
053:
054:                    BufferedReader in = new BufferedReader(new FileReader(f));
055:
056:                    Collection<String> lines = FileUtil.getLines(in);
057:                    assertEquals(100, lines.size());
058:                    for (int i = 0; i < 100; i++) {
059:                        assertTrue(lines.contains("line_" + i));
060:                    }
061:                } catch (Throwable th) {
062:                    th.printStackTrace();
063:                    fail(th.getMessage());
064:                }
065:            }
066:
067:            public void testReadLines() {
068:                try {
069:                    File f = new File(testUtil.getBaseDir(), "linetest.txt");
070:                    String encoding = "ISO-8859-1";
071:                    Writer w = EncodingUtil.createWriter(
072:                            new FileOutputStream(f), encoding);
073:                    w.write("Line 1\n");
074:                    w.write("Line 2\n");
075:                    w.close();
076:
077:                    BufferedReader in = EncodingUtil.createBufferedReader(f,
078:                            encoding);
079:                    StringBuilder content = new StringBuilder();
080:                    int lines = FileUtil.readLines(in, content, 5, "\n");
081:                    in.close();
082:                    assertEquals("Not enough lines", 2, lines);
083:                    assertEquals("Content not read properly",
084:                            "Line 1\nLine 2\n", content.toString());
085:
086:                    StringBuilder fileContent = new StringBuilder();
087:                    for (int i = 0; i < 15; i++) {
088:                        fileContent.append("Line " + i + "\n");
089:                    }
090:                    w = EncodingUtil.createWriter(new FileOutputStream(f),
091:                            encoding);
092:                    w.write(fileContent.toString());
093:                    w.close();
094:
095:                    content = new StringBuilder();
096:                    in = EncodingUtil.createBufferedReader(f, encoding);
097:                    lines = FileUtil.readLines(in, content, 10, "\n");
098:                    assertEquals("Not enough lines", 10, lines);
099:                    lines = FileUtil.readLines(in, content, 10, "\n");
100:                    in.close();
101:                    assertEquals("Not enough lines", 4, lines);
102:                    assertEquals("Wrong content retrieved", fileContent
103:                            .toString(), content.toString());
104:
105:                    fileContent = new StringBuilder();
106:                    for (int i = 0; i < 237; i++) {
107:                        fileContent.append("Line " + i + "\n");
108:                    }
109:                    w = EncodingUtil.createWriter(new FileOutputStream(f),
110:                            encoding);
111:                    w.write(fileContent.toString());
112:                    w.close();
113:
114:                    content = new StringBuilder(1000);
115:                    in = EncodingUtil.createBufferedReader(f, encoding);
116:                    lines = FileUtil.readLines(in, content, 10, "\n");
117:                    while (lines == 10) {
118:                        lines = FileUtil.readLines(in, content, 10, "\n");
119:                    }
120:                    in.close();
121:                    assertEquals("Wrong content retrieved", fileContent
122:                            .toString(), content.toString());
123:                } catch (Exception e) {
124:                    e.printStackTrace();
125:                    fail(e.getMessage());
126:                }
127:            }
128:
129:            public void testGetLineEnding() throws Exception {
130:                try {
131:                    File f = new File(testUtil.getBaseDir(), "ending.txt");
132:
133:                    String encoding = "ISO-8859-1";
134:                    Writer w = EncodingUtil.createWriter(
135:                            new FileOutputStream(f), encoding);
136:                    w.write("Line 1\n");
137:                    w.write("Line 2");
138:                    w.close();
139:
140:                    Reader r = EncodingUtil.createReader(f, encoding);
141:                    String ending = FileUtil.getLineEnding(r);
142:                    assertEquals("Unix line ending not detected", "\n", ending);
143:                    r.close();
144:
145:                    w = EncodingUtil.createWriter(new FileOutputStream(f),
146:                            encoding);
147:                    w.write("Line 1");
148:                    w.write("\r\n");
149:                    w.write("Line 2");
150:                    w.close();
151:
152:                    r = EncodingUtil.createReader(f, encoding);
153:                    ending = FileUtil.getLineEnding(r);
154:                    assertEquals("DOS line ending not detected", "\r\n", ending);
155:                    r.close();
156:                } catch (Exception e) {
157:                    e.printStackTrace();
158:                    fail(e.getMessage());
159:                }
160:            }
161:
162:            public void testEstimateRecords() throws Exception {
163:                try {
164:                    File f = new File(testUtil.getBaseDir(), "short_file.txt");
165:                    PrintWriter pw = new PrintWriter(new FileWriter(f));
166:                    pw.println("Line 1");
167:                    pw.close();
168:                    long r = FileUtil.estimateRecords(f, 10);
169:                    // as the file is only one line, we expect 1 as the result
170:                    assertEquals("Wrong lines estimated", 1, r);
171:
172:                    // Test empty file
173:                    pw = new PrintWriter(new FileWriter(f));
174:                    pw.close();
175:                    r = FileUtil.estimateRecords(f, 10);
176:                    assertEquals("Wrong lines estimated", 0, r);
177:
178:                    // Test normal file
179:                    pw = new PrintWriter(new FileWriter(f));
180:                    for (int i = 0; i < 100; i++) {
181:                        pw.println("Line data");
182:                    }
183:                    pw.close();
184:                    r = FileUtil.estimateRecords(f, 10);
185:                    assertEquals("Wrong lines estimated", 100, r);
186:                } catch (Throwable e) {
187:                    e.printStackTrace();
188:                    fail(e.getMessage());
189:                }
190:            }
191:
192:            public void testCopy() {
193:                try {
194:                    File sf = new File(testUtil.getBaseDir(), "sourcefile.dat");
195:                    OutputStream out = new FileOutputStream(sf);
196:                    for (int i = 0; i < 32768; i++) {
197:                        out.write(i);
198:                    }
199:                    out.close();
200:                    InputStream in = new FileInputStream(sf);
201:
202:                    File tf = new File("copy.dat");
203:                    OutputStream target = new FileOutputStream("copy.dat");
204:                    FileUtil.copy(in, target);
205:
206:                    long size = tf.length();
207:                    assertEquals("Wrong file size", tf.length(), sf.length());
208:
209:                    sf.delete();
210:                    tf.delete();
211:                } catch (Throwable e) {
212:                    e.printStackTrace();
213:                    fail(e.getMessage());
214:                }
215:            }
216:
217:            public void testReadCharacters() throws Exception {
218:                try {
219:                    File f = new File(testUtil.getBaseDir(), "chartest.txt");
220:                    PrintWriter pw = new PrintWriter(new FileWriter(f));
221:                    String content = "Don't panic, count to ten... then panic!";
222:                    pw.print(content);
223:                    pw.close();
224:
225:                    FileReader in = new FileReader(f);
226:                    String result = FileUtil.readCharacters(in);
227:                    assertEquals("Wrong content retrieved", content, result);
228:                } catch (Throwable e) {
229:                    e.printStackTrace();
230:                    fail(e.getMessage());
231:                }
232:            }
233:
234:            public void testReadBytes() throws Exception {
235:                try {
236:                    File sf = new File(testUtil.getBaseDir(), "sourcefile.dat");
237:                    OutputStream out = new FileOutputStream(sf);
238:                    for (int i = 0; i < 32768; i++) {
239:                        out.write(i);
240:                    }
241:                    out.close();
242:                    InputStream in = new FileInputStream(sf);
243:
244:                    byte[] b = FileUtil.readBytes(in);
245:                    assertEquals("Wrong file size", (int) sf.length(), b.length);
246:
247:                    assertEquals("Wrong content read from file", 1, b[1]);
248:                    assertEquals("Wrong content read from file", 2, b[2]);
249:                    assertEquals("Wrong content read from file", 3, b[3]);
250:                    sf.delete();
251:                } catch (Throwable e) {
252:                    e.printStackTrace();
253:                    fail(e.getMessage());
254:                }
255:            }
256:
257:            public void testGetCharacterLength() {
258:                try {
259:                    File f = new File(testUtil.getBaseDir(), "mydata.txt");
260:                    String encoding = "ISO-8859-1";
261:                    Writer out = new OutputStreamWriter(
262:                            new FileOutputStream(f), encoding);
263:                    String content = "This is a test";
264:                    out.write(content);
265:                    out.close();
266:
267:                    assertEquals(content.length(), FileUtil.getCharacterLength(
268:                            f, encoding));
269:
270:                    encoding = "UTF-8";
271:                    content = "\u00c3\u00b6\u00c3\u00a4\u00c3\u00bc\u00c3\u2013\u00c3\u201e\u00c3\u0153";
272:                    out = new OutputStreamWriter(new FileOutputStream(f),
273:                            encoding);
274:                    out.write(content);
275:                    out.close();
276:
277:                    assertEquals(content.length(), FileUtil.getCharacterLength(
278:                            f, encoding));
279:
280:                    f.delete();
281:                } catch (Throwable e) {
282:                    e.printStackTrace();
283:                    fail(e.getMessage());
284:                }
285:
286:            }
287:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.