KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > ExternalModificationTest


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.netbeans.core.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.api.registry.*;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26 import org.netbeans.spi.registry.BasicContext;
27 import org.netbeans.spi.registry.SpiUtils;
28 import org.openide.filesystems.FileLock;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.LocalFileSystem;
31 import org.openide.modules.ModuleInfo;
32 import org.openide.util.Lookup;
33
34 import java.io.OutputStream JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  *
39  * @author David Konecny
40  */

41 public class ExternalModificationTest extends NbTestCase {
42     
43     public ExternalModificationTest(String JavaDoc name) {
44         super (name);
45     }
46     
47     public static void main(String JavaDoc[] args) {
48         TestRunner.run(new NbTestSuite(ExternalModificationTest.class));
49     }
50     
51     protected void setUp () throws Exception JavaDoc {
52         clearWorkDir();
53         Lookup.getDefault().lookup(ModuleInfo.class);
54     }
55     
56     public void testExternalModification() throws Exception JavaDoc {
57         LocalFileSystem lfs = new LocalFileSystem();
58         lfs.setRootDirectory(getWorkDir());
59         FileObject exte = lfs.getRoot().createFolder("exte");
60
61         BasicContext root = new ResettableContextImpl(lfs.getRoot());
62         Context rootContext = SpiUtils.createContext(root);
63         
64         Context ctx = rootContext.getSubcontext("exte");
65
66         FileSystemListener l = new FileSystemListener();
67         rootContext.addContextListener(l);
68         FileSystemListener l2 = new FileSystemListener();
69         ctx.addContextListener(l2);
70         
71         assertTrue("There cannot be any binding yet", ctx.getBindingNames().size() == 0);
72         
73         FileObject fo2 = exte.createData("smth", "instance");
74         // this should create one binding
75
assertEquals("There must be one event fired", 1, l.b.size());
76         assertEquals("There must be one event fired", 1, l.all.size());
77         assertEquals("There must be one event fired", 1, l2.b.size());
78         assertEquals("There must be one event fired", 1, l2.all.size());
79         assertEquals("The event must be BindingEvent.BINDING_ADDED", BindingEvent.BINDING_ADDED, ((BindingEvent)l.b.get(0)).getType());
80         assertEquals("There must be one binding", 1, ctx.getBindingNames().size());
81         l.reset();
82         l2.reset();
83
84         FileLock lock = fo2.lock();
85         OutputStream JavaDoc os = fo2.getOutputStream(lock);
86         // write something
87
os.write(74);
88         os.close();
89         lock.releaseLock();
90         // this will generate another event
91
assertEquals("There must be one event fired", 1, l.b.size());
92         assertEquals("There must be one event fired", 1, l.all.size());
93         assertEquals("There must be one event fired", 1, l2.b.size());
94         assertEquals("There must be one event fired", 1, l2.all.size());
95         assertEquals("The event must be BindingEvent.BINDING_MODIFIED", BindingEvent.BINDING_MODIFIED, ((BindingEvent)l.b.get(0)).getType());
96         assertEquals("There must be one binding", 1, ctx.getBindingNames().size());
97         l.reset();
98         l2.reset();
99
100         fo2.delete();
101         // this should be one binding removed
102
assertEquals("There must be one event fired", 1, l.b.size());
103         assertEquals("There must be one event fired", 1, l.all.size());
104         assertEquals("There must be one event fired", 1, l2.b.size());
105         assertEquals("There must be one event fired", 1, l2.all.size());
106         assertEquals("The event must be BindingEvent.BINDING_REMOVED", BindingEvent.BINDING_REMOVED, ((BindingEvent)l.b.get(0)).getType());
107         assertEquals("There must be zero binding", 0, ctx.getBindingNames().size());
108         l.reset();
109         l2.reset();
110         
111         
112         // create some trash ignored by registry
113
fo2 = exte.createData("thrash", "odpad");
114         // there should be no change:
115
assertEquals("There must be zero event fired", 0, l.b.size());
116         assertEquals("There must be zero event fired", 0, l.all.size());
117         assertEquals("There must be zero event fired", 0, l2.b.size());
118         assertEquals("There must be zero event fired", 0, l2.all.size());
119         assertEquals("There must be zero binding only", 0, ctx.getBindingNames().size());
120         l.reset();
121         l2.reset();
122
123         lock = fo2.lock();
124         os = fo2.getOutputStream(lock);
125         // write something
126
os.write(74);
127         os.close();
128         lock.releaseLock();
129         // there should be no change:
130
assertEquals("There must be zero event fired", 0, l.b.size());
131         assertEquals("There must be zero event fired", 0, l.all.size());
132         assertEquals("There must be zero event fired", 0, l2.b.size());
133         assertEquals("There must be zero event fired", 0, l2.all.size());
134         assertEquals("There must be zero binding only", 0, ctx.getBindingNames().size());
135         l.reset();
136         l2.reset();
137         
138         fo2.delete();
139         // there should be no change:
140
assertEquals("There must be zero event fired", 0, l.b.size());
141         assertEquals("There must be zero event fired", 0, l.all.size());
142         assertEquals("There must be zero event fired", 0, l2.b.size());
143         assertEquals("There must be zero event fired", 0, l2.all.size());
144         assertEquals("There must be zero binding only", 0, ctx.getBindingNames().size());
145         l.reset();
146         l2.reset();
147         
148     }
149     
150     public static class FileSystemListener implements ContextListener {
151
152         ArrayList JavaDoc s;
153         ArrayList JavaDoc a;
154         ArrayList JavaDoc b;
155         ArrayList JavaDoc all;
156         String JavaDoc error = null;
157         
158         public FileSystemListener() {
159             reset();
160         }
161         
162         public void reset() {
163             s = new ArrayList JavaDoc();
164             b = new ArrayList JavaDoc();
165             a = new ArrayList JavaDoc();
166             all = new ArrayList JavaDoc();
167             error = null;
168         }
169         
170         public void subcontextChanged(SubcontextEvent evt) {
171             s.add(evt);
172             all.add(evt);
173         }
174     
175         public void bindingChanged(BindingEvent evt) {
176             b.add(evt);
177             all.add(evt);
178         }
179         
180         public void attributeChanged(AttributeEvent evt) {
181             a.add(evt);
182             all.add(evt);
183         }
184         
185     }
186 }
187
Popular Tags