KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.beans.VetoableChangeListener JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import org.openide.NotifyDescriptor;
27 import org.openide.cookies.EditorCookie;
28 import org.openide.filesystems.*;
29 import org.openide.nodes.Node;
30 import org.openide.text.DataEditorSupport;
31 import org.openide.util.Lookup;
32 import java.io.IOException JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import javax.swing.text.Document JavaDoc;
35 import org.netbeans.junit.*;
36 import org.openide.cookies.OpenCookie;
37
38
39 /** Simulates the deadlock from issue 60917
40  * @author Jaroslav Tulach
41  */

42 public class RefusesInvalidationTest extends NbTestCase {
43     
44     public RefusesInvalidationTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     
49     protected void setUp() throws Exception JavaDoc {
50         System.setProperty("org.openide.util.Lookup", RefusesInvalidationTest.class.getName() + "$Lkp");
51         
52         super.setUp();
53         
54         Lookup l = Lookup.getDefault();
55         if (!(l instanceof Lkp)) {
56             fail("Wrong lookup: " + l);
57         }
58         
59         clearWorkDir();
60     }
61     
62
63     
64     public void testWhatHappensWhenALoaderBecomesInvalidAndFileIsOpened() throws Exception JavaDoc {
65         final ForgetableLoader l = (ForgetableLoader)DataLoader.getLoader(ForgetableLoader.class);
66         FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), new String JavaDoc[] {
67             "folder/f.keep",
68             "folder/f.forget",
69         });
70
71         FileObject fo = lfs.findResource("folder");
72         final DataFolder f = DataFolder.findFolder(fo);
73
74         FileObject primary = lfs.findResource("folder/f.keep");
75
76         final DataObject our = DataObject.find(primary);
77         assertEquals("The right loader", l, our.getLoader());
78
79         class AddAndRemoveAFile implements FileSystem.AtomicAction {
80             DataObject[] all;
81             
82             public void run () throws IOException JavaDoc {
83                 FileObject[] two = (FileObject[])our.files().toArray(new FileObject[0]);
84                 assertEquals("Two", 2, two.length);
85                 
86                 // or secondary
87
two[1].delete();
88                 
89
90                 l.filesInside = new HashSet JavaDoc();
91                 all = f.getChildren();
92                 if (!l.filesInside.contains(two[0])) {
93                     fail("We should query our secondary file: " + l.filesInside);
94                 }
95             }
96         }
97         
98         AddAndRemoveAFile addRemove = new AddAndRemoveAFile();
99         f.getPrimaryFile().getFileSystem().runAtomicAction(addRemove);
100
101         FileObject[] children = f.getPrimaryFile().getChildren();
102         assertEquals("One child", 1, children.length);
103         DataObject obj = DataObject.find(children[0]);
104         assertEquals("Object found", children[0], obj.getPrimaryFile());
105         assertEquals("Object is the same as it tries to prevent to invalidate itself", our, obj);
106         
107         assertNotNull("Children computed", addRemove.all);
108         assertEquals("Three of them: " + Arrays.asList(addRemove.all), 1, addRemove.all.length);
109         assertEquals("Object is the same as it tries to prevent to invalidate itself", our, addRemove.all[0]);
110         
111         DataObject[] all = f.getChildren();
112         assertNotNull("Children computed", all);
113         assertEquals("Three of them: " + Arrays.asList(all), 1, all.length);
114         assertEquals("Object is the same as it tries to prevent to invalidate itself", our, all[0]);
115     }
116     
117     public static final class ForgetableLoader extends MultiFileLoader
118     implements VetoableChangeListener JavaDoc {
119         PropertyChangeEvent JavaDoc lastEvent;
120         HashSet JavaDoc filesInside;
121         
122         public ForgetableLoader () {
123             super(MultiDataObject.class);
124         }
125         protected String JavaDoc displayName() {
126             return "ForgetableLoader";
127         }
128         /** Recognizes just two files - .forget and .keep at once.
129          */

130         protected FileObject findPrimaryFile(FileObject fo) {
131             if (filesInside != null) {
132 // System.err.println("file: " + fo);
133
// Thread.dumpStack();
134
filesInside.add(fo);
135             } else {
136                 assertFalse("We cannot be queried from recognizer thread: ", FolderList.isFolderRecognizerThread());
137             }
138             
139             FileObject forget = FileUtil.findBrother(fo, "forget");
140             FileObject keep = FileUtil.findBrother(fo, "keep");
141             if (keep == null || forget == null) {
142                 return null;
143             }
144             return fo == keep || fo == forget ? keep : null;
145         }
146         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
147             MultiDataObject m = new MultiDataObject (primaryFile, this);
148             m.addVetoableChangeListener(this);
149             return m;
150         }
151         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
152             return new FileEntry (obj, primaryFile);
153         }
154         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
155             return new FileEntry(obj, secondaryFile);
156         }
157
158         public void vetoableChange(PropertyChangeEvent JavaDoc evt) throws PropertyVetoException JavaDoc {
159             this.lastEvent = evt;
160             throw new PropertyVetoException JavaDoc("Cannot change isValid", evt);
161         }
162     }
163     
164     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
165         public Lkp() {
166             this(new org.openide.util.lookup.InstanceContent());
167         }
168         
169         private Lkp(org.openide.util.lookup.InstanceContent ic) {
170             super(ic);
171             ic.add(new Pool ());
172         }
173     }
174     
175     private static final class Pool extends DataLoaderPool {
176         protected java.util.Enumeration JavaDoc loaders () {
177             DataLoader extra = DataLoader.getLoader(ForgetableLoader.class);
178             return org.openide.util.Enumerations.singleton (extra);
179         }
180     }
181 }
182
Popular Tags