1 19 20 package org.netbeans.core.startup; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.PrintWriter ; 27 import java.security.Permission ; 28 import java.util.jar.Attributes ; 29 import java.util.jar.JarEntry ; 30 import java.util.jar.JarOutputStream ; 31 import java.util.jar.Manifest ; 32 import org.netbeans.CLIHandler; 33 import org.netbeans.junit.NbTestCase; 34 35 36 39 public class CLILookupHelpTest extends NbTestCase { 40 File home, cluster2, user; 41 42 public CLILookupHelpTest(String name) { 43 super(name); 44 } 45 46 protected void setUp() throws Exception { 47 clearWorkDir(); 48 49 File p = new File (getWorkDir(), "par"); 50 home = new File (p, "cluster1"); 51 cluster2 = new File (p, "cluster2"); 52 user = new File (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 { 66 NoExit.disable = true; 67 } 68 69 public void testModuleInAClusterCanBeFound() throws Exception { 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 [] { "--help", "--userdir", user.toString() }); 76 fail("At the end this shall throw security exception"); 77 } catch (SecurityException 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 cluster, String moduleName, Class metaInfHandler) 87 throws IOException { 88 File xml = new File (new File (new File (cluster, "config"), "Modules"), moduleName + ".xml"); 89 File jar = new File (new File (cluster, "modules"), moduleName + ".jar"); 90 91 xml.getParentFile().mkdirs(); 92 jar.getParentFile().mkdirs(); 93 94 95 Manifest mf = new Manifest (); 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 os = new JarOutputStream (new FileOutputStream (jar), mf); 101 os.putNextEntry(new JarEntry ("META-INF/services/org.netbeans.CLIHandler")); 102 os.write(metaInfHandler.getName().getBytes()); 103 os.close(); 104 105 FileWriter w = new FileWriter (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 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 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 w) { 171 usageCnt++; 172 } 173 } 174 175 176 private static final class NoExit extends SecurityManager { 177 public static boolean disable; 178 179 public void checkExit(int status) { 180 if (!disable) { 181 throw new SecurityException (String.valueOf(status)); 182 } 183 } 184 185 public void checkPermission(Permission perm) { 186 187 } 188 189 public void checkPermission(Permission perm, Object context) { 190 191 } 192 193 } 194 } 195 | Popular Tags |