1 19 20 package org.netbeans.modules.uihandler; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import javax.swing.JButton ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import junit.framework.*; 28 import java.net.URL ; 29 import java.util.logging.Logger ; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.DialogDescriptor; 32 import org.xml.sax.SAXException ; 33 34 38 public class InstallerTest extends NbTestCase { 39 40 public InstallerTest(String testName) { 41 super(testName); 42 } 43 44 protected boolean runInEQ() { 45 return true; 46 } 47 48 protected void setUp() throws Exception { 49 clearWorkDir(); 50 51 Installer installer = Installer.findObject(Installer.class, true); 52 assertNotNull(installer); 53 54 System.setProperty("netbeans.user", getWorkDirPath()); 55 56 installer.restored(); 58 } 59 60 protected void tearDown() throws Exception { 61 Installer installer = Installer.findObject(Installer.class, true); 62 assertNotNull(installer); 63 installer.close(); 64 } 65 66 public void testLogsRereadOnStartup() throws Exception { 67 Logger log = Logger.getLogger("org.netbeans.ui"); log.warning("Something happened"); 69 70 Installer installer = Installer.findObject(Installer.class, true); 71 assertNotNull(installer); 72 installer.close(); 73 Installer.clearLogs(); 74 75 assertEquals("No logs right now", 0, Installer.getLogsSize()); 76 77 installer.restored(); 78 assertEquals("One log is available: " + Installer.getLogs(), 1, Installer.getLogsSize()); 79 assertEquals("The right message is there", 80 "Something happened", Installer.getLogs().get(0).getMessage() 81 ); 82 } 83 84 public void testReadListOfSubmitButtons() throws Exception { 85 String page = "<html><body><form action='http://xyz.cz' method='POST'>" + 86 "<input type='hidden' name='submit' value=\"Send Feedback\"/>" + 87 "\n" + 88 "</form></body></html>"; 89 90 InputStream is = new ByteArrayInputStream (page.getBytes()); 91 JButton def = new JButton ("Default"); 92 Object [] buttons = parseButtons(is, def); 93 is.close(); 94 95 assertNotNull("buttons parsed", buttons); 96 assertEquals("Second is default", def, buttons[1]); 97 assertEquals("There is one button", 2, buttons.length); 98 assertEquals("It is a button", JButton .class, buttons[0].getClass()); 99 JButton b = (JButton )buttons[0]; 100 assertEquals("It is named", "Send Feedback", b.getText()); 101 assertEquals("It url attribute is set", "http://xyz.cz", b.getClientProperty("url")); 102 } 103 104 public void testReadListOfSubmitButtonsWithAmpersand() throws Exception { 105 String page = "<html><body><form action='http://xyz.cz' method='POST'>" + 106 "<input type='hidden' name='submit' value=\"&Send Feedback\"/>" + 107 "\n" + 108 "</form></body></html>"; 109 110 InputStream is = new ByteArrayInputStream (page.getBytes()); 111 JButton def = new JButton ("Default"); 112 Object [] buttons = parseButtons(is, def); 113 is.close(); 114 115 assertNotNull("buttons parsed", buttons); 116 assertEquals("There is one button", 2, buttons.length); 117 assertEquals("Second is default", def, buttons[1]); 118 assertEquals("It is a button", JButton .class, buttons[0].getClass()); 119 JButton b = (JButton )buttons[0]; 120 assertEquals("It is named", "Send Feedback", b.getText()); 121 assertEquals("It url attribute is set", "http://xyz.cz", b.getClientProperty("url")); 122 assertEquals("Mnemonics", 'S', b.getMnemonic()); 123 } 124 125 public void testCanDefineExitButton() throws Exception { 126 String page = "<html><body><form action='http://xyz.cz' method='POST'>" + 127 "\n" + 128 "<input type='hidden' name='submit' value=\"&Send Feedback\"/>" + 129 "\n" + 130 "<input type='hidden' name='exit' value=\"&Cancel\"/>" + 131 "\n" + 132 "</form></body></html>"; 133 134 InputStream is = new ByteArrayInputStream (page.getBytes()); 135 JButton def = new JButton ("Default"); 136 Object [] buttons = parseButtons(is, def); 137 is.close(); 138 139 assertNotNull("buttons parsed", buttons); 140 assertEquals("There are two buttons", 2, buttons.length); 141 if (def == buttons[1]) { 142 fail("Second is default: " + buttons[1]); 143 } 144 assertEquals("It is a button", JButton .class, buttons[0].getClass()); 145 JButton b = (JButton )buttons[0]; 146 assertEquals("It is named", "Send Feedback", b.getText()); 147 assertEquals("It url attribute is set", "http://xyz.cz", b.getClientProperty("url")); 148 assertEquals("Mnemonics", 'S', b.getMnemonic()); 149 150 assertEquals("It is a button", JButton .class, buttons[1].getClass()); 151 b = (JButton )buttons[1]; 152 assertEquals("It is named", "Cancel", b.getText()); 153 assertNull("No url", b.getClientProperty("url")); 154 assertEquals("Mnemonics", 'C', b.getMnemonic()); 155 } 156 157 public void testReadAllButtons() throws Exception { 158 String page = 159 "<html><body><form action='http://xyz.cz' method='POST'>" + 160 " <input type='hidden' name='submit' value=\"&Send Feedback\"/>" + 161 " <input type='hidden' name='never-again' value=\"&No and do not Bother Again\"/>" + 162 "\n" + 163 " <input type='hidden' name='view-data' value=\"&View Data\"/>" + 164 "\n" + 165 "</form></body></html>"; 166 167 InputStream is = new ByteArrayInputStream (page.getBytes()); 168 Object [] buttons = parseButtons(is, null); 169 is.close(); 170 171 assertNotNull("buttons parsed", buttons); 172 assertEquals("There are 3 button", 3, buttons.length); 173 assertEquals("It is a button", JButton .class, buttons[0].getClass()); 174 assertEquals("It is a button2", JButton .class, buttons[1].getClass()); 175 assertEquals("It is a button3", JButton .class, buttons[2].getClass()); 176 177 { 178 JButton b = (JButton )buttons[0]; 179 assertEquals("It is named", "Send Feedback", b.getText()); 180 assertEquals("It url attribute is set", "http://xyz.cz", b.getClientProperty("url")); 181 assertEquals("Mnemonics", 'S', b.getMnemonic()); 182 assertEquals("submit", b.getActionCommand()); 183 URL [] url = new URL [1]; 184 String r = Installer.decodeButtons(b, url); 185 assertEquals("action is ", "submit", r); 186 assertEquals("no url", new URL ("http://xyz.cz"), url[0]); 187 } 188 { 189 JButton b = (JButton )buttons[1]; 190 assertEquals("It is named", "No and do not Bother Again", b.getText()); 191 assertEquals("It url attribute is not set", null, b.getClientProperty("url")); 192 assertEquals("Mnemonics", 'N', b.getMnemonic()); 193 assertEquals("never-again", b.getActionCommand()); 194 } 195 { 196 JButton b = (JButton )buttons[2]; 197 assertEquals("It is named", "View Data", b.getText()); 198 assertEquals("It url attribute is not set", null, b.getClientProperty("url")); 199 assertEquals("Mnemonics", 'V', b.getMnemonic()); 200 assertEquals("view-data", b.getActionCommand()); 201 202 URL [] url = new URL [1]; 203 String r = Installer.decodeButtons(b, url); 204 assertEquals("action is ", "view-data", r); 205 assertEquals("no url", null, url[0]); 206 } 207 } 208 209 public void testLeftAligned() throws Exception { 210 String page = 211 "<html><body><form action='http://xyz.cz' method='POST'>" + 212 " <input type='hidden' name='submit' value=\"&Send Feedback\"/>" + 213 " <input type='hidden' name='never-again' value=\"&No and do not Bother Again\"/>" + 214 "\n" + 215 " <input type='hidden' name='view-data' align='left' value=\"&View Data\" alt='Hide'/>" + 216 "\n" + 217 "</form></body></html>"; 218 219 InputStream is = new ByteArrayInputStream (page.getBytes()); 220 DialogDescriptor dd = new DialogDescriptor(null, null); 221 Installer.parseButtons(is, null, dd); 222 is.close(); 223 224 Object [] buttons = dd.getOptions(); 225 assertNotNull("buttons parsed", buttons); 226 assertEquals("There are 2 buttons", 2, buttons.length); 227 assertEquals("It is a button", JButton .class, buttons[0].getClass()); 228 assertEquals("It is a button2", JButton .class, buttons[1].getClass()); 229 230 { 231 JButton b = (JButton )buttons[0]; 232 assertEquals("It is named", "Send Feedback", b.getText()); 233 assertEquals("It url attribute is set", "http://xyz.cz", b.getClientProperty("url")); 234 assertEquals("Mnemonics", 'S', b.getMnemonic()); 235 assertEquals("submit", b.getActionCommand()); 236 URL [] url = new URL [1]; 237 String r = Installer.decodeButtons(b, url); 238 assertEquals("action is ", "submit", r); 239 assertEquals("no url", new URL ("http://xyz.cz"), url[0]); 240 } 241 { 242 JButton b = (JButton )buttons[1]; 243 assertEquals("It is named", "No and do not Bother Again", b.getText()); 244 assertEquals("It url attribute is not set", null, b.getClientProperty("url")); 245 assertEquals("Mnemonics", 'N', b.getMnemonic()); 246 assertEquals("never-again", b.getActionCommand()); 247 } 248 249 buttons = dd.getAdditionalOptions(); 250 assertNotNull("There are some additionals", buttons); 251 assertEquals("One is there", 1, buttons.length); 252 { 253 JButton b = (JButton )buttons[0]; 254 assertEquals("It is named", "View Data", b.getText()); 255 assertEquals("It url attribute is not set", null, b.getClientProperty("url")); 256 assertEquals("Mnemonics", 'V', b.getMnemonic()); 257 assertEquals("view-data", b.getActionCommand()); 258 259 URL [] url = new URL [1]; 260 String r = Installer.decodeButtons(b, url); 261 assertEquals("action is ", "view-data", r); 262 assertEquals("no url", null, url[0]); 263 assertEquals("alt is there", "Hide", b.getClientProperty("alt")); 264 } 265 } 266 267 private static Object [] parseButtons(InputStream is, Object def) throws IOException , ParserConfigurationException , SAXException { 268 DialogDescriptor dd = new DialogDescriptor(null, null); 269 Installer.parseButtons(is, def, dd); 270 return dd.getOptions(); 271 } 272 } 273 | Popular Tags |