KickJava   Java API By Example, From Geeks To Geeks.

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


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.FilterOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import org.netbeans.junit.*;
27
28 /**
29  * @author Radek Matous
30  */

31 public class FileObject75826Test extends NbTestCase {
32     /**
33      * filesystem containing created instances
34      */

35     private LocalFileSystem lfs;
36     private FileObject testFo;
37
38     public FileObject75826Test(String JavaDoc name) {
39         super(name);
40     }
41
42     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
43         junit.textui.TestRunner.run(new NbTestSuite(FileObject75826Test.class));
44     }
45
46     /**
47      * Setups variables.
48      */

49     protected void setUp() throws Exception JavaDoc {
50
51         TestUtilHid.destroyLocalFileSystem(getName());
52         lfs = (LocalFileSystem)TestUtilHid.createLocalFileSystem(getName(), new String JavaDoc[]{getName() });
53         lfs = new TestFileSystem(lfs, getName());
54         testFo = lfs.findResource(getName());
55         assertNotNull(testFo);
56     }
57
58     public void testOutputStreamFiresIOException() throws IOException JavaDoc {
59         OutputStream JavaDoc os = null;
60         FileLock lock = null;
61         try {
62             os = testFo.getOutputStream();
63             fail();
64         } catch (IOException JavaDoc ex) {}
65         try {
66             lock = testFo.lock();
67             assertNotNull(lock);
68             assertTrue(lock.isValid());
69         } finally {
70             if (lock != null && lock.isValid()) {
71                 lock.releaseLock();
72             }
73         }
74     }
75
76     public void testCloseStreamFiresIOException() throws IOException JavaDoc {
77         FileLock lock = null;
78         OutputStream JavaDoc os = testFo.getOutputStream();
79         try {
80             os.close();
81             fail();
82         } catch (IOException JavaDoc ex) {}
83         try {
84             lock = testFo.lock();
85             assertNotNull(lock);
86             assertTrue(lock.isValid());
87         } finally {
88             if (lock != null && lock.isValid()) {
89                 lock.releaseLock();
90             }
91         }
92     }
93
94     private static final class TestFileSystem extends LocalFileSystem {
95         TestFileSystem(LocalFileSystem lfs, String JavaDoc testName) throws Exception JavaDoc {
96             super();
97             if ("testOutputStreamFiresIOException".equals(testName)) {
98                 this.info = new LocalFileSystem.Impl(this) {
99                     public OutputStream JavaDoc outputStream(String JavaDoc name) throws java.io.IOException JavaDoc {
100                         throw new IOException JavaDoc();
101                     }
102                 };
103             } else if ("testCloseStreamFiresIOException".equals(testName)) {
104                 this.info = new LocalFileSystem.Impl(this) {
105                     public OutputStream JavaDoc outputStream(String JavaDoc name) throws java.io.IOException JavaDoc {
106                         return new FilterOutputStream JavaDoc(super.outputStream(name)) {
107                             public void close() throws IOException JavaDoc {
108                                 throw new IOException JavaDoc();
109                             }
110                         };
111                     }
112                 };
113             }
114             setRootDirectory(lfs.getRootDirectory());
115         }
116     }
117 }
Popular Tags