KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import org.netbeans.CLIHandler;
26 import org.netbeans.junit.NbTestCase;
27
28 /** Make sure the CLIHandler can be in modules and really work.
29  * @author Jaroslav Tulach
30  */

31 public class CLILookupExecTest extends NbTestCase {
32     File JavaDoc home, cluster2, user;
33
34     public CLILookupExecTest(String JavaDoc name) {
35         super(name);
36     }
37
38     protected void setUp() throws Exception JavaDoc {
39         clearWorkDir();
40
41         home = new File JavaDoc(getWorkDir(), "nb/cluster1");
42         cluster2 = new File JavaDoc(getWorkDir(), "nb/cluster2");
43         user = new File JavaDoc(getWorkDir(), "testuserdir");
44         
45         home.mkdirs();
46         cluster2.mkdirs();
47         user.mkdirs();
48         
49         System.setProperty("netbeans.home", home.toString());
50         System.setProperty("netbeans.dirs", cluster2.toString());
51     }
52     
53
54     protected void tearDown() throws Exception JavaDoc {
55     }
56     
57     public void testModuleInAClusterCanBeFound() throws Exception JavaDoc {
58         createJAR(home, "test-module-one", One.class);
59         createJAR(cluster2, "test-module-two", Two.class);
60         createJAR(user, "test-module-user", User.class);
61
62         org.netbeans.Main.main(new String JavaDoc[] { "--userdir", user.toString(), "--nosplash", "--one", "--two", "--three"});
63         org.netbeans.Main.finishInitialization();
64         
65         assertEquals("Usage one", 0, One.usageCnt); assertEquals("CLI one", 1, One.cliCnt);
66         assertEquals("Usage two", 0, Two.usageCnt); assertEquals("CLI two ", 1, Two.cliCnt);
67         assertEquals("Usage user", 0, User.usageCnt); assertEquals("CLI user", 1, User.cliCnt);
68     }
69
70     private static void createJAR(File JavaDoc cluster, String JavaDoc moduleName, Class JavaDoc metaInfHandler)
71     throws IOException JavaDoc {
72         CLILookupHelpTest.createJAR(cluster, moduleName, metaInfHandler);
73     }
74     
75     private static void assertArg(String JavaDoc[] arr, String JavaDoc expected) {
76         for (int i = 0; i < arr.length; i++) {
77             if (expected.equals(arr[i])) {
78                 arr[i] = null;
79                 return;
80             }
81         }
82         
83         fail("There should be: " + expected + " but was only: " + java.util.Arrays.asList(arr));
84     }
85
86     public static final class One extends CLIHandler {
87         public static int cliCnt;
88         public static int usageCnt;
89         
90         public One() {
91             super(WHEN_EXTRA);
92         }
93
94         protected int cli(CLIHandler.Args args) {
95             assertArg(args.getArguments(), "--one");
96             cliCnt++;
97             return 0;
98         }
99
100         protected void usage(PrintWriter JavaDoc w) {
101             usageCnt++;
102         }
103     }
104     public static final class Two extends CLIHandler {
105         public static int cliCnt;
106         public static int usageCnt;
107         
108         public Two() {
109             super(WHEN_EXTRA);
110         }
111
112         protected int cli(CLIHandler.Args args) {
113             assertArg(args.getArguments(), "--two");
114             cliCnt++;
115             return 0;
116         }
117
118         protected void usage(PrintWriter JavaDoc w) {
119             usageCnt++;
120         }
121     }
122     public static final class User extends CLIHandler {
123         public static int cliCnt;
124         public static int usageCnt;
125         
126         public User() {
127             super(WHEN_EXTRA);
128         }
129
130         protected int cli(CLIHandler.Args args) {
131             assertArg(args.getArguments(), "--three");
132             cliCnt++;
133             return 0;
134         }
135
136         protected void usage(PrintWriter JavaDoc w) {
137             usageCnt++;
138         }
139     }
140 }
141
Popular Tags