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 org.netbeans.junit.NbTestCase;
046:
047: /** Check the behaviour of CheckLinks.
048: *
049: * @author Jaroslav Tulach
050: */
051: public class CheckLinksTest extends NbTestCase {
052: public CheckLinksTest(String name) {
053: super (name);
054: }
055:
056: public void testByDefaultAllURLsAreAllowed() throws Exception {
057: java.io.File html = extractHTMLFile("<head></head><body>\n"
058: + "<a href=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n"
059: + "</body>");
060:
061: java.io.File f = PublicPackagesInProjectizedXMLTest
062: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
063: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
064: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
065: + "<target name=\"all\" >"
066: + " <checklinks checkexternal='false' basedir='"
067: + html.getParent()
068: + "' >"
069: + " <include name=\""
070: + html.getName()
071: + "\" />"
072: + " </checklinks>"
073: + "</target>"
074: + "</project>");
075: // success
076: PublicPackagesInProjectizedXMLTest.execute(f, new String[] {});
077: }
078:
079: public void testForbiddenExternalURLsAreCorrectlyReported()
080: throws Exception {
081: java.io.File html = extractHTMLFile("<head></head><body>\n"
082: + "<a href=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n"
083: + "</body>");
084:
085: java.io.File f = PublicPackagesInProjectizedXMLTest
086: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
087: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
088: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
089: + "<target name=\"all\" >"
090: + " <checklinks checkexternal='false' basedir='"
091: + html.getParent()
092: + "' >"
093: + " <include name=\""
094: + html.getName()
095: + "\" />"
096: + " <filter accept='false' pattern='http://www.netbeans.org/download/[a-zA-Z0-9\\.]*/javadoc/.*' /> "
097: + " </checklinks>"
098: + "</target>"
099: + "</project>");
100: try {
101: PublicPackagesInProjectizedXMLTest.execute(f,
102: new String[] {});
103: fail("This should fail as the URL is forbidden");
104: } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
105: // ok, this should fail on exit code
106: }
107: }
108:
109: public void testAnyURLCanBeForbidden() throws Exception {
110: java.io.File html = extractHTMLFile("<head></head><body>\n"
111: + "<a href=\"http://www.sex.org/index.hml\">Forbidden link</a>\n"
112: + "</body>");
113:
114: java.io.File f = PublicPackagesInProjectizedXMLTest
115: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
116: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
117: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
118: + "<target name=\"all\" >"
119: + " <checklinks checkexternal='false' basedir='"
120: + html.getParent()
121: + "' >"
122: + " <include name=\""
123: + html.getName()
124: + "\" />"
125: + " <filter accept='false' pattern='http://www.sex.org/.*' /> "
126: + " </checklinks>"
127: + "</target>"
128: + "</project>");
129: try {
130: PublicPackagesInProjectizedXMLTest.execute(f,
131: new String[] {});
132: fail("This should fail as the URL is forbidden");
133: } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
134: // ok, this should fail on exit code
135: }
136: }
137:
138: public void testIfAcceptedFirstThenItDoesNotMatterThatItIsForbiddenLater()
139: throws Exception {
140: java.io.File html = extractHTMLFile("<head></head><body>\n"
141: + "<a href=\"http://www.sex.org/index.hml\">Forbidden link</a>\n"
142: + "</body>");
143:
144: java.io.File f = PublicPackagesInProjectizedXMLTest
145: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
146: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
147: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
148: + "<target name=\"all\" >"
149: + " <checklinks checkexternal='false' basedir='"
150: + html.getParent()
151: + "' >"
152: + " <include name=\""
153: + html.getName()
154: + "\" />"
155: + " <filter accept='true' pattern='.*sex.*' /> "
156: + " <filter accept='false' pattern='http://www.sex.org/.*' /> "
157: + " </checklinks>"
158: + "</target>"
159: + "</project>");
160: // passes as .*sex.* is acceptable
161: PublicPackagesInProjectizedXMLTest.execute(f, new String[] {});
162: }
163:
164: public void testSkipCommentedOutLinks() throws Exception {
165: java.io.File html = extractHTMLFile("<head></head><body>\n"
166: + " <!-- This is commented out \n"
167: + "<a href=\"http://www.sex.org/index.hml\">Forbidden link</a>\n"
168: + " here ends the comment -->" + "</body>");
169:
170: java.io.File f = PublicPackagesInProjectizedXMLTest
171: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
172: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
173: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
174: + "<target name=\"all\" >"
175: + " <checklinks checkexternal='false' basedir='"
176: + html.getParent()
177: + "' >"
178: + " <include name=\""
179: + html.getName()
180: + "\" />"
181: + " <filter accept='false' pattern='.*sex.*' /> "
182: + " </checklinks>"
183: + "</target>"
184: + "</project>");
185: // passes as the forbidden URL is commented out
186: PublicPackagesInProjectizedXMLTest.execute(f, new String[] {});
187: }
188:
189: public void testDocFilesRelativeLinks() throws Exception {
190: java.io.File html = extractHTMLFile("<head></head><body>\n"
191: + "<a href=\"#RelativeLink\">This link should pass the checking</a>\n"
192: + "<a name=\"RelativeLink\"/>\n" + "</body>");
193:
194: java.io.File f = PublicPackagesInProjectizedXMLTest
195: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
196: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
197: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
198: + "<target name=\"all\" >"
199: + " <checklinks checkexternal='false' basedir='"
200: + html.getParent()
201: + "' >"
202: + " <include name=\""
203: + html.getName()
204: + "\" />"
205: + " </checklinks>"
206: + "</target>"
207: + "</project>");
208: // success
209: PublicPackagesInProjectizedXMLTest.execute(f, new String[] {});
210: }
211:
212: public void testDocFilesInvalidLinks() throws Exception {
213: java.io.File html = extractHTMLFile("<head></head><body>\n"
214: + "<a href=\"#InvalidLink\">This link should NOT pass the checking</a>\n"
215: + "</body>");
216:
217: java.io.File f = PublicPackagesInProjectizedXMLTest
218: .extractString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
219: + "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >"
220: + " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>"
221: + "<target name=\"all\" >"
222: + " <checklinks checkexternal='false' basedir='"
223: + html.getParent()
224: + "' >"
225: + " <include name=\""
226: + html.getName()
227: + "\" />"
228: + " </checklinks>"
229: + "</target>"
230: + "</project>");
231: // failure
232: try {
233: PublicPackagesInProjectizedXMLTest.execute(f,
234: new String[] {});
235: fail("This should fail as the link is broken");
236: } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
237: // ok, this should fail on exit code
238: }
239: }
240:
241: private static File extractHTMLFile(String s) throws Exception {
242: File f = PublicPackagesInProjectizedXMLTest.extractString(s);
243: File n = new File(f.getParentFile(), f.getName() + ".html");
244: assertTrue("Rename succeeded", f.renameTo(n));
245: return n;
246: }
247:
248: }
|