1 19 20 package org.openide.filesystems; 21 22 import org.netbeans.junit.NbTestCase; 23 import org.openide.util.Lookup; 24 25 33 public class URLMapperLookupTest extends NbTestCase { 34 35 public URLMapperLookupTest(String name) { 36 super(name); 37 } 38 39 protected void setUp() throws Exception { 40 System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.URLMapperLookupTest$Lkp"); 41 42 super.setUp(); 43 44 assertEquals ("Our lookup is registered", Lkp.class, org.openide.util.Lookup.getDefault().getClass()); 45 } 46 47 public void testIfIAskForAnItemThatAsksURLMapperAndThenAskOnceMoreAllMappersAreAsked () 48 throws Exception { 49 Object found = org.openide.util.Lookup.getDefault().lookup (QueryingPair.class); 50 assertNotNull (found); 51 52 MyUM.queried = null; 53 java.net.URL url = new java.net.URL ("http://www.netbeans.org"); 54 URLMapper.findFileObject(url); 55 56 assertEquals ("Really got the query thru", url, MyUM.queried); 57 } 58 59 61 private static class QueryingPair extends org.openide.util.lookup.AbstractLookup.Pair { 62 public boolean beBroken; 63 64 public java.lang.String getId() { 65 return getType ().toString(); 66 } 67 68 public java.lang.String getDisplayName() { 69 return getId (); 70 } 71 72 public java.lang.Class getType() { 73 return getClass (); 74 } 75 76 protected boolean creatorOf(java.lang.Object obj) { 77 return obj == this; 78 } 79 80 protected boolean instanceOf(java.lang.Class c) { 81 if (beBroken) { 82 beBroken = false; 83 try { 84 assertNull ("is still null", MyUM.queried); 85 java.net.URL url = new java.net.URL ("http://www.netbeans.org"); 86 URLMapper.findFileObject(url); 87 assertNull ("This query did not get thru", MyUM.queried); 88 } catch (java.net.MalformedURLException ex) { 89 ex.printStackTrace(); 90 fail ("No exceptions: " + ex.getMessage ()); 91 } 92 } 93 return c.isAssignableFrom(getType ()); 94 } 95 96 public java.lang.Object getInstance() { 97 return this; 98 } 99 } 100 101 private static final class MyUM extends URLMapper { 102 public static java.net.URL queried; 103 104 public org.openide.filesystems.FileObject[] getFileObjects(java.net.URL url) { 105 queried = url; 106 return null; 107 } 108 109 public java.net.URL getURL(org.openide.filesystems.FileObject fo, int type) { 110 return null; 111 } 112 } 113 114 115 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 116 private static org.openide.util.lookup.InstanceContent ic; 117 118 public Lkp () { 119 this (new org.openide.util.lookup.InstanceContent ()); 120 } 121 122 private Lkp (org.openide.util.lookup.InstanceContent ic) { 123 super (ic); 124 this.ic = ic; 125 } 126 127 protected void initialize() { 128 for (int i = 0; i < 1000; i++) { 132 ic.add (new Integer (i)); 133 } 134 135 QueryingPair qp = new QueryingPair(); 136 ic.addPair (qp); 137 ic.add (new MyUM ()); 138 139 140 qp.beBroken = true; 141 } 142 143 } } 145 | Popular Tags |