KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > storage > SettingsProviderTest


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.netbeans.modules.editor.settings.storage;
21
22 import java.awt.Color JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.swing.text.AttributeSet JavaDoc;
27 import javax.swing.text.StyleConstants JavaDoc;
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 /**
38  *
39  * @author vita
40  */

41 public class SettingsProviderTest extends NbTestCase {
42     
43     /** Creates a new instance of SettingsProviderTest */
44     public SettingsProviderTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50     
51         EditorTestLookup.setLookup(
52             new URL JavaDoc[] {
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 JavaDoc[] {},
60             getClass().getClassLoader()
61         );
62
63         // This is here to initialize Nb URL factory (org.netbeans.core.startup),
64
// which is needed by Nb EntityCatalog (org.netbeans.core).
65
// Also see the test dependencies in project.xml
66
Main.initializeURLFactory();
67     }
68     
69     public void testSpecialTestMimeType() throws Exception JavaDoc {
70         final String JavaDoc origMimeType = "text/x-orig";
71         final String JavaDoc specialTestMimeType = "test123456_" + origMimeType;
72         Marker origMarker;
73         
74         {
75             Lookup origLookup = MimeLookup.getLookup(MimePath.parse(origMimeType));
76             Collection JavaDoc<? 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 JavaDoc<? 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 JavaDoc {
102         final String JavaDoc mimeType = "text/x-orig";
103         
104         Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimeType));
105         
106         // Check the API class
107
Collection JavaDoc<? 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 JavaDoc {
119         final String JavaDoc origMimeType = "text/x-orig";
120         final String JavaDoc specialTestMimeType = "test123456_" + origMimeType;
121         
122         Lookup lookup = MimeLookup.getLookup(MimePath.parse(specialTestMimeType));
123         
124         // Check the API class
125
Collection JavaDoc<? 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 JavaDoc {
137         final String JavaDoc origMimeType = "text/x-orig";
138         final String JavaDoc specialTestMimeType = "test123456_" + origMimeType;
139         
140         Lookup lookup = MimeLookup.getLookup(MimePath.parse(specialTestMimeType));
141         
142         // Check the API class
143
Collection JavaDoc<? 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 JavaDoc<FontColorSettingsImpl> fcsiRef = new WeakReference JavaDoc<FontColorSettingsImpl>(fcsi);
165         WeakReference JavaDoc<MimePath> mimePathRef = new WeakReference JavaDoc<MimePath>(mimePath);
166         WeakReference JavaDoc<Lookup> lookupRef = new WeakReference JavaDoc<Lookup>(lookup);
167         WeakReference JavaDoc<FontColorSettings> fcsRef = new WeakReference JavaDoc<FontColorSettings>(fcs);
168     
169         fcsi = null;
170         mimePath = null;
171         lookup = null;
172         fcs = null;
173         
174         // release text/x-type-A from MimePath's LRU
175
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 JavaDoc<KeyBindingSettingsImpl> kbsiRef = new WeakReference JavaDoc<KeyBindingSettingsImpl>(kbsi);
192         WeakReference JavaDoc<MimePath> mimePathRef = new WeakReference JavaDoc<MimePath>(mimePath);
193         WeakReference JavaDoc<Lookup> lookupRef = new WeakReference JavaDoc<Lookup>(lookup);
194         WeakReference JavaDoc<KeyBindingSettings> kbsRef = new WeakReference JavaDoc<KeyBindingSettings>(kbs);
195     
196         kbsi = null;
197         mimePath = null;
198         lookup = null;
199         kbs = null;
200         
201         // release text/x-type-A from MimePath's LRU
202
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 JavaDoc 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 JavaDoc(0xAA0000), antXmlAttribs.getAttribute(StyleConstants.Background));
219         
220         AttributeSet JavaDoc 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 JavaDoc(0x00BB00), xmlAttribs.getAttribute(StyleConstants.Background));
223         
224         AttributeSet JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc(0xAA0000), attribsTypeA.getAttribute(StyleConstants.Background));
236         
237         AttributeSet JavaDoc 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 JavaDoc(0xBB0000), attribsTypeB.getAttribute(StyleConstants.Background));
240         
241         AttributeSet JavaDoc 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 JavaDoc(0xAAAA00), attribsBoth.getAttribute(StyleConstants.Background));
244
245         AttributeSet JavaDoc 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 JavaDoc(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     } // End of Marker class
266

267 }
268
Popular Tags