1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.awt.Color ; 23 import java.lang.ref.WeakReference ; 24 import java.net.URL ; 25 import java.util.Collection ; 26 import javax.swing.text.AttributeSet ; 27 import javax.swing.text.StyleConstants ; 28 import org.netbeans.api.editor.mimelookup.MimeLookup; 29 import org.netbeans.api.editor.mimelookup.MimePath; 30 import org.netbeans.api.editor.settings.FontColorSettings; 31 import org.netbeans.api.editor.settings.KeyBindingSettings; 32 import org.netbeans.core.startup.Main; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.filesystems.FileObject; 35 import org.openide.util.Lookup; 36 37 41 public class SettingsProviderTest extends NbTestCase { 42 43 44 public SettingsProviderTest(String name) { 45 super(name); 46 } 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 51 EditorTestLookup.setLookup( 52 new URL [] { 53 getClass().getClassLoader().getResource( 54 "org/netbeans/modules/editor/settings/storage/test-layer.xml"), 55 getClass().getClassLoader().getResource( 56 "org/netbeans/modules/editor/settings/storage/layer.xml"), 57 }, 58 getWorkDir(), 59 new Object [] {}, 60 getClass().getClassLoader() 61 ); 62 63 Main.initializeURLFactory(); 67 } 68 69 public void testSpecialTestMimeType() throws Exception { 70 final String origMimeType = "text/x-orig"; 71 final String specialTestMimeType = "test123456_" + origMimeType; 72 Marker origMarker; 73 74 { 75 Lookup origLookup = MimeLookup.getLookup(MimePath.parse(origMimeType)); 76 Collection <? extends Marker> markers = origLookup.lookupAll(Marker.class); 77 assertEquals("Wrong number of orig markers", 1, markers.size()); 78 79 origMarker = markers.iterator().next(); 80 assertNotNull("Orig marker is null", origMarker); 81 assertEquals("Wrong orig marker", 82 "Editors/text/x-orig/marker.instance", 83 origMarker.getHome().getPath()); 84 } 85 86 { 87 Lookup testLookup = MimeLookup.getLookup(MimePath.parse(specialTestMimeType)); 88 Collection <? extends Marker> markers = testLookup.lookupAll(Marker.class); 89 assertEquals("Wrong number of test markers", 1, markers.size()); 90 91 Marker testMarker = markers.iterator().next(); 92 assertNotNull("Test marker is null", testMarker); 93 assertEquals("Wrong test marker", 94 "Editors/text/x-orig/marker.instance", 95 testMarker.getHome().getPath()); 96 97 assertSame("Test marker and orig marker should be the same", origMarker, testMarker); 98 } 99 } 100 101 public void testColoringsForMimeType() throws Exception { 102 final String mimeType = "text/x-orig"; 103 104 Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimeType)); 105 106 Collection <? extends FontColorSettings> c = lookup.lookupAll(FontColorSettings.class); 108 assertEquals("Wrong number of fcs", 1, c.size()); 109 110 FontColorSettings fcs = c.iterator().next(); 111 assertNotNull("FCS should not be null", fcs); 112 assertTrue("Wrong fcs impl", fcs instanceof CompositeFCS); 113 114 CompositeFCS compositeFcs = (CompositeFCS) fcs; 115 assertEquals("CompositeFCS using wrong profile", EditorSettingsImpl.DEFAULT_PROFILE, compositeFcs.profile); 116 } 117 118 public void testColoringsForSpecialTestMimeType() throws Exception { 119 final String origMimeType = "text/x-orig"; 120 final String specialTestMimeType = "test123456_" + origMimeType; 121 122 Lookup lookup = MimeLookup.getLookup(MimePath.parse(specialTestMimeType)); 123 124 Collection <? extends FontColorSettings> c = lookup.lookupAll(FontColorSettings.class); 126 assertEquals("Wrong number of fcs", 1, c.size()); 127 128 FontColorSettings fcs = c.iterator().next(); 129 assertNotNull("FCS should not be null", fcs); 130 assertTrue("Wrong fcs impl", fcs instanceof CompositeFCS); 131 132 CompositeFCS compositeFcs = (CompositeFCS) fcs; 133 assertEquals("CompositeFCS should be using special test profile", "test123456", compositeFcs.profile); 134 } 135 136 public void testKeybindingsForSpecialTestMimeType() throws Exception { 137 final String origMimeType = "text/x-orig"; 138 final String specialTestMimeType = "test123456_" + origMimeType; 139 140 Lookup lookup = MimeLookup.getLookup(MimePath.parse(specialTestMimeType)); 141 142 Collection <? extends KeyBindingSettings> c = lookup.lookupAll(KeyBindingSettings.class); 144 assertEquals("Wrong number of kbs", 1, c.size()); 145 146 KeyBindingSettings kbs = c.iterator().next(); 147 assertNotNull("KBS should not be null", kbs); 148 assertTrue("Wrong kbs impl", kbs instanceof KeyBindingSettingsImpl.Immutable); 149 } 150 151 public void testLookupsCached() { 152 FontColorSettings fcs1 = MimeLookup.getLookup(MimePath.parse("text/x-type-A")).lookup(FontColorSettings.class); 153 FontColorSettings fcs2 = MimeLookup.getLookup(MimePath.parse("text/x-type-A")).lookup(FontColorSettings.class); 154 155 assertSame("FontColorSettings instances should be the same", fcs1, fcs2); 156 } 157 158 public void testLookupsGCedAfterFcs() { 159 MimePath mimePath = MimePath.parse("text/x-type-A"); 160 FontColorSettingsImpl fcsi = FontColorSettingsImpl.get(mimePath); 161 Lookup lookup = MimeLookup.getLookup(mimePath); 162 FontColorSettings fcs = lookup.lookup(FontColorSettings.class); 163 164 WeakReference <FontColorSettingsImpl> fcsiRef = new WeakReference <FontColorSettingsImpl>(fcsi); 165 WeakReference <MimePath> mimePathRef = new WeakReference <MimePath>(mimePath); 166 WeakReference <Lookup> lookupRef = new WeakReference <Lookup>(lookup); 167 WeakReference <FontColorSettings> fcsRef = new WeakReference <FontColorSettings>(fcs); 168 169 fcsi = null; 170 mimePath = null; 171 lookup = null; 172 fcs = null; 173 174 for(int i = 0; i < 10; i++) { 176 MimePath.parse("text/x-type-" + ('Z' + i)); 177 } 178 179 assertGC("FCSI hasn't been GCed", fcsiRef); 180 assertGC("MimePath hasn't been GCed", mimePathRef); 181 assertGC("Lookup hasn't been GCed", lookupRef); 182 assertGC("FCS hasn't been GCed", fcsRef); 183 } 184 185 public void testLookupsGCedAfterKbs() { 186 MimePath mimePath = MimePath.parse("text/x-type-A"); 187 KeyBindingSettingsImpl kbsi = KeyBindingSettingsImpl.get(mimePath); 188 Lookup lookup = MimeLookup.getLookup(mimePath); 189 KeyBindingSettings kbs = lookup.lookup(KeyBindingSettings.class); 190 191 WeakReference <KeyBindingSettingsImpl> kbsiRef = new WeakReference <KeyBindingSettingsImpl>(kbsi); 192 WeakReference <MimePath> mimePathRef = new WeakReference <MimePath>(mimePath); 193 WeakReference <Lookup> lookupRef = new WeakReference <Lookup>(lookup); 194 WeakReference <KeyBindingSettings> kbsRef = new WeakReference <KeyBindingSettings>(kbs); 195 196 kbsi = null; 197 mimePath = null; 198 lookup = null; 199 kbs = null; 200 201 for(int i = 0; i < 10; i++) { 203 MimePath.parse("text/x-type-" + ('Z' + i)); 204 } 205 206 assertGC("KBSI hasn't been GCed", kbsiRef); 207 assertGC("MimePath hasn't been GCed", mimePathRef); 208 assertGC("Lookup hasn't been GCed", lookupRef); 209 assertGC("KBS hasn't been GCed", kbsRef); 210 } 211 212 public void testInheritanceForAntPlusXml() { 213 MimePath mimePath = MimePath.parse("text/ant+xml"); 214 FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class); 215 216 AttributeSet antXmlAttribs = fcs.getTokenFontColors("test-inheritance-ant-xml"); 217 assertNotNull("Can't find coloring defined for text/ant+xml", antXmlAttribs); 218 assertEquals("Wrong bgColor in coloring defined for text/ant+xml", new Color (0xAA0000), antXmlAttribs.getAttribute(StyleConstants.Background)); 219 220 AttributeSet xmlAttribs = fcs.getTokenFontColors("test-inheritance-xml"); 221 assertNotNull("Can't find coloring defined for text/xml", xmlAttribs); 222 assertEquals("Wrong bgColor in coloring defined for text/xml", new Color (0x00BB00), xmlAttribs.getAttribute(StyleConstants.Background)); 223 224 AttributeSet attribs = fcs.getTokenFontColors("test-all-languages-super-default"); 225 assertNotNull("Can't find coloring defined for root", attribs); 226 assertEquals("Wrong bgColor in coloring defined for root", new Color (0xABCDEF), attribs.getAttribute(StyleConstants.Background)); 227 } 228 229 public void testInheritanceForEmbedded() { 230 MimePath mimePath = MimePath.parse("text/x-type-B/text/x-type-A"); 231 FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class); 232 233 AttributeSet attribsTypeA = fcs.getTokenFontColors("test-inheritance-typeA-specific"); 234 assertNotNull("Can't find coloring defined for text/x-type-A", attribsTypeA); 235 assertEquals("Wrong bgColor in coloring defined for text/x-type-A", new Color (0xAA0000), attribsTypeA.getAttribute(StyleConstants.Background)); 236 237 AttributeSet attribsTypeB = fcs.getTokenFontColors("test-inheritance-typeB-specific"); 238 assertNotNull("Can't find coloring defined for text/x-type-B", attribsTypeB); 239 assertEquals("Wrong bgColor in coloring defined for text/x-type-B", new Color (0xBB0000), attribsTypeB.getAttribute(StyleConstants.Background)); 240 241 AttributeSet attribsBoth = fcs.getTokenFontColors("test-inheritance-typeA-typeB"); 242 assertNotNull("Can't find coloring defined for both typeA and typeB", attribsBoth); 243 assertEquals("Wrong bgColor in coloring defined for both typeA typeB", new Color (0xAAAA00), attribsBoth.getAttribute(StyleConstants.Background)); 244 245 AttributeSet attribsEmbedded = fcs.getTokenFontColors("test-inheritance-typeA-embedded-in-typeB"); 246 assertNotNull("Can't find coloring defined for typeA embedded in typeB", attribsEmbedded); 247 assertEquals("Wrong bgColor in coloring defined for typeA embedded in typeB", new Color (0xAABB00), attribsEmbedded.getAttribute(StyleConstants.Background)); 248 } 249 250 public static Marker createMarker(FileObject f) { 251 return new Marker(f); 252 } 253 254 public static final class Marker { 255 256 private FileObject home; 257 258 public Marker(FileObject home) { 259 this.home = home; 260 } 261 262 public FileObject getHome() { 263 return home; 264 } 265 } 267 } 268 | Popular Tags |