KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.junit.NbTestSuite;
28 import org.openide.filesystems.FileAttributeEvent;
29 import org.openide.filesystems.FileChangeListener;
30 import org.openide.filesystems.FileEvent;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileRenameEvent;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.filesystems.MultiFileSystem;
35 /** Test layer cache manager.
36  * @author Jesse Glick
37  * @see "#20628"
38  */

39 public class BinaryCacheManagerTest extends CacheManagerTestBaseHid
40 implements CacheManagerTestBaseHid.ManagerFactory {
41     
42     public BinaryCacheManagerTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     public static void main(String JavaDoc[] args) {
47         if (System.getProperty("nbjunit.workdir") == null) {
48             // Hope java.io.tmpdir is set...
49
System.setProperty("nbjunit.workdir", System.getProperty("java.io.tmpdir"));
50         }
51         System.setProperty("org.openide.util.Lookup", "-");
52         System.setProperty("org.netbeans.core.projects.cache", "0");
53         TestRunner.run(new NbTestSuite(BinaryCacheManagerTest.class));
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         clearWorkDir();
59     }
60
61     //
62
// Manager factory methods
63
//
64
public LayerCacheManager createManager() throws Exception JavaDoc {
65         return new BinaryCacheManager(getWorkDir());
66     }
67
68     public boolean supportsTimestamps () {
69         return true;
70     }
71     
72     
73     //
74
// new test methods
75
//
76

77     public void testFastReplacement() throws Exception JavaDoc {
78         clearWorkDir();
79         LayerCacheManager m = new BinaryCacheManager(getWorkDir());
80         assertFalse(m.cacheExists());
81         // layer2.xml should override layer1.xml where necessary:
82
List JavaDoc urls = new ArrayList JavaDoc(Arrays.asList(new URL JavaDoc[] {
83             BinaryCacheManagerTest.class.getResource("data/layer2.xml"),
84             BinaryCacheManagerTest.class.getResource("data/layer1.xml"),
85         }));
86         FileSystem f = m.store(urls);
87         assertTrue(m.cacheExists());
88         FixedFileSystem base = new FixedFileSystem("ffs", "FFS");
89         base.add("baz/thongy", new FixedFileSystem.Instance(false, null, null, null, (URL JavaDoc)null));
90         final MFS mfs = new MFS(new FileSystem[] {base, f});
91         FileObject baz = mfs.findResource("baz");
92         assertNotNull(baz);
93         assertEquals(2, baz.getChildren().length);
94         FileObject thingy = mfs.findResource("baz/thingy");
95         assertNotNull(thingy);
96         L JavaDoc l = new L JavaDoc();
97         baz.addFileChangeListener(l);
98         //L l2 = new L();mfs.addFileChangeListener(l2);
99
urls.remove(0);
100         f = m.store(urls);
101         final FileSystem[] fss = new FileSystem[] {base, f};
102         mfs.runAtomicAction(new FileSystem.AtomicAction() {
103             public void run() {
104                 mfs._setDelegates(fss);
105             }
106         });
107         assertEquals(2, baz.getChildren().length);
108         assertTrue(thingy.isValid());
109         assertEquals(0, l.ac);
110         assertEquals(0, l.c);
111         assertEquals(0, l.dc);
112         assertEquals(0, l.d);
113         assertEquals(0, l.fc);
114         assertEquals(0, l.r);
115         urls.remove(0);
116         f = m.store(urls);
117         final FileSystem[] fss2 = new FileSystem[] {base, f};
118         mfs.runAtomicAction(new FileSystem.AtomicAction() {
119             public void run() {
120                 mfs._setDelegates(fss2);
121             }
122         });
123         assertEquals(1, baz.getChildren().length);
124         assertFalse(thingy.isValid());
125         assertEquals(0, l.ac);
126         assertEquals(0, l.c);
127         assertEquals(0, l.dc);
128         assertEquals(1, l.d);
129         assertEquals(0, l.fc);
130         assertEquals(0, l.r);
131     }
132     
133     // Make setDelegates public:
134
private static final class MFS extends MultiFileSystem {
135         public MFS(FileSystem[] fss) {
136             super(fss);
137         }
138         public void _setDelegates(FileSystem[] fss) {
139             setDelegates(fss);
140         }
141     }
142         
143     private static final class L implements FileChangeListener {
144         public int ac = 0, c = 0, dc = 0, d = 0, fc = 0, r = 0;
145         public void fileAttributeChanged(FileAttributeEvent fe) {
146             System.err.println("ac: " + fe.getFile().getPath());
147             ac++;
148         }
149         public void fileChanged(FileEvent fe) {
150             System.err.println("c: " + fe.getFile().getPath());
151             c++;
152         }
153         public void fileDataCreated(FileEvent fe) {
154             System.err.println("dc: " + fe.getFile().getPath());
155             dc++;
156         }
157         public void fileDeleted(FileEvent fe) {
158             System.err.println("d: " + fe.getFile().getPath());
159             d++;
160         }
161         public void fileFolderCreated(FileEvent fe) {
162             System.err.println("fc: " + fe.getFile().getPath());
163             fc++;
164         }
165         public void fileRenamed(FileRenameEvent fe) {
166             System.err.println("r: " + fe.getFile().getPath());
167             r++;
168         }
169     }
170     
171 }
172
Popular Tags