KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > ColorEditorTest


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.beaninfo.editors;
21
22 import java.awt.Color JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import junit.framework.TestCase;
25 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 /**
31  *
32  * @author Radim
33  */

34 public class ColorEditorTest extends TestCase {
35
36     public ColorEditorTest (String JavaDoc testName) {
37         super (testName);
38     }
39
40     public void testStoreToXml() throws Exception JavaDoc {
41         ColorEditor.SuperColor sc = new ColorEditor.SuperColor(
42                 "TextField.inactiveBackground",
43                 ColorEditor.SWING_PALETTE,
44                 Color.BLUE);
45         System.out.println("original "+sc);
46         XMLPropertyEditor propEd = new ColorEditor();
47         propEd.setValue(sc);
48         Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
49         Node JavaDoc element = propEd.storeToXML(doc);
50
51         NamedNodeMap JavaDoc nodeMap = element.getAttributes();
52         for (int i = 0; i < nodeMap.getLength(); i++) {
53             System.out.println("attr "+i+", "+nodeMap.item(i));
54         }
55         
56         propEd.readFromXML(element);
57         Color JavaDoc restoredColor = (Color JavaDoc)propEd.getValue();
58         System.out.println("restoredColor "+restoredColor);
59         assertEquals("Restored value has to be the same", sc, restoredColor);
60         assertTrue("It is SuperColor", restoredColor instanceof ColorEditor.SuperColor);
61         assertEquals ("Generate Java source with UI color.", "javax.swing.UIManager.getDefaults().getColor(\"TextField.inactiveBackground\")", propEd.getJavaInitializationString ());
62     }
63     
64     public void testStoreToXmlWhenUIResourceIsMissing() throws Exception JavaDoc {
65         ColorEditor.SuperColor sc = new ColorEditor.SuperColor(
66                 "Fake.inactiveBackground",
67                 ColorEditor.SWING_PALETTE,
68                 Color.BLUE);
69         System.out.println("original "+sc);
70         XMLPropertyEditor propEd = new ColorEditor();
71         propEd.setValue(sc);
72         Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
73         Node JavaDoc element = propEd.storeToXML(doc);
74
75         NamedNodeMap JavaDoc nodeMap = element.getAttributes();
76         for (int i = 0; i < nodeMap.getLength(); i++) {
77             System.out.println("attr "+i+", "+nodeMap.item(i));
78         }
79         
80         propEd.readFromXML(element);
81         Color JavaDoc restoredColor = (Color JavaDoc)propEd.getValue();
82         System.out.println("restoredColor "+restoredColor);
83         assertEquals("Restored value has to be the same", sc, restoredColor);
84         assertTrue("It is SuperColor", restoredColor instanceof ColorEditor.SuperColor);
85         assertEquals ("Generate Java source with UI color.", "javax.swing.UIManager.getDefaults().getColor(\"Fake.inactiveBackground\")", propEd.getJavaInitializationString ());
86     }
87     
88     public void testGetValue () throws Exception JavaDoc {
89         Color JavaDoc c = new Color JavaDoc (16, 16, 16);
90         System.out.println("original "+c);
91         XMLPropertyEditor propEd = new ColorEditor();
92         propEd.setValue(c);
93         Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
94         Node JavaDoc element = propEd.storeToXML(doc);
95
96         NamedNodeMap JavaDoc nodeMap = element.getAttributes();
97         for (int i = 0; i < nodeMap.getLength(); i++) {
98             System.out.println("attr "+i+", "+nodeMap.item(i));
99         }
100         
101         propEd.readFromXML(element);
102         Color JavaDoc restoredColor = (Color JavaDoc)propEd.getValue();
103         System.out.println("restoredColor "+restoredColor);
104         assertEquals("Restored value has to be the same", c, restoredColor);
105         assertTrue("It is Color", restoredColor instanceof Color JavaDoc);
106         assertFalse("It is not SuperColor", restoredColor instanceof ColorEditor.SuperColor);
107         System.out.println("GENERATE: " + propEd.getJavaInitializationString ());
108         assertEquals ("Generate Java source with UI color.", "new java.awt.Color(16, 16, 16)", propEd.getJavaInitializationString ());
109     }
110 }
111
Popular Tags