KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > AttributeChangeIsNotifiedTest


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.startup.layers;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.core.startup.layers.ModuleLayeredFileSystem;
28 import org.netbeans.core.startup.layers.SystemFileSystem;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.filesystems.FileAttributeEvent;
31 import org.openide.filesystems.FileChangeAdapter;
32 import org.openide.filesystems.FileEvent;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileRenameEvent;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.Repository;
37
38 /**
39  *
40  * @author Jaroslav Tulach
41  */

42 public class AttributeChangeIsNotifiedTest extends NbTestCase {
43     SystemFileSystem sfs;
44     
45     public AttributeChangeIsNotifiedTest(String JavaDoc testName) {
46         super(testName);
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         clearWorkDir();
51         
52         File JavaDoc u = new File JavaDoc(getWorkDir(), "userdir");
53         File JavaDoc uc = new File JavaDoc(u, "config");
54         uc.mkdirs();
55         System.setProperty("netbeans.user", u.toString());
56         File JavaDoc h = new File JavaDoc(getWorkDir(), "nb/installdir");
57         new File JavaDoc(h, "config").mkdirs();
58         System.setProperty("netbeans.home", h.toString());
59         
60         
61         sfs = (SystemFileSystem)Repository.getDefault().getDefaultFileSystem();
62         
63         File JavaDoc f = FileUtil.toFile(sfs.getRoot());
64         
65         assertEquals("Root is really on disk", uc, f);
66         
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70     }
71     
72     protected ModuleLayeredFileSystem getTheLayer(SystemFileSystem sfs) {
73         return sfs.getUserLayer();
74     }
75
76     public void testChangeOfAnAttributeInLayerIsFired() throws Exception JavaDoc {
77         doChangeOfAnAttributeInLayerIsFired(getTheLayer(sfs));
78     }
79     
80     private void doChangeOfAnAttributeInLayerIsFired(ModuleLayeredFileSystem fs) throws Exception JavaDoc {
81         File JavaDoc f1 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.List");
82         File JavaDoc f2 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.Button");
83         File JavaDoc f3 = changeOfAnAttributeInLayerIsFiredgenerateLayer("NoChange", "nochange");
84
85         {
86             ArrayList JavaDoc list = new ArrayList JavaDoc();
87             list.add(f1.toURL());
88             list.add(f3.toURL());
89             fs.setURLs (list);
90         }
91         
92         FileObject file = sfs.findResource("Folder/empty.xml");
93         assertNotNull("File found in layer", file);
94         
95         FSListener l = new FSListener();
96         file.addFileChangeListener(l);
97         
98         FileObject nochange = sfs.findResource("NoChange/empty.xml");
99         assertNotNull("File found in layer", nochange);
100         FSListener no = new FSListener();
101         nochange.addFileChangeListener(no);
102         
103         assertAttr("The first value is list", file, "value", "java.awt.List");
104         assertAttr("Imutable value is nochange", nochange, "value", "nochange");
105         
106         {
107             ArrayList JavaDoc list = new ArrayList JavaDoc();
108             list.add(f2.toURL());
109             list.add(f3.toURL());
110             fs.setURLs (list);
111         }
112         String JavaDoc v2 = (String JavaDoc) file.getAttribute("value");
113         assertEquals("The second value is button", "java.awt.Button", v2);
114         
115         assertEquals("One change: " + l.events, 1, l.events.size());
116         
117         if (!(l.events.get(0) instanceof FileAttributeEvent)) {
118             fail("Wrong event: " + l.events);
119         }
120         
121         assertAttr("Imutable value is still nochange", nochange, "value", "nochange");
122         assertEquals("No change in this attribute: " + no.events, 0, no.events.size());
123     }
124     
125     private static void assertAttr(String JavaDoc msg, FileObject fo, String JavaDoc attr, String JavaDoc value) throws IOException JavaDoc {
126         Object JavaDoc v = fo.getAttribute(attr);
127         assertEquals(msg + "[" + fo + "]", value, v);
128     }
129
130     int cnt;
131     private File JavaDoc changeOfAnAttributeInLayerIsFiredgenerateLayer(String JavaDoc folderName, String JavaDoc string) throws IOException JavaDoc {
132         File JavaDoc f = new File JavaDoc(getWorkDir(), "layer" + (cnt++) + ".xml");
133         FileWriter JavaDoc w = new FileWriter JavaDoc(f);
134         w.write(
135             "<filesystem>" +
136             "<folder name='" + folderName + "'>" +
137             " <file name='empty.xml' >" +
138             " <attr name='value' stringvalue='" + string + "' />" +
139             " </file>" +
140             "</folder>" +
141             "</filesystem>"
142         );
143         w.close();
144         return f;
145     }
146     
147     private static class FSListener extends FileChangeAdapter {
148         public List JavaDoc events = new ArrayList JavaDoc();
149         public List JavaDoc change = new ArrayList JavaDoc();
150         
151         
152         public void fileRenamed(FileRenameEvent fe) {
153             events.add(fe);
154         }
155
156         public void fileAttributeChanged(FileAttributeEvent fe) {
157             events.add(fe);
158         }
159
160         public void fileFolderCreated(FileEvent fe) {
161             events.add(fe);
162         }
163
164         public void fileDeleted(FileEvent fe) {
165             events.add(fe);
166         }
167
168         public void fileDataCreated(FileEvent fe) {
169             events.add(fe);
170         }
171
172         public void fileChanged(FileEvent fe) {
173             change.add(fe);
174         }
175         
176     }
177 }
178
Popular Tags