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
78 ResultPair[] testUrlScheme = { new ResultPair("http://", true), new ResultPair("ftp://", true), new ResultPair("h3t://", true),
79 new ResultPair("3ht://", false), new ResultPair("http:/", false), new ResultPair("http:", false), new ResultPair("http/", false),
80 new ResultPair("://", false), new ResultPair("", true) };
81
82 ResultPair[] testUrlAuthority = { new ResultPair("www.google.com", true), new ResultPair("go.com", true), new ResultPair("go.au", true),
83 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),
84 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),
85 new ResultPair("go.a", false), new ResultPair("go.a1a", true), new ResultPair("go.1aa", false), new ResultPair("aaa.", false),
86 new ResultPair(".aaa", false), new ResultPair("aaa", false), new ResultPair("", false) };
87
88 ResultPair[] testUrlPort = { new ResultPair(":80", true), new ResultPair(":65535", true), new ResultPair(":0", true), new ResultPair("", true),
89 new ResultPair(":-1", false), new ResultPair(":65636", true), new ResultPair(":65a", false) };
90
91 ResultPair[] testPath = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true), new ResultPair("/..", false),
92 new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("", true), new ResultPair("/test1/file", true),
93 new ResultPair("/..//file", false), new ResultPair("/test1//file", false) };
94
95
96 ResultPair[] testUrlPathOptions = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true),
97 new ResultPair("/..", false), new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("/#", false), new ResultPair("", true),
98 new ResultPair("/test1/file", true), new ResultPair("/t123/file", true), new ResultPair("/$23/file", true), new ResultPair("/../file", false),
99 new ResultPair("/..//file", false), new ResultPair("/test1//file", true), new ResultPair("/#/file", false) };
100
101 ResultPair[] testUrlQuery = { new ResultPair("?action=view", true), new ResultPair("?action=edit&mode=up", true), new ResultPair("", true) };
102
103 Object[] testUrlParts = { testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery };
104
105 Object[] testUrlPartsOptions = { testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery };
106
107 int[] testPartsIndex = { 0, 0, 0, 0, 0 };
108
109 ResultPair[] testScheme = { new ResultPair("http", true), new ResultPair("ftp", false), new ResultPair("httpd", false), new ResultPair("telnet", false) };
110
111 @BeforeEach
112 protected void setUp() {
113 for (int index = 0; index < testPartsIndex.length - 1; index++) {
114 testPartsIndex[index] = 0;
115 }
116 }
117
118 @Test
119 public void testIsValid() {
120 testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
121 setUp();
122 final int options = UrlValidator.ALLOW_2_SLASHES + UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.NO_FRAGMENTS;
123
124 testIsValid(testUrlPartsOptions, options);
125 }
126
127
128
129
130
131
132 void testIsValid(final Object[] testObjects, final int options) {
133 final UrlValidator urlVal = new UrlValidator(null, options);
134 assertTrue(urlVal.isValid("http://www.google.com"));
135 assertTrue(urlVal.isValid("http://www.google.com/"));
136 int statusPerLine = 60;
137 int printed = 0;
138 if (printIndex) {
139 statusPerLine = 6;
140 }
141 do {
142 final StringBuilder testBuffer = new StringBuilder();
143 boolean expected = true;
144 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
145 final int index = testPartsIndex[testPartsIndexIndex];
146 final ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex];
147 testBuffer.append(part[index].item);
148 expected &= part[index].valid;
149 }
150 final String url = testBuffer.toString();
151 final boolean result = urlVal.isValid(url);
152 assertEquals(expected, result, url);
153 if (printStatus) {
154 if (printIndex) {
155 System.out.print(testPartsIndextoString());
156 } else if (result == expected) {
157 System.out.print('.');
158 } else {
159 System.out.print('X');
160 }
161 printed++;
162 if (printed == statusPerLine) {
163 System.out.println();
164 printed = 0;
165 }
166 }
167 } while (incrementTestPartsIndex(testPartsIndex, testObjects));
168 if (printStatus) {
169 System.out.println();
170 }
171 }
172
173 @Test
174 public void testIsValidScheme() {
175 if (printStatus) {
176 System.out.print("\n testIsValidScheme() ");
177 }
178 final String[] schemes = { "http", "gopher" };
179
180 final UrlValidator urlVal = new UrlValidator(schemes, 0);
181 for (final ResultPair testPair : testScheme) {
182 final boolean result = urlVal.isValidScheme(testPair.item);
183 assertEquals(testPair.valid, result, testPair.item);
184 if (printStatus) {
185 if (result == testPair.valid) {
186 System.out.print('.');
187 } else {
188 System.out.print('X');
189 }
190 }
191 }
192 if (printStatus) {
193 System.out.println();
194 }
195
196 }
197
198 private String testPartsIndextoString() {
199 final StringBuilder carryMsg = new StringBuilder("{");
200 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
201 carryMsg.append(testPartsIndex[testPartsIndexIndex]);
202 if (testPartsIndexIndex < testPartsIndex.length - 1) {
203 carryMsg.append(',');
204 } else {
205 carryMsg.append('}');
206 }
207 }
208 return carryMsg.toString();
209
210 }
211
212 @Test
213 public void testValidateUrl() {
214 assertTrue(true);
215 }
216
217 @Test
218 public void testValidator202() {
219 final String[] schemes = { "http", "https" };
220 final UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
221 urlValidator.isValid(
222 "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");
223 }
224
225 @Test
226 public void testValidator204() {
227 final String[] schemes = { "http", "https" };
228 final UrlValidator urlValidator = new UrlValidator(schemes);
229 assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
230 }
231
232 }