1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileDescriptor ; 26 import java.io.FileOutputStream ; 27 import java.io.InputStream ; 28 import java.io.PrintStream ; 29 import java.io.PrintWriter ; 30 import java.net.InetAddress ; 31 import java.net.URL ; 32 import java.security.Permission ; 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 import java.util.List ; 36 import junit.framework.AssertionFailedError; 37 import org.netbeans.junit.NbTestCase; 38 39 43 public class PublicPackagesInProjectizedXMLTest extends NbTestCase { 44 public PublicPackagesInProjectizedXMLTest (String name) { 45 super (name); 46 } 47 48 public void testPackageCannotContainComma () throws Exception { 49 java.io.File f = extractString ( 50 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 51 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" + 52 " <type>org.netbeans.modules.apisupport.project</type>" + 53 " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" + 54 " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" + 55 " <public-packages>" + 56 " <package>org,org.apache.bsf</package>" + 57 " </public-packages>" + 58 " <javadoc/>" + 59 " </data></configuration>" + 60 "</project>" 61 ); 62 try { 63 execute ("GarbageUnderPackages.xml", new String [] { "-Dproject.file=" + f }); 64 fail ("This should fail as the public package definition contains comma"); 65 } catch (ExecutionError ex) { 66 } 68 } 69 70 public void testPackageCannotContainStar () throws Exception { 71 java.io.File f = extractString ( 72 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 73 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" + 74 " <type>org.netbeans.modules.apisupport.project</type>" + 75 " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" + 76 " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" + 77 " <public-packages>" + 78 " <package>org.**</package>" + 79 " </public-packages>" + 80 " <javadoc/>" + 81 " </data></configuration>" + 82 "</project>" 83 ); 84 try { 85 execute ("GarbageUnderPackages.xml", new String [] { "-Dproject.file=" + f }); 86 fail ("This should fail as the public package definition contains *"); 87 } catch (ExecutionError ex) { 88 } 90 } 91 92 public void testPublicPackagesCannotContainGarbageSubelements () throws Exception { 93 java.io.File f = extractString ( 94 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 95 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" + 96 " <type>org.netbeans.modules.apisupport.project</type>" + 97 " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" + 98 " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" + 99 " <public-packages>" + 100 " <pkgs>org.hello</pkgs>" + 101 " </public-packages>" + 102 " <javadoc/>" + 103 " </data></configuration>" + 104 "</project>" 105 ); 106 try { 107 execute ("GarbageUnderPackages.xml", new String [] { "-Dproject.file=" + f }); 108 fail ("This should fail as the public package definition contains *"); 109 } catch (ExecutionError ex) { 110 } 112 } 113 114 public void testItIsPossibleToDefineSubpackages () throws Exception { 115 java.io.File f = extractString ( 116 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 117 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" + 118 " <type>org.netbeans.modules.apisupport.project</type>" + 119 " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" + 120 " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" + 121 " <public-packages>" + 122 " <subpackages>org.hello</subpackages>" + 123 " </public-packages>" + 124 " <javadoc/>" + 125 " </data></configuration>" + 126 "</project>" 127 ); 128 execute ("GarbageUnderPackages.xml", new String [] { "-Dproject.file=" + f, "-Dexpected.public.packages=org.hello.**" }); 129 } 130 131 154 155 public void testSubpackagesDoNotWorkForJavadocNowButThisWorksWhenSpecifiedByHand () throws Exception { 156 java.io.File f = extractString ( 157 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 158 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" + 159 " <type>org.netbeans.modules.apisupport.project</type>" + 160 " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" + 161 " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" + 162 " <public-packages>" + 163 " <subpackages>org.hello</subpackages>" + 164 " </public-packages>" + 165 " <javadoc/>" + 166 " </data></configuration>" + 167 "</project>" 168 ); 169 execute ("GarbageUnderPackages.xml", new String [] { "-Djavadoc.pac=some", "-Dproject.file=" + f, "withjavadoc" }); 170 } 171 172 final static String readFile (java.io.File f) throws java.io.IOException { 173 int s = (int)f.length (); 174 byte[] data = new byte[s]; 175 assertEquals ("Read all data", s, new java.io.FileInputStream (f).read (data)); 176 177 return new String (data); 178 } 179 180 final static File extractString (String res) throws Exception { 181 File f = File.createTempFile("res", ".xml"); 182 f.deleteOnExit (); 183 184 FileOutputStream os = new FileOutputStream (f); 185 InputStream is = new ByteArrayInputStream (res.getBytes("UTF-8")); 186 for (;;) { 187 int ch = is.read (); 188 if (ch == -1) break; 189 os.write (ch); 190 } 191 os.close (); 192 193 return f; 194 } 195 196 final static File extractResource(String res) throws Exception { 197 URL u = PublicPackagesInProjectizedXMLTest.class.getResource(res); 198 assertNotNull ("Resource should be found " + res, u); 199 200 File f = File.createTempFile("res", ".xml"); 201 f.deleteOnExit (); 202 203 FileOutputStream os = new FileOutputStream (f); 204 InputStream is = u.openStream(); 205 for (;;) { 206 int ch = is.read (); 207 if (ch == -1) break; 208 os.write (ch); 209 } 210 os.close (); 211 212 return f; 213 } 214 215 final static void execute (String res, String [] args) throws Exception { 216 execute (extractResource (res), args); 217 } 218 219 private static ByteArrayOutputStream out; 220 private static ByteArrayOutputStream err; 221 222 final static String getStdOut() { 223 return out.toString(); 224 } 225 final static String getStdErr() { 226 return err.toString(); 227 } 228 229 final static void execute(File f, String [] args) throws Exception { 230 if (! (System.getSecurityManager () instanceof MySecMan)) { 232 out = new java.io.ByteArrayOutputStream (); 233 err = new java.io.ByteArrayOutputStream (); 234 System.setOut (new java.io.PrintStream (out)); 235 System.setErr (new java.io.PrintStream (err)); 236 237 System.setSecurityManager (new MySecMan ()); 238 } 239 240 MySecMan sec = (MySecMan)System.getSecurityManager(); 241 242 256 List arr = new ArrayList (); 257 arr.add ("-f"); 258 arr.add (f.toString ()); 259 arr.addAll(Arrays.asList(args)); 260 261 262 out.reset (); 263 err.reset (); 264 265 try { 266 sec.setActive(true); 267 org.apache.tools.ant.Main.main ((String [])arr.toArray (new String [0])); 268 } catch (MySecExc ex) { 269 assertNotNull ("The only one to throw security exception is MySecMan and should set exitCode", sec.exitCode); 270 ExecutionError.assertExitCode ( 271 "Execution has to finish without problems", 272 sec.exitCode.intValue () 273 ); 274 } finally { 275 sec.setActive(false); 276 } 277 } 278 279 static class ExecutionError extends AssertionFailedError { 280 public final int exitCode; 281 282 public ExecutionError (String msg, int e) { 283 super (msg); 284 this.exitCode = e; 285 } 286 287 public static void assertExitCode (String msg, int e) { 288 if (e != 0) { 289 throw new ExecutionError ( 290 msg + " was: " + e + "\nOutput: " + out.toString () + 291 "\nError: " + err.toString (), 292 e 293 ); 294 } 295 } 296 } 297 298 private static class MySecExc extends SecurityException { 299 public void printStackTrace() { 300 } 301 public void printStackTrace(PrintStream ps) { 302 } 303 public void printStackTrace(PrintWriter ps) { 304 } 305 } 306 307 private static class MySecMan extends SecurityManager { 308 public Integer exitCode; 309 310 private boolean active; 311 312 public void checkExit (int status) { 313 if (active) { 314 exitCode = new Integer (status); 315 throw new MySecExc (); 316 } 317 } 318 319 public void checkPermission(Permission perm, Object context) { 320 } 321 322 public void checkPermission(Permission perm) { 323 330 } 331 332 public void checkMulticast(InetAddress maddr) { 333 } 334 335 public void checkAccess (ThreadGroup g) { 336 } 337 338 public void checkWrite (String file) { 339 } 340 341 public void checkLink (String lib) { 342 } 343 344 public void checkExec (String cmd) { 345 } 346 347 public void checkDelete (String file) { 348 } 349 350 public void checkPackageAccess (String pkg) { 351 } 352 353 public void checkPackageDefinition (String pkg) { 354 } 355 356 public void checkPropertyAccess (String key) { 357 } 358 359 public void checkRead (String file) { 360 } 361 362 public void checkSecurityAccess (String target) { 363 } 364 365 public void checkWrite(FileDescriptor fd) { 366 } 367 368 public void checkListen (int port) { 369 } 370 371 public void checkRead(FileDescriptor fd) { 372 } 373 374 public void checkMulticast(InetAddress maddr, byte ttl) { 375 } 376 377 public void checkAccess (Thread t) { 378 } 379 380 public void checkConnect (String host, int port, Object context) { 381 } 382 383 public void checkRead (String file, Object context) { 384 } 385 386 public void checkConnect (String host, int port) { 387 } 388 389 public void checkAccept (String host, int port) { 390 } 391 392 public void checkMemberAccess (Class clazz, int which) { 393 } 394 395 public void checkSystemClipboardAccess () { 396 } 397 398 public void checkSetFactory () { 399 } 400 401 public void checkCreateClassLoader () { 402 } 403 404 public void checkAwtEventQueueAccess () { 405 } 406 407 public void checkPrintJobAccess () { 408 } 409 410 public void checkPropertiesAccess () { 411 } 412 413 void setActive(boolean b) { 414 active = b; 415 } 416 } } 418 | Popular Tags |