1 19 20 package org.netbeans.modules.uihandler; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.util.ResourceBundle ; 25 import java.util.logging.Handler ; 26 import java.util.logging.Level ; 27 import junit.framework.TestCase; 28 import java.util.logging.LogRecord ; 29 import org.netbeans.lib.uihandler.LogRecords; 30 import org.openide.nodes.Node; 31 32 36 public class UINodeTest extends TestCase { 37 38 public UINodeTest(String testName) { 39 super(testName); 40 } 41 42 protected void setUp() throws Exception { 43 } 44 45 protected void tearDown() throws Exception { 46 } 47 48 public void testDisplayNameOfTheNode() throws Exception { 49 LogRecord r = new LogRecord (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 [] { new Integer (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 { 64 LogRecord r = new LogRecord (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 { 73 LogRecord r = new LogRecord (Level.WARNING, null); 74 r.setThrown(new Exception ()); 75 Node n = UINode.create(r); 76 assertNotNull(n); 77 assertNotNull(n.getName()); 78 assertSerializedWell(n); 79 } 80 public void testHasNonNullNameWhenMessageIsGiven() throws Exception { 81 Exception my = new Exception ("Ahoj"); 82 LogRecord r = new LogRecord (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 { 91 LogRecord r = n.getLookup().lookup(LogRecord .class); 92 assertNotNull("There is a log record", r); 93 ByteArrayOutputStream os = new ByteArrayOutputStream (); 94 LogRecords.write(os, r); 95 os.close(); 96 97 { 98 ByteArrayInputStream is = new ByteArrayInputStream (os.toByteArray()); 99 class H extends Handler { 100 public LogRecord nr; 101 102 public void publish(LogRecord arg0) { 103 assertNull("First call", nr); 104 nr = arg0; 105 } 106 107 public void flush() { 108 } 109 110 public void close() throws SecurityException { 111 } 112 } 113 114 H handler = new H(); 115 LogRecords.scan(is, handler); 116 LogRecord 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 { 126 LogRecord one; 127 128 public void publish(LogRecord 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 { 137 } 138 } 139 140 H handler = new H(); 141 142 { 143 ByteArrayInputStream is = new ByteArrayInputStream (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 |