1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28
29
30 @Deprecated
31 public class UrlTest {
32
33 static boolean incrementTestPartsIndex(final int[] testPartsIndex, final Object[] testParts) {
34 boolean carry = true;
35 boolean maxIndex = true;
36 for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
37 int index = testPartsIndex[testPartsIndexIndex];
38 final ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex];
39 if (carry) {
40 if (index < part.length - 1) {
41 index++;
42 testPartsIndex[testPartsIndexIndex] = index;
43 carry = false;
44 } else {
45 testPartsIndex[testPartsIndexIndex] = 0;
46 carry = true;
47 }
48 }
49 maxIndex &= index == part.length - 1;
50 }
51
52 return !maxIndex;
53 }
54
55
56
57
58
59
60 public static void main(final String[] argv) {
61
62 final UrlTest fct = new UrlTest();
63 fct.setUp();
64 fct.testIsValid();
65 fct.testIsValidScheme();
66 }
67
68 private final boolean printStatus = false;
69
70 private final boolean printIndex = false;
71
72
73
74
75
76
77 ResultPair[] testUrlScheme = { new ResultPair("http://", true), new ResultPair("ftp://", true), new ResultPair("h3t://", true),
78 new ResultPair("3ht://", false), new ResultPair("http:/", false), new ResultPair("http:", false), new ResultPair("http/", false),
79 new ResultPair("://", false), new ResultPair("", true) };
80
81 ResultPair[] testUrlAuthority = { new ResultPair("www.google.com", true), new ResultPair("go.com", true), new ResultPair("go.au", true),
82 new ResultPair("0.0.0.0", true), new ResultPair("255.255.255.255", true), new ResultPair("256.256.256.256", false), new ResultPair("255.com", true),
83 new ResultPair("1.2.3.4.5", false), new ResultPair("1.2.3.4.", false), new ResultPair("1.2.3", false), new ResultPair(".1.2.3.4", false),
84 new ResultPair("go.a", false), new ResultPair("go.a1a", true), new ResultPair("go.1aa", false), new ResultPair("aaa.", false),
85 new ResultPair(".aaa", false), new ResultPair("aaa", false), new ResultPair("", false) };
86
87 ResultPair[] testUrlPort = { new ResultPair(":80", true), new ResultPair(":65535", true), new ResultPair(":0", true), new ResultPair("", true),
88 new ResultPair(":-1", false), new ResultPair(":65636", true), new ResultPair(":65a", false) };
89
90 ResultPair[] testPath = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true), new ResultPair("/..", false),
91 new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("", true), new ResultPair("/test1/file", true),
92 new ResultPair("/..//file", false), new ResultPair("/test1//file", false) };
93
94
95 ResultPair[] testUrlPathOptions = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true),
96 new ResultPair("/..", false), new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("/#", false), new ResultPair("", true),
97 new ResultPair("/test1/file", true), new ResultPair("/t123/file", true), new ResultPair("/$23/file", true), new ResultPair("/../file", false),
98 new ResultPair("/..//file", false), new ResultPair("/test1//file", true), new ResultPair("/#/file", false) };
99
100 ResultPair[] testUrlQuery = { new ResultPair("?action=view", true), new ResultPair("?action=edit&mode=up", true), new ResultPair("", true) };
101
102 Object[] testUrlParts = { testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery };
103
104 Object[] testUrlPartsOptions = { testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery };
105
106 int[] testPartsIndex = { 0, 0, 0, 0, 0 };
107
108 ResultPair[] testScheme = { new ResultPair("http", true), new ResultPair("ftp", false), new ResultPair("httpd", false), new ResultPair("telnet", false) };
109
110 @BeforeEach
111 protected void setUp() {
112 for (int index = 0; index < testPartsIndex.length - 1; index++) {
113 testPartsIndex[index] = 0;
114 }
115 }
116
117 @Test
118 public void testIsValid() {
119 testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
120 setUp();
121 final int options = UrlValidator.ALLOW_2_SLASHES + UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.NO_FRAGMENTS;
122
123 testIsValid(testUrlPartsOptions, options);
124 }
125
126
127
128
129
130
131 void testIsValid(final Object[] testObjects, final int options) {
132 final UrlValidator urlVal = new UrlValidator(null, options);
133 assertTrue(urlVal.isValid("http://www.google.com"));
134 assertTrue(urlVal.isValid("http://www.google.com/"));
135 int statusPerLine = 60;
136 int printed = 0;
137 if (printIndex) {
138 statusPerLine = 6;
139 }
140 do {
141 final StringBuilder testBuffer = new StringBuilder();
142 boolean expected = true;
143 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
144 final int index = testPartsIndex[testPartsIndexIndex];
145 final ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex];
146 testBuffer.append(part[index].item);
147 expected &= part[index].valid;
148 }
149 final String url = testBuffer.toString();
150 final boolean result = urlVal.isValid(url);
151 assertEquals(expected, result, url);
152 if (printStatus) {
153 if (printIndex) {
154 System.out.print(testPartsIndextoString());
155 } else if (result == expected) {
156 System.out.print('.');
157 } else {
158 System.out.print('X');
159 }
160 printed++;
161 if (printed == statusPerLine) {
162 System.out.println();
163 printed = 0;
164 }
165 }
166 } while (incrementTestPartsIndex(testPartsIndex, testObjects));
167 if (printStatus) {
168 System.out.println();
169 }
170 }
171
172 @Test
173 public void testIsValidScheme() {
174 if (printStatus) {
175 System.out.print("\n testIsValidScheme() ");
176 }
177 final String[] schemes = { "http", "gopher" };
178
179 final UrlValidator urlVal = new UrlValidator(schemes, 0);
180 for (final ResultPair testPair : testScheme) {
181 final boolean result = urlVal.isValidScheme(testPair.item);
182 assertEquals(testPair.valid, result, testPair.item);
183 if (printStatus) {
184 if (result == testPair.valid) {
185 System.out.print('.');
186 } else {
187 System.out.print('X');
188 }
189 }
190 }
191 if (printStatus) {
192 System.out.println();
193 }
194
195 }
196
197 private String testPartsIndextoString() {
198 final StringBuilder carryMsg = new StringBuilder("{");
199 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
200 carryMsg.append(testPartsIndex[testPartsIndexIndex]);
201 if (testPartsIndexIndex < testPartsIndex.length - 1) {
202 carryMsg.append(',');
203 } else {
204 carryMsg.append('}');
205 }
206 }
207 return carryMsg.toString();
208
209 }
210
211 @Test
212 public void testValidateUrl() {
213 assertTrue(true);
214 }
215
216 @Test
217 public void testValidator202() {
218 final String[] schemes = { "http", "https" };
219 final UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
220 urlValidator.isValid(
221 "http://www.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.log");
222 }
223
224 @Test
225 public void testValidator204() {
226 final String[] schemes = { "http", "https" };
227 final UrlValidator urlValidator = new UrlValidator(schemes);
228 assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
229 }
230
231 }