KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25 import junit.framework.Test;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.junit.NbTestSuite;
28
29 /**
30  *
31  * @author Jaroslav Tulach
32  */

33 public class CheckLicenseTest extends NbTestCase {
34
35     public CheckLicenseTest(String JavaDoc testName) {
36         super(testName);
37     }
38
39     public static Test suite() {
40         //return new CheckLicenseTest("testReplaceHTMLLicense");
41
return new NbTestSuite(CheckLicenseTest.class);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45     }
46
47     protected void tearDown() throws Exception JavaDoc {
48     }
49
50     public void testWeCanSearchForSunPublicLicense() throws Exception JavaDoc {
51         java.io.File JavaDoc license = PublicPackagesInProjectizedXMLTest.extractString(
52             "<!-- Sun Public License -->\n" +
53             "<head></head><body>\n" +
54             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
55             "</body>"
56         );
57       
58         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
59             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
60             "<project name=\"Test\" basedir=\".\" default=\"all\" >" +
61             " <taskdef name=\"checkl\" classname=\"org.netbeans.nbbuild.CheckLicense\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
62             "<target name=\"all\" >" +
63             " <checkl fragment='Sun Public' >" +
64             " <fileset dir='" + license.getParent() + "'>" +
65             " <include name=\"" + license.getName () + "\" />" +
66             " </fileset>\n" +
67             " </checkl>" +
68             "</target>" +
69             "</project>"
70         );
71         // success
72
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
73
74         if (PublicPackagesInProjectizedXMLTest.getStdErr().indexOf(license.getPath()) > - 1) {
75             fail("file name shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdErr());
76         }
77         if (PublicPackagesInProjectizedXMLTest.getStdErr().indexOf("no license") > - 1) {
78             fail("warning shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdErr());
79         }
80     }
81
82     public void testTheTaskFailsIfItIsPresent() throws Exception JavaDoc {
83         java.io.File JavaDoc license = PublicPackagesInProjectizedXMLTest.extractString(
84             "<!-- Sun Public License -->\n" +
85             "<head></head><body>\n" +
86             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
87             "</body>"
88         );
89         java.io.File JavaDoc license2 = PublicPackagesInProjectizedXMLTest.extractString(
90             "<!-- Sun Public License -->\n" +
91             "<head></head><body>\n" +
92             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
93             "</body>"
94         );
95         assertEquals(license.getParent(), license2.getParent());
96       
97         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
98             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
99             "<project name=\"Test\" basedir=\".\" default=\"all\" >" +
100             " <taskdef name=\"checkl\" classname=\"org.netbeans.nbbuild.CheckLicense\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
101             "<target name=\"all\" >" +
102             " <checkl fragment='Sun Public' fail='whenpresent' >" +
103             " <fileset dir='" + license.getParent() + "'>" +
104             " <include name=\"" + license.getName () + "\" />" +
105             " <include name=\"" + license2.getName () + "\" />" +
106             " </fileset>\n" +
107             " </checkl>" +
108             "</target>" +
109             "</project>"
110         );
111         try {
112             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
113             fail("Should fail as the license is missing");
114         } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
115             // ok
116
}
117         
118         String JavaDoc out = PublicPackagesInProjectizedXMLTest.getStdErr();
119         if (out.indexOf(license.getName()) == -1) {
120             fail(license.getName() + " should be there: " + out);
121         }
122         if (out.indexOf(license2.getName()) == -1) {
123             fail(license2.getName() + " should be there: " + out);
124         }
125     }
126     
127     public void testTheTaskReportsIfItIsMissing() throws Exception JavaDoc {
128         java.io.File JavaDoc license = PublicPackagesInProjectizedXMLTest.extractString(
129             "<head></head><body>\n" +
130             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
131             "</body>"
132         );
133       
134         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
135             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
136             "<project name=\"Test\" basedir=\".\" default=\"all\" >" +
137             " <taskdef name=\"checkl\" classname=\"org.netbeans.nbbuild.CheckLicense\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
138             "<target name=\"all\" >" +
139             " <checkl fragment='Sun Public' >" +
140             " <fileset dir='" + license.getParent() + "'>" +
141             " <include name=\"" + license.getName () + "\" />" +
142             " </fileset>\n" +
143             " </checkl>" +
144             "</target>" +
145             "</project>"
146         );
147         // success
148
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
149         
150         if (PublicPackagesInProjectizedXMLTest.getStdErr().indexOf(license.getPath()) == - 1) {
151             fail("file name shall be there: " + PublicPackagesInProjectizedXMLTest.getStdErr());
152         }
153         if (PublicPackagesInProjectizedXMLTest.getStdErr().indexOf("no license") == - 1) {
154             fail("warning shall be there: " + PublicPackagesInProjectizedXMLTest.getStdErr());
155         }
156     }
157
158     public void testNoReportsWhenInFailMode() throws Exception JavaDoc {
159         java.io.File JavaDoc license = PublicPackagesInProjectizedXMLTest.extractString(
160             "<head></head><body>\n" +
161             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
162             "</body>"
163         );
164       
165         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
166             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
167             "<project name=\"Test\" basedir=\".\" default=\"all\" >" +
168             " <taskdef name=\"checkl\" classname=\"org.netbeans.nbbuild.CheckLicense\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
169             "<target name=\"all\" >" +
170             " <checkl fragment='Sun Public' fail='whenpresent'>" +
171             " <fileset dir='" + license.getParent() + "'>" +
172             " <include name=\"" + license.getName () + "\" />" +
173             " </fileset>\n" +
174             " </checkl>" +
175             "</target>" +
176             "</project>"
177         );
178         // success
179
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
180         
181         if (PublicPackagesInProjectizedXMLTest.getStdErr().indexOf(license.getPath()) != - 1) {
182             fail("file name shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdErr());
183         }
184         if (PublicPackagesInProjectizedXMLTest.getStdErr().indexOf("no license") != - 1) {
185             fail("warning shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdErr());
186         }
187     }
188     
189     public void testTheTaskFailsIfItIsMissing() throws Exception JavaDoc {
190         java.io.File JavaDoc license = PublicPackagesInProjectizedXMLTest.extractString(
191             "<head></head><body>\n" +
192             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
193             "</body>"
194         );
195       
196         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
197             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
198             "<project name=\"Test\" basedir=\".\" default=\"all\" >" +
199             " <taskdef name=\"checkl\" classname=\"org.netbeans.nbbuild.CheckLicense\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
200             "<target name=\"all\" >" +
201             " <checkl fragment='Sun Public' fail='whenmissing' >" +
202             " <fileset dir='" + license.getParent() + "'>" +
203             " <include name=\"" + license.getName () + "\" />" +
204             " </fileset>\n" +
205             " </checkl>" +
206             "</target>" +
207             "</project>"
208         );
209         try {
210             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
211             fail("Should fail as the license is missing");
212         } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
213             // ok
214
}
215     }
216     
217     public void testReplaceJavaLicense() throws Exception JavaDoc {
218         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractString(
219 "/*\n" +
220 " * Sun Public License Notice\n" +
221 " *\n" +
222 " * The contents of this file are subject to the Sun Public License\n" +
223 " * Version 1.0 (the \"License\"). You may not use this file except in\n" +
224 " * compliance with the License. A copy of the License is available at\n" +
225 " * http://www.sun.com/\n" +
226 " *\n" +
227 " * The Original Code is NetBeans. The Initial Developer of the Original\n" +
228 " * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun\n" +
229 " * Microsystems, Inc. All Rights Reserved.\n" +
230 " */\n" +
231 "\n" +
232 "package test;\n" +
233 "public class MyTest {\n" +
234 " public static int FIELD = 1;\n" +
235 "}\n" +
236 "\n"
237         );
238         File JavaDoc java = new File JavaDoc(tmp.getParentFile(), "MyTest.java");
239         tmp.renameTo(java);
240         assertTrue("File exists", java.exists());
241       
242         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
243
244         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
245             "-verbose",
246             "-Ddir=" + java.getParent(),
247             "-Dinclude=" + java.getName(),
248         });
249         
250         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf(java.getPath()) == - 1) {
251             fail("file name shall be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
252         }
253         
254         
255         assertTrue("Still exists", java.exists());
256         
257         String JavaDoc content = PublicPackagesInProjectizedXMLTest.readFile(java);
258         {
259             Matcher JavaDoc m = Pattern.compile("\\* *Ahoj *\\* *Jardo").matcher(content.replace('\n', ' '));
260             if (!m.find()) {
261                 fail("Replacement shall be there together with prefix:\n" + content);
262             }
263         }
264         
265         {
266             Matcher JavaDoc m = Pattern.compile("^ \\*New. \\*Warning", Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
267             if (!m.find()) {
268                 fail("warning shall be there:\n" + content);
269             }
270         }
271         
272         {
273             String JavaDoc[] lines = content.split("\n");
274             if (lines.length < 5) {
275                 fail("There should be more than five lines: " + content);
276             }
277             for (int i = 0; i < lines.length; i++) {
278                 if (lines[i].length() == 0) {
279                     fail("There is an empty line: " + content);
280                 }
281                 if (lines[i].equals(" */")) {
282                     break;
283                 }
284                 if (lines[i].endsWith(" ")) {
285                     fail("Ends with space: '" + lines[i] + "' in:\n" + content);
286                 }
287             }
288         }
289     }
290
291     
292     public void testReplaceHTMLLicense() throws Exception JavaDoc {
293         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
294
295         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseHtmlExample.xml");
296         File JavaDoc html = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
297         tmp.renameTo(html);
298         assertTrue("File exists", html.exists());
299       
300
301         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
302             "-verbose",
303             "-Ddir=" + html.getParent(),
304             "-Dinclude=" + html.getName(),
305         });
306         
307         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf(html.getPath()) == - 1) {
308             fail("file name shall be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
309         }
310         
311         
312         assertTrue("Still exists", html.exists());
313         
314         String JavaDoc content = PublicPackagesInProjectizedXMLTest.readFile(html);
315         {
316             Matcher JavaDoc m = Pattern.compile(" *- *Ahoj *- *Jardo").matcher(content.replace('\n', ' '));
317             if (!m.find()) {
318                 fail("Replacement shall be there together with prefix:\n" + content);
319             }
320         }
321         
322         {
323             Matcher JavaDoc m = Pattern.compile("^ *-New. *-Warning", Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
324             if (!m.find()) {
325                 fail("warning shall be there:\n" + content);
326             }
327         }
328         
329         {
330             String JavaDoc[] lines = content.split("\n");
331             if (lines.length < 5) {
332                 fail("There should be more than five lines: " + content);
333             }
334             for (int i = 0; i < lines.length; i++) {
335                 if (lines[i].length() == 0) {
336                     fail("There is an empty line: " + content);
337                 }
338                 if (lines[i].indexOf("-->") >= 0) {
339                     break;
340                 }
341                 if (lines[i].endsWith(" ")) {
342                     fail("Ends with space: '" + lines[i] + "' in:\n" + content);
343                 }
344             }
345         }
346     }
347
348     public void testNoReplaceWhenNoHTMLLicense() throws Exception JavaDoc {
349         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
350
351         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractString(
352             "<head></head><body>\n" +
353             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
354             "1997-2006" +
355             "</body>"
356         );
357         File JavaDoc html = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
358         tmp.renameTo(html);
359         assertTrue("File exists", html.exists());
360       
361
362         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
363             "-Ddir=" + html.getParent(),
364             "-Dinclude=" + html.getName(),
365         });
366         
367         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf(html.getPath()) != - 1) {
368             fail("file name shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
369         }
370         
371     }
372
373     public void testMayReplaces() throws Exception JavaDoc {
374         if (isWindows()) return;
375         
376         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
377
378         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractString(
379             "<head></head><body>\n" +
380             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
381             "Original Code\n" +
382             "Original Code\n" +
383             "Original Code\n" +
384             "Original Code\n" +
385             "</body>"
386         );
387         File JavaDoc html = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
388         tmp.renameTo(html);
389         assertTrue("File exists", html.exists());
390       
391
392         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
393             "-Ddir=" + html.getParent(),
394             "-Dinclude=" + html.getName(),
395         });
396         
397         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf("Original Code") != - 1) {
398             fail("Original Code shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
399         }
400
401         String JavaDoc out = PublicPackagesInProjectizedXMLTest.readFile(html);
402         int first = out.indexOf("Original Software");
403         if (first == - 1) {
404             fail("Original Software shall be there: " + out);
405         }
406         if (out.indexOf("Original Software", first + 1) == - 1) {
407             fail("Original Software shall be there: " + out);
408         }
409     }
410     
411     
412     public void testWrongLineBeginningsWhenNoPrefix() throws Exception JavaDoc {
413         String JavaDoc txt = "<!--\n" +
414         " Sun Public License Notice\n" +
415         "\n" +
416         "The contents of this file are subject to the Sun Public License\n" +
417         "Version 1.0 (the 'License'). You may not use this file except in\n" +
418         "compliance with the License. A copy of the License is available at\n" +
419         "http://www.sun.com/\n" +
420         "\n" +
421         "The Original Code is NetBeans. The Initial Developer of the Original\n" +
422         "Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun\n" +
423         "Microsystems, Inc. All Rights Reserved.\n" +
424         "-->\n";
425         String JavaDoc script = createScript();
426         
427     
428         File JavaDoc fileScript = PublicPackagesInProjectizedXMLTest.extractString(script);
429         File JavaDoc fileTxt = PublicPackagesInProjectizedXMLTest.extractString(txt);
430         
431         PublicPackagesInProjectizedXMLTest.execute (fileScript, new String JavaDoc[] {
432             "-Ddir=" + fileTxt.getParent(),
433             "-Dinclude=" + fileTxt.getName(),
434         });
435         
436         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf("Original Code") != - 1) {
437             fail("Original Code shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
438         }
439
440         String JavaDoc out = PublicPackagesInProjectizedXMLTest.readFile(fileTxt);
441         
442         String JavaDoc[] arr = out.split("\n");
443         for (int i = 0; i < arr.length; i++) {
444             if (arr[i].endsWith(" ")) {
445                 fail("Ends with space: '" + arr[i] + "' in:\n" + out);
446             }
447             if (arr[i].length() < 2) {
448                 continue;
449             }
450             if (arr[i].charAt(0) != ' ') {
451                 continue;
452             }
453             
454             fail("This line seems to start with space:\n" + arr[i] + "\nwhich is wrong in whole output:\n" + out);
455         }
456     }
457
458     private static String JavaDoc createScript() {
459         String JavaDoc script =
460     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
461     "<project name=\"Test\" basedir=\".\" default=\"all\" >" +
462     " <taskdef name=\"checklicense\" classname=\"org.netbeans.nbbuild.CheckLicense\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
463     "<target name=\"all\" >" +
464 " <checklicense >\n" +
465 " <fileset dir='${dir}'>\n" +
466 " <include name='${include}'/>" +
467 " </fileset>\n" +
468 "\n" +
469 " <convert \n" +
470 " token='^([ \\t]*[^ \\n]+[ \\t]?)?[ \\t]*Sun Public License Notice' \n" +
471 " prefix='true'\n" +
472 " >\n" +
473 " <line text='The contents of this file are subject to the terms of the Common Development'/>\n" +
474 " <line text='and Distribution License (the License). You may not use this file except in'/>\n" +
475 " <line text='compliance with the License.'/>\n" +
476 " </convert>\n" +
477 " <convert \n" +
478 " token='The *contents *of *this *file *are\n" +
479 " *subject *to *the *Sun *Public.*available.*at.*([hH][tT][tT][pP]://www.sun.com/|http://jalopy.sf.net/license-spl.html)'\n" +
480 " >\n" +
481 " <line text='You can obtain a copy of the License at http://www.netbeans.org/cddl.html'/>\n" +
482 " <line text='or http://www.netbeans.org/cddl.txt.'/>\n" +
483 " <line text=''/>\n" +
484 " <line text='When distributing Covered Code, include this CDDL Header Notice in each file'/>\n" +
485 " <line text='and include the License file at http://www.netbeans.org/cddl.txt.'/>\n" +
486 " <line text='If applicable, add the following below the CDDL Header, with the fields'/>\n" +
487 " <line text='enclosed by brackets [] replaced by your own identifying information:'/>\n" +
488 " <line text='\"Portions Copyrighted [year] [name of copyright owner]\"'/>\n" +
489 " </convert>\n" +
490 " <convert token='1997-[0-2][09][09][0-9]' replace='1997-2006'/>\n" +
491 " <convert token='Original\\n[^A-Za-z]*Code' replace='Original\\nSoftware' replaceall='true'/>\n" +
492 " <convert token='Original Code' replace='Original Software' replaceall='true'/>\n" +
493 " </checklicense>\n" +
494 " </target>\n" +
495 " </project>\n";
496         return script;
497     }
498     
499     public void testReplacesTextSeparatedByNewLine() throws Exception JavaDoc {
500         if (isWindows()) return;
501         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
502
503         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractString(
504             "/*\n" +
505             " * Sun Public License Notice\n" +
506             " * \n" +
507             " * The contents of this file are subject to the Sun Public License\n" +
508             " * Version 1.0 (the 'License'). You may not use this file except in\n" +
509             " * compliance with the License. A copy of the License is available at\n" +
510             " * http://www.sun.com/\n" +
511             " * \n" +
512             " * The Original Code is NetBeans. The Initial Developer of the Original\n" +
513             " * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun\n" +
514             " * Microsystems, Inc. All Rights Reserved.\n" +
515             " */\n" +
516             "\n" +
517             "\n" +
518             "package org.openide.text;\n"
519         );
520         File JavaDoc java = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
521         tmp.renameTo(java);
522         assertTrue("File exists", java.exists());
523       
524
525         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
526             "-Ddir=" + java.getParent(),
527             "-Dinclude=" + java.getName(),
528         });
529         
530         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf("Code") != - 1) {
531             fail("Original Code shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
532         }
533
534         String JavaDoc out = PublicPackagesInProjectizedXMLTest.readFile(java);
535         int first = out.indexOf("Original Software");
536         if (first == - 1) {
537             fail("Original Software shall be there: " + out);
538         }
539         if (out.indexOf("Software", first + 25) == - 1) {
540             fail("Original Software shall be there: " + out);
541         }
542         
543         String JavaDoc[] lines = out.split("\n");
544         for (int i = 0; i < lines.length; i++) {
545             if (lines[i].length() > 80) {
546                 fail("Too long line:\n" + lines[i] + "\n in file:\n" + out);
547             }
548             if (lines[i].endsWith(" ")) {
549                 fail("Ends with space: '" + lines[i] + "' in:\n" + out);
550             }
551         }
552     }
553     
554     
555     
556     public void testWorksOnEmptyFile() throws Exception JavaDoc {
557         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
558
559         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractString("");
560         File JavaDoc html = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
561         tmp.renameTo(html);
562         assertTrue("File exists", html.exists());
563       
564
565         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
566             "-Ddir=" + html.getParent(),
567             "-Dinclude=" + html.getName(),
568         });
569         
570         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf(html.getPath()) != - 1) {
571             fail("file name shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
572         }
573         
574     }
575     
576     public void testReplacePropertiesLicense() throws Exception JavaDoc {
577         if (isWindows()) return;
578         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
579
580         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicensePropertiesExample.properties");
581         File JavaDoc html = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
582         tmp.renameTo(html);
583         assertTrue("File exists", html.exists());
584       
585
586         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
587             "-verbose",
588             "-Ddir=" + html.getParent(),
589             "-Dinclude=" + html.getName(),
590         });
591         
592         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf(html.getPath()) == - 1) {
593             fail("file name shall be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
594         }
595         
596         
597         assertTrue("Still exists", html.exists());
598         
599         
600         String JavaDoc content = PublicPackagesInProjectizedXMLTest.readFile(html);
601         
602         if (!content.startsWith("#")) {
603             fail("Shall start with #:\n" + content);
604         }
605         
606         {
607             Matcher JavaDoc m = Pattern.compile(" *\\# *Ahoj *\\# *Jardo").matcher(content.replace('\n', ' '));
608             if (!m.find()) {
609                 fail("Replacement shall be there together with prefix:\n" + content);
610             }
611         }
612         
613         {
614             Matcher JavaDoc m = Pattern.compile("^ *\\#New. *\\#Warning", Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
615             if (!m.find()) {
616                 fail("warning shall be there:\n" + content);
617             }
618         }
619         
620         {
621             String JavaDoc[] lines = content.split("\n");
622             if (lines.length < 5) {
623                 fail("There should be more than five lines: " + content);
624             }
625             for (int i = 0; i < lines.length; i++) {
626                 if (lines[i].endsWith(" ")) {
627                     fail("Ends with space: '" + lines[i] + "' in:\n" + content);
628                 }
629                 if (lines[i].length() == 0) {
630                     fail("There is an empty line: " + content);
631                 }
632                 if (lines[i].indexOf("All Rights") >= 0) {
633                     break;
634                 }
635             }
636         }
637         
638         {
639             if (content.indexOf("2002") != -1) {
640                 fail("No reference to year 2002:\n" + content);
641             }
642             if (content.indexOf("2006") == -1) {
643                 fail("There should be a ref to 2006:\n" + content);
644             }
645         }
646     }
647
648     private static boolean isWindows() {
649         String JavaDoc name = System.getProperty("os.name");
650         return name != null && name.toLowerCase().indexOf("windows") >= 0;
651     }
652     
653     public void testReplaceXMLLicense() throws Exception JavaDoc {
654         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseAnt.xml");
655
656         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractResource("CheckLicenseXmlExample.xml");
657         File JavaDoc xml = new File JavaDoc(tmp.getParentFile(), "MyTest.xml");
658         tmp.renameTo(xml);
659         assertTrue("File exists", xml.exists());
660       
661
662         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
663             "-verbose",
664             "-Ddir=" + xml.getParent(),
665             "-Dinclude=" + xml.getName(),
666         });
667         
668         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf(xml.getPath()) == - 1) {
669             fail("file name shall be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
670         }
671         
672         
673         assertTrue("Still exists", xml.exists());
674         
675         
676         String JavaDoc content = PublicPackagesInProjectizedXMLTest.readFile(xml);
677         
678         if (!content.startsWith("<")) {
679             fail("Shall start with <:\n" + content);
680         }
681         
682         {
683             Matcher JavaDoc m = Pattern.compile(" *Ahoj *Jardo").matcher(content.replace('\n', ' '));
684             if (!m.find()) {
685                 fail("Replacement shall be there together with prefix:\n" + content);
686             }
687         }
688         
689         {
690             Matcher JavaDoc m = Pattern.compile("^ *New. *Warning", Pattern.MULTILINE | Pattern.DOTALL).matcher(content);
691             if (!m.find()) {
692                 fail("warning shall be there:\n" + content);
693             }
694         }
695         
696         {
697             if (content.indexOf("2002") != -1) {
698                 fail("No reference to year 2002:\n" + content);
699             }
700             if (content.indexOf("2006") == -1) {
701                 fail("There should be a ref to 2006:\n" + content);
702             }
703         }
704     }
705
706     public void testProblemsWithTermEmulator() throws Exception JavaDoc {
707         String JavaDoc txt =
708             "/* \n" +
709             " * Sun Public License Notice\n" +
710             " *\n" +
711             " * The contents of this file are subject to the Sun Public License Version\n" +
712             " * 1.0 (the \"License\"). You may not use this file except in compliance\n" +
713             " * with the License. A copy of the License is available at\n" +
714             " * http://www.sun.com/\n" +
715             " * \n" +
716             " * The Original Code is Terminal Emulator.\n" +
717             " * The Initial Developer of the Original Code is Sun Microsystems, Inc..\n" +
718             " * Portions created by Sun Microsystems, Inc. are Copyright (C) 2001.\n" +
719             " * All Rights Reserved.\n" +
720             " *\n" +
721             " * Contributor(s): Ivan Soleimanipour.\n" +
722             " */\n";
723         String JavaDoc script = createScript();
724         
725     
726         File JavaDoc fileScript = PublicPackagesInProjectizedXMLTest.extractString(script);
727         File JavaDoc fileTxt = PublicPackagesInProjectizedXMLTest.extractString(txt);
728         
729         PublicPackagesInProjectizedXMLTest.execute (fileScript, new String JavaDoc[] {
730             "-Ddir=" + fileTxt.getParent(),
731             "-Dinclude=" + fileTxt.getName(),
732         });
733         
734         if (PublicPackagesInProjectizedXMLTest.getStdOut().indexOf("Original Code") != - 1) {
735             fail("Original Code shall not be there: " + PublicPackagesInProjectizedXMLTest.getStdOut());
736         }
737
738         String JavaDoc out = PublicPackagesInProjectizedXMLTest.readFile(fileTxt);
739
740
741         if (out.indexOf("Sun Public") >= 0) {
742             fail(out);
743         }
744     }
745
746     
747     public void testDoubleHtmlComments() throws Exception JavaDoc {
748         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString(createScript());
749
750         java.io.File JavaDoc tmp = PublicPackagesInProjectizedXMLTest.extractString(
751 "<!--\n" +
752 " -- Sun Public License Notice\n" +
753 " --\n" +
754 " -- The contents of this file are subject to the Sun Public License\n" +
755 " -- Version 1.0 (the \"License\"). You may not use this file except in\n" +
756 " -- compliance with the License. A copy of the License is available at\n" +
757 " -- http://www.sun.com/\n" +
758 " --\n" +
759 " -- The Original Code is NetBeans. The Initial Developer of the Original\n" +
760 " -- Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun\n" +
761 " -- Microsystems, Inc. All Rights Reserved.\n" +
762 " -->\n"
763         );
764         File JavaDoc file = new File JavaDoc(tmp.getParentFile(), "MyTest.html");
765         tmp.renameTo(file);
766         assertTrue("File exists", file.exists());
767       
768
769         PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] {
770             "-Ddir=" + file.getParent(),
771             "-Dinclude=" + file.getName(),
772         });
773         
774         String JavaDoc out = PublicPackagesInProjectizedXMLTest.readFile(file);
775         int first = out.indexOf("Sun Public");
776         if (first != - 1) {
777             fail("Sun Public shall not be there:\n" + out);
778         }
779     }
780     
781     public void testDoNotReplaceSpacesBeyondTheLicense() throws Exception JavaDoc {
782         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
783         sb.append('A');
784         for (int i = 0; i < 10000; i++) {
785             sb.append(' ');
786         }
787         sb.append('B');
788         
789         java.io.File JavaDoc license = PublicPackagesInProjectizedXMLTest.extractString(
790             "<!-- Sun Public License Notice -->\n" +
791             "<head></head><body>\n" +
792             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
793             "</body>" +
794             sb
795         );
796         String JavaDoc script = createScript();
797         
798     
799         PublicPackagesInProjectizedXMLTest.execute (
800             PublicPackagesInProjectizedXMLTest.extractString(script),
801             new String JavaDoc[] {
802             "-Ddir=" + license.getParent(),
803             "-Dinclude=" + license.getName(),
804         });
805         
806         String JavaDoc out = PublicPackagesInProjectizedXMLTest.readFile(license);
807
808
809         if (out.indexOf("Sun Public") >= 0) {
810             fail(out);
811         }
812         
813         Matcher JavaDoc m = Pattern.compile("A( *)B").matcher(out);
814         if (!m.find()) {
815             fail("There should be long line:\n" + out);
816         }
817         if (m.group(1).length() != 10000) {
818             fail("There should be 10000 spaces, but is only: " + m.group(1).length() + "\n" + out);
819         }
820     }
821 }
822
823       
824
Popular Tags