001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.editor.settings.storage.fontscolors;
042:
043: import java.awt.Color;
044: import java.io.IOException;
045: import java.net.URL;
046: import java.util.HashMap;
047: import java.util.Map;
048: import javax.swing.text.AttributeSet;
049: import javax.swing.text.StyleConstants;
050: import org.netbeans.api.editor.mimelookup.MimePath;
051: import org.netbeans.api.editor.settings.AttributesUtilities;
052: import org.netbeans.api.editor.settings.EditorStyleConstants;
053: import org.netbeans.core.startup.Main;
054: import org.netbeans.junit.NbTestCase;
055: import org.netbeans.modules.editor.settings.storage.EditorTestLookup;
056: import org.netbeans.modules.editor.settings.storage.api.EditorSettingsStorage;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.Repository;
059:
060: /**
061: *
062: * @author Vita Stejskal
063: */
064: public class ColoringStorageTest extends NbTestCase {
065:
066: /** Creates a new instance of ColoringStorageTest */
067: public ColoringStorageTest(String name) {
068: super (name);
069: }
070:
071: protected @Override
072: void setUp() throws Exception {
073: super .setUp();
074:
075: EditorTestLookup
076: .setLookup(
077: new URL[] {
078: getClass()
079: .getClassLoader()
080: .getResource(
081: "org/netbeans/modules/editor/settings/storage/fontscolors/test-layer-ColoringStorageTest.xml"),
082: getClass()
083: .getClassLoader()
084: .getResource(
085: "org/netbeans/modules/editor/settings/storage/layer.xml"),
086: getClass()
087: .getClassLoader()
088: .getResource(
089: "org/netbeans/core/resources/mf-layer.xml"), // for MIMEResolverImpl to work
090: }, getWorkDir(), new Object[] {}, getClass()
091: .getClassLoader());
092:
093: // This is here to initialize Nb URL factory (org.netbeans.core.startup),
094: // which is needed by Nb EntityCatalog (org.netbeans.core).
095: // Also see the test dependencies in project.xml
096: Main.initializeURLFactory();
097: }
098:
099: public void testAllLanguages() throws IOException {
100: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
101: .<String, AttributeSet> get(ColoringStorage.ID);
102: Map<String, AttributeSet> colorings = ess.load(MimePath.EMPTY,
103: "MyProfileXyz", true); //NOI18N
104: assertNotNull("Colorings map should not be null", colorings);
105: assertEquals("Wrong number of colorings", 1, colorings.size());
106:
107: AttributeSet c = colorings
108: .get("test-all-languages-super-default");
109: assertNotNull(
110: "Should have test-all-languages-super-default coloring",
111: c);
112: assertEquals("Wrong bgColor", new Color(0xABCDEF), c
113: .getAttribute(StyleConstants.Background));
114: }
115:
116: public void testAllLanguages2() throws IOException {
117: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
118: .<String, AttributeSet> get(ColoringStorage.ID);
119: Map<String, AttributeSet> colorings = ess.load(MimePath.EMPTY,
120: "MyProfileXyz", false); //NOI18N
121: assertNotNull("Colorings map should not be null", colorings);
122: assertEquals("Wrong number of colorings", 1, colorings.size());
123:
124: AttributeSet c = colorings
125: .get("test-all-languages-super-default");
126: assertNotNull(
127: "Should have test-all-languages-super-default coloring",
128: c);
129: assertEquals("Wrong bgColor", new Color(0xABCDEF), c
130: .getAttribute(StyleConstants.Background));
131: }
132:
133: public void testAllLanguagesHighlights() {
134: ColoringStorage cs = new ColoringStorage(false);
135: Map<String, AttributeSet> colorings = cs.load(MimePath.EMPTY,
136: "MyProfileXyz", true); //NOI18N
137: assertNotNull("Colorings map should not be null", colorings);
138: assertEquals("Wrong number of colorings", 1, colorings.size());
139:
140: AttributeSet c = colorings.get("test-text-limit-line");
141: assertNotNull("Should have test-text-limit-line coloring", c);
142: assertEquals("Wrong bgColor", new Color(0x010101), c
143: .getAttribute(StyleConstants.Foreground));
144: }
145:
146: public void testAllLanguagesHighlights2() {
147: ColoringStorage cs = new ColoringStorage(false);
148: Map<String, AttributeSet> colorings = cs.load(MimePath.EMPTY,
149: "MyProfileXyz", false); //NOI18N
150: assertNotNull("Colorings map should not be null", colorings);
151: assertEquals("Wrong number of colorings", 1, colorings.size());
152:
153: AttributeSet c = colorings.get("test-text-limit-line");
154: assertNotNull("Should have test-text-limit-line coloring", c);
155: assertEquals("Wrong bgColor", new Color(0x010101), c
156: .getAttribute(StyleConstants.Foreground));
157: }
158:
159: public void testMultipleFiles() throws IOException {
160: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
161: .<String, AttributeSet> get(ColoringStorage.ID);
162: MimePath mimePath = MimePath.parse("text/x-type-A");
163: Map<String, AttributeSet> colorings = ess.load(mimePath,
164: "MyProfileXyz", false); //NOI18N
165: assertNotNull("Colorings map should not be null", colorings);
166: assertEquals("Wrong number of colorings", 3, colorings.size());
167:
168: // Check coloring in module A only
169: {
170: AttributeSet c = colorings.get("module-A-coloring");
171: assertNotNull("Should have module-A-coloring coloring", c);
172: assertEquals("Wrong bgColor", new Color(0xAA0000), c
173: .getAttribute(StyleConstants.Background));
174: }
175: {
176: // Check coloring in module B only
177: AttributeSet c = colorings.get("module-B-coloring");
178: assertNotNull("Should have module-B-coloring coloring", c);
179: assertEquals("Wrong bgColor", new Color(0xBB0000), c
180: .getAttribute(StyleConstants.Background));
181: }
182: {
183: // Check shared coloring
184: AttributeSet c = colorings.get("both-modules-coloring");
185: assertNotNull("Should have both-modules-coloring coloring",
186: c);
187: assertEquals("Wrong bgColor", new Color(0xAA0000), c
188: .getAttribute(StyleConstants.Background));
189: assertEquals("Wrong foreColor", new Color(0x00BB00), c
190: .getAttribute(StyleConstants.Foreground));
191: assertEquals("Wrong underline", new Color(0x0000BB), c
192: .getAttribute(StyleConstants.Underline));
193: }
194: }
195:
196: public void testDeleteFiles() throws IOException {
197: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
198: .<String, AttributeSet> get(ColoringStorage.ID);
199: MimePath mimePath = MimePath.parse("text/x-type-A");
200: ess.delete(mimePath, "MyProfileXyz", true);
201:
202: FileObject profileHome = Repository
203: .getDefault()
204: .getDefaultFileSystem()
205: .findResource(
206: "Editors/text/x-type-A/FontsColors/MyProfileXyz/Defaults");
207: assertNotNull("Can't find profileHome", profileHome);
208:
209: FileObject[] files = profileHome.getChildren();
210: assertEquals("There should be no files", 0, files.length);
211:
212: Map<String, AttributeSet> colorings = ess.load(mimePath,
213: "MyProfileXyz", true); //NOI18N
214: assertNotNull("There still should be colorings map", colorings);
215: assertEquals("Colorings map should be empty", 0, colorings
216: .size());
217: }
218:
219: public void testWriteColorings() throws IOException {
220: final String name1 = "new-coloring";
221: final Color color1 = new Color(0x1F2F3F);
222: final String name2 = "both-modules-coloring";
223: final Color color2 = new Color(0x1F2F3F);
224:
225: MimePath mimePath = MimePath.parse("text/x-type-A");
226: Map<String, AttributeSet> newColorings = new HashMap<String, AttributeSet>();
227: newColorings.put(name1, AttributesUtilities.createImmutable(
228: StyleConstants.Underline, color1,
229: StyleConstants.NameAttribute, name1));
230: newColorings.put(name2, AttributesUtilities.createImmutable(
231: StyleConstants.StrikeThrough, color2,
232: StyleConstants.NameAttribute, name2));
233:
234: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
235: .<String, AttributeSet> get(ColoringStorage.ID);
236: ess.save(mimePath, "MyProfileXyz", false, newColorings);
237:
238: FileObject settingFile = Repository
239: .getDefault()
240: .getDefaultFileSystem()
241: .findResource(
242: "Editors/text/x-type-A/FontsColors/MyProfileXyz/org-netbeans-modules-editor-settings-CustomFontsColors-tokenColorings.xml");
243: assertNotNull("Can't find custom settingFile", settingFile);
244: assertEquals("Wrong mime type", ColoringStorage.MIME_TYPE,
245: settingFile.getMIMEType());
246:
247: Map<String, AttributeSet> colorings = ess.load(mimePath,
248: "MyProfileXyz", false); //NOI18N
249: assertNotNull("Colorings map should not be null", colorings);
250: assertEquals("Wrong number of colorings", 3, colorings.size());
251:
252: AttributeSet c = colorings.get(name1);
253: assertNull("Coloring '" + name1
254: + "' not defined by modules should be ignored", c);
255:
256: AttributeSet c2 = colorings.get(name2);
257: assertNotNull("Should have " + name2 + " coloring", c2);
258: assertEquals("Wrong number of attributes", 3, c2
259: .getAttributeCount());
260: assertEquals("Wrong strikeThrough", color2, c2
261: .getAttribute(StyleConstants.StrikeThrough));
262: assertEquals("Wrong NameAttribute", name2, c2
263: .getAttribute(StyleConstants.NameAttribute));
264: assertEquals("Wrong DisplayName", name2, c2
265: .getAttribute(EditorStyleConstants.DisplayName));
266: }
267:
268: public void testLegacyFilesWithNoDTD_Issue113137() {
269: ColoringStorage cs = new ColoringStorage(true);
270: Map<String, AttributeSet> colorings = cs.load(MimePath
271: .parse("text/x-legacy"), "NetBeans", false); //NOI18N
272: assertNotNull("Colorings map should not be null", colorings);
273: assertEquals("Wrong number of colorings", 2, colorings.size());
274: {
275: AttributeSet c = colorings.get("pp-active-block");
276: assertNotNull("Should have pp-active-block coloring", c);
277: assertEquals("Wrong bgColor", new Color(0xfffae1f0), c
278: .getAttribute(StyleConstants.Background));
279: }
280: {
281: AttributeSet c = colorings.get("pp-inactive-block");
282: assertNotNull("Should have pp-inactive-block coloring", c);
283: assertEquals("Wrong bgColor", new Color(0xffebe1fa), c
284: .getAttribute(StyleConstants.Background));
285: }
286: }
287: }
|