KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.FileOutputStream JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.security.Permission JavaDoc;
28 import java.util.jar.Attributes JavaDoc;
29 import java.util.jar.JarEntry JavaDoc;
30 import java.util.jar.JarOutputStream JavaDoc;
31 import java.util.jar.Manifest JavaDoc;
32 import org.netbeans.CLIHandler;
33 import org.netbeans.junit.NbTestCase;
34
35
36 /** Make sure the CLIHandler can be in modules and really work.
37  * @author Jaroslav Tulach
38  */

39 public class CLILookupHelpTest extends NbTestCase {
40     File JavaDoc home, cluster2, user;
41     
42     public CLILookupHelpTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     protected void setUp() throws Exception JavaDoc {
47         clearWorkDir();
48
49         File JavaDoc p = new File JavaDoc(getWorkDir(), "par");
50         home = new File JavaDoc(p, "cluster1");
51         cluster2 = new File JavaDoc(p, "cluster2");
52         user = new File JavaDoc(getWorkDir(), "testuserdir");
53         
54         home.mkdirs();
55         cluster2.mkdirs();
56         user.mkdirs();
57         
58         System.setProperty("netbeans.home", home.toString());
59         System.setProperty("netbeans.dirs", cluster2.toString());
60         
61         System.setSecurityManager(new NoExit());
62     }
63     
64
65     protected void tearDown() throws Exception JavaDoc {
66         NoExit.disable = true;
67     }
68     
69     public void testModuleInAClusterCanBeFound() throws Exception JavaDoc {
70         createJAR(home, "test-module-one", One.class);
71         createJAR(cluster2, "test-module-two", Two.class);
72         createJAR(user, "test-module-user", User.class);
73
74         try {
75             org.netbeans.Main.main(new String JavaDoc[] { "--help", "--userdir", user.toString() });
76             fail("At the end this shall throw security exception");
77         } catch (SecurityException JavaDoc ex) {
78             assertEquals("Exit code shall be two", "2", ex.getMessage());
79         }
80         
81         assertEquals("Usage one", 1, One.usageCnt); assertEquals("CLI", 0, One.cliCnt);
82         assertEquals("Usage two", 1, Two.usageCnt); assertEquals("CLI", 0, Two.cliCnt);
83         assertEquals("Usage user", 1, User.usageCnt); assertEquals("CLI", 0, User.cliCnt);
84     }
85
86     static void createJAR(File JavaDoc cluster, String JavaDoc moduleName, Class JavaDoc metaInfHandler)
87     throws IOException JavaDoc {
88         File JavaDoc xml = new File JavaDoc(new File JavaDoc(new File JavaDoc(cluster, "config"), "Modules"), moduleName + ".xml");
89         File JavaDoc jar = new File JavaDoc(new File JavaDoc(cluster, "modules"), moduleName + ".jar");
90         
91         xml.getParentFile().mkdirs();
92         jar.getParentFile().mkdirs();
93         
94         
95         Manifest JavaDoc mf = new Manifest JavaDoc();
96         mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
97         mf.getMainAttributes().putValue("OpenIDE-Module", moduleName.replace('-', '.'));
98         mf.getMainAttributes().putValue("OpenIDE-Module-Public-Packages", "-");
99         
100         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jar), mf);
101         os.putNextEntry(new JarEntry JavaDoc("META-INF/services/org.netbeans.CLIHandler"));
102         os.write(metaInfHandler.getName().getBytes());
103         os.close();
104         
105         FileWriter JavaDoc w = new FileWriter JavaDoc(xml);
106         w.write(
107 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
108 "<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Module Status 1.0//EN\"\n" +
109 " \"http://www.netbeans.org/dtds/module-status-1_0.dtd\">\n" +
110 "<module name=\"" + moduleName.replace('-', '.') + "\">\n" +
111 " <param name=\"autoload\">false</param>\n" +
112 " <param name=\"eager\">false</param>\n" +
113 " <param name=\"enabled\">true</param>\n" +
114 " <param name=\"jar\">modules/" + moduleName + ".jar</param>\n" +
115 " <param name=\"release\">2</param>\n" +
116 " <param name=\"reloadable\">false</param>\n" +
117 " <param name=\"specversion\">3.4.0.1</param>\n" +
118 "</module>");
119         w.close();
120     }
121
122     
123     public static final class One extends CLIHandler {
124         public static int cliCnt;
125         public static int usageCnt;
126         
127         public One() {
128             super(WHEN_EXTRA);
129         }
130
131         protected int cli(CLIHandler.Args args) {
132             cliCnt++;
133             return 0;
134         }
135
136         protected void usage(PrintWriter JavaDoc w) {
137             usageCnt++;
138         }
139     }
140     public static final class Two extends CLIHandler {
141         public static int cliCnt;
142         public static int usageCnt;
143         
144         public Two() {
145             super(WHEN_EXTRA);
146         }
147
148         protected int cli(CLIHandler.Args args) {
149             cliCnt++;
150             return 0;
151         }
152
153         protected void usage(PrintWriter JavaDoc w) {
154             usageCnt++;
155         }
156     }
157     public static final class User extends CLIHandler {
158         public static int cliCnt;
159         public static int usageCnt;
160         
161         public User() {
162             super(WHEN_EXTRA);
163         }
164
165         protected int cli(CLIHandler.Args args) {
166             cliCnt++;
167             return 0;
168         }
169
170         protected void usage(PrintWriter JavaDoc w) {
171             usageCnt++;
172         }
173     }
174     
175     
176     private static final class NoExit extends SecurityManager JavaDoc {
177         public static boolean disable;
178         
179         public void checkExit(int status) {
180             if (!disable) {
181                 throw new SecurityException JavaDoc(String.valueOf(status));
182             }
183         }
184
185         public void checkPermission(Permission JavaDoc perm) {
186             
187         }
188
189         public void checkPermission(Permission JavaDoc perm, Object JavaDoc context) {
190             
191         }
192         
193     }
194 }
195
Popular Tags