KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.Arrays JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import org.netbeans.junit.Log;
25 import org.openide.ErrorManager;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileSystem;
28 import org.openide.filesystems.Repository;
29 import org.openide.nodes.Index;
30 import org.openide.nodes.Node;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.filesystems.LocalFileSystem;
33 import org.openide.util.Mutex;
34
35
36 /**
37  * Tests Index cookio of DataFolder (when uses DataFilter).
38  *
39  * @author Jiri Rechtacek
40  */

41 public class DataFolderIndexTest extends LoggingTestCaseHid {
42     DataFolder df;
43     FileObject fo;
44     ErrorManager ERR;
45     
46     /** Constructor required by JUnit.
47      * @param testName method name to be used as testcase
48      */

49     public DataFolderIndexTest(String JavaDoc s) {
50         super(s);
51     }
52     
53     protected void setUp () throws Exception JavaDoc {
54         registerIntoLookup(new Pool ());
55         
56         
57         ERR = org.openide.ErrorManager.getDefault().getInstance("TEST-" + getName());
58         
59         FileObject old = Repository.getDefault().getDefaultFileSystem().findResource("TestTemplates");
60         if (old != null) {
61             old.delete();
62         }
63         
64         fo = Repository.getDefault().getDefaultFileSystem().getRoot().createFolder("TestTemplates");
65         df = DataFolder.findFolder(fo);
66         assertNotNull("DataFolder found for AA", df);
67         
68         df.getPrimaryFile().createData("marie");
69         df.getPrimaryFile().createData("jakub");
70         df.getPrimaryFile().createData("eva");
71         df.getPrimaryFile().createData("adam");
72         
73         assertNotNull("Folder " + df + " has a children.", df.getChildren());
74         assertEquals("Folder " + df + " has 4 childs.", 4, df.getChildren().length);
75     }
76     
77     public void testIndexWithoutInitialization() throws Exception JavaDoc {
78         Node n = df.getNodeDelegate();
79         
80         Index fromNode = (Index) n.getLookup ().lookup (Index.class);
81         assertNotNull ("DataFolderNode has Index.", fromNode);
82
83         int x = fromNode.getNodesCount();
84         assertEquals("The same number of nodes like folder children", df.getChildren().length, x);
85     }
86
87     public void testIndexWithoutInitializationInReadAccess() throws Exception JavaDoc {
88         CharSequence JavaDoc seq = Log.enable("", Level.INFO);
89         
90         org.openide.nodes.Children.MUTEX.readAccess(new Mutex.ExceptionAction () {
91             public Object JavaDoc run () throws Exception JavaDoc {
92                 Node n = df.getNodeDelegate();
93
94                 Index fromNode = (Index) n.getLookup ().lookup (Index.class);
95                 assertNotNull ("DataFolderNode has Index.", fromNode);
96
97                 int x = fromNode.getNodesCount();
98                 assertEquals("Folder has few children", 4, df.getChildren().length);
99                 assertEquals("Cannot initialize the count in nodes as we are in read access", 0, x);
100                 return null;
101             }
102         });
103         
104         if (seq.length() > 0) {
105             fail("No messages shall be reported:\n" + seq);
106         }
107     }
108     
109     public void testIndexNodesWithoutInitialization() throws Exception JavaDoc {
110         Node n = df.getNodeDelegate();
111         
112         Index fromNode = (Index) n.getLookup ().lookup (Index.class);
113         assertNotNull ("DataFolderNode has Index.", fromNode);
114
115         int x = fromNode.getNodes().length;
116         assertEquals("The same number of nodes like folder children", df.getChildren().length, x);
117     }
118     
119     public void testWithoutFilter () throws Exception JavaDoc {
120         testMatchingIndexes (df, df.getNodeDelegate ());
121     }
122     
123     public void testIndexCookieOfferedOnlyWhenAppropriate() throws Exception JavaDoc {
124         Node n = df.getNodeDelegate();
125         assertNotNull("have an index cookie on SFS", n.getLookup().lookup(Index.class));
126         LocalFileSystem lfs = new LocalFileSystem();
127         lfs.setRootDirectory(getWorkDir());
128         n = DataFolder.findFolder(lfs.getRoot()).getNodeDelegate();
129         assertNull("have no index cookie on a local folder", n.getLookup().lookup(Index.class));
130         FileSystem fs = FileUtil.createMemoryFileSystem();
131         FileObject x = fs.getRoot().createFolder("x");
132         FileObject y = fs.getRoot().createFolder("y");
133         y.setAttribute("DataFolder.Index.reorderable", Boolean.TRUE);
134         n = DataFolder.findFolder(x).getNodeDelegate();
135         assertNull("have no index cookie on a random folder in a random FS", n.getLookup().lookup(Index.class));
136         n = DataFolder.findFolder(y).getNodeDelegate();
137         assertNotNull("do have index cookie if magic attr is set", n.getLookup().lookup(Index.class));
138     }
139     
140     private void testMatchingIndexes (DataFolder f, Node n) {
141         ERR.log("Starting the test");
142         Node [] arr = n.getChildren ().getNodes (true);
143         ERR.log("Children created");
144         
145         Index fromNode = (Index) n.getLookup ().lookup (Index.class);
146         assertNotNull ("DataFolderNode has Index.", fromNode);
147         ERR.log("Index from node: " + fromNode);
148         
149         Index fromFolder = new DataFolder.Index (f, n);
150         assertNotNull ("DataFolderNode has Index.", fromFolder);
151         ERR.log("Index from folder: " + fromFolder);
152         
153         int fromNodeCount = fromNode.getNodesCount ();
154         assertTrue ("Index contains some items", fromNodeCount > 0);
155         ERR.log("count computed: " + fromNodeCount);
156         
157         int fromFolderCount = fromFolder.getNodesCount ();
158         assertTrue ("Index contains some items", fromFolderCount > 0);
159         
160         Node[] arrNode = fromNode.getNodes ();
161         Node[] arrFolder = fromFolder.getNodes ();
162         
163         ERR.log ("Node's index: " + Arrays.asList (arrNode));
164         ERR.log ("Folder's index: " + Arrays.asList (arrFolder));
165         
166         for (int i = 0; i < arr.length; i++) {
167             log("Computing index for [" + i + "] which is node " + arr[i]);
168             int index = fromNode.indexOf (arr [i]);
169             log("Index computed to be " + index);
170             
171             log("Computing index from the folder [" + i + "]: " + arr[i]);
172             int folderIndex = fromFolder.indexOf (arr [i]);
173             log("Folder index is to be " + folderIndex);
174             
175             if (folderIndex != index) {
176                 fail(
177                     i + "th iteration - Node " + arr [i] +
178                     " has as same position in Node's Index [" +
179                     Arrays.asList (fromNode.getNodes ()) + "]" +
180                     "as in folder's Index [" +
181                     Arrays.asList (fromFolder.getNodes ()) + "]. folder: " +
182                     folderIndex + " node: " + index
183                 );
184             }
185         }
186     }
187     
188     private static final class Pool extends DataLoaderPool {
189         public static DataLoader extra;
190         
191         
192         protected java.util.Enumeration JavaDoc loaders () {
193             if (extra == null) {
194                 return org.openide.util.Enumerations.empty ();
195             } else {
196                 return org.openide.util.Enumerations.singleton (extra);
197             }
198         }
199     }
200 }
201
Popular Tags