KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > NullPointer35897Test


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.loaders;
21
22 import org.openide.filesystems.*;
23 import org.openide.loaders.*;
24 import java.beans.*;
25 import java.io.IOException JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.junit.*;
28
29 /*
30  * Tries to reproduce NPE from #35897 issue.
31  */

32 public class NullPointer35897Test extends NbTestCase {
33     private FileSystem lfs;
34     private FileObject file;
35     private L loader;
36     private D obj;
37
38     public NullPointer35897Test (String JavaDoc name) {
39         super (name);
40     }
41
42     protected void setUp () throws Exception JavaDoc {
43         TestUtilHid.destroyLocalFileSystem (getName ());
44         String JavaDoc fsstruct [] = new String JavaDoc [] {
45             "dir/simple.simple"
46         };
47         lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
48         Repository.getDefault ().addFileSystem (lfs);
49
50         file = lfs.findResource (fsstruct[0]);
51         
52         loader = (L)DataLoader.getLoader (L.class);
53         AddLoaderManuallyHid.addRemoveLoader (loader, true);
54     }
55     
56     //Clear all stuff when the test finish
57
protected void tearDown () throws Exception JavaDoc {
58         AddLoaderManuallyHid.addRemoveLoader (loader, false);
59         TestUtilHid.destroyLocalFileSystem (getName ());
60     }
61     
62     public void test35897 () throws Exception JavaDoc {
63         class InitObj implements Runnable JavaDoc {
64             public void run () {
65                 try {
66                     obj = (D)DataObject.find (file);
67                 } catch (DataObjectNotFoundException ex) {
68                     ex.printStackTrace();
69                     fail ("Unexpected exception");
70                 }
71             }
72         }
73         InitObj init = new InitObj ();
74         
75         org.openide.util.RequestProcessor.Task task;
76         synchronized (loader) {
77             task = org.openide.util.RequestProcessor.getDefault ().post (init);
78             loader.wait ();
79         }
80         
81         assertTrue ("The creation of DataObject is blocked in constructor", loader.waitingInConstructor);
82
83         Repository.getDefault ().removeFileSystem (lfs);
84         
85         synchronized (loader) {
86             loader.notifyAll ();
87         }
88         task.waitFinished ();
89         
90         assertNotNull ("The object has been finished", obj);
91     }
92     
93     private static class D extends MultiDataObject {
94         private boolean constructorFinished;
95         
96         public D (FileObject pf, L loader) throws IOException JavaDoc {
97             super(pf, loader);
98
99             synchronized (loader) {
100                 try {
101                     loader.waitingInConstructor = true;
102                     loader.notifyAll ();
103                     loader.wait (2000);
104                 } catch (InterruptedException JavaDoc ex) {
105                     ex.printStackTrace();
106                     fail ("No interruptions please");
107                 } finally {
108                     loader.waitingInConstructor = false;
109                     constructorFinished = true;
110                 }
111             }
112         }
113         
114         
115
116         protected org.openide.nodes.Node createNodeDelegate() {
117             return org.openide.nodes.Node.EMPTY;
118         }
119         
120         public java.util.Set JavaDoc files () {
121             assertTrue ("This can be called only if the constructor is finished", constructorFinished);
122             return super.files ();
123         }
124         
125     }
126
127     private static class L extends MultiFileLoader {
128         public boolean waitingInConstructor;
129         
130         public L () {
131             super(D.class.getName());
132         }
133         protected String JavaDoc displayName() {
134             return "L";
135         }
136
137         protected FileObject findPrimaryFile (FileObject obj) {
138             return obj.hasExt ("simple") ? obj : null;
139         }
140
141         protected MultiDataObject createMultiObject(FileObject pf) throws IOException JavaDoc {
142             return new D(pf, this);
143         }
144
145
146         protected MultiDataObject.Entry createSecondaryEntry (MultiDataObject x, FileObject obj) {
147             throw new IllegalStateException JavaDoc ();
148         }
149
150         protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject x, FileObject obj) {
151             return new org.openide.loaders.FileEntry (x, obj);
152         }
153     }
154
155     
156 }
157
Popular Tags