KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > Main


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.test.editor.app;
21
22 import org.netbeans.modules.java.editor.options.JavaOptions;
23 import org.netbeans.test.editor.app.core.*;
24 import org.netbeans.test.editor.app.util.*;
25 import org.netbeans.test.editor.app.gui.actions.*;
26 import org.netbeans.test.editor.app.gui.*;
27 import org.netbeans.test.editor.app.core.actions.*;
28 import org.netbeans.test.editor.app.core.cookies.*;
29
30 import org.openide.loaders.XMLDataObject;
31 import org.openide.xml.XMLUtil;
32
33 import org.openide.options.SystemOption;
34
35 import java.beans.*;
36 import java.io.*;
37 import java.net.URL JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import java.util.*;
40 import javax.xml.parsers.*; //!!!
41

42 import org.w3c.dom.*;
43 import org.w3c.dom.Document JavaDoc;
44 import org.apache.xerces.parsers.DOMParser;
45 import org.netbeans.modules.editor.html.HTMLKit;
46 import org.netbeans.modules.editor.java.JavaKit;
47 import org.netbeans.modules.editor.options.BaseOptions;
48 import org.netbeans.modules.html.editor.options.HTMLOptions;
49 import org.openide.util.Lookup;
50 import org.openide.util.lookup.ProxyLookup;
51 import org.xml.sax.InputSource JavaDoc;
52
53
54
55 /** Static class for execution of Editor Test Application.
56  * @author ehucka
57  * @version 2.0
58  */

59
60 public class Main extends java.lang.Object JavaDoc {
61     
62     public static Test test;
63     public static TestEditorFrame frame;
64     private static boolean changed=false;
65     
66     private static String JavaDoc fileName = null;
67     
68     public static PrintStream log;
69     
70     private static PrintStream oldErr;
71     
72     private static boolean debug = true;
73     
74     private static boolean compAutoPopup;
75     private static boolean docAutoPopup;
76     private static boolean htmlAutoPopup;
77     
78     private static boolean succeded = false;
79     
80     public static Thread JavaDoc mainThread=null;
81     
82     private static void registerActions() {
83         ActionRegistry.clear();
84         ActionRegistry.getDefault().addAction(PerformCookie.class, new TestExecuteAction());
85         ActionRegistry.getDefault().addAction(LoggingCookie.class, new TestStartLoggingAction());
86         ActionRegistry.getDefault().addAction(LoggingCookie.class, new TestStopLoggingAction());
87         ActionRegistry.getDefault().addAction(PackCookie.class, new TestPackAction());
88         TestGroup.createNewTypes();
89     }
90     
91     public static boolean getSucceded() {
92         return succeded;
93     }
94     
95     private static void performTest(String JavaDoc[] args) {
96         int count = args.length;
97         String JavaDoc lastOpened = null;
98         
99         if (count % 2 == 1)
100             count--;
101         for (int cntr = 0; cntr < count; cntr += 2) {
102             if (args[cntr] == null) {
103                 Main.log("Supposed to open *null* file - impossible!");
104                 succeded = false;
105                 return;
106             }
107             if (!args[cntr].equals(lastOpened)) {
108                 if (!openTest(args[cntr])) {
109                     Main.log("While trying to open file: " + args[0] + " an unexcpected error occurs.");
110                     succeded = false;
111                     return;
112                 } else {
113                     lastOpened = args[cntr];
114                 }
115             }
116             if (args[cntr + 1].length() > 0) {
117                 Test.setTesting();
118                 test.perform(args[cntr + 1]);
119             }
120         }
121         succeded = true;
122     }
123     
124     private static void backupAndSettings() {
125         OutputStream file = null;
126         try {
127             log = new PrintStream(new MultipleOutputStream(new OutputStream[] {
128                 new OutputStream() {
129                     char[] buffer=new char[512];
130                     int i=0;
131                     public void write(int b) {
132                         buffer[i++]=(char)b;
133                         if (b == '\n') {
134                             if (frame != null) {
135                                 frame.appendHistory(new String JavaDoc(buffer,0,i));
136                             }
137                             i=0;
138                         } else if (i == 512) {
139                             if (frame != null) {
140                                 frame.appendHistory(new String JavaDoc(buffer));
141                             }
142                             i=0;
143                         }
144                     }
145                 }, System.err
146             }));
147         } catch (Exception JavaDoc ex) {
148             ex.printStackTrace();
149         }
150         oldErr = System.err;
151         System.setErr(log);
152         Main.log("Setting log file finished.");
153         
154         JavaOptions opts = (JavaOptions)(SystemOption.findObject(JavaOptions.class));
155         if (opts == null) {
156             System.err.println("Didn't find Java options from SystemOptions. Try Lookup for Base Options...");
157             BaseOptions bo;
158             bo=(BaseOptions)(Lookup.getDefault().lookup(BaseOptions.class));
159             opts = (JavaOptions)(bo.getOptions(JavaKit.class));
160             if (opts == null) {
161                 System.err.println("Base Options don't contain JavaKit Options.");
162                 return;
163             }
164         }
165         compAutoPopup = opts.getCompletionAutoPopup();
166         opts.setCompletionAutoPopup(false);
167         docAutoPopup = opts.getJavaDocAutoPopup();
168         opts.setJavaDocAutoPopup(false);
169         
170         HTMLOptions hopts = (HTMLOptions)(SystemOption.findObject(HTMLOptions.class));
171         if (hopts == null) {
172             System.err.println("Didn't find HTML options from SystemOptions. Try Lookup for Base Options...");
173             BaseOptions bo;
174             bo=(BaseOptions)(Lookup.getDefault().lookup(BaseOptions.class));
175             hopts = (HTMLOptions)(bo.getOptions(HTMLKit.class));
176             if (hopts == null) {
177                 System.err.println("Base Options don't contain JavaKit Options.");
178                 return;
179             }
180         }
181         htmlAutoPopup = hopts.getCompletionAutoPopup();
182         hopts.setCompletionAutoPopup(false);
183     }
184     
185     /** Start Editor Test Application. It must be started by Internal Execurion in IDE.
186      * User can starts it either for test design or for automated test execution.
187      * For automated test execution <CODE>args</CODE> contains name of the test file
188      * and name of called Call Action.
189      * @param args test file and call action's name
190      */

191     public static void main(String JavaDoc args[]) {
192         //backup completion autopopup option state
193
backupAndSettings();
194         frame= new TestEditorFrame();
195         
196         //Editor kit heve to be set in AWT thread
197
try {
198             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
199                 public void run() {
200                     frame.getEditor().setEditorKit(1);
201                 }
202             });
203         } catch (Exception JavaDoc e) {
204             e.printStackTrace(System.err);
205         }
206         if (args.length > 0) {
207             frame.show();
208             System.err.println("to perform");
209             performTest(args);
210             Main.log("Performing finished!");
211             // System.err.println(frame.getEditor().getText());
212
frame.killFrame();
213             closeTest();
214             finish();
215             return;
216         }
217         registerActions();
218         test = new Test("Default");
219         test.addPropertyChangeListener(new PropertyChangeListener() {
220             public void propertyChange(final java.beans.PropertyChangeEvent JavaDoc p1) {
221                 changed=true;
222             }
223         });
224         frame.setTest(test);
225         test.logger = new Logger(frame.getEditor());
226         frame.newRootWindow();
227         frame.show();
228     }
229     
230     public static void setFileName(String JavaDoc name) {
231         fileName = name;
232         if (frame != null) {
233             frame.setTitleFileName(name);
234         }
235     }
236     
237     public static String JavaDoc getFileName() {
238         return fileName;
239     }
240     
241     public static boolean newTest() {
242         if (changed) return false;
243         test = new Test("Default");
244         test.addPropertyChangeListener(new PropertyChangeListener() {
245             public void propertyChange(final java.beans.PropertyChangeEvent JavaDoc p1) {
246                 changed=true;
247             }
248         });
249         test.logger = new Logger(frame.getEditor());
250         frame.setTest(test);
251         setFileName("");
252         log("New test created");
253         return true;
254     }
255     
256     public static Test loadTest(URL JavaDoc testFile) {
257         setFileName(null);
258         log("Test "+fileName+" opened.");
259         try {
260             return loadTest(testFile.openStream());
261         } catch( IOException e ) {
262             Main.log("Application cannot read test from URL: " + testFile );
263             e.printStackTrace();
264             return null;
265         }
266     }
267     
268     public static Test loadTest(String JavaDoc aFileName) {
269         setFileName(aFileName);
270         log("Test "+fileName+" opened.");
271         try {
272             return loadTest(new FileInputStream(aFileName));
273         } catch( IOException e ) {
274             Main.log("Application cannot read test from file: " + aFileName );
275             e.printStackTrace();
276             return null;
277         }
278     }
279     
280     private static Test loadTest(InputStream is) {
281         Test test = null;
282         try {
283             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
284             try {
285                 if (debug)
286                     Main.log("Loading test:");
287                 DOMParser parser = new DOMParser();
288                 parser.parse(new InputSource JavaDoc(new BufferedInputStream(is)));
289                 Element el = (Element)parser.getDocument().getElementsByTagName("Test").item(0);
290                 test = new Test(el);
291                 if (debug)
292                     Main.log("Rebuilding loggers:");
293                 test.rebuidlLoggers();
294                 if (debug)
295                     Main.log("Done.");
296             } catch( org.xml.sax.SAXException JavaDoc e ) {
297                 Main.log("App cannot parse file: " + e );
298                 e.printStackTrace();
299                 return null;
300             }
301         } catch(ParserConfigurationException e) {
302             Main.log("DocumentBuilder cannot be created!");
303             e.printStackTrace();
304             return null;
305         } catch (Exception JavaDoc ex) {
306             Main.log("Exception during the opening of file.");
307             ex.printStackTrace();
308         }
309         return test;
310     }
311     
312     private static PropertyChangeListener listener = null;
313     
314     public static boolean openTest(String JavaDoc aFileName) {
315         Test wasRead = null;
316         URL JavaDoc inIDE = Main.class.getResource(aFileName);
317         
318         if (inIDE == null) {
319             wasRead = loadTest(aFileName);
320             if (wasRead == null)
321                 return false;
322         } else {
323             wasRead = loadTest(inIDE);
324             if (wasRead == null)
325                 return false;
326         }
327         
328         test = wasRead;
329         test.addPropertyChangeListener(listener = new PropertyChangeListener() {
330             public void propertyChange(final java.beans.PropertyChangeEvent JavaDoc p1) {
331                 changed=true;
332             }
333         });
334         test.logger = new Logger(frame.getEditor());
335         frame.setTest(test);
336         changed=false;
337         return true;
338     }
339     
340     public static void closeTest() {
341         test.removePropertyChangeListener(listener);
342     }
343     
344     public static boolean saveTest() {
345         boolean res = saveTest(test, fileName);
346         
347         if (res)
348             changed=false;
349         return res;
350     }
351     
352     public static boolean saveTest(Test test, OutputStream outStream) {
353         Main.log("Attempt to write Test to file.");
354         try {
355             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
356             DocumentBuilder builder = factory.newDocumentBuilder();
357             DOMImplementation implementation = builder.getDOMImplementation();
358             
359             Document JavaDoc document = implementation.createDocument("", "Test",null);
360             Element element = (Element) document.getElementsByTagName("Test").item(0);
361             
362             element = test.toXML(element);
363             XMLUtil.write(document,outStream, "UTF-8");
364             Main.log("Saved with name: " + fileName + ".");
365         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
366             Main.log("XML exception.");
367             return false;
368         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
369             Main.log("Unsupported encoding: UTF-8.");
370             return false;
371         } catch (java.io.IOException JavaDoc e) {
372             Main.log("Unknown IO exception.");
373             return false;
374         };
375         Main.log("End.");
376         return true;
377     }
378     
379     public static boolean saveTest(Test test, String JavaDoc aFileName) {
380         if (aFileName == null)
381             return false;
382         try {
383             return saveTest(test, new FileOutputStream(aFileName));
384         } catch (java.io.IOException JavaDoc e) {
385             Main.log("File " + fileName + " not found.");
386         }
387         return false;
388     }
389     
390     public static boolean saveAsTest(String JavaDoc aFileName) {
391         Main.log("saveAsTest: " + aFileName);
392         if (aFileName == null)
393             return false;
394         setFileName(aFileName);
395         return saveTest();
396     }
397     
398     public static boolean finish() {
399         JavaOptions opts = (JavaOptions)(SystemOption.findObject(JavaOptions.class));
400         opts.setCompletionAutoPopup(compAutoPopup);
401         opts.setJavaDocAutoPopup(docAutoPopup);
402         HTMLOptions hopts = (HTMLOptions)(SystemOption.findObject(HTMLOptions.class));
403         hopts.setCompletionAutoPopup(htmlAutoPopup);
404         
405         Scheduler.finishScheduler();
406         if (log != null) {
407             log.close();
408             System.setErr(oldErr);
409         }
410         frame=null;
411         test=null;
412         return true;
413     }
414     
415     public static boolean isChanged() {
416         return changed;
417     }
418     
419     public static boolean isNoname() {
420         return (fileName == null || fileName.length() == 0);
421     }
422     
423     public static void log(String JavaDoc text) {
424         System.err.println(text);
425         System.err.flush();
426     }
427     
428     public static boolean question(String JavaDoc text) {
429         return frame.quest(text);
430     }
431 }
432
Popular Tags