Source Code Cross Referenced for CheckLicenseTest.java in  » IDE-Netbeans » nbbuild » org » netbeans » nbbuild » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » nbbuild » org.netbeans.nbbuild 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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