KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31 import java.util.jar.JarOutputStream JavaDoc;
32 import java.util.jar.Manifest JavaDoc;
33 import java.util.zip.ZipEntry JavaDoc;
34 import java.util.zip.ZipOutputStream JavaDoc;
35 import junit.framework.*;
36
37 import org.netbeans.junit.*;
38
39
40 /** Check the behaviour of CheckLinks.
41  *
42  * @author Jaroslav Tulach
43  */

44 public class CheckLinksTest extends NbTestCase {
45     public CheckLinksTest (String JavaDoc name) {
46         super (name);
47     }
48
49     public void testByDefaultAllURLsAreAllowed () throws Exception JavaDoc {
50         java.io.File JavaDoc html = extractHTMLFile (
51             "<head></head><body>\n" +
52             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
53             "</body>"
54         );
55       
56         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
57             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
58             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
59             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
60             "<target name=\"all\" >" +
61             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
62             " <include name=\"" + html.getName () + "\" />" +
63             " </checklinks>" +
64             "</target>" +
65             "</project>"
66         );
67         // success
68
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
69     }
70
71     
72     public void testForbidenExternalURLsAreCorrectlyReported () throws Exception JavaDoc {
73         java.io.File JavaDoc html = extractHTMLFile (
74             "<head></head><body>\n" +
75             "<a HREF=\"http://www.netbeans.org/download/dev/javadoc/OpenAPIs/index.hml\">Forbidden link</a>\n" +
76             "</body>"
77         );
78       
79         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
80             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
81             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
82             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
83             "<target name=\"all\" >" +
84             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
85             " <include name=\"" + html.getName () + "\" />" +
86             " <filter accept='false' pattern='http://www.netbeans.org/download/[a-zA-Z0-9\\.]*/javadoc/.*' /> " +
87             " </checklinks>" +
88             "</target>" +
89             "</project>"
90         );
91         try {
92             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
93             fail ("This should fail as the URL is forbidden");
94         } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
95             // ok, this should fail on exit code
96
}
97     }
98   
99   
100     public void testAnyURLCanBeForbidden () throws Exception JavaDoc {
101         java.io.File JavaDoc html = extractHTMLFile (
102             "<head></head><body>\n" +
103             "<a HREF=\"http://www.sex.org/index.hml\">Forbidden link</a>\n" +
104             "</body>"
105         );
106       
107         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
108             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
109             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
110             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
111             "<target name=\"all\" >" +
112             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
113             " <include name=\"" + html.getName () + "\" />" +
114             " <filter accept='false' pattern='http://www.sex.org/.*' /> " +
115             " </checklinks>" +
116             "</target>" +
117             "</project>"
118         );
119         try {
120             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
121             fail ("This should fail as the URL is forbidden");
122         } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
123             // ok, this should fail on exit code
124
}
125     }
126
127     public void testIfAcceptedFirstThenItDoesNotMatterThatItIsForbiddenLater () throws Exception JavaDoc {
128         java.io.File JavaDoc html = extractHTMLFile (
129             "<head></head><body>\n" +
130             "<a HREF=\"http://www.sex.org/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 Arch\" basedir=\".\" default=\"all\" >" +
137             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
138             "<target name=\"all\" >" +
139             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
140             " <include name=\"" + html.getName () + "\" />" +
141             " <filter accept='true' pattern='.*sex.*' /> " +
142             " <filter accept='false' pattern='http://www.sex.org/.*' /> " +
143             " </checklinks>" +
144             "</target>" +
145             "</project>"
146         );
147         // passes as .*sex.* is acceptable
148
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
149     }
150     
151     
152     public void testSkipCommentedOutLinks () throws Exception JavaDoc {
153         java.io.File JavaDoc html = extractHTMLFile (
154             "<head></head><body>\n" +
155             " <!-- This is commented out \n" +
156             "<a HREF=\"http://www.sex.org/index.hml\">Forbidden link</a>\n" +
157             " here ends the comment -->" +
158             "</body>"
159         );
160       
161         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
162             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
163             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
164             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
165             "<target name=\"all\" >" +
166             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
167             " <include name=\"" + html.getName () + "\" />" +
168             " <filter accept='false' pattern='.*sex.*' /> " +
169             " </checklinks>" +
170             "</target>" +
171             "</project>"
172         );
173         // passes as the forbidden URL is commented out
174
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
175     }
176     
177     
178     public void testDocFilesRelativeLinks () throws Exception JavaDoc {
179         java.io.File JavaDoc html = extractHTMLFile (
180             "<head></head><body>\n" +
181             "<a HREF=\"#RelativeLink\">This link should pass the checking</a>\n" +
182         "<a name=\"RelativeLink\"/>\n" +
183             "</body>"
184         );
185       
186         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
187             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
188             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
189             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
190             "<target name=\"all\" >" +
191             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
192             " <include name=\"" + html.getName () + "\" />" +
193             " </checklinks>" +
194             "</target>" +
195             "</project>"
196         );
197         // success
198
PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
199     }
200     
201     
202     public void testDocFilesInvalidLinks () throws Exception JavaDoc {
203         java.io.File JavaDoc html = extractHTMLFile (
204             "<head></head><body>\n" +
205             "<a HREF=\"#InvalidLink\">This link should NOT pass the checking</a>\n" +
206             "</body>"
207         );
208       
209         java.io.File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (
210             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
211             "<project name=\"Test Arch\" basedir=\".\" default=\"all\" >" +
212             " <taskdef name=\"checklinks\" classname=\"org.netbeans.nbbuild.CheckLinks\" classpath=\"${nb_all}/nbbuild/nbantext.jar\"/>" +
213             "<target name=\"all\" >" +
214             " <checklinks checkexternal='false' failonerror='true' basedir='" + html.getParent() + "' >" +
215             " <include name=\"" + html.getName () + "\" />" +
216             " </checklinks>" +
217             "</target>" +
218             "</project>"
219         );
220         // failure
221
try {
222             PublicPackagesInProjectizedXMLTest.execute (f, new String JavaDoc[] { });
223             fail ("This should fail as the link is broken");
224         } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) {
225             // ok, this should fail on exit code
226
}
227     }
228     
229     
230     private static File JavaDoc extractHTMLFile (String JavaDoc s) throws Exception JavaDoc {
231         File JavaDoc f = PublicPackagesInProjectizedXMLTest.extractString (s);
232         File JavaDoc n = new File JavaDoc (f.getParentFile (), f.getName () + ".html");
233         assertTrue ("Rename succeeded", f.renameTo (n));
234         return n;
235     }
236     
237 }
238
Popular Tags