KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import junit.framework.AssertionFailedError;
30 import org.netbeans.junit.MockServices;
31 import org.netbeans.junit.NbTestCase;
32 import org.openide.ErrorManager;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.util.Enumerations;
35 import org.openide.util.RequestProcessor;
36 import org.openide.util.Task;
37
38 /** Does a change in order on folder fire the right properties?
39  *
40  * @author Jaroslav Tulach
41  */

42 public class DataFolderSetOrderTest extends NbTestCase
43 implements PropertyChangeListener JavaDoc {
44     private DataFolder aa;
45     private DataFolder bb;
46     private ArrayList JavaDoc events = new ArrayList JavaDoc ();
47     private static Task previous;
48     
49     public DataFolderSetOrderTest (String JavaDoc name) {
50         super (name);
51     }
52
53     /** If execution fails we wrap the exception with
54      * new log message.
55      */

56     protected void runTest () throws Throwable JavaDoc {
57         ErrManager.messages.append ("Starting test ");
58         ErrManager.messages.append (getName ());
59         ErrManager.messages.append ('\n');
60         
61         try {
62             super.runTest ();
63         } catch (AssertionFailedError ex) {
64             AssertionFailedError ne = new AssertionFailedError (ex.getMessage () + " Log:\n" + ErrManager.messages);
65             ne.setStackTrace (ex.getStackTrace ());
66             throw ne;
67         }
68     }
69     
70     protected void setUp () throws Exception JavaDoc {
71         clearWorkDir();
72
73         MockServices.setServices(new Class JavaDoc[] {ErrManager.class, Pool.class});
74         
75         if (previous != null) {
76             previous.waitFinished ();
77         }
78         
79         TestUtilHid.destroyLocalFileSystem (getName());
80         String JavaDoc fsstruct [] = new String JavaDoc [] {
81             "AA/X.txt",
82             "AA/Y.txt",
83             "BB/X.slow",
84         };
85         
86         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir (), fsstruct);
87
88         aa = DataFolder.findFolder (lfs.findResource ("AA"));
89         bb = DataFolder.findFolder (lfs.findResource ("BB"));
90         
91         aa.addPropertyChangeListener (this);
92     }
93     
94     protected void tearDown () throws Exception JavaDoc {
95         final DataLoader l = DataLoader.getLoader(DataObjectInvalidationTest.SlowDataLoader.class);
96         
97         aa.removePropertyChangeListener (this);
98     }
99     
100     private void makeFolderRecognizerBusy () throws Exception JavaDoc {
101         if (getName().indexOf ("Busy") < 0) {
102             return;
103         }
104         
105         final DataLoader l = DataLoader.getLoader(DataObjectInvalidationTest.SlowDataLoader.class);
106         synchronized (l) {
107             // this will trigger bb.getChildren
108
previous = RequestProcessor.getDefault().post(new Runnable JavaDoc() {
109                 public void run () {
110                     DataObject[] arr = bb.getChildren ();
111                 }
112             });
113             
114             // waits till the recognition blocks in the new SlowDataObject
115
l.wait ();
116         }
117         
118         // now the folder recognizer is blocked at least for 2s
119
}
120
121     private void doTest () throws Exception JavaDoc {
122         DataObject[] arr = aa.getChildren ();
123         assertEquals ("Two objects", 2, arr.length);
124         ArrayList JavaDoc l = new ArrayList JavaDoc (Arrays.asList (arr));
125         Collections.reverse (l);
126         
127         assertEquals ("No changes yet", 0, events.size ());
128         makeFolderRecognizerBusy ();
129         aa.setOrder ((DataObject[])l.toArray (new DataObject[0]));
130         
131         DataObject[] narr = aa.getChildren ();
132         assertEquals ("Two again", 2, narr.length);
133         
134         assertSame ("1 == 2", arr[0], narr[1]);
135         assertSame ("2 == 1", arr[1], narr[0]);
136         
137 // PENDING-JST: Should be this, but if (2 != events.size () || !events.contains (DataFolder.PROP_ORDER) || !events.contains (DataFolder.PROP_CHILDREN)) {
138
// lets test at least for this:
139
if (!events.contains (DataFolder.PROP_ORDER) || !events.contains (DataFolder.PROP_CHILDREN)) {
140             fail ("Wrong events: " + events);
141         }
142     }
143     
144     /* XXX how does this differ from testReorderWithoutChecks?!
145     public void testReorderWithoutChecksWhenFolderRecognizerIsBusy() throws Exception {
146         doTest ();
147     }
148      */

149     
150     public void testReorderWithoutChecks () throws Exception JavaDoc {
151         doTest ();
152     }
153     
154     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
155         events.add (evt.getPropertyName ());
156     }
157
158     //
159
// Logging support
160
//
161
public static final class ErrManager extends ErrorManager {
162         public static final StringBuffer JavaDoc messages = new StringBuffer JavaDoc ();
163         
164         private String JavaDoc prefix;
165         
166         public ErrManager () {
167             this (null);
168         }
169         public ErrManager (String JavaDoc prefix) {
170             this.prefix = prefix;
171         }
172         
173         public Throwable JavaDoc annotate (Throwable JavaDoc t, int severity, String JavaDoc message, String JavaDoc localizedMessage, Throwable JavaDoc stackTrace, Date JavaDoc date) {
174             return t;
175         }
176         
177         public Throwable JavaDoc attachAnnotations (Throwable JavaDoc t, ErrorManager.Annotation[] arr) {
178             return t;
179         }
180         
181         public ErrorManager.Annotation[] findAnnotations (Throwable JavaDoc t) {
182             return null;
183         }
184         
185         public ErrorManager getInstance (String JavaDoc name) {
186             if (
187                 name.startsWith ("org.openide.loaders.FolderList")
188 // || name.startsWith ("org.openide.loaders.FolderInstance")
189
) {
190                 return new ErrManager ('[' + name + ']');
191             } else {
192                 // either new non-logging or myself if I am non-logging
193
return new ErrManager ();
194             }
195         }
196         
197         public void log (int severity, String JavaDoc s) {
198             if (prefix != null) {
199                 messages.append (prefix);
200                 messages.append (s);
201                 messages.append ('\n');
202             }
203         }
204         
205         public void notify (int severity, Throwable JavaDoc t) {
206             log (severity, t.getMessage ());
207         }
208         
209         public boolean isNotifiable (int severity) {
210             return prefix != null;
211         }
212         
213         public boolean isLoggable (int severity) {
214             return prefix != null;
215         }
216         
217     } // end of ErrManager
218

219     public static final class Pool extends DataLoaderPool {
220         
221         protected Enumeration JavaDoc loaders() {
222             return Enumerations.singleton(DataLoader.getLoader(DataObjectInvalidationTest.SlowDataLoader.class));
223         }
224         
225     } // end of Pool
226
}
227
Popular Tags