1 21 22 package org.apache.commons.validator; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 31 public class UrlTest extends TestCase { 32 33 private boolean printStatus = false; 34 private boolean printIndex = false; 36 public UrlTest(String testName) { 37 super(testName); 38 } 39 40 public static Test suite() { 41 return new TestSuite(UrlTest.class); 42 } 43 44 protected void setUp() { 45 for (int index = 0; index < testPartsIndex.length - 1; index++) { 46 testPartsIndex[index] = 0; 47 } 48 } 49 50 protected void tearDown() { 51 } 52 53 public void testIsValid() { 54 testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES); 55 System.out.println("\nTesting options ...."); 56 setUp(); 57 int options = 58 UrlValidator.ALLOW_2_SLASHES 59 + UrlValidator.ALLOW_ALL_SCHEMES 60 + UrlValidator.NO_FRAGMENTS; 61 62 testIsValid(testUrlPartsOptions, options); 63 } 64 65 public void testIsValidScheme() { 66 if (printStatus) { 67 System.out.print("\n testIsValidScheme() "); 68 } 69 String [] schemes = {"http", "gopher"}; 70 UrlValidator urlVal = new UrlValidator(schemes, 0); 72 for (int sIndex = 0; sIndex < testScheme.length; sIndex++) { 73 TestPair testPair = testScheme[sIndex]; 74 boolean result = urlVal.isValidScheme(testPair.item); 75 assertEquals(testPair.item, testPair.valid, result); 76 if (printStatus) { 77 if (result == testPair.valid) { 78 System.out.print('.'); 79 } else { 80 System.out.print('X'); 81 } 82 } 83 } 84 if (printStatus) { 85 System.out.println(); 86 } 87 88 } 89 90 96 public void testIsValid(Object [] testObjects, int options) { 97 UrlValidator urlVal = new UrlValidator(null, options); 98 int statusPerLine = 60; 99 int printed = 0; 100 if (printIndex) { 101 statusPerLine = 6; 102 } 103 do { 104 StringBuffer testBuffer = new StringBuffer (); 105 boolean expected = true; 106 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { 107 int index = testPartsIndex[testPartsIndexIndex]; 108 TestPair[] part = (TestPair[]) testObjects[testPartsIndexIndex]; 109 testBuffer.append(part[index].item); 110 expected &= part[index].valid; 111 } 112 String url = testBuffer.toString(); 113 boolean result = urlVal.isValid(url); 114 assertEquals(url, expected, result); 115 if (printStatus) { 116 if (printIndex) { 117 System.out.print(testPartsIndextoString()); 118 } else { 119 if (result == expected) { 120 System.out.print('.'); 121 } else { 122 System.out.print('X'); 123 } 124 } 125 printed++; 126 if (printed == statusPerLine) { 127 System.out.println(); 128 printed = 0; 129 } 130 } 131 } while (incrementTestPartsIndex(testPartsIndex, testObjects)); 132 if (printStatus) { 133 System.out.println(); 134 } 135 } 136 137 static boolean incrementTestPartsIndex(int[] testPartsIndex, Object [] testParts) { 138 boolean carry = true; boolean maxIndex = true; 140 for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) { 141 int index = testPartsIndex[testPartsIndexIndex]; 142 TestPair[] part = (TestPair[]) testParts[testPartsIndexIndex]; 143 if (carry) { 144 if (index < part.length - 1) { 145 index++; 146 testPartsIndex[testPartsIndexIndex] = index; 147 carry = false; 148 } else { 149 testPartsIndex[testPartsIndexIndex] = 0; 150 carry = true; 151 } 152 } 153 maxIndex &= (index == (part.length - 1)); 154 } 155 156 157 return (!maxIndex); 158 } 159 160 private String testPartsIndextoString() { 161 StringBuffer carryMsg = new StringBuffer ("{"); 162 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { 163 carryMsg.append(testPartsIndex[testPartsIndexIndex]); 164 if (testPartsIndexIndex < testPartsIndex.length - 1) { 165 carryMsg.append(','); 166 } else { 167 carryMsg.append('}'); 168 } 169 } 170 return carryMsg.toString(); 171 172 } 173 174 public void testValidateUrl() { 175 assertTrue(true); 176 } 177 178 182 public static void main(String [] argv) { 183 184 UrlTest fct = new UrlTest("url test"); 185 fct.setUp(); 186 fct.testIsValid(); 187 fct.testIsValidScheme(); 188 } 189 198 TestPair[] testUrlScheme = {new TestPair("http://", true), 199 new TestPair("ftp://", true), 200 new TestPair("h3t://", true), 201 new TestPair("3ht://", false), 202 new TestPair("http:/", false), 203 new TestPair("http:", false), 204 new TestPair("http/", false), 205 new TestPair("://", false), 206 new TestPair("", true)}; 207 208 TestPair[] testUrlAuthority = {new TestPair("www.google.com", true), 209 new TestPair("go.com", true), 210 new TestPair("go.au", true), 211 new TestPair("0.0.0.0", true), 212 new TestPair("255.255.255.255", true), 213 new TestPair("256.256.256.256", false), 214 new TestPair("255.com", true), 215 new TestPair("1.2.3.4.5", false), 216 new TestPair("1.2.3.4.", false), 217 new TestPair("1.2.3", false), 218 new TestPair(".1.2.3.4", false), 219 new TestPair("go.a", false), 220 new TestPair("go.a1a", true), 221 new TestPair("go.1aa", false), 222 new TestPair("aaa.", false), 223 new TestPair(".aaa", false), 224 new TestPair("aaa", false), 225 new TestPair("", false) 226 }; 227 TestPair[] testUrlPort = {new TestPair(":80", true), 228 new TestPair(":65535", true), 229 new TestPair(":0", true), 230 new TestPair("", true), 231 new TestPair(":-1", false), 232 new TestPair(":65636", true), 233 new TestPair(":65a", false) 234 }; 235 TestPair[] testPath = {new TestPair("/test1", true), 236 new TestPair("/t123", true), 237 new TestPair("/$23", true), 238 new TestPair("/..", false), 239 new TestPair("/../", false), 240 new TestPair("/test1/", false), 241 new TestPair("", false), 242 new TestPair("/test1/file", true), 243 new TestPair("/..//file", false), 244 new TestPair("/test1//file", false) 245 }; 246 TestPair[] testUrlPathOptions = {new TestPair("/test1", true), 248 new TestPair("/t123", true), 249 new TestPair("/$23", true), 250 new TestPair("/..", false), 251 new TestPair("/../", false), 252 new TestPair("/test1/", false), 253 new TestPair("/#", false), 254 new TestPair("", false), 255 new TestPair("/test1/file", true), 256 new TestPair("/t123/file", true), 257 new TestPair("/$23/file", true), 258 new TestPair("/../file", false), 259 new TestPair("/..//file", false), 260 new TestPair("/test1//file", true), 261 new TestPair("/#/file", false) 262 }; 263 264 TestPair[] testUrlQuery = {new TestPair("?action=view", true), 265 new TestPair("?action=edit&mode=up", true), 266 new TestPair("", true) 267 }; 268 269 Object [] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery}; 270 Object [] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery}; 271 int[] testPartsIndex = {0, 0, 0, 0, 0}; 272 273 TestPair[] testScheme = {new TestPair("http", true), 275 new TestPair("ftp", false), 276 new TestPair("httpd", false), 277 new TestPair("telnet", false)}; 278 279 280 } | Popular Tags |