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:
042: package org.netbeans.beaninfo.editors;
043:
044: import java.awt.Color;
045: import java.util.Locale;
046: import javax.xml.parsers.DocumentBuilderFactory;
047: import junit.framework.TestCase;
048: import org.openide.explorer.propertysheet.editors.XMLPropertyEditor;
049: import org.openide.util.NbBundle;
050: import org.w3c.dom.Document;
051: import org.w3c.dom.NamedNodeMap;
052: import org.w3c.dom.Node;
053:
054: /**
055: *
056: * @author Radim
057: */
058: public class ColorEditorTest extends TestCase {
059:
060: public ColorEditorTest(String testName) {
061: super (testName);
062: }
063:
064: public void testStoreToXml() throws Exception {
065: ColorEditor.SuperColor sc = new ColorEditor.SuperColor(
066: "TextField.inactiveBackground",
067: ColorEditor.SWING_PALETTE, Color.BLUE);
068: System.out.println("original " + sc);
069: XMLPropertyEditor propEd = new ColorEditor();
070: propEd.setValue(sc);
071: Document doc = DocumentBuilderFactory.newInstance()
072: .newDocumentBuilder().newDocument();
073: Node element = propEd.storeToXML(doc);
074:
075: // NamedNodeMap nodeMap = element.getAttributes();
076: // for (int i = 0; i < nodeMap.getLength(); i++) {
077: // System.out.println("attr "+i+", "+nodeMap.item(i));
078: // }
079:
080: propEd.readFromXML(element);
081: Color restoredColor = (Color) propEd.getValue();
082: System.out.println("restoredColor " + restoredColor);
083: assertEquals("Restored value has to be the same", sc,
084: restoredColor);
085: assertTrue("It is SuperColor",
086: restoredColor instanceof ColorEditor.SuperColor);
087: assertEquals(
088: "Generate Java source with UI color.",
089: "javax.swing.UIManager.getDefaults().getColor(\"TextField.inactiveBackground\")",
090: propEd.getJavaInitializationString());
091: }
092:
093: public void testStoreToXmlWithVaryingLocale() throws Exception {
094: Locale loc = Locale.getDefault();
095: Locale.setDefault(new Locale("cs", "CZ"));
096: ColorEditor.SuperColor sc = new ColorEditor.SuperColor(NbBundle
097: .getMessage(ColorEditor.class, "LAB_Blue"),
098: ColorEditor.AWT_PALETTE, Color.BLUE);
099: XMLPropertyEditor propEd = new ColorEditor();
100: propEd.setValue(sc);
101: System.out.println("original " + sc + " Java "
102: + propEd.getJavaInitializationString());
103: String javaCode = propEd.getJavaInitializationString();
104: Document doc = DocumentBuilderFactory.newInstance()
105: .newDocumentBuilder().newDocument();
106: Node element = propEd.storeToXML(doc);
107:
108: NamedNodeMap nodeMap = element.getAttributes();
109: for (int i = 0; i < nodeMap.getLength(); i++) {
110: System.out.println("attr " + i + ", " + nodeMap.item(i));
111: }
112:
113: Locale.setDefault(new Locale("en"));
114: ColorEditor.awtColorNames = null; // clear the cache of localized names
115:
116: propEd.readFromXML(element);
117: Color restoredColor = (Color) propEd.getValue();
118: System.out.println("restoredColor " + restoredColor + " Java "
119: + propEd.getJavaInitializationString());
120: assertEquals(sc.getBlue(), restoredColor.getBlue());
121: assertEquals(sc.getGreen(), restoredColor.getGreen());
122: assertEquals(sc.getBlue(), restoredColor.getBlue());
123: assertTrue("It is SuperColor",
124: restoredColor instanceof ColorEditor.SuperColor);
125: assertEquals("Java code works", javaCode, propEd
126: .getJavaInitializationString());
127: doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
128: .newDocument();
129: element = propEd.storeToXML(doc);
130:
131: Locale.setDefault(new Locale("cs", "CZ"));
132: ColorEditor.awtColorNames = null; // clear the cache of localized names
133:
134: // back to czech is even identical
135: propEd.readFromXML(element);
136: restoredColor = (Color) propEd.getValue();
137: assertTrue("It is SuperColor",
138: restoredColor instanceof ColorEditor.SuperColor);
139: assertEquals("Java code works", javaCode, propEd
140: .getJavaInitializationString());
141: assertEquals("Restored value has to be the same", sc,
142: restoredColor);
143:
144: Locale.setDefault(loc);
145: }
146:
147: public void testStoreToXmlWhenUIResourceIsMissing()
148: throws Exception {
149: ColorEditor.SuperColor sc = new ColorEditor.SuperColor(
150: "Fake.inactiveBackground", ColorEditor.SWING_PALETTE,
151: Color.BLUE);
152: System.out.println("original " + sc);
153: XMLPropertyEditor propEd = new ColorEditor();
154: propEd.setValue(sc);
155: Document doc = DocumentBuilderFactory.newInstance()
156: .newDocumentBuilder().newDocument();
157: Node element = propEd.storeToXML(doc);
158:
159: NamedNodeMap nodeMap = element.getAttributes();
160: for (int i = 0; i < nodeMap.getLength(); i++) {
161: System.out.println("attr " + i + ", " + nodeMap.item(i));
162: }
163:
164: propEd.readFromXML(element);
165: Color restoredColor = (Color) propEd.getValue();
166: System.out.println("restoredColor " + restoredColor);
167: assertEquals("Restored value has to be the same", sc,
168: restoredColor);
169: assertTrue("It is SuperColor",
170: restoredColor instanceof ColorEditor.SuperColor);
171: assertEquals(
172: "Generate Java source with UI color.",
173: "javax.swing.UIManager.getDefaults().getColor(\"Fake.inactiveBackground\")",
174: propEd.getJavaInitializationString());
175: }
176:
177: public void testGetValue() throws Exception {
178: Color c = new Color(16, 16, 16);
179: System.out.println("original " + c);
180: XMLPropertyEditor propEd = new ColorEditor();
181: propEd.setValue(c);
182: Document doc = DocumentBuilderFactory.newInstance()
183: .newDocumentBuilder().newDocument();
184: Node element = propEd.storeToXML(doc);
185:
186: NamedNodeMap nodeMap = element.getAttributes();
187: for (int i = 0; i < nodeMap.getLength(); i++) {
188: System.out.println("attr " + i + ", " + nodeMap.item(i));
189: }
190:
191: propEd.readFromXML(element);
192: Color restoredColor = (Color) propEd.getValue();
193: System.out.println("restoredColor " + restoredColor);
194: assertEquals("Restored value has to be the same", c,
195: restoredColor);
196: assertTrue("It is Color", restoredColor instanceof Color);
197: assertFalse("It is not SuperColor",
198: restoredColor instanceof ColorEditor.SuperColor);
199: System.out.println("GENERATE: "
200: + propEd.getJavaInitializationString());
201: assertEquals("Generate Java source with UI color.",
202: "new java.awt.Color(16, 16, 16)", propEd
203: .getJavaInitializationString());
204: }
205: }
|