Source Code Cross Referenced for FileUtilsTestCase.java in  » Library » apache-common-IO » org » apache » commons » io » 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 » Library » apache common IO » org.apache.commons.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:        package org.apache.commons.io;
0018:
0019:        import java.io.File;
0020:        import java.io.FileInputStream;
0021:        import java.io.FileOutputStream;
0022:        import java.io.IOException;
0023:        import java.io.OutputStream;
0024:        import java.net.URL;
0025:        import java.util.Arrays;
0026:        import java.util.Collection;
0027:        import java.util.Date;
0028:        import java.util.GregorianCalendar;
0029:        import java.util.HashMap;
0030:        import java.util.Iterator;
0031:        import java.util.List;
0032:        import java.util.Map;
0033:        import java.util.zip.CRC32;
0034:        import java.util.zip.Checksum;
0035:
0036:        import junit.framework.Test;
0037:        import junit.framework.TestSuite;
0038:        import junit.textui.TestRunner;
0039:
0040:        import org.apache.commons.io.filefilter.WildcardFilter;
0041:        import org.apache.commons.io.testtools.FileBasedTestCase;
0042:
0043:        /**
0044:         * This is used to test FileUtils for correctness.
0045:         *
0046:         * @author Peter Donald
0047:         * @author Matthew Hawthorne
0048:         * @author Stephen Colebourne
0049:         * @author Jim Harrington
0050:         * @version $Id: FileUtilsTestCase.java 503497 2007-02-04 22:15:11Z ggregory $
0051:         * @see FileUtils
0052:         */
0053:        public class FileUtilsTestCase extends FileBasedTestCase {
0054:
0055:            // Test data
0056:
0057:            /**
0058:             * Size of test directory.
0059:             */
0060:            private static final int TEST_DIRECTORY_SIZE = 0;
0061:
0062:            /** Delay in milliseconds to make sure test for "last modified date" are accurate */
0063:            //private static final int LAST_MODIFIED_DELAY = 600;
0064:            private File testFile1;
0065:            private File testFile2;
0066:
0067:            private static int testFile1Size;
0068:            private static int testFile2Size;
0069:
0070:            public static void main(String[] args) {
0071:                TestRunner.run(suite());
0072:            }
0073:
0074:            public static Test suite() {
0075:                return new TestSuite(FileUtilsTestCase.class);
0076:            }
0077:
0078:            public FileUtilsTestCase(String name) throws IOException {
0079:                super (name);
0080:
0081:                testFile1 = new File(getTestDirectory(), "file1-test.txt");
0082:                testFile2 = new File(getTestDirectory(), "file1a-test.txt");
0083:
0084:                testFile1Size = (int) testFile1.length();
0085:                testFile2Size = (int) testFile2.length();
0086:            }
0087:
0088:            /** @see junit.framework.TestCase#setUp() */
0089:            protected void setUp() throws Exception {
0090:                getTestDirectory().mkdirs();
0091:                createFile(testFile1, testFile1Size);
0092:                createFile(testFile2, testFile2Size);
0093:                FileUtils.deleteDirectory(getTestDirectory());
0094:                getTestDirectory().mkdirs();
0095:                createFile(testFile1, testFile1Size);
0096:                createFile(testFile2, testFile2Size);
0097:            }
0098:
0099:            /** @see junit.framework.TestCase#tearDown() */
0100:            protected void tearDown() throws Exception {
0101:                FileUtils.deleteDirectory(getTestDirectory());
0102:            }
0103:
0104:            //-----------------------------------------------------------------------
0105:            public void test_openInputStream_exists() throws Exception {
0106:                File file = new File(getTestDirectory(), "test.txt");
0107:                createLineBasedFile(file, new String[] { "Hello" });
0108:                FileInputStream in = null;
0109:                try {
0110:                    in = FileUtils.openInputStream(file);
0111:                    assertEquals('H', in.read());
0112:                } finally {
0113:                    IOUtils.closeQuietly(in);
0114:                }
0115:            }
0116:
0117:            public void test_openInputStream_existsButIsDirectory()
0118:                    throws Exception {
0119:                File directory = new File(getTestDirectory(), "subdir");
0120:                directory.mkdirs();
0121:                FileInputStream in = null;
0122:                try {
0123:                    in = FileUtils.openInputStream(directory);
0124:                    fail();
0125:                } catch (IOException ioe) {
0126:                    // expected
0127:                } finally {
0128:                    IOUtils.closeQuietly(in);
0129:                }
0130:            }
0131:
0132:            public void test_openInputStream_notExists() throws Exception {
0133:                File directory = new File(getTestDirectory(), "test.txt");
0134:                FileInputStream in = null;
0135:                try {
0136:                    in = FileUtils.openInputStream(directory);
0137:                    fail();
0138:                } catch (IOException ioe) {
0139:                    // expected
0140:                } finally {
0141:                    IOUtils.closeQuietly(in);
0142:                }
0143:            }
0144:
0145:            //-----------------------------------------------------------------------
0146:            void openOutputStream_noParent(boolean createFile) throws Exception {
0147:                File file = new File("test.txt");
0148:                assertNull(file.getParentFile());
0149:                try {
0150:                    if (createFile) {
0151:                        createLineBasedFile(file, new String[] { "Hello" });
0152:                    }
0153:                    FileOutputStream out = null;
0154:                    try {
0155:                        out = FileUtils.openOutputStream(file);
0156:                        out.write(0);
0157:                    } finally {
0158:                        IOUtils.closeQuietly(out);
0159:                    }
0160:                    assertEquals(true, file.exists());
0161:                } finally {
0162:                    if (file.delete() == false) {
0163:                        file.deleteOnExit();
0164:                    }
0165:                }
0166:            }
0167:
0168:            public void test_openOutputStream_noParentCreateFile()
0169:                    throws Exception {
0170:                openOutputStream_noParent(true);
0171:            }
0172:
0173:            public void test_openOutputStream_noParentNoFile() throws Exception {
0174:                openOutputStream_noParent(false);
0175:            }
0176:
0177:            public void test_openOutputStream_exists() throws Exception {
0178:                File file = new File(getTestDirectory(), "test.txt");
0179:                createLineBasedFile(file, new String[] { "Hello" });
0180:                FileOutputStream out = null;
0181:                try {
0182:                    out = FileUtils.openOutputStream(file);
0183:                    out.write(0);
0184:                } finally {
0185:                    IOUtils.closeQuietly(out);
0186:                }
0187:                assertEquals(true, file.exists());
0188:            }
0189:
0190:            public void test_openOutputStream_existsButIsDirectory()
0191:                    throws Exception {
0192:                File directory = new File(getTestDirectory(), "subdir");
0193:                directory.mkdirs();
0194:                FileOutputStream out = null;
0195:                try {
0196:                    out = FileUtils.openOutputStream(directory);
0197:                    fail();
0198:                } catch (IOException ioe) {
0199:                    // expected
0200:                } finally {
0201:                    IOUtils.closeQuietly(out);
0202:                }
0203:            }
0204:
0205:            public void test_openOutputStream_notExists() throws Exception {
0206:                File file = new File(getTestDirectory(), "a/test.txt");
0207:                FileOutputStream out = null;
0208:                try {
0209:                    out = FileUtils.openOutputStream(file);
0210:                    out.write(0);
0211:                } finally {
0212:                    IOUtils.closeQuietly(out);
0213:                }
0214:                assertEquals(true, file.exists());
0215:            }
0216:
0217:            public void test_openOutputStream_notExistsCannotCreate()
0218:                    throws Exception {
0219:                // according to Wikipedia, most filing systems have a 256 limit on filename
0220:                String longStr = "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"
0221:                        + "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"
0222:                        + "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"
0223:                        + "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"
0224:                        + "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"
0225:                        + "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"; // 300 chars
0226:                File file = new File(getTestDirectory(), "a/" + longStr
0227:                        + "/test.txt");
0228:                FileOutputStream out = null;
0229:                try {
0230:                    out = FileUtils.openOutputStream(file);
0231:                    fail();
0232:                } catch (IOException ioe) {
0233:                    // expected
0234:                } finally {
0235:                    IOUtils.closeQuietly(out);
0236:                }
0237:            }
0238:
0239:            //-----------------------------------------------------------------------
0240:            // byteCountToDisplaySize
0241:            public void testByteCountToDisplaySize() {
0242:                assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
0243:                assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");
0244:                assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024),
0245:                        "1 MB");
0246:                assertEquals(FileUtils
0247:                        .byteCountToDisplaySize(1024 * 1024 * 1024), "1 GB");
0248:            }
0249:
0250:            //-----------------------------------------------------------------------
0251:            public void testToFile1() throws Exception {
0252:                URL url = new URL("file", null, "a/b/c/file.txt");
0253:                File file = FileUtils.toFile(url);
0254:                assertEquals(true, file.toString().indexOf("file.txt") >= 0);
0255:            }
0256:
0257:            public void testToFile2() throws Exception {
0258:                URL url = new URL("file", null, "a/b/c/file%20n%61me.tx%74");
0259:                File file = FileUtils.toFile(url);
0260:                assertEquals(true,
0261:                        file.toString().indexOf("file name.txt") >= 0);
0262:            }
0263:
0264:            public void testToFile3() throws Exception {
0265:                assertEquals(null, FileUtils.toFile((URL) null));
0266:                assertEquals(null, FileUtils.toFile(new URL(
0267:                        "http://jakarta.apache.org")));
0268:            }
0269:
0270:            public void testToFile4() throws Exception {
0271:                URL url = new URL("file", null, "a/b/c/file%2Xn%61me.txt");
0272:                try {
0273:                    FileUtils.toFile(url);
0274:                    fail();
0275:                } catch (IllegalArgumentException ex) {
0276:                }
0277:            }
0278:
0279:            // toFiles
0280:
0281:            public void testToFiles1() throws Exception {
0282:                URL[] urls = new URL[] { new URL("file", null, "file1.txt"),
0283:                        new URL("file", null, "file2.txt"), };
0284:                File[] files = FileUtils.toFiles(urls);
0285:
0286:                assertEquals(urls.length, files.length);
0287:                assertEquals("File: " + files[0], true, files[0].toString()
0288:                        .indexOf("file1.txt") >= 0);
0289:                assertEquals("File: " + files[1], true, files[1].toString()
0290:                        .indexOf("file2.txt") >= 0);
0291:            }
0292:
0293:            public void testToFiles2() throws Exception {
0294:                URL[] urls = new URL[] { new URL("file", null, "file1.txt"),
0295:                        null, };
0296:                File[] files = FileUtils.toFiles(urls);
0297:
0298:                assertEquals(urls.length, files.length);
0299:                assertEquals("File: " + files[0], true, files[0].toString()
0300:                        .indexOf("file1.txt") >= 0);
0301:                assertEquals("File: " + files[1], null, files[1]);
0302:            }
0303:
0304:            public void testToFiles3() throws Exception {
0305:                URL[] urls = null;
0306:                File[] files = FileUtils.toFiles(urls);
0307:
0308:                assertEquals(0, files.length);
0309:            }
0310:
0311:            public void testToFiles4() throws Exception {
0312:                URL[] urls = new URL[] { new URL("file", null, "file1.txt"),
0313:                        new URL("http", "jakarta.apache.org", "file1.txt"), };
0314:                try {
0315:                    FileUtils.toFiles(urls);
0316:                    fail();
0317:                } catch (IllegalArgumentException ex) {
0318:                }
0319:            }
0320:
0321:            // toURLs
0322:
0323:            public void testToURLs1() throws Exception {
0324:                File[] files = new File[] {
0325:                        new File(getTestDirectory(), "file1.txt"),
0326:                        new File(getTestDirectory(), "file2.txt"), };
0327:                URL[] urls = FileUtils.toURLs(files);
0328:
0329:                assertEquals(files.length, urls.length);
0330:                assertEquals(true, urls[0].toExternalForm().startsWith("file:"));
0331:                assertEquals(true, urls[0].toExternalForm()
0332:                        .indexOf("file1.txt") >= 0);
0333:                assertEquals(true, urls[1].toExternalForm().startsWith("file:"));
0334:                assertEquals(true, urls[1].toExternalForm()
0335:                        .indexOf("file2.txt") >= 0);
0336:            }
0337:
0338:            //    public void testToURLs2() throws Exception {
0339:            //        File[] files = new File[] {
0340:            //            new File(getTestDirectory(), "file1.txt"),
0341:            //            null,
0342:            //        };
0343:            //        URL[] urls = FileUtils.toURLs(files);
0344:            //        
0345:            //        assertEquals(files.length, urls.length);
0346:            //        assertEquals(true, urls[0].toExternalForm().startsWith("file:"));
0347:            //        assertEquals(true, urls[0].toExternalForm().indexOf("file1.txt") > 0);
0348:            //        assertEquals(null, urls[1]);
0349:            //    }
0350:            //
0351:            //    public void testToURLs3() throws Exception {
0352:            //        File[] files = null;
0353:            //        URL[] urls = FileUtils.toURLs(files);
0354:            //        
0355:            //        assertEquals(0, urls.length);
0356:            //    }
0357:
0358:            // contentEquals
0359:
0360:            public void testContentEquals() throws Exception {
0361:                // Non-existent files
0362:                File file = new File(getTestDirectory(), getName());
0363:                File file2 = new File(getTestDirectory(), getName() + "2");
0364:                // both don't  exist
0365:                assertTrue(FileUtils.contentEquals(file, file));
0366:                assertTrue(FileUtils.contentEquals(file, file2));
0367:                assertTrue(FileUtils.contentEquals(file2, file2));
0368:                assertTrue(FileUtils.contentEquals(file2, file));
0369:
0370:                // Directories
0371:                try {
0372:                    FileUtils.contentEquals(getTestDirectory(),
0373:                            getTestDirectory());
0374:                    fail("Comparing directories should fail with an IOException");
0375:                } catch (IOException ioe) {
0376:                    //expected
0377:                }
0378:
0379:                // Different files
0380:                File objFile1 = new File(getTestDirectory(), getName()
0381:                        + ".object");
0382:                objFile1.deleteOnExit();
0383:                FileUtils.copyURLToFile(getClass().getResource(
0384:                        "/java/lang/Object.class"), objFile1);
0385:
0386:                File objFile1b = new File(getTestDirectory(), getName()
0387:                        + ".object2");
0388:                objFile1.deleteOnExit();
0389:                FileUtils.copyURLToFile(getClass().getResource(
0390:                        "/java/lang/Object.class"), objFile1b);
0391:
0392:                File objFile2 = new File(getTestDirectory(), getName()
0393:                        + ".collection");
0394:                objFile2.deleteOnExit();
0395:                FileUtils.copyURLToFile(getClass().getResource(
0396:                        "/java/util/Collection.class"), objFile2);
0397:
0398:                assertEquals(false, FileUtils.contentEquals(objFile1, objFile2));
0399:                assertEquals(false, FileUtils
0400:                        .contentEquals(objFile1b, objFile2));
0401:                assertEquals(true, FileUtils.contentEquals(objFile1, objFile1b));
0402:
0403:                assertEquals(true, FileUtils.contentEquals(objFile1, objFile1));
0404:                assertEquals(true, FileUtils
0405:                        .contentEquals(objFile1b, objFile1b));
0406:                assertEquals(true, FileUtils.contentEquals(objFile2, objFile2));
0407:
0408:                // Equal files
0409:                file.createNewFile();
0410:                file2.createNewFile();
0411:                assertEquals(true, FileUtils.contentEquals(file, file));
0412:                assertEquals(true, FileUtils.contentEquals(file, file2));
0413:            }
0414:
0415:            // copyURLToFile
0416:
0417:            public void testCopyURLToFile() throws Exception {
0418:                // Creates file
0419:                File file = new File(getTestDirectory(), getName());
0420:                file.deleteOnExit();
0421:
0422:                // Loads resource
0423:                String resourceName = "/java/lang/Object.class";
0424:                FileUtils.copyURLToFile(getClass().getResource(resourceName),
0425:                        file);
0426:
0427:                // Tests that resuorce was copied correctly
0428:                FileInputStream fis = new FileInputStream(file);
0429:                try {
0430:                    assertTrue("Content is not equal.", IOUtils.contentEquals(
0431:                            getClass().getResourceAsStream(resourceName), fis));
0432:                } finally {
0433:                    fis.close();
0434:                }
0435:                //TODO Maybe test copy to itself like for copyFile()
0436:            }
0437:
0438:            // forceMkdir
0439:
0440:            public void testForceMkdir() throws Exception {
0441:                // Tests with existing directory
0442:                FileUtils.forceMkdir(getTestDirectory());
0443:
0444:                // Creates test file
0445:                File testFile = new File(getTestDirectory(), getName());
0446:                testFile.deleteOnExit();
0447:                testFile.createNewFile();
0448:                assertTrue("Test file does not exist.", testFile.exists());
0449:
0450:                // Tests with existing file
0451:                try {
0452:                    FileUtils.forceMkdir(testFile);
0453:                    fail("Exception expected.");
0454:                } catch (IOException ex) {
0455:                }
0456:
0457:                testFile.delete();
0458:
0459:                // Tests with non-existent directory
0460:                FileUtils.forceMkdir(testFile);
0461:                assertTrue("Directory was not created.", testFile.exists());
0462:            }
0463:
0464:            // sizeOfDirectory
0465:
0466:            public void testSizeOfDirectory() throws Exception {
0467:                File file = new File(getTestDirectory(), getName());
0468:
0469:                // Non-existent file
0470:                try {
0471:                    FileUtils.sizeOfDirectory(file);
0472:                    fail("Exception expected.");
0473:                } catch (IllegalArgumentException ex) {
0474:                }
0475:
0476:                // Creates file
0477:                file.createNewFile();
0478:                file.deleteOnExit();
0479:
0480:                // Existing file
0481:                try {
0482:                    FileUtils.sizeOfDirectory(file);
0483:                    fail("Exception expected.");
0484:                } catch (IllegalArgumentException ex) {
0485:                }
0486:
0487:                // Existing directory
0488:                file.delete();
0489:                file.mkdir();
0490:
0491:                assertEquals("Unexpected directory size", TEST_DIRECTORY_SIZE,
0492:                        FileUtils.sizeOfDirectory(file));
0493:            }
0494:
0495:            // isFileNewer / isFileOlder
0496:            public void testIsFileNewerOlder() throws Exception {
0497:                File reference = new File(getTestDirectory(),
0498:                        "FileUtils-reference.txt");
0499:                File oldFile = new File(getTestDirectory(), "FileUtils-old.txt");
0500:                File newFile = new File(getTestDirectory(), "FileUtils-new.txt");
0501:                File invalidFile = new File(getTestDirectory(),
0502:                        "FileUtils-invalid-file.txt");
0503:
0504:                // Create Files
0505:                createFile(oldFile, 0);
0506:
0507:                do {
0508:                    try {
0509:                        Thread.sleep(1000);
0510:                    } catch (InterruptedException ie) {
0511:                        // ignore
0512:                    }
0513:                    createFile(reference, 0);
0514:                } while (oldFile.lastModified() == reference.lastModified());
0515:
0516:                Date date = new Date();
0517:                long now = date.getTime();
0518:
0519:                do {
0520:                    try {
0521:                        Thread.sleep(1000);
0522:                    } catch (InterruptedException ie) {
0523:                        // ignore
0524:                    }
0525:                    createFile(newFile, 0);
0526:                } while (reference.lastModified() == newFile.lastModified());
0527:
0528:                // Test isFileNewer()
0529:                assertFalse("Old File - Newer - File", FileUtils.isFileNewer(
0530:                        oldFile, reference));
0531:                assertFalse("Old File - Newer - Date", FileUtils.isFileNewer(
0532:                        oldFile, date));
0533:                assertFalse("Old File - Newer - Mili", FileUtils.isFileNewer(
0534:                        oldFile, now));
0535:                assertTrue("New File - Newer - File", FileUtils.isFileNewer(
0536:                        newFile, reference));
0537:                assertTrue("New File - Newer - Date", FileUtils.isFileNewer(
0538:                        newFile, date));
0539:                assertTrue("New File - Newer - Mili", FileUtils.isFileNewer(
0540:                        newFile, now));
0541:                assertFalse("Invalid - Newer - File", FileUtils.isFileNewer(
0542:                        invalidFile, reference));
0543:
0544:                // Test isFileOlder()
0545:                assertTrue("Old File - Older - File", FileUtils.isFileOlder(
0546:                        oldFile, reference));
0547:                assertTrue("Old File - Older - Date", FileUtils.isFileOlder(
0548:                        oldFile, date));
0549:                assertTrue("Old File - Older - Mili", FileUtils.isFileOlder(
0550:                        oldFile, now));
0551:                assertFalse("New File - Older - File", FileUtils.isFileOlder(
0552:                        newFile, reference));
0553:                assertFalse("New File - Older - Date", FileUtils.isFileOlder(
0554:                        newFile, date));
0555:                assertFalse("New File - Older - Mili", FileUtils.isFileOlder(
0556:                        newFile, now));
0557:                assertFalse("Invalid - Older - File", FileUtils.isFileOlder(
0558:                        invalidFile, reference));
0559:
0560:                // ----- Test isFileNewer() exceptions -----
0561:                // Null File
0562:                try {
0563:                    FileUtils.isFileNewer(null, now);
0564:                    fail("Newer Null, expected IllegalArgumentExcepion");
0565:                } catch (IllegalArgumentException expected) {
0566:                    // expected result
0567:                }
0568:
0569:                // Null reference File
0570:                try {
0571:                    FileUtils.isFileNewer(oldFile, (File) null);
0572:                    fail("Newer Null reference, expected IllegalArgumentExcepion");
0573:                } catch (IllegalArgumentException expected) {
0574:                    // expected result
0575:                }
0576:
0577:                // Invalid reference File
0578:                try {
0579:                    FileUtils.isFileNewer(oldFile, invalidFile);
0580:                    fail("Newer invalid reference, expected IllegalArgumentExcepion");
0581:                } catch (IllegalArgumentException expected) {
0582:                    // expected result
0583:                }
0584:
0585:                // Null reference Date
0586:                try {
0587:                    FileUtils.isFileNewer(oldFile, (Date) null);
0588:                    fail("Newer Null date, expected IllegalArgumentExcepion");
0589:                } catch (IllegalArgumentException expected) {
0590:                    // expected result
0591:                }
0592:
0593:                // ----- Test isFileOlder() exceptions -----
0594:                // Null File
0595:                try {
0596:                    FileUtils.isFileOlder(null, now);
0597:                    fail("Older Null, expected IllegalArgumentExcepion");
0598:                } catch (IllegalArgumentException expected) {
0599:                    // expected result
0600:                }
0601:
0602:                // Null reference File
0603:                try {
0604:                    FileUtils.isFileOlder(oldFile, (File) null);
0605:                    fail("Older Null reference, expected IllegalArgumentExcepion");
0606:                } catch (IllegalArgumentException expected) {
0607:                    // expected result
0608:                }
0609:
0610:                // Invalid reference File
0611:                try {
0612:                    FileUtils.isFileOlder(oldFile, invalidFile);
0613:                    fail("Older invalid reference, expected IllegalArgumentExcepion");
0614:                } catch (IllegalArgumentException expected) {
0615:                    // expected result
0616:                }
0617:
0618:                // Null reference Date
0619:                try {
0620:                    FileUtils.isFileOlder(oldFile, (Date) null);
0621:                    fail("Older Null date, expected IllegalArgumentExcepion");
0622:                } catch (IllegalArgumentException expected) {
0623:                    // expected result
0624:                }
0625:
0626:            }
0627:
0628:            //    // TODO Remove after debugging
0629:            //    private void log(Object obj) {
0630:            //        System.out.println(
0631:            //            FileUtilsTestCase.class +" " + getName() + " " + obj);
0632:            //    }
0633:
0634:            // copyFile
0635:
0636:            public void testCopyFile1() throws Exception {
0637:                File destination = new File(getTestDirectory(), "copy1.txt");
0638:
0639:                //Thread.sleep(LAST_MODIFIED_DELAY);
0640:                //This is to slow things down so we can catch if 
0641:                //the lastModified date is not ok
0642:
0643:                FileUtils.copyFile(testFile1, destination);
0644:                assertTrue("Check Exist", destination.exists());
0645:                assertTrue("Check Full copy",
0646:                        destination.length() == testFile1Size);
0647:                /* disabled: Thread.sleep doesn't work reliantly for this case
0648:                assertTrue("Check last modified date preserved", 
0649:                    testFile1.lastModified() == destination.lastModified());*/
0650:            }
0651:
0652:            public void testCopyFile2() throws Exception {
0653:                File destination = new File(getTestDirectory(), "copy2.txt");
0654:
0655:                //Thread.sleep(LAST_MODIFIED_DELAY);
0656:                //This is to slow things down so we can catch if 
0657:                //the lastModified date is not ok
0658:
0659:                FileUtils.copyFile(testFile1, destination);
0660:                assertTrue("Check Exist", destination.exists());
0661:                assertTrue("Check Full copy",
0662:                        destination.length() == testFile2Size);
0663:                /* disabled: Thread.sleep doesn't work reliably for this case
0664:                assertTrue("Check last modified date preserved", 
0665:                    testFile1.lastModified() == destination.lastModified());*/
0666:            }
0667:
0668:            public void testCopyToSelf() throws Exception {
0669:                File destination = new File(getTestDirectory(), "copy3.txt");
0670:                //Prepare a test file
0671:                FileUtils.copyFile(testFile1, destination);
0672:
0673:                try {
0674:                    FileUtils.copyFile(destination, destination);
0675:                    fail("file copy to self should not be possible");
0676:                } catch (IOException ioe) {
0677:                    //we want the exception, copy to self should be illegal
0678:                }
0679:            }
0680:
0681:            public void testCopyFile2WithoutFileDatePreservation()
0682:                    throws Exception {
0683:                File destination = new File(getTestDirectory(), "copy2.txt");
0684:
0685:                //Thread.sleep(LAST_MODIFIED_DELAY);
0686:                //This is to slow things down so we can catch if 
0687:                //the lastModified date is not ok
0688:
0689:                FileUtils.copyFile(testFile1, destination, false);
0690:                assertTrue("Check Exist", destination.exists());
0691:                assertTrue("Check Full copy",
0692:                        destination.length() == testFile2Size);
0693:                /* disabled: Thread.sleep doesn't work reliantly for this case
0694:                assertTrue("Check last modified date modified", 
0695:                    testFile1.lastModified() != destination.lastModified());*/
0696:            }
0697:
0698:            public void testCopyDirectoryToDirectory_NonExistingDest()
0699:                    throws Exception {
0700:                createFile(testFile1, 1234);
0701:                createFile(testFile2, 4321);
0702:                File srcDir = getTestDirectory();
0703:                File subDir = new File(srcDir, "sub");
0704:                subDir.mkdir();
0705:                File subFile = new File(subDir, "A.txt");
0706:                FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
0707:                File destDir = new File(System.getProperty("java.io.tmpdir"),
0708:                        "tmp-FileUtilsTestCase");
0709:                FileUtils.deleteDirectory(destDir);
0710:                File actualDestDir = new File(destDir, srcDir.getName());
0711:
0712:                FileUtils.copyDirectoryToDirectory(srcDir, destDir);
0713:
0714:                assertTrue("Check exists", destDir.exists());
0715:                assertTrue("Check exists", actualDestDir.exists());
0716:                assertEquals("Check size", FileUtils.sizeOfDirectory(srcDir),
0717:                        FileUtils.sizeOfDirectory(actualDestDir));
0718:                assertEquals(true, new File(actualDestDir, "sub/A.txt")
0719:                        .exists());
0720:                FileUtils.deleteDirectory(destDir);
0721:            }
0722:
0723:            public void testCopyDirectoryToNonExistingDest() throws Exception {
0724:                createFile(testFile1, 1234);
0725:                createFile(testFile2, 4321);
0726:                File srcDir = getTestDirectory();
0727:                File subDir = new File(srcDir, "sub");
0728:                subDir.mkdir();
0729:                File subFile = new File(subDir, "A.txt");
0730:                FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
0731:                File destDir = new File(System.getProperty("java.io.tmpdir"),
0732:                        "tmp-FileUtilsTestCase");
0733:                FileUtils.deleteDirectory(destDir);
0734:
0735:                FileUtils.copyDirectory(srcDir, destDir);
0736:
0737:                assertTrue("Check exists", destDir.exists());
0738:                assertEquals("Check size", FileUtils.sizeOfDirectory(srcDir),
0739:                        FileUtils.sizeOfDirectory(destDir));
0740:                assertEquals(true, new File(destDir, "sub/A.txt").exists());
0741:                FileUtils.deleteDirectory(destDir);
0742:            }
0743:
0744:            public void testCopyDirectoryToExistingDest() throws Exception {
0745:                createFile(testFile1, 1234);
0746:                createFile(testFile2, 4321);
0747:                File srcDir = getTestDirectory();
0748:                File subDir = new File(srcDir, "sub");
0749:                subDir.mkdir();
0750:                File subFile = new File(subDir, "A.txt");
0751:                FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
0752:                File destDir = new File(System.getProperty("java.io.tmpdir"),
0753:                        "tmp-FileUtilsTestCase");
0754:                FileUtils.deleteDirectory(destDir);
0755:                destDir.mkdirs();
0756:
0757:                FileUtils.copyDirectory(srcDir, destDir);
0758:
0759:                assertEquals(FileUtils.sizeOfDirectory(srcDir), FileUtils
0760:                        .sizeOfDirectory(destDir));
0761:                assertEquals(true, new File(destDir, "sub/A.txt").exists());
0762:            }
0763:
0764:            public void testCopyDirectoryErrors() throws Exception {
0765:                try {
0766:                    FileUtils.copyDirectory(null, null);
0767:                    fail();
0768:                } catch (NullPointerException ex) {
0769:                }
0770:                try {
0771:                    FileUtils.copyDirectory(new File("a"), null);
0772:                    fail();
0773:                } catch (NullPointerException ex) {
0774:                }
0775:                try {
0776:                    FileUtils.copyDirectory(null, new File("a"));
0777:                    fail();
0778:                } catch (NullPointerException ex) {
0779:                }
0780:                try {
0781:                    FileUtils.copyDirectory(new File("doesnt-exist"), new File(
0782:                            "a"));
0783:                    fail();
0784:                } catch (IOException ex) {
0785:                }
0786:                try {
0787:                    FileUtils.copyDirectory(testFile1, new File("a"));
0788:                    fail();
0789:                } catch (IOException ex) {
0790:                }
0791:                try {
0792:                    FileUtils.copyDirectory(getTestDirectory(), testFile1);
0793:                    fail();
0794:                } catch (IOException ex) {
0795:                }
0796:                try {
0797:                    FileUtils.copyDirectory(getTestDirectory(),
0798:                            getTestDirectory());
0799:                    fail();
0800:                } catch (IOException ex) {
0801:                }
0802:            }
0803:
0804:            // forceDelete
0805:
0806:            public void testForceDeleteAFile1() throws Exception {
0807:                File destination = new File(getTestDirectory(), "copy1.txt");
0808:                destination.createNewFile();
0809:                assertTrue("Copy1.txt doesn't exist to delete", destination
0810:                        .exists());
0811:                FileUtils.forceDelete(destination);
0812:                assertTrue("Check No Exist", !destination.exists());
0813:            }
0814:
0815:            public void testForceDeleteAFile2() throws Exception {
0816:                File destination = new File(getTestDirectory(), "copy2.txt");
0817:                destination.createNewFile();
0818:                assertTrue("Copy2.txt doesn't exist to delete", destination
0819:                        .exists());
0820:                FileUtils.forceDelete(destination);
0821:                assertTrue("Check No Exist", !destination.exists());
0822:            }
0823:
0824:            // copyFileToDirectory
0825:
0826:            public void testCopyFile1ToDir() throws Exception {
0827:                File directory = new File(getTestDirectory(), "subdir");
0828:                if (!directory.exists())
0829:                    directory.mkdirs();
0830:                File destination = new File(directory, testFile1.getName());
0831:
0832:                //Thread.sleep(LAST_MODIFIED_DELAY);
0833:                //This is to slow things down so we can catch if 
0834:                //the lastModified date is not ok
0835:
0836:                FileUtils.copyFileToDirectory(testFile1, directory);
0837:                assertTrue("Check Exist", destination.exists());
0838:                assertTrue("Check Full copy",
0839:                        destination.length() == testFile1Size);
0840:                /* disabled: Thread.sleep doesn't work reliantly for this case
0841:                assertTrue("Check last modified date preserved", 
0842:                    testFile1.lastModified() == destination.lastModified());*/
0843:
0844:                try {
0845:                    FileUtils.copyFileToDirectory(destination, directory);
0846:                    fail("Should not be able to copy a file into the same directory as itself");
0847:                } catch (IOException ioe) {
0848:                    //we want that, cannot copy to the same directory as the original file
0849:                }
0850:            }
0851:
0852:            public void testCopyFile2ToDir() throws Exception {
0853:                File directory = new File(getTestDirectory(), "subdir");
0854:                if (!directory.exists())
0855:                    directory.mkdirs();
0856:                File destination = new File(directory, testFile1.getName());
0857:
0858:                //Thread.sleep(LAST_MODIFIED_DELAY);
0859:                //This is to slow things down so we can catch if 
0860:                //the lastModified date is not ok
0861:
0862:                FileUtils.copyFileToDirectory(testFile1, directory);
0863:                assertTrue("Check Exist", destination.exists());
0864:                assertTrue("Check Full copy",
0865:                        destination.length() == testFile2Size);
0866:                /* disabled: Thread.sleep doesn't work reliantly for this case
0867:                assertTrue("Check last modified date preserved", 
0868:                    testFile1.lastModified() == destination.lastModified());*/
0869:            }
0870:
0871:            // forceDelete
0872:
0873:            public void testForceDeleteDir() throws Exception {
0874:                File testDirectory = getTestDirectory();
0875:                FileUtils.forceDelete(testDirectory.getParentFile());
0876:                assertTrue("Check No Exist", !testDirectory.getParentFile()
0877:                        .exists());
0878:            }
0879:
0880:            /**
0881:             *  Test the FileUtils implementation.
0882:             */
0883:            public void testFileUtils() throws Exception {
0884:                // Loads file from classpath
0885:                File file1 = new File(getTestDirectory(), "test.txt");
0886:                String filename = file1.getAbsolutePath();
0887:
0888:                //Create test file on-the-fly (used to be in CVS)
0889:                OutputStream out = new java.io.FileOutputStream(file1);
0890:                try {
0891:                    out.write("This is a test".getBytes("UTF-8"));
0892:                } finally {
0893:                    out.close();
0894:                }
0895:
0896:                File file2 = new File(getTestDirectory(), "test2.txt");
0897:
0898:                FileUtils.writeStringToFile(file2, filename, "UTF-8");
0899:                assertTrue(file2.exists());
0900:                assertTrue(file2.length() > 0);
0901:
0902:                String file2contents = FileUtils.readFileToString(file2,
0903:                        "UTF-8");
0904:                assertTrue("Second file's contents correct", filename
0905:                        .equals(file2contents));
0906:
0907:                assertTrue(file2.delete());
0908:
0909:                String contents = FileUtils.readFileToString(
0910:                        new File(filename), "UTF-8");
0911:                assertTrue("FileUtils.fileRead()", contents
0912:                        .equals("This is a test"));
0913:
0914:            }
0915:
0916:            public void testTouch() throws IOException {
0917:                File file = new File(getTestDirectory(), "touch.txt");
0918:                if (file.exists()) {
0919:                    file.delete();
0920:                }
0921:                assertTrue("Bad test: test file still exists", !file.exists());
0922:                FileUtils.touch(file);
0923:                assertTrue("FileUtils.touch() created file", file.exists());
0924:                FileOutputStream out = new FileOutputStream(file);
0925:                assertEquals("Created empty file.", 0, file.length());
0926:                out.write(0);
0927:                out.close();
0928:                assertEquals("Wrote one byte to file", 1, file.length());
0929:                long y2k = new GregorianCalendar(2000, 0, 1).getTime()
0930:                        .getTime();
0931:                boolean res = file.setLastModified(y2k); // 0L fails on Win98
0932:                assertEquals("Bad test: set lastModified failed", true, res);
0933:                assertEquals("Bad test: set lastModified set incorrect value",
0934:                        y2k, file.lastModified());
0935:                long now = System.currentTimeMillis();
0936:                FileUtils.touch(file);
0937:                assertEquals("FileUtils.touch() didn't empty the file.", 1,
0938:                        file.length());
0939:                assertEquals("FileUtils.touch() changed lastModified", false,
0940:                        y2k == file.lastModified());
0941:                assertEquals(
0942:                        "FileUtils.touch() changed lastModified to more than now-3s",
0943:                        true, file.lastModified() >= (now - 3000));
0944:                assertEquals(
0945:                        "FileUtils.touch() changed lastModified to less than now+3s",
0946:                        true, file.lastModified() <= (now + 3000));
0947:            }
0948:
0949:            public void testListFiles() throws Exception {
0950:                File srcDir = getTestDirectory();
0951:                File subDir = new File(srcDir, "list_test");
0952:                subDir.mkdir();
0953:
0954:                String[] fileNames = { "a.txt", "b.txt", "c.txt", "d.txt",
0955:                        "e.txt", "f.txt" };
0956:                int[] fileSizes = { 123, 234, 345, 456, 678, 789 };
0957:
0958:                for (int i = 0; i < fileNames.length; ++i) {
0959:                    File theFile = new File(subDir, fileNames[i]);
0960:                    createFile(theFile, fileSizes[i]);
0961:                }
0962:
0963:                Collection files = FileUtils.listFiles(subDir,
0964:                        new WildcardFilter("*.*"), new WildcardFilter("*"));
0965:
0966:                int count = files.size();
0967:                Object[] fileObjs = files.toArray();
0968:
0969:                assertEquals(files.size(), fileNames.length);
0970:
0971:                Map foundFileNames = new HashMap();
0972:
0973:                for (int i = 0; i < count; ++i) {
0974:                    boolean found = false;
0975:                    for (int j = 0; ((!found) && (j < fileNames.length)); ++j) {
0976:                        if (fileNames[j].equals(((File) fileObjs[i]).getName())) {
0977:                            foundFileNames.put(fileNames[j], fileNames[j]);
0978:                            found = true;
0979:                        }
0980:                    }
0981:                }
0982:
0983:                assertEquals(foundFileNames.size(), fileNames.length);
0984:
0985:                subDir.delete();
0986:            }
0987:
0988:            public void testIterateFiles() throws Exception {
0989:                File srcDir = getTestDirectory();
0990:                File subDir = new File(srcDir, "list_test");
0991:                subDir.mkdir();
0992:
0993:                String[] fileNames = { "a.txt", "b.txt", "c.txt", "d.txt",
0994:                        "e.txt", "f.txt" };
0995:                int[] fileSizes = { 123, 234, 345, 456, 678, 789 };
0996:
0997:                for (int i = 0; i < fileNames.length; ++i) {
0998:                    File theFile = new File(subDir, fileNames[i]);
0999:                    createFile(theFile, fileSizes[i]);
1000:                }
1001:
1002:                Iterator files = FileUtils.iterateFiles(subDir,
1003:                        new WildcardFilter("*.*"), new WildcardFilter("*"));
1004:
1005:                Map foundFileNames = new HashMap();
1006:
1007:                while (files.hasNext()) {
1008:                    boolean found = false;
1009:                    String fileName = ((File) files.next()).getName();
1010:
1011:                    for (int j = 0; ((!found) && (j < fileNames.length)); ++j) {
1012:                        if (fileNames[j].equals(fileName)) {
1013:                            foundFileNames.put(fileNames[j], fileNames[j]);
1014:                            found = true;
1015:                        }
1016:                    }
1017:                }
1018:
1019:                assertEquals(foundFileNames.size(), fileNames.length);
1020:
1021:                subDir.delete();
1022:            }
1023:
1024:            public void testReadFileToString() throws Exception {
1025:                File file = new File(getTestDirectory(), "read.obj");
1026:                FileOutputStream out = new FileOutputStream(file);
1027:                byte[] text = "Hello /u1234".getBytes("UTF8");
1028:                out.write(text);
1029:                out.close();
1030:
1031:                String data = FileUtils.readFileToString(file, "UTF8");
1032:                assertEquals("Hello /u1234", data);
1033:            }
1034:
1035:            public void testReadFileToByteArray() throws Exception {
1036:                File file = new File(getTestDirectory(), "read.txt");
1037:                FileOutputStream out = new FileOutputStream(file);
1038:                out.write(11);
1039:                out.write(21);
1040:                out.write(31);
1041:                out.close();
1042:
1043:                byte[] data = FileUtils.readFileToByteArray(file);
1044:                assertEquals(3, data.length);
1045:                assertEquals(11, data[0]);
1046:                assertEquals(21, data[1]);
1047:                assertEquals(31, data[2]);
1048:            }
1049:
1050:            public void testReadLines() throws Exception {
1051:                File file = newFile("lines.txt");
1052:                try {
1053:                    String[] data = new String[] { "hello", "/u1234", "",
1054:                            "this is", "some text" };
1055:                    createLineBasedFile(file, data);
1056:
1057:                    List lines = FileUtils.readLines(file, "UTF-8");
1058:                    assertEquals(Arrays.asList(data), lines);
1059:                } finally {
1060:                    deleteFile(file);
1061:                }
1062:            }
1063:
1064:            public void testWriteStringToFile1() throws Exception {
1065:                File file = new File(getTestDirectory(), "write.txt");
1066:                FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");
1067:                byte[] text = "Hello /u1234".getBytes("UTF8");
1068:                assertEqualContent(text, file);
1069:            }
1070:
1071:            public void testWriteStringToFile2() throws Exception {
1072:                File file = new File(getTestDirectory(), "write.txt");
1073:                FileUtils.writeStringToFile(file, "Hello /u1234", null);
1074:                byte[] text = "Hello /u1234".getBytes();
1075:                assertEqualContent(text, file);
1076:            }
1077:
1078:            public void testWriteByteArrayToFile() throws Exception {
1079:                File file = new File(getTestDirectory(), "write.obj");
1080:                byte[] data = new byte[] { 11, 21, 31 };
1081:                FileUtils.writeByteArrayToFile(file, data);
1082:                assertEqualContent(data, file);
1083:            }
1084:
1085:            public void testWriteLines_4arg() throws Exception {
1086:                Object[] data = new Object[] { "hello",
1087:                        new StringBuffer("world"), "", "this is", null,
1088:                        "some text" };
1089:                List list = Arrays.asList(data);
1090:
1091:                File file = newFile("lines.txt");
1092:                FileUtils.writeLines(file, "US-ASCII", list, "*");
1093:
1094:                String expected = "hello*world**this is**some text*";
1095:                String actual = FileUtils.readFileToString(file, "US-ASCII");
1096:                assertEquals(expected, actual);
1097:            }
1098:
1099:            public void testWriteLines_4arg_Writer_nullData() throws Exception {
1100:                File file = newFile("lines.txt");
1101:                FileUtils.writeLines(file, "US-ASCII", (List) null, "*");
1102:
1103:                assertEquals("Sizes differ", 0, file.length());
1104:            }
1105:
1106:            public void testWriteLines_4arg_nullSeparator() throws Exception {
1107:                Object[] data = new Object[] { "hello",
1108:                        new StringBuffer("world"), "", "this is", null,
1109:                        "some text" };
1110:                List list = Arrays.asList(data);
1111:
1112:                File file = newFile("lines.txt");
1113:                FileUtils.writeLines(file, "US-ASCII", list, null);
1114:
1115:                String expected = "hello" + IOUtils.LINE_SEPARATOR + "world"
1116:                        + IOUtils.LINE_SEPARATOR + IOUtils.LINE_SEPARATOR
1117:                        + "this is" + IOUtils.LINE_SEPARATOR
1118:                        + IOUtils.LINE_SEPARATOR + "some text"
1119:                        + IOUtils.LINE_SEPARATOR;
1120:                String actual = FileUtils.readFileToString(file, "US-ASCII");
1121:                assertEquals(expected, actual);
1122:            }
1123:
1124:            public void testWriteLines_3arg_nullSeparator() throws Exception {
1125:                Object[] data = new Object[] { "hello",
1126:                        new StringBuffer("world"), "", "this is", null,
1127:                        "some text" };
1128:                List list = Arrays.asList(data);
1129:
1130:                File file = newFile("lines.txt");
1131:                FileUtils.writeLines(file, "US-ASCII", list);
1132:
1133:                String expected = "hello" + IOUtils.LINE_SEPARATOR + "world"
1134:                        + IOUtils.LINE_SEPARATOR + IOUtils.LINE_SEPARATOR
1135:                        + "this is" + IOUtils.LINE_SEPARATOR
1136:                        + IOUtils.LINE_SEPARATOR + "some text"
1137:                        + IOUtils.LINE_SEPARATOR;
1138:                String actual = FileUtils.readFileToString(file, "US-ASCII");
1139:                assertEquals(expected, actual);
1140:            }
1141:
1142:            //-----------------------------------------------------------------------
1143:            public void testChecksumCRC32() throws Exception {
1144:                // create a test file
1145:                String text = "Imagination is more important than knowledge - Einstein";
1146:                File file = new File(getTestDirectory(), "checksum-test.txt");
1147:                FileUtils.writeStringToFile(file, text, "US-ASCII");
1148:
1149:                // compute the expected checksum
1150:                Checksum expectedChecksum = new CRC32();
1151:                expectedChecksum.update(text.getBytes("US-ASCII"), 0, text
1152:                        .length());
1153:                long expectedValue = expectedChecksum.getValue();
1154:
1155:                // compute the checksum of the file
1156:                long resultValue = FileUtils.checksumCRC32(file);
1157:
1158:                assertEquals(expectedValue, resultValue);
1159:            }
1160:
1161:            public void testChecksum() throws Exception {
1162:                // create a test file
1163:                String text = "Imagination is more important than knowledge - Einstein";
1164:                File file = new File(getTestDirectory(), "checksum-test.txt");
1165:                FileUtils.writeStringToFile(file, text, "US-ASCII");
1166:
1167:                // compute the expected checksum
1168:                Checksum expectedChecksum = new CRC32();
1169:                expectedChecksum.update(text.getBytes("US-ASCII"), 0, text
1170:                        .length());
1171:                long expectedValue = expectedChecksum.getValue();
1172:
1173:                // compute the checksum of the file
1174:                Checksum testChecksum = new CRC32();
1175:                Checksum resultChecksum = FileUtils
1176:                        .checksum(file, testChecksum);
1177:                long resultValue = resultChecksum.getValue();
1178:
1179:                assertSame(testChecksum, resultChecksum);
1180:                assertEquals(expectedValue, resultValue);
1181:            }
1182:
1183:            public void testChecksumOnNullFile() throws Exception {
1184:                try {
1185:                    FileUtils.checksum((File) null, new CRC32());
1186:                    fail();
1187:                } catch (NullPointerException ex) {
1188:                    // expected
1189:                }
1190:            }
1191:
1192:            public void testChecksumOnNullChecksum() throws Exception {
1193:                // create a test file
1194:                String text = "Imagination is more important than knowledge - Einstein";
1195:                File file = new File(getTestDirectory(), "checksum-test.txt");
1196:                FileUtils.writeStringToFile(file, text, "US-ASCII");
1197:                try {
1198:                    FileUtils.checksum(file, (Checksum) null);
1199:                    fail();
1200:                } catch (NullPointerException ex) {
1201:                    // expected
1202:                }
1203:            }
1204:
1205:            public void testChecksumOnDirectory() throws Exception {
1206:                try {
1207:                    FileUtils.checksum(new File("."), new CRC32());
1208:                    fail();
1209:                } catch (IllegalArgumentException ex) {
1210:                    // expected
1211:                }
1212:            }
1213:
1214:            public void testChecksumDouble() throws Exception {
1215:                // create a test file
1216:                String text1 = "Imagination is more important than knowledge - Einstein";
1217:                File file1 = new File(getTestDirectory(), "checksum-test.txt");
1218:                FileUtils.writeStringToFile(file1, text1, "US-ASCII");
1219:
1220:                // create a second test file
1221:                String text2 = "To be or not to be - Shakespeare";
1222:                File file2 = new File(getTestDirectory(), "checksum-test2.txt");
1223:                FileUtils.writeStringToFile(file2, text2, "US-ASCII");
1224:
1225:                // compute the expected checksum
1226:                Checksum expectedChecksum = new CRC32();
1227:                expectedChecksum.update(text1.getBytes("US-ASCII"), 0, text1
1228:                        .length());
1229:                expectedChecksum.update(text2.getBytes("US-ASCII"), 0, text2
1230:                        .length());
1231:                long expectedValue = expectedChecksum.getValue();
1232:
1233:                // compute the checksum of the file
1234:                Checksum testChecksum = new CRC32();
1235:                FileUtils.checksum(file1, testChecksum);
1236:                FileUtils.checksum(file2, testChecksum);
1237:                long resultValue = testChecksum.getValue();
1238:
1239:                assertEquals(expectedValue, resultValue);
1240:            }
1241:
1242:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.