KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.MultiFileSystem;
33 /** Test layer cache managers generally.
34  * @author Jesse Glick
35  */

36 public abstract class CacheManagerTestBaseHid extends NbTestCase {
37     private long initTime = System.currentTimeMillis ();
38     
39     /**
40      * Called from layer, do not rename!
41      */

42     public static Object JavaDoc method(FileObject fo, String JavaDoc attr) {
43         //System.err.println("CMTBH.m: fo=" + fo.getClass().getName() + "<" + fo.getPath() + ">; attr=" + attr + "; x=" + fo.getAttribute("x"));
44
return String.valueOf(fo.getAttribute("x")) + "/" + attr;
45     }
46     
47     public static Object JavaDoc map1(Map JavaDoc map) {
48         return String.valueOf(map.get("x"));
49     }
50     public static Object JavaDoc map2(Map JavaDoc map, String JavaDoc attr) {
51         return String.valueOf(map.get("x")) + "/" + attr;
52     }
53     
54     protected CacheManagerTestBaseHid(String JavaDoc name) {
55         super(name);
56     }
57     
58     protected static interface ManagerFactory {
59         LayerCacheManager createManager() throws Exception JavaDoc;
60         boolean supportsTimestamps ();
61     }
62     
63     public void testCacheManager() throws Exception JavaDoc {
64         ManagerFactory mf = (ManagerFactory)this;
65         
66         clearWorkDir();
67         LayerCacheManager m = mf.createManager();
68         assertFalse(m.cacheExists());
69         // layer2.xml should override layer1.xml where necessary:
70
List JavaDoc urls = Arrays.asList(new URL JavaDoc[] {
71             CacheManagerTestBaseHid.class.getResource("data/layer2.xml"),
72             CacheManagerTestBaseHid.class.getResource("data/layer1.xml"),
73         });
74         FileSystem f;
75         if (m.supportsLoad()) {
76             f = m.createEmptyFileSystem();
77             assertEquals(Collections.EMPTY_LIST, Arrays.asList(f.getRoot().getChildren()));
78             m.store(f, urls);
79         } else {
80             f = m.store(urls);
81         }
82         // Initial run.
83
checkStruct(f);
84         if (mf.supportsTimestamps ()) {
85             checkLastModified (f, "foo/test2", "data/test2a");
86             checkLastModified (f, "bar/test5", "data/layer2.xml");
87             checkLastModified (f, "baz/thingy", "data/layer1.xml");
88             checkLastModified (f, "foo/test1", "data/layer1.xml");
89             checkLastModified (f, "bug39210/inline.txt", "data/layer1.xml");
90         }
91         if (m.cacheExists()) {
92             // Now check the persistence.
93
m = mf.createManager();
94             f = m.createLoadedFileSystem();
95             checkStruct(f);
96             if (m.supportsLoad()) {
97                 // Also check load operation.
98
f = m.createEmptyFileSystem();
99                 assertEquals(Collections.EMPTY_LIST, Arrays.asList(f.getRoot().getChildren()));
100                 m.load(f);
101                 checkStruct(f);
102             }
103         }
104     }
105     
106     private void checkLastModified (FileSystem f, String JavaDoc file, String JavaDoc resource) throws Exception JavaDoc {
107         FileObject obj = f.findResource (file);
108         assertNotNull (file + " found", obj);
109         
110         long time = obj.lastModified ().getTime ();
111         URL JavaDoc url = CacheManagerTestBaseHid.class.getResource(resource);
112         long resourceTime = url.openConnection ().getLastModified ();
113         
114         if (initTime < resourceTime) {
115             fail ("The time of the resource " + file + " (" + resourceTime + ") is likely older than iniciation of this class (" + initTime + ")");
116         }
117
118         assertEquals ("Time of " + file + " is the same as URL time of " + resource, resourceTime, time);
119         
120         
121     }
122     
123     private void checkStruct(FileSystem f) throws Exception JavaDoc {
124         assertEquals("Root has 5 children", 5, f.getRoot().getChildren().length);
125         assertEquals(1, f.findResource("bar").getChildren().length);
126         assertEquals("", slurp(f, "bar/test5"));
127         assertEquals(5, f.findResource("foo").getChildren().length);
128         // XXX not clear if this is in fact supposed to be empty instead:
129
//assertEquals("lala", slurp(f, "foo/test1"));
130
assertEquals("two", attr(f, "foo/test1", "y"));
131         assertEquals("rara!", slurp(f, "foo/test2"));
132         assertEquals("hi!", slurp(f, "foo/test3"));
133         assertEquals("three", attr(f, "foo/test3", "x"));
134         assertEquals("one too", attr(f, "foo/test3", "y"));
135         assertEquals("", slurp(f, "foo/test4"));
136         // #29356: methodvalue should pass in MultiFileObject, not the original FileObject:
137
FixedFileSystem ffs = new FixedFileSystem("ffs", "FFS");
138         FixedFileSystem.Instance i = new FixedFileSystem.Instance(false, null, null, null, (URL JavaDoc)null);
139         i.writeAttribute("x", "val");
140         ffs.add("foo/29356", i);
141         MultiFileSystem mfs = new MultiFileSystem(new FileSystem[] {f, ffs});
142         assertEquals("val", attr(ffs, "foo/29356", "x"));
143         assertEquals("val", attr(mfs, "foo/29356", "x"));
144         assertEquals("val/a", attr(mfs, "foo/29356", "a"));
145         assertEquals("val", attr(mfs, "foo/29356", "map1"));
146         assertEquals("val/map2", attr(mfs, "foo/29356", "map2"));
147     }
148     
149     private static String JavaDoc slurp(FileSystem f, String JavaDoc path) throws IOException JavaDoc {
150         FileObject fo = f.findResource(path);
151         if (fo == null) return null;
152         InputStream JavaDoc is = fo.getInputStream();
153         StringBuffer JavaDoc text = new StringBuffer JavaDoc((int)fo.getSize());
154         byte[] buf = new byte[1024];
155         int read;
156         while ((read = is.read(buf)) != -1) {
157             text.append(new String JavaDoc(buf, 0, read, "US-ASCII"));
158         }
159         return text.toString();
160     }
161     
162     private static Object JavaDoc attr(FileSystem f, String JavaDoc path, String JavaDoc a) throws IOException JavaDoc {
163         FileObject fo = f.findResource(path);
164         if (fo == null) return null;
165         return fo.getAttribute(a);
166     }
167     
168 }
169
Popular Tags