KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > MainCLITest


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;
21
22 import java.io.*;
23 import junit.textui.TestRunner;
24 import org.netbeans.junit.*;
25 import java.util.*;
26 import java.util.jar.JarOutputStream JavaDoc;
27 import junit.framework.AssertionFailedError;
28 import org.openide.util.RequestProcessor;
29
30 /**
31  * Test the command-line-interface handler.
32  * @author Jaroslav Tulach
33  */

34 public class MainCLITest extends NbTestCase {
35     public MainCLITest (String JavaDoc name) {
36         super(name);
37     }
38
39     public void testHandlersCanBeInUserDir () throws Exception JavaDoc {
40         clearWorkDir ();
41
42         class H extends CLIHandler {
43             public H() {
44                 super(WHEN_INIT);
45             }
46             
47             protected int cli(Args args) {
48                 String JavaDoc[] arr = args.getArguments ();
49                 for (int i = 0; i < arr.length; i++) {
50                     if (arr[i] == "--userdir") {
51                         System.setProperty ("netbeans.user", arr[i + 1]);
52                         return 0;
53                     }
54                 }
55                 fail ("One of the arguments should be --userdir: " + Arrays.asList (arr));
56                 return 0;
57             }
58             
59             protected void usage(PrintWriter w) {}
60         }
61         
62         File dir = super.getWorkDir ();
63         File lib = new File (dir, "core");
64         lib.mkdirs ();
65         File jar = new File (lib, "sample.jar");
66         JarOutputStream JavaDoc os = new JarOutputStream JavaDoc (new FileOutputStream (jar));
67         os.putNextEntry (new java.util.zip.ZipEntry JavaDoc ("META-INF/services/org.netbeans.CLIHandler"));
68         os.write (TestHandler.class.getName ().getBytes ());
69         String JavaDoc res = "/" + TestHandler.class.getName ().replace ('.', '/') + ".class";
70         os.putNextEntry (new java.util.zip.ZipEntry JavaDoc (res));
71         org.openide.filesystems.FileUtil.copy (getClass().getResourceAsStream (res), os);
72         os.close ();
73         
74         TestHandler.called = false;
75
76         String JavaDoc[] args = new String JavaDoc[] { "--userdir", dir.toString () };
77         assertFalse ("User dir is not correct. Will be set by org.netbeans.core.CLIOptions", dir.toString ().equals (System.getProperty ("netbeans.user")));
78         int result = MainImpl.execute (args, null, null, null, null);
79         Main.finishInitialization ();
80         assertEquals ("User set", dir.toString (), System.getProperty ("netbeans.user"));
81         assertTrue ("CLI Handler from user dir was called", TestHandler.called);
82     }
83
84     /** Sample handler
85      */

86     public static final class TestHandler extends CLIHandler {
87         public static boolean called;
88         
89         public TestHandler () {
90             super (CLIHandler.WHEN_INIT);
91         }
92         
93         protected int cli (org.netbeans.CLIHandler.Args args) {
94             called = true;
95             return 0;
96         }
97         
98         protected void usage (PrintWriter w) {
99         }
100         
101     }
102 }
103
Popular Tags