KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > io > test > FileUtilTestCase


1 /*
2  * Copyright The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.io.test;
9
10 import java.io.*;
11 import junit.framework.TestCase;
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import org.apache.avalon.excalibur.io.FileUtil;
15
16 /**
17  * This is used to test FileUtil for correctness.
18  *
19  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
20  */

21 public final class FileUtilTestCase
22     extends TestCase
23 {
24     private final int FILE1_SIZE = 1;
25     private final int FILE2_SIZE = 1024 * 4 + 1;
26
27     private final File m_testDirectory;
28     private final File m_testFile1;
29     private final File m_testFile2;
30
31     public FileUtilTestCase( final String JavaDoc name )
32         throws IOException
33     {
34         super( name );
35
36         m_testDirectory = (new File( "test/io/" )).getAbsoluteFile();
37         if( !m_testDirectory.exists() )
38         {
39             m_testDirectory.mkdirs();
40         }
41
42         m_testFile1 = new File( m_testDirectory, "file1-test.txt" );
43         m_testFile2 = new File( m_testDirectory, "file2-test.txt" );
44
45         createFile( m_testFile1, FILE1_SIZE );
46         createFile( m_testFile2, FILE2_SIZE );
47     }
48
49     private void createFile( final File file, final long size )
50         throws IOException
51     {
52         final BufferedOutputStream output =
53             new BufferedOutputStream( new FileOutputStream( file ) );
54
55         for( int i = 0; i < size; i++ )
56         {
57             output.write( (byte)'X' );
58         }
59
60         output.close();
61     }
62
63     public static Test suite()
64         throws IOException
65     {
66         final TestSuite suite = new TestSuite();
67         suite.addTest( new FileUtilTestCase( "testCopyFile1" ) );
68         suite.addTest( new FileUtilTestCase( "testCopyFile2" ) );
69         suite.addTest( new FileUtilTestCase( "testForceDeleteAFile1" ) );
70         suite.addTest( new FileUtilTestCase( "testForceDeleteAFile2" ) );
71         suite.addTest( new FileUtilTestCase( "testCopyFile1ToDir" ) );
72         suite.addTest( new FileUtilTestCase( "testCopyFile2ToDir" ) );
73         suite.addTest( new FileUtilTestCase( "testForceDeleteDir" ) );
74         suite.addTest( new FileUtilTestCase( "testResolveFileDotDot" ) );
75         suite.addTest( new FileUtilTestCase( "testResolveFileDot" ) );
76         suite.addTest( new FileUtilTestCase( "testNormalize" ) );
77         return suite;
78     }
79
80     public void testCopyFile1()
81         throws Exception JavaDoc
82     {
83         final File destination = new File( m_testDirectory, "copy1.txt" );
84         FileUtil.copyFile( m_testFile1, destination );
85         assertTrue( "Check Exist", destination.exists() );
86         assertTrue( "Check Full copy", destination.length() == FILE1_SIZE );
87     }
88
89     public void testCopyFile2()
90         throws Exception JavaDoc
91     {
92         final File destination = new File( m_testDirectory, "copy2.txt" );
93         FileUtil.copyFile( m_testFile2, destination );
94         assertTrue( "Check Exist", destination.exists() );
95         assertTrue( "Check Full copy", destination.length() == FILE2_SIZE );
96     }
97
98     public void testForceDeleteAFile1()
99         throws Exception JavaDoc
100     {
101         final File destination = new File( m_testDirectory, "copy1.txt" );
102         destination.createNewFile();
103         assertTrue( "Copy1.txt doesn't exist to delete", destination.exists() );
104         FileUtil.forceDelete( destination );
105         assertTrue( "Check No Exist", !destination.exists() );
106     }
107
108     public void testForceDeleteAFile2()
109         throws Exception JavaDoc
110     {
111         final File destination = new File( m_testDirectory, "copy2.txt" );
112         destination.createNewFile();
113         assertTrue( "Copy2.txt doesn't exist to delete", destination.exists() );
114         FileUtil.forceDelete( destination );
115         assertTrue( "Check No Exist", !destination.exists() );
116     }
117
118     public void testCopyFile1ToDir()
119         throws Exception JavaDoc
120     {
121         final File directory = new File( m_testDirectory, "subdir" );
122         if( !directory.exists() ) directory.mkdirs();
123         final File destination = new File( directory, "file1-test.txt" );
124         FileUtil.copyFileToDirectory( m_testFile1, directory );
125         assertTrue( "Check Exist", destination.exists() );
126         assertTrue( "Check Full copy", destination.length() == FILE1_SIZE );
127     }
128
129     public void testCopyFile2ToDir()
130         throws Exception JavaDoc
131     {
132         final File directory = new File( m_testDirectory, "subdir" );
133         if( !directory.exists() ) directory.mkdirs();
134         final File destination = new File( directory, "file2-test.txt" );
135         FileUtil.copyFileToDirectory( m_testFile2, directory );
136         assertTrue( "Check Exist", destination.exists() );
137         assertTrue( "Check Full copy", destination.length() == FILE2_SIZE );
138     }
139
140     public void testForceDeleteDir()
141         throws Exception JavaDoc
142     {
143         FileUtil.forceDelete( m_testDirectory.getParentFile() );
144         assertTrue( "Check No Exist", !m_testDirectory.getParentFile().exists() );
145     }
146
147     public void testResolveFileDotDot()
148         throws Exception JavaDoc
149     {
150         final File file = FileUtil.resolveFile( m_testDirectory, ".." );
151         assertEquals( "Check .. operator", file, m_testDirectory.getParentFile() );
152     }
153
154     public void testResolveFileDot()
155         throws Exception JavaDoc
156     {
157         final File file = FileUtil.resolveFile( m_testDirectory, "." );
158         assertEquals( "Check . operator", file, m_testDirectory );
159     }
160
161     public void testNormalize()
162         throws Exception JavaDoc
163     {
164         final String JavaDoc[] src =
165         {
166             "", "/", "///", "/foo", "/foo//", "/./", "/foo/./", "/foo/./bar",
167             "/foo/../bar", "/foo/../bar/../baz", "/foo/bar/../../baz", "/././",
168             "/foo/./../bar", "/foo/.././bar/", "//foo//./bar", "/../",
169             "/foo/../../"
170         };
171
172         final String JavaDoc[] dest =
173         {
174             "", "/", "/", "/foo", "/foo/", "/", "/foo/", "/foo/bar", "/bar",
175             "/baz", "/baz", "/", "/bar", "/bar/", "/foo/bar", null, null
176         };
177
178         assertEquals( "Oops, test writer goofed", src.length, dest.length );
179
180         for( int i = 0; i < src.length; i++ )
181         {
182             assertEquals( "Check if '" + src[ i ] + "' normalized to '" + dest[ i ] + "'",
183                           dest[ i ], FileUtil.normalize( src[i] ) );
184         }
185     }
186 }
187
Popular Tags