KickJava   Java API By Example, From Geeks To Geeks.

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


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 behavior of the license check "api".
26  */

27 public class NonGuiHandleCheckOfLicenseTest extends NbTestCase {
28     private static NonGuiHandleCheckOfLicenseTest instance;
29
30     private File JavaDoc user;
31     private File JavaDoc userVar;
32     private boolean updaterInvoked;
33     private Throwable JavaDoc toThrow;
34
35     public static void showLicensePanel () 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 (NonGuiHandleCheckOfLicenseTest.class));
43         }
44     }
45
46     public NonGuiHandleCheckOfLicenseTest (String JavaDoc name) {
47         super(name);
48     }
49
50     protected void setUp () throws Exception JavaDoc {
51         clearWorkDir ();
52         CLIOptions.clearForTests ();
53         
54         File JavaDoc home = new File JavaDoc (getWorkDir (), "nb/home");
55         user = new File JavaDoc (getWorkDir (), "user");
56         userVar = new File JavaDoc (user,"var");
57         
58         assertTrue ("Home dir created", home.mkdirs ());
59         assertTrue ("User dir created", user.mkdirs ());
60         
61         System.setProperty ("netbeans.home", home.toString ());
62         System.setProperty ("netbeans.user", user.toString ());
63         
64         System.setProperty ("netbeans.accept_license_class", NonGuiHandleCheckOfLicenseTest.class.getName ());
65         
66         File JavaDoc f = new File JavaDoc(userVar,"license_accepted");
67         if (f.exists()) {
68             f.delete();
69         }
70         
71         instance = this;
72     }
73     
74     protected void tearDown () throws Exception JavaDoc {
75         instance = null;
76     }
77     
78     private void nowDoTheInstall () throws Throwable JavaDoc {
79         assertTrue ("Called from AWT thread", javax.swing.SwingUtilities.isEventDispatchThread ());
80         if (toThrow != null) {
81             Throwable JavaDoc t = toThrow;
82             toThrow = null;
83             throw t;
84         }
85         
86         updaterInvoked = true;
87     }
88     
89     /** Test if check is invoked when there is not file "var/license_accepted" */
90     public void testIfTheUserDirIsEmptyTheLicenseCheckIsInvoked () {
91         assertTrue ("Ok, returns without problems", Main.handleLicenseCheck ());
92         assertTrue ("the main method invoked", updaterInvoked);
93         
94         toThrow = new RuntimeException JavaDoc ();
95         
96         //File "var/license_accepted" is created during first call in user dir
97
//then license check is not invoked anymore
98
assertTrue ("The check is not called anymore 1", Main.handleLicenseCheck ());
99         assertTrue ("The check is not called anymore 2", Main.handleLicenseCheck ());
100         assertTrue ("The check is not called anymore 3", Main.handleLicenseCheck ());
101         assertTrue ("The check is not called anymore 4", Main.handleLicenseCheck ());
102         
103         File JavaDoc f = new File JavaDoc(userVar,"license_accepted");
104         if (f.exists()) {
105             f.delete();
106         }
107     }
108     
109     public void testIfInvokedAndThrowsExceptionTheExecutionStops () {
110         toThrow = new RuntimeException JavaDoc();
111         
112         assertFalse ("Says no as exception was thrown", Main.handleLicenseCheck());
113         assertNull ("Justs to be sure the exception was cleared", toThrow);
114     }
115     
116     public void testIfThrowsUserCancelExThenLicenseCheckIsCalledAgain () {
117         toThrow = new org.openide.util.UserCancelException();
118         assertFalse("Says no as user did not accept the license", Main.handleLicenseCheck());
119         assertNull("Justs to be sure the exception was cleared", toThrow);
120         
121         toThrow = new org.openide.util.UserCancelException();
122         assertFalse("Says no as user did not accept the license", Main.handleLicenseCheck());
123         assertNull("Justs to be sure the exception was cleared", toThrow);
124     }
125     
126 }
127
Popular Tags