KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.WeakReference JavaDoc;
23 import java.util.*;
24 import org.openide.filesystems.FileSystem;
25 import org.openide.filesystems.Repository;
26 import org.netbeans.junit.*;
27 import org.openide.filesystems.*;
28
29 /** Delete of an folder is said to be slow due to
30  * poor implementation of DataShadow validate functionality.
31  * @author Jaroslav Tulach
32  */

33 public class DataShadowSlowness39981Test extends NbTestCase implements OperationListener {
34     /** List of DataObject */
35     private List shadows, brokenShadows;
36     /** folder to work with */
37     private DataFolder folder;
38     /** fs we work on */
39     private FileSystem lfs;
40     /** start time of the test */
41     private long time;
42     /** number of created objects */
43     private int createdObjects;
44     /** created children */
45     private DataObject[] arr;
46     
47     public DataShadowSlowness39981Test (String JavaDoc name) {
48         super(name);
49     }
50     
51     public static NbTestSuite suite () {
52         return NbTestSuite.speedSuite (DataShadowSlowness39981Test.class, 20, 5);
53     }
54     
55     protected void setUp() throws Exception JavaDoc {
56         clearWorkDir();
57         
58         lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
59             "shadows/",
60             "brokenshadows",
61             "folder/original.txt",
62             "folder/orig.txt",
63             "modify/"
64         });
65         FileObject root = FileUtil.toFileObject(FileUtil.toFile(lfs.getRoot()));
66         
67         int count = getTestNumber ();
68         
69         shadows = createShadows (
70             DataObject.find (root.getFileObject("folder/original.txt")),
71             DataFolder.findFolder (root.getFileObject("shadows")),
72             count
73         );
74         
75         brokenShadows = /*Collections.EMPTY_LIST; */createShadows (
76             DataObject.find (root.getFileObject("folder/orig.txt")),
77             DataFolder.findFolder (root.getFileObject("shadows")),
78             count
79         );
80         
81         DataObject.find (root.getFileObject("folder/orig.txt")).delete ();
82         
83         ListIterator it = brokenShadows.listIterator ();
84         while (it.hasNext ()) {
85             DataObject obj = (DataObject)it.next ();
86             assertFalse ("Is not valid", obj.isValid ());
87             assertTrue ("Used to be shadow", obj instanceof DataShadow);
88             DataObject newObj = DataObject.find (obj.getPrimaryFile ());
89             assertTrue ("They are different", newObj != obj);
90             assertFalse ("It is not shadow, as it is broken", newObj instanceof DataShadow);
91             
92             it.set (newObj);
93         }
94         
95         FileObject files = root.getFileObject("modify");
96         for (int i = 0; i < 200; i++) {
97             FileUtil.createData (files, "empty" + i + ".txt");
98         }
99         
100         assertEquals ("Children created", 200, files.getChildren ().length);
101         
102         folder = DataFolder.findFolder (files);
103         time = System.currentTimeMillis ();
104     }
105     
106     private static List createShadows (DataObject original, DataFolder target, int count) throws java.io.IOException JavaDoc {
107         ArrayList list = new ArrayList (count);
108         for (int i = 0; i < count; i++) {
109             DataShadow shad = DataShadow.create(target, original.getName()+i, original, "shadow");
110             list.add (shad);
111         }
112         return list;
113     }
114     
115     protected void tearDown() throws Exception JavaDoc {
116         ArrayList weaks = new ArrayList();
117         addWeakRefs(Arrays.asList(arr), weaks);
118         addWeakRefs(brokenShadows, weaks);
119         addWeakRefs(shadows, weaks);
120         addWeakRefs(Collections.singleton(lfs), weaks);
121         addWeakRefs(Collections.singleton(folder), weaks);
122         
123         arr = null;
124         brokenShadows = null;
125         shadows = null;
126         lfs = null;
127         folder = null;
128         
129         
130         WeakReference JavaDoc[] refArr = (WeakReference JavaDoc[])weaks.toArray(new WeakReference JavaDoc[0]);
131         for (int i = 0; i < refArr.length; i++) {
132             assertGC(i + " - gc(" + refArr[i].get() + "): ", refArr[i]);
133         }
134         
135         
136     }
137     
138     private static void addWeakRefs(Collection objects, List weakRefsToAdd) {
139         Iterator it = objects.iterator();
140         while (it.hasNext()) {
141             Object JavaDoc obj = it.next();
142             weakRefsToAdd.add(new WeakReference JavaDoc(obj));
143         }
144     }
145     
146     private void createChildren () {
147         DataLoaderPool pool = DataLoaderPool.getDefault ();
148         pool.addOperationListener (this);
149         
150         arr = folder.getChildren ();
151         
152         pool.removeOperationListener (this);
153         
154         if (arr.length > createdObjects) {
155             fail ("The children of the folder should not be created before the getChildren method is called. Children: " + arr.length + " created: " + createdObjects);
156         }
157     }
158     
159     public void test0 () throws Exception JavaDoc {
160         createChildren ();
161     }
162     
163     public void test10 () throws java.io.IOException JavaDoc {
164         createChildren ();
165     }
166     
167     public void test99 () throws java.io.IOException JavaDoc {
168         createChildren ();
169     }
170
171     public void test245 () throws java.io.IOException JavaDoc {
172         createChildren ();
173     }
174     
175     public void test552 () throws java.io.IOException JavaDoc {
176         createChildren ();
177     }
178     
179 // public void test987 () throws Exception {
180
// createChildren ();
181
// }
182

183     
184     public void operationCopy (org.openide.loaders.OperationEvent.Copy ev) {
185     }
186     
187     public void operationCreateFromTemplate (org.openide.loaders.OperationEvent.Copy ev) {
188     }
189     
190     public void operationCreateShadow (org.openide.loaders.OperationEvent.Copy ev) {
191     }
192     
193     public void operationDelete (OperationEvent ev) {
194     }
195     
196     public void operationMove (org.openide.loaders.OperationEvent.Move ev) {
197     }
198     
199     public void operationPostCreate (OperationEvent ev) {
200         createdObjects++;
201     }
202     
203     public void operationRename (org.openide.loaders.OperationEvent.Rename ev) {
204     }
205     
206 }
207
Popular Tags