1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.Arrays ; 29 import java.util.StringTokenizer ; 30 import java.util.jar.JarEntry ; 31 import java.util.jar.JarOutputStream ; 32 import java.util.jar.Manifest ; 33 import java.util.zip.ZipEntry ; 34 import java.util.zip.ZipOutputStream ; 35 import junit.framework.*; 36 37 import org.netbeans.junit.*; 38 39 40 44 public class CheckLinksTest extends NbTestCase { 45 public CheckLinksTest (String name) { 46 super (name); 47 } 48 49 public void testByDefaultAllURLsAreAllowed () throws Exception { 50 java.io.File 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 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 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 69 } 70 71 72 public void testForbidenExternalURLsAreCorrectlyReported () throws Exception { 73 java.io.File 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 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 [] { }); 93 fail ("This should fail as the URL is forbidden"); 94 } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) { 95 } 97 } 98 99 100 public void testAnyURLCanBeForbidden () throws Exception { 101 java.io.File 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 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 [] { }); 121 fail ("This should fail as the URL is forbidden"); 122 } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) { 123 } 125 } 126 127 public void testIfAcceptedFirstThenItDoesNotMatterThatItIsForbiddenLater () throws Exception { 128 java.io.File 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 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 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 149 } 150 151 152 public void testSkipCommentedOutLinks () throws Exception { 153 java.io.File 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 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 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 175 } 176 177 178 public void testDocFilesRelativeLinks () throws Exception { 179 java.io.File 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 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 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 199 } 200 201 202 public void testDocFilesInvalidLinks () throws Exception { 203 java.io.File 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 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 try { 222 PublicPackagesInProjectizedXMLTest.execute (f, new String [] { }); 223 fail ("This should fail as the link is broken"); 224 } catch (PublicPackagesInProjectizedXMLTest.ExecutionError ex) { 225 } 227 } 228 229 230 private static File extractHTMLFile (String s) throws Exception { 231 File f = PublicPackagesInProjectizedXMLTest.extractString (s); 232 File n = new File (f.getParentFile (), f.getName () + ".html"); 233 assertTrue ("Rename succeeded", f.renameTo (n)); 234 return n; 235 } 236 237 } 238 | Popular Tags |