KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FileOps


1 /* Test the following file operations:
2  *
3  * canRead
4  * false - a target that is already open by another process
5  * false - the target does not exist
6  * true - the file exists and there are no sharing issues
7  * canWrite
8  * true - the file exists and there are no sharing issues
9  * false - the file is marked read-only
10  * false - the file does not exist
11  * delete
12  * true - the file existed and was succcessfully deleted
13  * false - the target did not exist
14  * false - the target is a share, server, workgroup or similar
15  * false - the target or file under the target directory failed was read-only
16  * exists
17  * true - the target, share, IPC share, named pipe, server, or workgroup exists
18  * false - the opposite of the above
19  * isDirectory
20  * true - the target is a workgroup, server, share, or directory
21  * false - the target is not one of the above or does not exist
22  * isFile
23  * direct opposite of isDirectory
24  * isHidden
25  * true - target is share ending in $ or marked as hidden
26  * length
27  * the file was created to be no larger than ~2G and reports back the size specified
28  * mkdir
29  * true - a directory was created successfuly
30  * false - the directory could not be created
31  * renameTo
32  * true - the target was renamed
33  */

34
35 import jcifs.smb.*;
36 import java.io.IOException JavaDoc;
37 import java.util.Date JavaDoc;
38
39 public class FileOps {
40
41     static final int ATTR_ALL = SmbFile.ATTR_ARCHIVE | SmbFile.ATTR_HIDDEN | SmbFile.ATTR_READONLY | SmbFile.ATTR_SYSTEM;
42
43     public static void main( String JavaDoc argv[] ) throws Exception JavaDoc {
44
45         if( argv.length != 1 ) {
46             System.out.println( "Must provide an SMB URL of a remote location on which tests will be conducted." );
47             System.exit( 1 );
48         }
49
50         SmbFile s = new SmbFile( argv[0] );
51         SmbFile d = new SmbFile( s, "JcifsTestOpsDir/" );
52
53     // delete - Delete the directory if it exists
54

55         try {
56             d.delete();
57         } catch( SmbException se ) {
58             System.out.println( "okay - delete " + d + " failed: " + se.getMessage() );
59         }
60         System.out.println( "okay - delete " + d + " successful" );
61
62     // exists - Test the directory that should not exist
63

64         if( d.exists() ) {
65             System.out.println( "fail - " + d + " still exists" );
66             System.exit( 1 );
67         } else {
68             System.out.println( "okay - " + d + " does not exist" );
69         }
70
71     // mkdir - Create the directory
72

73         d.mkdir();
74         System.out.println( "okay - mkdir " + d + " successful" );
75
76     // exist - Test the directory which should exist now
77

78         if( d.exists() ) {
79             System.out.println( "okay - " + d + " exists" );
80         } else {
81             System.out.println( "fail - " + d + " was not successfuly created" );
82             System.exit( 1 );
83         }
84
85     // mkdir - Try to create a directory even though it already exists
86

87         try {
88             d.mkdir();
89             System.out.println( "fail - mkdir " + d + " successful" );
90             System.exit( 1 );
91         } catch( SmbException se ) {
92             System.out.println( "okay - mkdir " + d + " failed: " + se.getMessage() );
93         }
94
95     // Create a file to test against
96

97     SmbFile f = null;
98     try {
99         f = new SmbFile( d, "foo.txt" );
100         SmbFileOutputStream o = new SmbFileOutputStream( f );
101         o.write( "The Common Internet File System (CIFS) is the de-facto file sharing protocol on the Microsoft Windows platform. It is the underlying networking protocol used when accessing shares with Windows Explorer, the Network Neighborhood, via a Map Network Drive... dialog, the C:\\> net use * \\\\server\\share commands, or smbclient on UNIX, smbfs on Linux, and elsewhere.\r\n".getBytes() );
102         o.close();
103     } catch( IOException JavaDoc ioe ) {
104         System.out.println( "fail - could not create file " + d + "foo.txt: " + ioe.getMessage() );
105     }
106     System.out.println( "okay - created file " + d + "foo.txt" );
107
108     // canRead - Test to see if the new file can be read
109

110         if( f.canRead() ) {
111             System.out.println( "okay - canRead " + f + " successful" );
112         } else {
113             System.out.println( "fail - canRead " + f + " failed" );
114             System.exit( 1 );
115         }
116
117     // canWrite, getAttributes - Test the file for writing
118

119         if( f.canWrite() && (f.getAttributes() & SmbFile.ATTR_READONLY) == 0 ) {
120             System.out.println( "okay - canWrite " + f + " successful" );
121         } else {
122             System.out.println( "fail - canWrite " + f + " failed" );
123             System.exit( 1 );
124         }
125
126     // setReadOnly
127

128         try {
129             f.setReadOnly();
130             System.out.println( "okay - setReadOnly " + f + " successful" );
131         } catch( SmbException se ) {
132             System.out.println( "fail - setReadOnly " + f + " failed: " + se.getMessage() );
133         }
134
135     // canWrite - Test the file for writing
136

137         if( f.canWrite() ) {
138             System.out.println( "fail - canWrite " + f + " returned true but it should have been marked read-only ... continuing on" );
139         } else {
140             System.out.println( "okay - canWrite " + f + " failed" );
141         }
142
143     // Try to open the file for writing
144

145         try {
146             SmbFileOutputStream w = new SmbFileOutputStream( f );
147             w.close();
148             System.out.println( "fail - successfuly opened " + f + " for writing even though it should be marked read-only ... continuing on" );
149         } catch( IOException JavaDoc ioe ) {
150             System.out.println( "okay - correctly failed to open " + f + " for writing: " + ioe.getMessage() );
151         }
152
153     // renameTo - rename the file to bar.txt
154

155         SmbFile b = new SmbFile( d, "bar.txt" );
156
157         try {
158             f.renameTo( b );
159             System.out.println( "okay - renameTo " + f + " to " + b + " successful even with read-only" );
160             try {
161                 b.renameTo(f);
162             } catch( SmbException se ) {
163                 System.out.println( "fail - but failed to rename file back to original!" );
164                 throw se;
165             }
166         } catch( SmbException se ) {
167             System.out.println( "fail - renameTo " + f + " should have been successful even though the file is marked read-only: " + se.getMessage() );
168         }
169
170     // setAttributes
171

172         try {
173             f.setAttributes( 0xFFFF );
174             System.out.println( "okay - setAttributes " + f + " successful" );
175         } catch( SmbException se ) {
176             System.out.println( "fail - setAttributes " + f + " failed: " + se.getMessage() );
177         }
178
179     // getAttributes
180

181         int attr;
182
183         if((( attr = f.getAttributes() ) & ATTR_ALL ) == ATTR_ALL ) {
184             System.out.println( "okay - getAttributes " + f + " successful" );
185         } else {
186             System.out.println( "fail - getAttributes " + f + " failed: 0x" + jcifs.util.Hexdump.toHexString( attr, 4 ));
187             System.exit( 1 );
188         }
189
190     // isHidden - Test to see if the file is hidden
191

192         if( f.isHidden() ) {
193             System.out.println( "okay - isHidden " + f + " is hidden" );
194         } else {
195             System.out.println( "fail - isHidden " + f + " is not hidden but it should be ... continuing on" );
196         }
197
198     // canRead - Test canRead again with both hidden and read-only on
199

200         if( f.canRead() ) {
201             System.out.println( "okay - canRead " + f + " was successful with read-only and hidden both on" );
202         } else {
203             System.out.println( "fail - canRead " + f + " failed with read-only and hidden both on" );
204         }
205
206     // canWrite - Test the file for writing again with read-only and hidden
207

208         if( f.canWrite() ) {
209             System.out.println( "fail - canWrite " + f + " was successful even though read-only is set ... continuing on" );
210         } else {
211             System.out.println( "okay - canWrite " + f + " failed as it should being that read-only is set" );
212         }
213
214     // isDirectory - Test file as a directory
215

216         if( f.isDirectory() ) {
217             System.out.println( "fail - isDirectory " + f + " returned true but it is NOT a directory" );
218         } else {
219             System.out.println( "okay - isDirectory " + f + " is not a directory" );
220         }
221
222     // isDirectory - Test directory as a directory
223

224         if( d.isDirectory() ) {
225             System.out.println( "okay - isDirectory " + d + " is a directory" );
226         } else {
227             System.out.println( "fail - isDirectory " + d + " returned false but it really is a directory" );
228         }
229
230     // isDirectory - Test directory that does not exist
231

232         b = new SmbFile( d, "bogus" );
233
234         if( b.isDirectory() ) {
235             System.out.println( "fail - isDirectory " + b + " returned true but it does not exist" );
236         } else {
237             System.out.println( "okay - isDirectory " + b + " does not exist" );
238         }
239
240     // isFile - Test file as a file
241

242         if( f.isFile() ) {
243             System.out.println( "okay - isFile " + f + " is a file" );
244         } else {
245             System.out.println( "fail - isFile " + f + " return false but it is NOT a file" );
246         }
247
248     // isFile - Test directory as a file
249

250         if( d.isFile() ) {
251             System.out.println( "fail - isFile " + d + " returned true but it is NOT a file" );
252         } else {
253             System.out.println( "okay - isFile " + d + " is not a file" );
254         }
255
256     // length - Check to ensure that the length of the file is correct
257

258         if( f.length() == 363 ) {
259             System.out.println( "okay - length " + f + " is correct" );
260         } else {
261             System.out.println( "fail - length " + f + " is wrong: " + f.length() );
262         }
263
264     // setReadWrite
265

266         try {
267             f.setReadWrite();
268             System.out.println( "okay - setReadWrite " + f + " successful" );
269         } catch( SmbException se ) {
270             System.out.println( "fail - setReadWrite " + f + " failed: " + se.getMessage() );
271         }
272
273     // setLastModified
274

275         long t = (new Date JavaDoc()).getTime() - 1000 * 60;
276
277         try {
278             f.setLastModified( t );
279             System.out.println( "okay - setLastModified " + f + " successful" );
280         } catch( SmbException se ) {
281             System.out.println( "fail - setLastModified " + f + " failed: " + se.getMessage() );
282         }
283
284     // lastModified
285

286         if( f.lastModified() == t ) {
287             System.out.println( "okay - lastModified " + f + " is correct" );
288         } else {
289             System.out.println( "fail - lastModified " + f + " is wrong: " + f.lastModified() + " vs " + t );
290         }
291
292     // setCreateTime
293

294         try {
295             f.setCreateTime( t );
296             System.out.println( "okay - setCreateTime " + f + " successful" );
297         } catch( SmbException se ) {
298             System.out.println( "fail - setCreateTime " + f + " failed: " + se.getMessage() );
299         }
300
301     // createTime
302

303         if( f.createTime() == t ) {
304             System.out.println( "okay - createTime " + f + " is correct" );
305         } else {
306             System.out.println( "fail - createTime " + f + " is wrong: " + f.createTime() + " vs " + t );
307         }
308
309     // createNewFile
310

311     // delete - See if we can delete the file even though it's read-only
312

313         try {
314             f.delete();
315             System.out.println( "okay - delete " + f + " successful even though the file was read-only" );
316         } catch( SmbException se ) {
317             System.out.println( "fail - delete " + f + " should have turned off the read-only attribute to deleted the file: " + se.getMessage() );
318         }
319
320         SmbFile r = new SmbFile( d.getParent(), "JcifsDeleteMe/" );
321
322     // Must delete any left over directory from a previous run
323

324         try {
325             r.delete();
326             System.out.println( "okay - delete " + r + " successful" );
327         } catch( SmbException se ) {
328             System.out.println( "okay - delete " + r + " probably wasn't there: " + se.getMessage() );
329         }
330
331     // renameTo - Rename the whole directory to JcifsDeleteMe
332

333         try {
334             d.renameTo( r );
335             System.out.println( "okay - renameTo " + d + " successful even though it is a directory" );
336         } catch( SmbException se ) {
337             System.out.println( "fail - renameTo " + d + " failed: " + se.getMessage() );
338         }
339
340     // delete - Now delete the whole workspace
341

342         try {
343             r.delete();
344             System.out.println( "okay - delete " + r + " successful" );
345         } catch( SmbException se ) {
346             System.out.println( "fail - delete " + r + " failed: " + se.getMessage() );
347         }
348     }
349 }
350
351
Popular Tags