KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > NonGuiHandleImportOfUserDirTest


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.core.startup;
21
22 import java.io.File JavaDoc;
23 import org.netbeans.junit.NbTestCase;
24
25 /** Tests the behaviour of the import user dir "api".
26  */

27 public class NonGuiHandleImportOfUserDirTest extends NbTestCase {
28     private static NonGuiHandleImportOfUserDirTest instance;
29
30     private File JavaDoc user;
31     private boolean updaterInvoked;
32     private Throwable JavaDoc toThrow;
33
34
35     public static void main(java.lang.String JavaDoc[] args) throws Throwable JavaDoc {
36         if (instance != null) {
37             // ok this is invoked from the test by the core-launcher
38
instance.nowDoTheInstall ();
39             return;
40         } else {
41             // initial start
42
junit.textui.TestRunner.run(new junit.framework.TestSuite (NonGuiHandleImportOfUserDirTest.class));
43         }
44     }
45     public NonGuiHandleImportOfUserDirTest (String JavaDoc name) {
46         super(name);
47     }
48
49     protected void setUp () throws Exception JavaDoc {
50         clearWorkDir ();
51         CLIOptions.clearForTests ();
52         
53         File JavaDoc home = new File JavaDoc (getWorkDir (), "nb/home");
54         user = new File JavaDoc (getWorkDir (), "user");
55         
56         assertTrue ("Home dir created", home.mkdirs ());
57         assertTrue ("User dir created", user.mkdirs ());
58         
59         System.setProperty ("netbeans.home", home.toString ());
60         System.setProperty ("netbeans.user", user.toString ());
61         
62         System.setProperty ("netbeans.importclass", NonGuiHandleImportOfUserDirTest.class.getName ());
63         
64         instance = this;
65     }
66     
67     protected void tearDown () throws Exception JavaDoc {
68         instance = null;
69     }
70     
71     
72     private void nowDoTheInstall () throws Throwable JavaDoc {
73         assertTrue ("Called from AWT thread", javax.swing.SwingUtilities.isEventDispatchThread ());
74         if (toThrow != null) {
75             Throwable JavaDoc t = toThrow;
76             toThrow = null;
77             throw t;
78         }
79         
80         updaterInvoked = true;
81     }
82     
83     public void testIfTheUserDirIsEmptyTheUpdaterIsInvoked () {
84         assertTrue ("Ok, returns without problems", Main.handleImportOfUserDir ());
85         assertTrue ("the main method invoked", updaterInvoked);
86         
87         toThrow = new RuntimeException JavaDoc ();
88
89         assertTrue ("The install is not called anymore 1", Main.handleImportOfUserDir ());
90         assertTrue ("The install is not called anymore 2", Main.handleImportOfUserDir ());
91         assertTrue ("The install is not called anymore 3", Main.handleImportOfUserDir ());
92         assertTrue ("The install is not called anymore 4", Main.handleImportOfUserDir ());
93     }
94
95     public void testIfInvokedAndThrowsExceptionTheExecutionStops () {
96         toThrow = new RuntimeException JavaDoc ();
97         
98         assertFalse ("Says no as exception was thrown", Main.handleImportOfUserDir ());
99         assertNull ("Justs to be sure the exception was cleared", toThrow);
100     }
101     
102     public void testIfThrowsUserCancelExThenUpdateIsFinished () {
103         toThrow = new org.openide.util.UserCancelException ();
104         
105         assertTrue ("Says yes as user canceled the import", Main.handleImportOfUserDir ());
106         assertNull ("Justs to be sure the exception was cleared", toThrow);
107         
108         assertTrue ("The install is not called anymore 1", Main.handleImportOfUserDir ());
109     }
110     
111     public void testExecutionGoesOnWhenThereIsIncorrctClass() {
112         System.setProperty ("netbeans.importclass", "IDoNotExists");
113         assertFalse ("Says no as class does not exists", Main.handleImportOfUserDir ());
114     }
115 }
116
Popular Tags