1 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 ; 27 import junit.framework.AssertionFailedError; 28 import org.openide.util.RequestProcessor; 29 30 34 public class MainCLITest extends NbTestCase { 35 public MainCLITest (String name) { 36 super(name); 37 } 38 39 public void testHandlersCanBeInUserDir () throws Exception { 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 [] 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 os = new JarOutputStream (new FileOutputStream (jar)); 67 os.putNextEntry (new java.util.zip.ZipEntry ("META-INF/services/org.netbeans.CLIHandler")); 68 os.write (TestHandler.class.getName ().getBytes ()); 69 String res = "/" + TestHandler.class.getName ().replace ('.', '/') + ".class"; 70 os.putNextEntry (new java.util.zip.ZipEntry (res)); 71 org.openide.filesystems.FileUtil.copy (getClass().getResourceAsStream (res), os); 72 os.close (); 73 74 TestHandler.called = false; 75 76 String [] args = new String [] { "--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 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 |