KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > IsArchiveFileTest


1 /*
2    * The contents of this file are subject to the terms of the Common Development
3    * and Distribution License (the License). You may not use this file except in
4    * compliance with the License.
5    *
6    * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7    * or http://www.netbeans.org/cddl.txt.
8    *
9    * When distributing Covered Code, include this CDDL Header Notice in each file
10    * and include the License file at http://www.netbeans.org/cddl.txt.
11    * If applicable, add the following below the CDDL Header, with the fields
12    * enclosed by brackets [] replaced by your own identifying information:
13    * "Portions Copyrighted [year] [name of copyright owner]"
14    *
15    * The Original Software is NetBeans. The Initial Developer of the Original
16    * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17    * Microsystems, Inc. All Rights Reserved.
18    */

19
20 package org.openide.filesystems;
21
22
23 import java.io.OutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.jar.JarOutputStream JavaDoc;
26 import java.util.zip.ZipEntry JavaDoc;
27 import org.netbeans.junit.*;
28
29 /**
30  *Test behavior of FileUtil.isArchiveFile
31  *
32  * @author Tomas Zezula
33  */

34 public class IsArchiveFileTest extends NbTestCase {
35     /**
36      * filesystem containing created instances
37      */

38     private LocalFileSystem lfs;
39     private FileObject directory;
40     private FileObject brokenArchive;
41     private FileObject archive;
42     private FileObject file;
43     private FileObject emptyFile;
44
45     /**
46      * Creates new test
47      */

48     public IsArchiveFileTest(String JavaDoc name) {
49         super(name);
50     }
51
52     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
53         junit.textui.TestRunner.run(new NbTestSuite(IsArchiveFileTest.class));
54     }
55
56     /**
57      * Setups variables.
58      */

59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         clearWorkDir();
62         lfs = new LocalFileSystem ();
63         lfs.setRootDirectory(this.getWorkDir());
64         Repository.getDefault().addFileSystem(lfs);
65         FileObject root = lfs.getRoot();
66         directory = root.createFolder("dir");
67         brokenArchive = root.createData ("brokenArchive.jar");
68         archive = root.createData("archive.jar");
69         FileLock lock = archive.lock();
70         try {
71             JarOutputStream JavaDoc out = new JarOutputStream JavaDoc (archive.getOutputStream(lock));
72             try {
73                 out.putNextEntry(new ZipEntry JavaDoc("foo"));
74                 out.closeEntry();
75             } finally {
76                 out.close();
77             }
78         } finally {
79             lock.releaseLock();
80         }
81         file = root.createData ("file.txt");
82         lock = file.lock ();
83         try {
84             OutputStream JavaDoc out = file.getOutputStream(lock);
85             try {
86                 out.write ("Test file".getBytes());
87             } finally {
88                 out.close();
89             }
90         } finally {
91             lock.releaseLock();
92         }
93         emptyFile = root.createData("emptyFile.txt");
94     }
95     
96     protected void tearDown() throws Exception JavaDoc {
97         Repository.getDefault().removeFileSystem(lfs);
98         super.tearDown();
99     }
100     
101
102     public void testIsArchivFile () throws Exception JavaDoc {
103         assertFalse (FileUtil.isArchiveFile(directory));
104         assertFalse (FileUtil.isArchiveFile(brokenArchive));
105         assertTrue (FileUtil.isArchiveFile(archive));
106         assertFalse (FileUtil.isArchiveFile(file));
107         assertFalse (FileUtil.isArchiveFile(emptyFile));
108         
109         assertFalse (FileUtil.isArchiveFile(new URL JavaDoc("jar:file:/foo.jar!/")));
110         assertFalse (FileUtil.isArchiveFile(new URL JavaDoc("file:/foo/")));
111         assertTrue (FileUtil.isArchiveFile(new URL JavaDoc("file:/foo.jar")));
112     }
113
114    
115 }
116   
117   
118   
119
Popular Tags