001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.nbbuild;
043:
044: import java.io.ByteArrayInputStream;
045: import java.io.ByteArrayOutputStream;
046: import java.io.File;
047: import java.io.FileDescriptor;
048: import java.io.FileOutputStream;
049: import java.io.InputStream;
050: import java.io.PrintStream;
051: import java.io.PrintWriter;
052: import java.net.InetAddress;
053: import java.net.URL;
054: import java.security.Permission;
055: import java.util.ArrayList;
056: import java.util.Arrays;
057: import java.util.List;
058: import junit.framework.AssertionFailedError;
059: import org.netbeans.junit.NbTestCase;
060:
061: /** Check the behaviour of <public-packages> in project.xml modules.
062: *
063: * @author Jaroslav Tulach
064: */
065: public class PublicPackagesInProjectizedXMLTest extends NbTestCase {
066: public PublicPackagesInProjectizedXMLTest(String name) {
067: super (name);
068: }
069:
070: public void testPackageCannotContainComma() throws Exception {
071: java.io.File f = extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
072: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">"
073: + " <type>org.netbeans.modules.apisupport.project</type>"
074: + " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">"
075: + " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>"
076: + " <public-packages>"
077: + " <package>org,org.apache.bsf</package>"
078: + " </public-packages>"
079: + " <javadoc/>"
080: + " </data></configuration>" + "</project>");
081: try {
082: execute("GarbageUnderPackages.xml",
083: new String[] { "-Dproject.file=" + f });
084: fail("This should fail as the public package definition contains comma");
085: } catch (ExecutionError ex) {
086: // ok, this should fail on exit code
087: }
088: }
089:
090: public void testPackageCannotContainStar() throws Exception {
091: java.io.File f = extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
092: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">"
093: + " <type>org.netbeans.modules.apisupport.project</type>"
094: + " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">"
095: + " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>"
096: + " <public-packages>"
097: + " <package>org.**</package>"
098: + " </public-packages>"
099: + " <javadoc/>"
100: + " </data></configuration>" + "</project>");
101: try {
102: execute("GarbageUnderPackages.xml",
103: new String[] { "-Dproject.file=" + f });
104: fail("This should fail as the public package definition contains *");
105: } catch (ExecutionError ex) {
106: // ok, this should fail on exit code
107: }
108: }
109:
110: public void testPublicPackagesCannotContainGarbageSubelements()
111: throws Exception {
112: java.io.File f = extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
113: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">"
114: + " <type>org.netbeans.modules.apisupport.project</type>"
115: + " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">"
116: + " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>"
117: + " <public-packages>"
118: + " <pkgs>org.hello</pkgs>"
119: + " </public-packages>"
120: + " <javadoc/>"
121: + " </data></configuration>" + "</project>");
122: try {
123: execute("GarbageUnderPackages.xml",
124: new String[] { "-Dproject.file=" + f });
125: fail("This should fail as the public package definition contains *");
126: } catch (ExecutionError ex) {
127: // ok, this should fail on exit code
128: }
129: }
130:
131: public void testItIsPossibleToDefineSubpackages() throws Exception {
132: java.io.File f = extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
133: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">"
134: + " <type>org.netbeans.modules.apisupport.project</type>"
135: + " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">"
136: + " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>"
137: + " <public-packages>"
138: + " <subpackages>org.hello</subpackages>"
139: + " </public-packages>"
140: + " <javadoc/>"
141: + " </data></configuration>" + "</project>");
142: execute("GarbageUnderPackages.xml", new String[] {
143: "-Dproject.file=" + f,
144: "-Dexpected.public.packages=org.hello.**" });
145: }
146:
147: /* DISABLED because of fix for #52135:
148: public void testSubpackagesDoNotWorkForJavadocNow () throws Exception {
149: java.io.File f = extractString (
150: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
151: "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" +
152: " <type>org.netbeans.modules.apisupport.project</type>" +
153: " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" +
154: " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" +
155: " <public-packages>" +
156: " <subpackages>org.hello</subpackages>" +
157: " </public-packages>" +
158: " <javadoc/>" +
159: " </data></configuration>" +
160: "</project>"
161: );
162: try {
163: execute ("GarbageUnderPackages.xml", new String[] { "-Dproject.file=" + f, "withjavadoc" });
164: fail ("We do not support <subpackage> when javadoc packages are requested, so the execution should fail");
165: } catch (ExecutionError ex) {
166: // ok
167: }
168: }
169: */
170:
171: public void testSubpackagesDoNotWorkForJavadocNowButThisWorksWhenSpecifiedByHand()
172: throws Exception {
173: java.io.File f = extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
174: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">"
175: + " <type>org.netbeans.modules.apisupport.project</type>"
176: + " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">"
177: + " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>"
178: + " <public-packages>"
179: + " <subpackages>org.hello</subpackages>"
180: + " </public-packages>"
181: + " <javadoc/>"
182: + " </data></configuration>" + "</project>");
183: execute("GarbageUnderPackages.xml", new String[] {
184: "-Djavadoc.pac=some", "-Dproject.file=" + f,
185: "withjavadoc" });
186: }
187:
188: final static String readFile(java.io.File f)
189: throws java.io.IOException {
190: int s = (int) f.length();
191: byte[] data = new byte[s];
192: assertEquals("Read all data", s, new java.io.FileInputStream(f)
193: .read(data));
194:
195: return new String(data);
196: }
197:
198: final static File extractString(String res) throws Exception {
199: File f = File.createTempFile("res", ".xml");
200: f.deleteOnExit();
201:
202: FileOutputStream os = new FileOutputStream(f);
203: InputStream is = new ByteArrayInputStream(res.getBytes("UTF-8"));
204: for (;;) {
205: int ch = is.read();
206: if (ch == -1)
207: break;
208: os.write(ch);
209: }
210: os.close();
211:
212: return f;
213: }
214:
215: final static File extractResource(String res) throws Exception {
216: URL u = PublicPackagesInProjectizedXMLTest.class
217: .getResource(res);
218: assertNotNull("Resource should be found " + res, u);
219:
220: File f = File.createTempFile("res", ".xml");
221: f.deleteOnExit();
222:
223: FileOutputStream os = new FileOutputStream(f);
224: InputStream is = u.openStream();
225: for (;;) {
226: int ch = is.read();
227: if (ch == -1)
228: break;
229: os.write(ch);
230: }
231: os.close();
232:
233: return f;
234: }
235:
236: final static void execute(String res, String[] args)
237: throws Exception {
238: execute(extractResource(res), args);
239: }
240:
241: private static ByteArrayOutputStream out;
242: private static ByteArrayOutputStream err;
243:
244: final static String getStdOut() {
245: return out.toString();
246: }
247:
248: final static String getStdErr() {
249: return err.toString();
250: }
251:
252: final static void execute(File f, String[] args) throws Exception {
253: // we need security manager to prevent System.exit
254: if (!(System.getSecurityManager() instanceof MySecMan)) {
255: out = new java.io.ByteArrayOutputStream();
256: err = new java.io.ByteArrayOutputStream();
257: System.setOut(new java.io.PrintStream(out));
258: System.setErr(new java.io.PrintStream(err));
259:
260: System.setSecurityManager(new MySecMan());
261: }
262:
263: MySecMan sec = (MySecMan) System.getSecurityManager();
264:
265: // Jesse claims that this is not the right way how the execution
266: // of an ant script should be invoked:
267: //
268: // better IMHO to just run the task directly
269: // (setProject() and similar, configure its bean properties, and call
270: // execute()), or just make a new Project and initialize it.
271: // ant.Main.main is not intended for embedded use. Then you could get rid
272: // of the SecurityManager stuff, would be cleaner I think.
273: //
274: // If I had to write this once again, I would try to follow the
275: // "just make a new Project and initialize it", but as this works
276: // for me now, I leave it for the time when somebody really
277: // needs that...
278:
279: List arr = new ArrayList();
280: arr.add("-f");
281: arr.add(f.toString());
282: arr.addAll(Arrays.asList(args));
283:
284: out.reset();
285: err.reset();
286:
287: try {
288: sec.setActive(true);
289: org.apache.tools.ant.Main.main((String[]) arr
290: .toArray(new String[0]));
291: } catch (MySecExc ex) {
292: assertNotNull(
293: "The only one to throw security exception is MySecMan and should set exitCode",
294: sec.exitCode);
295: ExecutionError.assertExitCode(
296: "Execution has to finish without problems",
297: sec.exitCode.intValue());
298: } finally {
299: sec.setActive(false);
300: }
301: }
302:
303: static class ExecutionError extends AssertionFailedError {
304: public final int exitCode;
305:
306: public ExecutionError(String msg, int e) {
307: super (msg);
308: this .exitCode = e;
309: }
310:
311: public static void assertExitCode(String msg, int e) {
312: if (e != 0) {
313: throw new ExecutionError(msg + " was: " + e
314: + "\nOutput: " + out.toString() + "\nError: "
315: + err.toString(), e);
316: }
317: }
318: }
319:
320: private static class MySecExc extends SecurityException {
321: public void printStackTrace() {
322: }
323:
324: public void printStackTrace(PrintStream ps) {
325: }
326:
327: public void printStackTrace(PrintWriter ps) {
328: }
329: }
330:
331: private static class MySecMan extends SecurityManager {
332: public Integer exitCode;
333:
334: private boolean active;
335:
336: public void checkExit(int status) {
337: if (active) {
338: exitCode = new Integer(status);
339: throw new MySecExc();
340: }
341: }
342:
343: public void checkPermission(Permission perm, Object context) {
344: }
345:
346: public void checkPermission(Permission perm) {
347: /*
348: if (perm instanceof RuntimePermission) {
349: if (perm.getName ().equals ("setIO")) {
350: throw new MySecExc ();
351: }
352: }
353: */
354: }
355:
356: public void checkMulticast(InetAddress maddr) {
357: }
358:
359: public void checkAccess(ThreadGroup g) {
360: }
361:
362: public void checkWrite(String file) {
363: }
364:
365: public void checkLink(String lib) {
366: }
367:
368: public void checkExec(String cmd) {
369: }
370:
371: public void checkDelete(String file) {
372: }
373:
374: public void checkPackageAccess(String pkg) {
375: }
376:
377: public void checkPackageDefinition(String pkg) {
378: }
379:
380: public void checkPropertyAccess(String key) {
381: }
382:
383: public void checkRead(String file) {
384: }
385:
386: public void checkSecurityAccess(String target) {
387: }
388:
389: public void checkWrite(FileDescriptor fd) {
390: }
391:
392: public void checkListen(int port) {
393: }
394:
395: public void checkRead(FileDescriptor fd) {
396: }
397:
398: public void checkMulticast(InetAddress maddr, byte ttl) {
399: }
400:
401: public void checkAccess(Thread t) {
402: }
403:
404: public void checkConnect(String host, int port, Object context) {
405: }
406:
407: public void checkRead(String file, Object context) {
408: }
409:
410: public void checkConnect(String host, int port) {
411: }
412:
413: public void checkAccept(String host, int port) {
414: }
415:
416: public void checkMemberAccess(Class clazz, int which) {
417: }
418:
419: public void checkSystemClipboardAccess() {
420: }
421:
422: public void checkSetFactory() {
423: }
424:
425: public void checkCreateClassLoader() {
426: }
427:
428: public void checkAwtEventQueueAccess() {
429: }
430:
431: public void checkPrintJobAccess() {
432: }
433:
434: public void checkPropertiesAccess() {
435: }
436:
437: void setActive(boolean b) {
438: active = b;
439: }
440: } // end of MySecMan
441: }
|