KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > uihandler > UINodeTest


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.uihandler;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.util.logging.Handler JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import junit.framework.TestCase;
28 import java.util.logging.LogRecord JavaDoc;
29 import org.netbeans.lib.uihandler.LogRecords;
30 import org.openide.nodes.Node;
31
32 /**
33  *
34  * @author Jaroslav Tulach
35  */

36 public class UINodeTest extends TestCase {
37     
38     public UINodeTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46     }
47
48     public void testDisplayNameOfTheNode() throws Exception JavaDoc {
49         LogRecord JavaDoc r = new LogRecord JavaDoc(Level.INFO, "test_msg");
50         r.setResourceBundleName("org.netbeans.modules.uihandler.TestBundle");
51         r.setResourceBundle(ResourceBundle.getBundle("org.netbeans.modules.uihandler.TestBundle"));
52         r.setParameters(new Object JavaDoc[] { new Integer JavaDoc(1), "Ahoj" });
53         
54         Node n = UINode.create(r);
55         assertEquals("Name is taken from the message", "test_msg", n.getName());
56         
57         if (!n.getDisplayName().matches(".*Ahoj.*1.*")) {
58             fail("wrong display name, shall contain Ahoj and 1: " + n.getDisplayName());
59         }
60         assertSerializedWell(n);
61     }
62     
63     public void testSomeNPE() throws Exception JavaDoc {
64         LogRecord JavaDoc r = new LogRecord JavaDoc(Level.FINE, "UI_ACTION_EDITOR");
65         Node n = UINode.create(r);
66         assertNotNull(n);
67         assertEquals("No name", "", n.getDisplayName());
68         assertNotNull(n.getName());
69         assertSerializedWell(n);
70     }
71     
72     public void testHasNonNullName() throws Exception JavaDoc {
73         LogRecord JavaDoc r = new LogRecord JavaDoc(Level.WARNING, null);
74         r.setThrown(new Exception JavaDoc());
75         Node n = UINode.create(r);
76         assertNotNull(n);
77         assertNotNull(n.getName());
78         assertSerializedWell(n);
79     }
80     public void testHasNonNullNameWhenMessageIsGiven() throws Exception JavaDoc {
81         Exception JavaDoc my = new Exception JavaDoc("Ahoj");
82         LogRecord JavaDoc r = new LogRecord JavaDoc(Level.WARNING, my.getMessage());
83         r.setThrown(my);
84         Node n = UINode.create(r);
85         assertNotNull(n);
86         assertNotNull(n.getName());
87         assertSerializedWell(n);
88     }
89     
90     private static void assertSerializedWell(Node n) throws Exception JavaDoc {
91         LogRecord JavaDoc r = n.getLookup().lookup(LogRecord JavaDoc.class);
92         assertNotNull("There is a log record", r);
93         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
94         LogRecords.write(os, r);
95         os.close();
96
97         {
98             ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(os.toByteArray());
99             class H extends Handler JavaDoc {
100                 public LogRecord JavaDoc nr;
101                 
102                 public void publish(LogRecord JavaDoc arg0) {
103                     assertNull("First call", nr);
104                     nr = arg0;
105                 }
106
107                 public void flush() {
108                 }
109
110                 public void close() throws SecurityException JavaDoc {
111                 }
112             }
113             
114             H handler = new H();
115             LogRecords.scan(is, handler);
116             LogRecord JavaDoc nr = handler.nr;
117             is.close();
118
119             Node newNode = UINode.create(nr);
120
121             assertEquals("name", n.getName(), newNode.getName());
122             assertEquals("displayName", n.getDisplayName(), newNode.getDisplayName());
123             assertEquals("htmlName", n.getHtmlDisplayName(), newNode.getHtmlDisplayName());
124         }
125         class H extends Handler JavaDoc {
126             LogRecord JavaDoc one;
127             
128             public void publish(LogRecord JavaDoc a) {
129                 assertNull("This is first one: " + a, one);
130                 one = a;
131             }
132
133             public void flush() {
134             }
135
136             public void close() throws SecurityException JavaDoc {
137             }
138         }
139         
140         H handler = new H();
141         
142         {
143             ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(os.toByteArray());
144             LogRecords.scan(is, handler);
145             is.close();
146
147             Node newNode = UINode.create(handler.one);
148
149             assertEquals("name", n.getName(), newNode.getName());
150             assertEquals("displayName", n.getDisplayName(), newNode.getDisplayName());
151             assertEquals("htmlName", n.getHtmlDisplayName(), newNode.getHtmlDisplayName());
152         }
153     }
154 }
155
Popular Tags