Update ooo320-m1
[ooovba.git] / cppuhelper / qa / unourl / cppu_unourl.cxx
blob5d39de84fd381fc5b85cf2ab80854da041d1b344
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cppu_unourl.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <cppunit/simpleheader.hxx>
33 #include "cppuhelper/unourl.hxx"
34 #include "rtl/malformeduriexception.hxx"
35 #include "rtl/strbuf.hxx"
36 #include "rtl/string.h"
37 #include "rtl/textenc.h"
38 #include "rtl/ustring.hxx"
39 #include "sal/types.h"
41 namespace cppu_unourl
43 class UrlTest : public CppUnit::TestFixture
45 public:
46 void testDescriptorParsing()
48 struct Test
50 char const * pInput;
51 bool bValid;
53 static Test const aTests[]
54 = { { "", false },
55 { "abc", true },
56 { "Abc", true },
57 { "aBC", true },
58 { "ABC", true },
59 { "1abc", true },
60 { "123", true },
61 { "abc-1", false },
62 { "ab%63", false },
63 { "abc,", false },
64 { "abc,def=", true },
65 { "abc,Def=", true },
66 { "abc,DEF=", true },
67 { "abc,1def=", true },
68 { "abc,123=", true },
69 { "abc,def-1=", false },
70 { "abc,def", false },
71 { "abc,def=xxx,def=xxx", false },
72 { "abc,def=xxx,ghi=xxx", true },
73 { "abc,,def=xxx", false },
74 { "abc,def=xxx,,ghi=xxx", false },
75 { "abc,def=xxx,ghi=xxx,", false },
76 { "abc,def=%", true },
77 { "abc,def=%1", true },
78 { "abc,def=%00", true },
79 { "abc,def=%22", true },
80 { "abc,def=\"", true },
81 { "abc,def=%ed%a0%80", true } };
82 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
84 bool bValid = false;
85 try
87 cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii(
88 aTests[i].pInput));
89 bValid = true;
91 catch (rtl::MalformedUriException &)
94 if (aTests[i].bValid)
96 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
98 else
100 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
105 void testDescriptorDescriptor()
107 struct Test
109 char const * pInput;
110 char const * pDescriptor;
112 static Test const aTests[]
113 = {{ "abc", "abc" },
114 { "Abc", "Abc" },
115 { "aBC", "aBC" },
116 { "ABC", "ABC" },
117 { "1abc", "1abc" },
118 { "123", "123" },
119 { "abc,def=", "abc,def=" },
120 { "abc,Def=", "abc,Def=" },
121 { "abc,DEF=", "abc,DEF=" },
122 { "abc,1def=", "abc,1def=" },
123 { "abc,123=", "abc,123=" },
124 { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" },
125 { "abc,def=%", "abc,def=%" },
126 { "abc,def=%1", "abc,def=%1" },
127 { "abc,def=%00", "abc,def=%00" },
128 { "abc,def=%22", "abc,def=%22" },
129 { "abc,def=\"", "abc,def=\"" },
130 { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } };
131 bool bResult = true;
132 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
134 bool bValid = false;
135 rtl::OUString aDescriptor;
138 aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
139 aTests[i].pInput)).
140 getDescriptor();
141 bValid = true;
143 catch (rtl::MalformedUriException &)
146 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
147 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
148 aDescriptor.equalsAscii(
149 aTests[i].pDescriptor));
154 void testDescriptorName()
156 struct Test
158 char const * pInput;
159 char const * pName;
161 static Test const aTests[]
162 = { { "abc", "abc" },
163 { "Abc", "abc" },
164 { "aBC", "abc" },
165 { "ABC", "abc" },
166 { "1abc", "1abc" },
167 { "123", "123" },
168 { "abc,def=", "abc" },
169 { "abc,Def=", "abc" },
170 { "abc,DEF=", "abc" },
171 { "abc,1def=", "abc" },
172 { "abc,123=", "abc" },
173 { "abc,def=xxx,ghi=xxx", "abc" },
174 { "abc,def=%", "abc" },
175 { "abc,def=%1", "abc" },
176 { "abc,def=%00", "abc" },
177 { "abc,def=%22", "abc" },
178 { "abc,def=\"", "abc" },
179 { "abc,def=%ed%a0%80", "abc" } };
180 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
182 bool bValid = false;
183 rtl::OUString aName;
186 aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
187 aTests[i].pInput)).getName();
188 bValid = true;
190 catch (rtl::MalformedUriException &)
193 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
194 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
195 aName.equalsAscii(aTests[i].pName));
199 void testDescriptorKey(void)
201 struct Test
203 char const * pInput;
204 char const * pKey;
205 bool bPresent;
207 static Test const aTests[]
208 = { { "abc", "abc", false },
209 { "abc", "def", false },
210 { "1abc", "def", false },
211 { "123", "def", false },
212 { "abc,def=", "abc", false },
213 { "abc,def=", "def", true },
214 { "abc,def=", "defg", false },
215 { "abc,def=", "de", false },
216 { "abc,def=", "ghi", false },
217 { "abc,Def=", "def", true },
218 { "abc,Def=", "Def", true },
219 { "abc,Def=", "dEF", true },
220 { "abc,Def=", "DEF", true },
221 { "abc,def=xxx,ghi=xxx", "abc", false },
222 { "abc,def=xxx,ghi=xxx", "def", true },
223 { "abc,def=xxx,ghi=xxx", "ghi", true },
224 { "abc,def=xxx,ghi=xxx", "jkl", false } };
225 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
227 bool bValid = false;
228 bool bPresent = false;
231 bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
232 aTests[i].pInput)).
233 hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
234 bValid = true;
236 catch (rtl::MalformedUriException &)
239 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
240 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
241 bPresent == aTests[i].bPresent);
245 void testDescriptorValue()
247 struct Test
249 char const * pInput;
250 char const * pKey;
251 char const * pValue;
253 static Test const aTests[]
254 = { { "abc", "abc", "" },
255 { "abc", "def", "" },
256 { "1abc", "def", "" },
257 { "123", "def", "" },
258 { "abc,def=", "abc", "" },
259 { "abc,def=", "def", "" },
260 { "abc,def=", "defg", "" },
261 { "abc,def=", "de", "" },
262 { "abc,def=", "ghi", "" },
263 { "abc,Def=", "def", "" },
264 { "abc,Def=", "Def", "" },
265 { "abc,Def=", "dEF", "" },
266 { "abc,Def=", "DEF", "" },
267 { "abc,def=xxx,ghi=xxx", "abc", "" },
268 { "abc,def=xxx,ghi=xxx", "def", "xxx" },
269 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" },
270 { "abc,def=xxx,ghi=xxx", "jkl", "" },
271 { "abc,def=%", "def", "%" },
272 { "abc,def=%1", "def", "%1" },
273 { "abc,def=%22", "def", "\"" },
274 { "abc,def=\"", "def", "\"" },
275 { "abc,def=abc", "def", "abc" },
276 { "abc,def=Abc", "def", "Abc" },
277 { "abc,def=aBC", "def", "aBC" },
278 { "abc,def=ABC", "def", "ABC" },
279 { "abc,def=%,ghi=", "def", "%" },
280 { "abc,def=%1,ghi=", "def", "%1" },
281 { "abc,def=%22,ghi=", "def", "\"" },
282 { "abc,def=\",ghi=", "def", "\"" },
283 { "abc,def=abc,ghi=", "def", "abc" },
284 { "abc,def=Abc,ghi=", "def", "Abc" },
285 { "abc,def=aBC,ghi=", "def", "aBC" },
286 { "abc,def=ABC,ghi=", "def", "ABC" },
287 { "abc,abc=,def=%", "def", "%" },
288 { "abc,abc=,def=%1", "def", "%1" },
289 { "abc,abc=,def=%22", "def", "\"" },
290 { "abc,abc=,def=\"", "def", "\"" },
291 { "abc,abc=,def=abc", "def", "abc" },
292 { "abc,abc=,def=Abc", "def", "Abc" },
293 { "abc,abc=,def=aBC", "def", "aBC" },
294 { "abc,abc=,def=ABC", "def", "ABC" } };
295 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
297 bool bValid = false;
298 rtl::OUString aValue;
301 aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
302 aTests[i].pInput)).
303 getParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
304 bValid = true;
306 catch (rtl::MalformedUriException &)
308 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
309 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
310 aValue.equalsAscii(aTests[i].pValue));
314 void testUrlParsing()
316 struct Test
318 char const * pInput;
319 bool bValid;
321 static Test const aTests[]
322 = { { "", false },
323 { "abc", false },
324 { "uno", false },
325 { "uno:", false },
326 { "uno:abc;def;ghi", true },
327 { "Uno:abc;def;ghi", true },
328 { "uNO:abc;def;ghi", true },
329 { "UNO:abc;def;ghi", true },
330 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true },
331 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false },
332 { "uno:abc;def;", false },
333 { "uno:abc;def;a", true },
334 { "uno:abc;def;A", true },
335 { "uno:abc;def;1", true },
336 { "uno:abc;def;$&+,/:=?@", true },
337 { "uno:abc;def;%24&+,/:=?@", false } };
338 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
340 bool bValid = false;
343 cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput));
344 bValid = true;
346 catch (rtl::MalformedUriException &)
349 if (aTests[i].bValid)
351 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
353 else
355 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
361 void testUrlConnection()
363 struct Test
365 char const * pInput;
366 char const * pConnection;
368 static Test const aTests[]
369 = { { "uno:abc;def;ghi", "abc" },
370 { "uno:Abc;def;ghi", "Abc" },
371 { "uno:aBC;def;ghi", "aBC" },
372 { "uno:ABC;def;ghi", "ABC" },
373 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
374 "abc,def=xxx,ghi=xxx" } };
375 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
377 bool bValid = false;
378 rtl::OUString aConnection;
381 aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii(
382 aTests[i].pInput)).
383 getConnection().getDescriptor();
384 bValid = true;
386 catch (rtl::MalformedUriException &)
388 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
389 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
390 aConnection.equalsAscii(
391 aTests[i].pConnection));
395 void testUrlProtocol()
397 struct Test
399 char const * pInput;
400 char const * pProtocol;
402 static Test const aTests[]
403 = { { "uno:abc;def;ghi", "def" },
404 { "uno:abc;Def;ghi", "Def" },
405 { "uno:abc;dEF;ghi", "dEF" },
406 { "uno:abc;DEF;ghi", "DEF" },
407 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
408 "def,ghi=xxx,jkl=xxx" } };
409 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
411 bool bValid = false;
412 rtl::OUString aProtocol;
415 aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii(
416 aTests[i].pInput)).
417 getProtocol().getDescriptor();
418 bValid = true;
420 catch (rtl::MalformedUriException &)
422 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
423 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
424 aProtocol.equalsAscii(
425 aTests[i].pProtocol));
429 void testUrlObjectName()
431 struct Test
433 char const * pInput;
434 char const * pObjectName;
436 static Test const aTests[]
437 = { { "uno:abc;def;ghi", "ghi" },
438 { "uno:abc;def;Ghi", "Ghi" },
439 { "uno:abc;def;gHI", "gHI" },
440 { "uno:abc;def;GHI", "GHI" },
441 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" },
442 { "uno:abc;def;a", "a" },
443 { "uno:abc;def;A", "A" },
444 { "uno:abc;def;1", "1" },
445 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } };
446 for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
448 bool bValid = false;
449 rtl::OUString aObjectName;
452 aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii(
453 aTests[i].pInput)).getObjectName();
454 bValid = true;
456 catch (rtl::MalformedUriException &)
458 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
459 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
460 aObjectName.equalsAscii(
461 aTests[i].pObjectName));
465 // Automatic registration code
466 CPPUNIT_TEST_SUITE(UrlTest);
467 CPPUNIT_TEST(testDescriptorParsing);
468 CPPUNIT_TEST(testDescriptorDescriptor);
469 CPPUNIT_TEST(testDescriptorName);
470 CPPUNIT_TEST(testDescriptorKey);
471 CPPUNIT_TEST(testDescriptorValue);
472 CPPUNIT_TEST(testUrlParsing);
473 CPPUNIT_TEST(testUrlConnection);
474 CPPUNIT_TEST(testUrlProtocol);
475 CPPUNIT_TEST(testUrlObjectName);
476 CPPUNIT_TEST_SUITE_END();
478 } // namespace cppu_ifcontainer
480 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(cppu_unourl::UrlTest,
481 "cppu_unourl");
483 NOADDITIONAL;