KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > PublicPackagesInProjectizedXMLTest


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.nbbuild;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileDescriptor JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.net.InetAddress JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.security.Permission JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.List JavaDoc;
36 import junit.framework.AssertionFailedError;
37 import org.netbeans.junit.NbTestCase;
38
39 /** Check the behaviour of <public-packages> in project.xml modules.
40  *
41  * @author Jaroslav Tulach
42  */

43 public class PublicPackagesInProjectizedXMLTest extends NbTestCase {
44     public PublicPackagesInProjectizedXMLTest (String JavaDoc name) {
45         super (name);
46     }
47     
48     public void testPackageCannotContainComma () throws Exception JavaDoc {
49         java.io.File JavaDoc 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 JavaDoc[] { "-Dproject.file=" + f });
64             fail ("This should fail as the public package definition contains comma");
65         } catch (ExecutionError ex) {
66             // ok, this should fail on exit code
67
}
68     }
69
70     public void testPackageCannotContainStar () throws Exception JavaDoc {
71         java.io.File JavaDoc 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 JavaDoc[] { "-Dproject.file=" + f });
86             fail ("This should fail as the public package definition contains *");
87         } catch (ExecutionError ex) {
88             // ok, this should fail on exit code
89
}
90     }
91
92     public void testPublicPackagesCannotContainGarbageSubelements () throws Exception JavaDoc {
93         java.io.File JavaDoc 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 JavaDoc[] { "-Dproject.file=" + f });
108             fail ("This should fail as the public package definition contains *");
109         } catch (ExecutionError ex) {
110             // ok, this should fail on exit code
111
}
112     }
113     
114     public void testItIsPossibleToDefineSubpackages () throws Exception JavaDoc {
115         java.io.File JavaDoc 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 JavaDoc[] { "-Dproject.file=" + f, "-Dexpected.public.packages=org.hello.**" });
129     }
130     
131     /* DISABLED because of fix for #52135:
132     public void testSubpackagesDoNotWorkForJavadocNow () throws Exception {
133         java.io.File f = extractString (
134             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
135             "<project xmlns=\"http://www.netbeans.org/ns/project/1\">" +
136             " <type>org.netbeans.modules.apisupport.project</type>" +
137             " <configuration><data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2\">" +
138             " <code-name-base>org.netbeans.modules.scripting.bsf</code-name-base>" +
139             " <public-packages>" +
140             " <subpackages>org.hello</subpackages>" +
141             " </public-packages>" +
142             " <javadoc/>" +
143             " </data></configuration>" +
144             "</project>"
145         );
146         try {
147             execute ("GarbageUnderPackages.xml", new String[] { "-Dproject.file=" + f, "withjavadoc" });
148             fail ("We do not support <subpackage> when javadoc packages are requested, so the execution should fail");
149         } catch (ExecutionError ex) {
150             // ok
151         }
152     }
153      */

154
155     public void testSubpackagesDoNotWorkForJavadocNowButThisWorksWhenSpecifiedByHand () throws Exception JavaDoc {
156         java.io.File JavaDoc 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 JavaDoc[] { "-Djavadoc.pac=some", "-Dproject.file=" + f, "withjavadoc" });
170     }
171     
172     final static String JavaDoc readFile (java.io.File JavaDoc f) throws java.io.IOException JavaDoc {
173         int s = (int)f.length ();
174         byte[] data = new byte[s];
175         assertEquals ("Read all data", s, new java.io.FileInputStream JavaDoc (f).read (data));
176         
177         return new String JavaDoc (data);
178     }
179     
180     final static File JavaDoc extractString (String JavaDoc res) throws Exception JavaDoc {
181         File JavaDoc f = File.createTempFile("res", ".xml");
182         f.deleteOnExit ();
183         
184         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
185         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(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 JavaDoc extractResource(String JavaDoc res) throws Exception JavaDoc {
197         URL JavaDoc u = PublicPackagesInProjectizedXMLTest.class.getResource(res);
198         assertNotNull ("Resource should be found " + res, u);
199         
200         File JavaDoc f = File.createTempFile("res", ".xml");
201         f.deleteOnExit ();
202         
203         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
204         InputStream JavaDoc 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 JavaDoc res, String JavaDoc[] args) throws Exception JavaDoc {
216         execute (extractResource (res), args);
217     }
218     
219     private static ByteArrayOutputStream JavaDoc out;
220     private static ByteArrayOutputStream JavaDoc err;
221     
222     final static String JavaDoc getStdOut() {
223         return out.toString();
224     }
225     final static String JavaDoc getStdErr() {
226         return err.toString();
227     }
228     
229     final static void execute(File JavaDoc f, String JavaDoc[] args) throws Exception JavaDoc {
230         // we need security manager to prevent System.exit
231
if (! (System.getSecurityManager () instanceof MySecMan)) {
232             out = new java.io.ByteArrayOutputStream JavaDoc ();
233             err = new java.io.ByteArrayOutputStream JavaDoc ();
234             System.setOut (new java.io.PrintStream JavaDoc (out));
235             System.setErr (new java.io.PrintStream JavaDoc (err));
236             
237             System.setSecurityManager (new MySecMan ());
238         }
239         
240         MySecMan sec = (MySecMan)System.getSecurityManager();
241         
242         // Jesse claims that this is not the right way how the execution
243
// of an ant script should be invoked:
244
//
245
// better IMHO to just run the task directly
246
// (setProject() and similar, configure its bean properties, and call
247
// execute()), or just make a new Project and initialize it.
248
// ant.Main.main is not intended for embedded use. Then you could get rid
249
// of the SecurityManager stuff, would be cleaner I think.
250
//
251
// If I had to write this once again, I would try to follow the
252
// "just make a new Project and initialize it", but as this works
253
// for me now, I leave it for the time when somebody really
254
// needs that...
255

256         List JavaDoc arr = new ArrayList JavaDoc();
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 JavaDoc[])arr.toArray (new String JavaDoc[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 JavaDoc msg, int e) {
283             super (msg);
284             this.exitCode = e;
285         }
286         
287         public static void assertExitCode (String JavaDoc 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 JavaDoc {
299         public void printStackTrace() {
300         }
301         public void printStackTrace(PrintStream JavaDoc ps) {
302         }
303         public void printStackTrace(PrintWriter JavaDoc ps) {
304         }
305     }
306     
307     private static class MySecMan extends SecurityManager JavaDoc {
308         public Integer JavaDoc exitCode;
309         
310         private boolean active;
311         
312         public void checkExit (int status) {
313             if (active) {
314                 exitCode = new Integer JavaDoc (status);
315                 throw new MySecExc ();
316             }
317         }
318
319         public void checkPermission(Permission JavaDoc perm, Object JavaDoc context) {
320         }
321
322         public void checkPermission(Permission JavaDoc perm) {
323         /*
324             if (perm instanceof RuntimePermission) {
325                 if (perm.getName ().equals ("setIO")) {
326                     throw new MySecExc ();
327                 }
328             }
329          */

330         }
331
332         public void checkMulticast(InetAddress JavaDoc maddr) {
333         }
334
335         public void checkAccess (ThreadGroup JavaDoc g) {
336         }
337
338         public void checkWrite (String JavaDoc file) {
339         }
340
341         public void checkLink (String JavaDoc lib) {
342         }
343
344         public void checkExec (String JavaDoc cmd) {
345         }
346
347         public void checkDelete (String JavaDoc file) {
348         }
349
350         public void checkPackageAccess (String JavaDoc pkg) {
351         }
352
353         public void checkPackageDefinition (String JavaDoc pkg) {
354         }
355
356         public void checkPropertyAccess (String JavaDoc key) {
357         }
358
359         public void checkRead (String JavaDoc file) {
360         }
361
362         public void checkSecurityAccess (String JavaDoc target) {
363         }
364
365         public void checkWrite(FileDescriptor JavaDoc fd) {
366         }
367
368         public void checkListen (int port) {
369         }
370
371         public void checkRead(FileDescriptor JavaDoc fd) {
372         }
373
374         public void checkMulticast(InetAddress JavaDoc maddr, byte ttl) {
375         }
376
377         public void checkAccess (Thread JavaDoc t) {
378         }
379
380         public void checkConnect (String JavaDoc host, int port, Object JavaDoc context) {
381         }
382
383         public void checkRead (String JavaDoc file, Object JavaDoc context) {
384         }
385
386         public void checkConnect (String JavaDoc host, int port) {
387         }
388
389         public void checkAccept (String JavaDoc host, int port) {
390         }
391
392         public void checkMemberAccess (Class JavaDoc 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     } // end of MySecMan
417
}
418
Popular Tags