1 19 20 package org.openide.loaders; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.Date ; 28 import java.util.Enumeration ; 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 42 public class DataFolderSetOrderTest extends NbTestCase 43 implements PropertyChangeListener { 44 private DataFolder aa; 45 private DataFolder bb; 46 private ArrayList events = new ArrayList (); 47 private static Task previous; 48 49 public DataFolderSetOrderTest (String name) { 50 super (name); 51 } 52 53 56 protected void runTest () throws Throwable { 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 { 71 clearWorkDir(); 72 73 MockServices.setServices(new Class [] {ErrManager.class, Pool.class}); 74 75 if (previous != null) { 76 previous.waitFinished (); 77 } 78 79 TestUtilHid.destroyLocalFileSystem (getName()); 80 String fsstruct [] = new String [] { 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 { 95 final DataLoader l = DataLoader.getLoader(DataObjectInvalidationTest.SlowDataLoader.class); 96 97 aa.removePropertyChangeListener (this); 98 } 99 100 private void makeFolderRecognizerBusy () throws Exception { 101 if (getName().indexOf ("Busy") < 0) { 102 return; 103 } 104 105 final DataLoader l = DataLoader.getLoader(DataObjectInvalidationTest.SlowDataLoader.class); 106 synchronized (l) { 107 previous = RequestProcessor.getDefault().post(new Runnable () { 109 public void run () { 110 DataObject[] arr = bb.getChildren (); 111 } 112 }); 113 114 l.wait (); 116 } 117 118 } 120 121 private void doTest () throws Exception { 122 DataObject[] arr = aa.getChildren (); 123 assertEquals ("Two objects", 2, arr.length); 124 ArrayList l = new ArrayList (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 if (!events.contains (DataFolder.PROP_ORDER) || !events.contains (DataFolder.PROP_CHILDREN)) { 140 fail ("Wrong events: " + events); 141 } 142 } 143 144 149 150 public void testReorderWithoutChecks () throws Exception { 151 doTest (); 152 } 153 154 public void propertyChange (PropertyChangeEvent evt) { 155 events.add (evt.getPropertyName ()); 156 } 157 158 public static final class ErrManager extends ErrorManager { 162 public static final StringBuffer messages = new StringBuffer (); 163 164 private String prefix; 165 166 public ErrManager () { 167 this (null); 168 } 169 public ErrManager (String prefix) { 170 this.prefix = prefix; 171 } 172 173 public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 174 return t; 175 } 176 177 public Throwable attachAnnotations (Throwable t, ErrorManager.Annotation[] arr) { 178 return t; 179 } 180 181 public ErrorManager.Annotation[] findAnnotations (Throwable t) { 182 return null; 183 } 184 185 public ErrorManager getInstance (String name) { 186 if ( 187 name.startsWith ("org.openide.loaders.FolderList") 188 ) { 190 return new ErrManager ('[' + name + ']'); 191 } else { 192 return new ErrManager (); 194 } 195 } 196 197 public void log (int severity, String 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 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 } 219 public static final class Pool extends DataLoaderPool { 220 221 protected Enumeration loaders() { 222 return Enumerations.singleton(DataLoader.getLoader(DataObjectInvalidationTest.SlowDataLoader.class)); 223 } 224 225 } } 227 | Popular Tags |