KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import junit.framework.*;
28 import java.net.URL JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.DialogDescriptor;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  *
36  * @author Jaroslav Tulach
37  */

38 public class InstallerTest extends NbTestCase {
39     
40     public InstallerTest(String JavaDoc testName) {
41         super(testName);
42     }
43     
44     protected boolean runInEQ() {
45         return true;
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         clearWorkDir();
50         
51         Installer installer = Installer.findObject(Installer.class, true);
52         assertNotNull(installer);
53
54         System.setProperty("netbeans.user", getWorkDirPath());
55         
56         // setup the listing
57
installer.restored();
58     }
59
60     protected void tearDown() throws Exception JavaDoc {
61         Installer installer = Installer.findObject(Installer.class, true);
62         assertNotNull(installer);
63         installer.close();
64     }
65     
66     public void testLogsRereadOnStartup() throws Exception JavaDoc {
67         Logger JavaDoc log = Logger.getLogger("org.netbeans.ui"); // NOI18N
68
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 JavaDoc {
85         String JavaDoc 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 JavaDoc is = new ByteArrayInputStream JavaDoc(page.getBytes());
91         JButton JavaDoc def = new JButton JavaDoc("Default");
92         Object JavaDoc[] 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 JavaDoc.class, buttons[0].getClass());
99         JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc {
105         String JavaDoc page = "<html><body><form action='http://xyz.cz' method='POST'>" +
106             "<input type='hidden' name='submit' value=\"&amp;Send Feedback\"/>" +
107             "\n" +
108             "</form></body></html>";
109         
110         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(page.getBytes());
111         JButton JavaDoc def = new JButton JavaDoc("Default");
112         Object JavaDoc[] 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 JavaDoc.class, buttons[0].getClass());
119         JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc {
126         String JavaDoc page = "<html><body><form action='http://xyz.cz' method='POST'>" +
127             "\n" +
128             "<input type='hidden' name='submit' value=\"&amp;Send Feedback\"/>" +
129             "\n" +
130             "<input type='hidden' name='exit' value=\"&amp;Cancel\"/>" +
131             "\n" +
132             "</form></body></html>";
133         
134         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(page.getBytes());
135         JButton JavaDoc def = new JButton JavaDoc("Default");
136         Object JavaDoc[] 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 JavaDoc.class, buttons[0].getClass());
145         JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc.class, buttons[1].getClass());
151         b = (JButton JavaDoc)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 JavaDoc {
158         String JavaDoc page =
159             "<html><body><form action='http://xyz.cz' method='POST'>" +
160             " <input type='hidden' name='submit' value=\"&amp;Send Feedback\"/>" +
161             " <input type='hidden' name='never-again' value=\"&amp;No and do not Bother Again\"/>" +
162             "\n" +
163             " <input type='hidden' name='view-data' value=\"&amp;View Data\"/>" +
164             "\n" +
165             "</form></body></html>";
166         
167         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(page.getBytes());
168         Object JavaDoc[] 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 JavaDoc.class, buttons[0].getClass());
174         assertEquals("It is a button2", JButton JavaDoc.class, buttons[1].getClass());
175         assertEquals("It is a button3", JButton JavaDoc.class, buttons[2].getClass());
176         
177         {
178             JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc[] url = new URL JavaDoc[1];
184             String JavaDoc r = Installer.decodeButtons(b, url);
185             assertEquals("action is ", "submit", r);
186             assertEquals("no url", new URL JavaDoc("http://xyz.cz"), url[0]);
187         }
188         {
189             JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc b = (JButton JavaDoc)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 JavaDoc[] url = new URL JavaDoc[1];
203             String JavaDoc 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 JavaDoc {
210         String JavaDoc page =
211             "<html><body><form action='http://xyz.cz' method='POST'>" +
212             " <input type='hidden' name='submit' value=\"&amp;Send Feedback\"/>" +
213             " <input type='hidden' name='never-again' value=\"&amp;No and do not Bother Again\"/>" +
214             "\n" +
215             " <input type='hidden' name='view-data' align='left' value=\"&amp;View Data\" alt='Hide'/>" +
216             "\n" +
217             "</form></body></html>";
218         
219         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(page.getBytes());
220         DialogDescriptor dd = new DialogDescriptor(null, null);
221         Installer.parseButtons(is, null, dd);
222         is.close();
223         
224         Object JavaDoc[] buttons = dd.getOptions();
225         assertNotNull("buttons parsed", buttons);
226         assertEquals("There are 2 buttons", 2, buttons.length);
227         assertEquals("It is a button", JButton JavaDoc.class, buttons[0].getClass());
228         assertEquals("It is a button2", JButton JavaDoc.class, buttons[1].getClass());
229         
230         {
231             JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc[] url = new URL JavaDoc[1];
237             String JavaDoc r = Installer.decodeButtons(b, url);
238             assertEquals("action is ", "submit", r);
239             assertEquals("no url", new URL JavaDoc("http://xyz.cz"), url[0]);
240         }
241         {
242             JButton JavaDoc b = (JButton JavaDoc)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 JavaDoc b = (JButton JavaDoc)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 JavaDoc[] url = new URL JavaDoc[1];
260             String JavaDoc 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 JavaDoc[] parseButtons(InputStream JavaDoc is, Object JavaDoc def) throws IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
268         DialogDescriptor dd = new DialogDescriptor(null, null);
269         Installer.parseButtons(is, def, dd);
270         return dd.getOptions();
271     }
272 }
273
Popular Tags