merge the formfield patch from ooo-build
[ooovba.git] / svtools / qa / test_URIHelper.cxx
blob3f989fbe4a84e197944d941a81f917b5aaa75887
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: test_URIHelper.cxx,v $
10 * $Revision: 1.11 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "sal/config.h"
36 #include <cstddef>
38 #include "com/sun/star/lang/Locale.hpp"
39 #include "com/sun/star/lang/XComponent.hpp"
40 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
41 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
42 #include "com/sun/star/ucb/Command.hpp"
43 #include "com/sun/star/ucb/CommandAbortedException.hpp"
44 #include "com/sun/star/ucb/IllegalIdentifierException.hpp"
45 #include "com/sun/star/ucb/XCommandProcessor.hpp"
46 #include "com/sun/star/ucb/XContent.hpp"
47 #include "com/sun/star/ucb/XContentIdentifier.hpp"
48 #include "com/sun/star/ucb/XContentProvider.hpp"
49 #include "com/sun/star/ucb/XContentProviderManager.hpp"
50 #include "com/sun/star/uno/Any.hxx"
51 #include "com/sun/star/uno/Exception.hpp"
52 #include "com/sun/star/uno/Reference.hxx"
53 #include "com/sun/star/uno/RuntimeException.hpp"
54 #include "com/sun/star/uno/Sequence.hxx"
55 #include "com/sun/star/uno/XComponentContext.hpp"
56 #include "com/sun/star/uri/XUriReference.hpp"
57 #include "cppuhelper/bootstrap.hxx"
58 #include "cppuhelper/implbase1.hxx"
59 #include "cppuhelper/implbase2.hxx"
60 #include "cppunit/simpleheader.hxx"
61 #include "osl/diagnose.h"
62 #include "rtl/strbuf.hxx"
63 #include "rtl/string.h"
64 #include "rtl/string.hxx"
65 #include "rtl/textenc.h"
66 #include "rtl/ustring.h"
67 #include "rtl/ustring.hxx"
68 #include "sal/types.h"
69 #include "tools/solar.h"
70 #include "unotools/charclass.hxx"
72 #include "urihelper.hxx"
74 // This test needs a UNO component context that supports various services (the
75 // UCB, an UriReferenceFactory, ...), so it is best executed within an OOo
76 // installation.
78 namespace com { namespace sun { namespace star { namespace ucb {
79 class XCommandEnvironment;
80 class XContentEventListener;
81 } } } }
83 namespace {
85 namespace css = com::sun::star;
87 // This class only implements that subset of functionality of a proper
88 // css::ucb::Content that is known to be needed here:
89 class Content:
90 public cppu::WeakImplHelper2<
91 css::ucb::XContent, css::ucb::XCommandProcessor >
93 public:
94 explicit Content(
95 css::uno::Reference< css::ucb::XContentIdentifier > const & identifier);
97 virtual css::uno::Reference< css::ucb::XContentIdentifier > SAL_CALL
98 getIdentifier() throw (css::uno::RuntimeException) {
99 return m_identifier;
102 virtual rtl::OUString SAL_CALL getContentType()
103 throw (css::uno::RuntimeException)
105 return rtl::OUString();
108 virtual void SAL_CALL addContentEventListener(
109 css::uno::Reference< css::ucb::XContentEventListener > const &)
110 throw (css::uno::RuntimeException)
113 virtual void SAL_CALL removeContentEventListener(
114 css::uno::Reference< css::ucb::XContentEventListener > const &)
115 throw (css::uno::RuntimeException)
118 virtual sal_Int32 SAL_CALL createCommandIdentifier()
119 throw (css::uno::RuntimeException)
121 return 0;
124 virtual css::uno::Any SAL_CALL execute(
125 css::ucb::Command const & command, sal_Int32 commandId,
126 css::uno::Reference< css::ucb::XCommandEnvironment > const &)
127 throw (
128 css::uno::Exception, css::ucb::CommandAbortedException,
129 css::uno::RuntimeException);
131 virtual void SAL_CALL abort(sal_Int32) throw (css::uno::RuntimeException) {}
133 private:
134 static char const m_prefix[];
136 css::uno::Reference< css::ucb::XContentIdentifier > m_identifier;
139 char const Content::m_prefix[] = "test:";
141 Content::Content(
142 css::uno::Reference< css::ucb::XContentIdentifier > const & identifier):
143 m_identifier(identifier)
145 OSL_ASSERT(m_identifier.is());
146 rtl::OUString uri(m_identifier->getContentIdentifier());
147 if (!uri.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM(m_prefix))
148 || uri.indexOf('#', RTL_CONSTASCII_LENGTH(m_prefix)) != -1)
150 throw css::ucb::IllegalIdentifierException();
154 css::uno::Any Content::execute(
155 css::ucb::Command const & command, sal_Int32,
156 css::uno::Reference< css::ucb::XCommandEnvironment > const &)
157 throw (
158 css::uno::Exception, css::ucb::CommandAbortedException,
159 css::uno::RuntimeException)
161 if (!command.Name.equalsAsciiL(
162 RTL_CONSTASCII_STRINGPARAM("getCasePreservingURL")))
164 throw css::uno::RuntimeException();
166 // If any non-empty segment starts with anything but '0', '1', or '2', fail;
167 // otherwise, if the last non-empty segment starts with '1', add a final
168 // slash, and if the last non-empty segment starts with '2', remove a final
169 // slash (if any); also, turn the given uri into all-lowercase:
170 rtl::OUString uri(m_identifier->getContentIdentifier());
171 sal_Unicode c = '0';
172 for (sal_Int32 i = RTL_CONSTASCII_LENGTH(m_prefix); i != -1;) {
173 rtl::OUString seg(uri.getToken(0, '/', i));
174 if (seg.getLength() > 0) {
175 c = seg[0];
176 if (c < '0' || c > '2') {
177 throw css::uno::Exception();
181 switch (c) {
182 case '1':
183 uri += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
184 break;
185 case '2':
186 if (uri.getLength() > 0 && uri[uri.getLength() - 1] == '/') {
187 uri = uri.copy(0, uri.getLength() -1);
189 break;
191 return css::uno::makeAny(uri.toAsciiLowerCase());
194 class Provider: public cppu::WeakImplHelper1< css::ucb::XContentProvider > {
195 public:
196 virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(
197 css::uno::Reference< css::ucb::XContentIdentifier > const & identifier)
198 throw (css::ucb::IllegalIdentifierException, css::uno::RuntimeException)
200 return new Content(identifier);
203 virtual sal_Int32 SAL_CALL compareContentIds(
204 css::uno::Reference< css::ucb::XContentIdentifier > const & id1,
205 css::uno::Reference< css::ucb::XContentIdentifier > const & id2)
206 throw (css::uno::RuntimeException)
208 OSL_ASSERT(id1.is() && id2.is());
209 return
210 id1->getContentIdentifier().compareTo(id2->getContentIdentifier());
214 class Test: public CppUnit::TestFixture {
215 public:
216 virtual void setUp();
218 void finish();
220 void testNormalizedMakeRelative();
222 void testFindFirstURLInText();
224 CPPUNIT_TEST_SUITE(Test);
225 CPPUNIT_TEST(testNormalizedMakeRelative);
226 CPPUNIT_TEST(testFindFirstURLInText);
227 CPPUNIT_TEST(finish);
228 CPPUNIT_TEST_SUITE_END();
230 private:
231 static css::uno::Reference< css::uno::XComponentContext > m_context;
234 void Test::setUp() {
235 // For whatever reason, on W32 it does not work to create/destroy a fresh
236 // component context for each test in Test::setUp/tearDown; therefore, a
237 // single component context is used for all tests and destroyed in the last
238 // pseudo-test "finish":
239 if (!m_context.is()) {
240 m_context = cppu::defaultBootstrap_InitialComponentContext();
244 void Test::finish() {
245 css::uno::Reference< css::lang::XComponent >(
246 m_context, css::uno::UNO_QUERY_THROW)->dispose();
249 void Test::testNormalizedMakeRelative() {
250 css::uno::Sequence< css::uno::Any > args(2);
251 args[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
252 args[1] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
253 css::uno::Reference< css::ucb::XContentProviderManager >(
254 (css::uno::Reference< css::lang::XMultiComponentFactory >(
255 m_context->getServiceManager(), css::uno::UNO_QUERY_THROW)->
256 createInstanceWithArgumentsAndContext(
257 rtl::OUString(
258 RTL_CONSTASCII_USTRINGPARAM(
259 "com.sun.star.ucb.UniversalContentBroker")),
260 args, m_context)),
261 css::uno::UNO_QUERY_THROW)->registerContentProvider(
262 new Provider, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test")),
263 true);
264 struct Test {
265 char const * base;
266 char const * absolute;
267 char const * relative;
269 static Test const tests[] = {
270 { "hierarchical:/", "mailto:def@a.b.c.", "mailto:def@a.b.c." },
271 { "hierarchical:/", "a/b/c", "a/b/c" },
272 { "hierarchical:/a", "hierarchical:/a/b/c?d#e", "/a/b/c?d#e" },
273 { "hierarchical:/a/", "hierarchical:/a/b/c?d#e", "b/c?d#e" },
274 { "test:/0/0/a", "test:/0/b", "../b" },
275 { "test:/1/1/a", "test:/1/b", "../b" },
276 { "test:/2/2//a", "test:/2/b", "../../b" },
277 { "test:/0a/b", "test:/0A/c#f", "c#f" },
278 { "file:///usr/bin/nonex1/nonex2",
279 "file:///usr/bin/nonex1/nonex3/nonex4", "nonex3/nonex4" },
280 { "file:///usr/bin/nonex1/nonex2#fragmentA",
281 "file:///usr/bin/nonex1/nonex3/nonex4#fragmentB",
282 "nonex3/nonex4#fragmentB" },
283 { "file:///usr/nonex1/nonex2", "file:///usr/nonex3", "../nonex3" },
284 { "file:///c:/windows/nonex1", "file:///c:/nonex2", "../nonex2" },
285 #if defined WNT
286 { "file:///c:/nonex1/nonex2", "file:///C:/nonex1/nonex3/nonex4",
287 "nonex3/nonex4" }
288 #endif
290 for (std::size_t i = 0; i < sizeof tests / sizeof tests[0]; ++i) {
291 css::uno::Reference< css::uri::XUriReference > ref(
292 URIHelper::normalizedMakeRelative(
293 m_context, rtl::OUString::createFromAscii(tests[i].base),
294 rtl::OUString::createFromAscii(tests[i].absolute)));
295 bool ok = tests[i].relative == 0
296 ? !ref.is()
297 : ref.is() && ref->getUriReference().equalsAscii(tests[i].relative);
298 rtl::OString msg;
299 if (!ok) {
300 rtl::OStringBuffer buf;
301 buf.append('<');
302 buf.append(tests[i].base);
303 buf.append(RTL_CONSTASCII_STRINGPARAM(">, <"));
304 buf.append(tests[i].absolute);
305 buf.append(RTL_CONSTASCII_STRINGPARAM(">: "));
306 if (ref.is()) {
307 buf.append('<');
308 buf.append(
309 rtl::OUStringToOString(
310 ref->getUriReference(), RTL_TEXTENCODING_UTF8));
311 buf.append('>');
312 } else {
313 buf.append(RTL_CONSTASCII_STRINGPARAM("none"));
315 buf.append(RTL_CONSTASCII_STRINGPARAM(" instead of "));
316 if (tests[i].relative == 0) {
317 buf.append(RTL_CONSTASCII_STRINGPARAM("none"));
318 } else {
319 buf.append('<');
320 buf.append(tests[i].relative);
321 buf.append('>');
323 msg = buf.makeStringAndClear();
325 CPPUNIT_ASSERT_MESSAGE(msg.getStr(), ok);
329 void Test::testFindFirstURLInText() {
330 struct Test {
331 char const * input;
332 char const * result;
333 xub_StrLen begin;
334 xub_StrLen end;
336 static Test const tests[] = {
337 { "...ftp://bla.bla.bla/blubber/...",
338 "ftp://bla.bla.bla/blubber/", 3, 29 },
339 { "..\\ftp://bla.bla.bla/blubber/...", 0, 0, 0 },
340 { "..\\ftp:\\\\bla.bla.bla\\blubber/...",
341 "file://bla.bla.bla/blubber%2F", 7, 29 },
342 { "http://sun.com", "http://sun.com/", 0, 14 },
343 { "http://sun.com/", "http://sun.com/", 0, 15 },
344 { "http://www.xerox.com@www.pcworld.com/go/3990332.htm", 0, 0, 0 },
345 { "ftp://www.xerox.com@www.pcworld.com/go/3990332.htm",
346 "ftp://www.xerox.com@www.pcworld.com/go/3990332.htm", 0, 50 },
347 { "Version.1.2.3", 0, 0, 0 },
348 { "Version:1.2.3", 0, 0, 0 },
349 { "a.b.c", 0, 0, 0 },
350 { "file:///a|...", "file:///a:", 0, 10 },
351 { "file:///a||...", "file:///a%7C%7C", 0, 11 },
352 { "file:///a|/bc#...", "file:///a:/bc", 0, 13 },
353 { "file:///a|/bc#de...", "file:///a:/bc#de", 0, 16 },
354 { "abc.def.ghi,ftp.xxx.yyy/zzz...", "ftp://ftp.xxx.yyy/zzz", 12, 27 },
355 { "abc.def.ghi,Ftp.xxx.yyy/zzz...", "ftp://Ftp.xxx.yyy/zzz", 12, 27 },
356 { "abc.def.ghi,www.xxx.yyy...", "http://www.xxx.yyy/", 12, 23 },
357 { "abc.def.ghi,wwww.xxx.yyy...", 0, 0, 0 },
358 { "abc.def.ghi,wWW.xxx.yyy...", "http://wWW.xxx.yyy/", 12, 23 },
359 { "Bla {mailto.me@abc.def.g.h.i}...",
360 "mailto:%7Bmailto.me@abc.def.g.h.i", 4, 28 },
361 { "abc@def@ghi", 0, 0, 0 },
362 { "lala@sun.com", "mailto:lala@sun.com", 0, 12 },
363 { "1lala@sun.com", "mailto:1lala@sun.com", 0, 13 },
364 { "aaa_bbb@xxx.yy", "mailto:aaa_bbb@xxx.yy", 0, 14 },
365 { "{a:\\bla/bla/bla...}", "file:///a:/bla/bla/bla", 1, 15 },
366 { "#b:/c/d#e#f#", "file:///b:/c/d", 1, 7 },
367 { "a:/", "file:///a:/", 0, 3 },
368 { ".component:", 0, 0, 0 },
369 { ".uno:", 0, 0, 0 },
370 { "cid:", 0, 0, 0 },
371 { "data:", 0, 0, 0 },
372 { "db:", 0, 0, 0 },
373 { "file:", 0, 0, 0 },
374 { "ftp:", 0, 0, 0 },
375 { "http:", 0, 0, 0 },
376 { "https:", 0, 0, 0 },
377 { "imap:", 0, 0, 0 },
378 { "javascript:", 0, 0, 0 },
379 { "ldap:", 0, 0, 0 },
380 { "macro:", 0, 0, 0 },
381 { "mailto:", 0, 0, 0 },
382 { "news:", 0, 0, 0 },
383 { "out:", 0, 0, 0 },
384 { "pop3:", 0, 0, 0 },
385 { "private:", 0, 0, 0 },
386 { "slot:", 0, 0, 0 },
387 { "staroffice.component:", 0, 0, 0 },
388 { "staroffice.db:", 0, 0, 0 },
389 { "staroffice.factory:", 0, 0, 0 },
390 { "staroffice.helpid:", 0, 0, 0 },
391 { "staroffice.java:", 0, 0, 0 },
392 { "staroffice.macro:", 0, 0, 0 },
393 { "staroffice.out:", 0, 0, 0 },
394 { "staroffice.pop3:", 0, 0, 0 },
395 { "staroffice.private:", 0, 0, 0 },
396 { "staroffice.searchfolder:", 0, 0, 0 },
397 { "staroffice.slot:", 0, 0, 0 },
398 { "staroffice.trashcan:", 0, 0, 0 },
399 { "staroffice.uno:", 0, 0, 0 },
400 { "staroffice.vim:", 0, 0, 0 },
401 { "staroffice:", 0, 0, 0 },
402 { "vim:", 0, 0, 0 },
403 { "vnd.sun.star.cmd:", 0, 0, 0 },
404 { "vnd.sun.star.help:", 0, 0, 0 },
405 { "vnd.sun.star.hier:", 0, 0, 0 },
406 { "vnd.sun.star.odma:", 0, 0, 0 },
407 { "vnd.sun.star.pkg:", 0, 0, 0 },
408 { "vnd.sun.star.script:", 0, 0, 0 },
409 { "vnd.sun.star.webdav:", 0, 0, 0 },
410 { "vnd.sun.star.wfs:", 0, 0, 0 },
411 { "generic:path", 0, 0, 0 },
412 { "wfs:", 0, 0, 0 }
414 CharClass charClass(
415 css::uno::Reference< css::lang::XMultiServiceFactory >(
416 m_context->getServiceManager(), css::uno::UNO_QUERY_THROW),
417 com::sun::star::lang::Locale(
418 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en")),
419 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("US")), rtl::OUString()));
420 for (std::size_t i = 0; i < sizeof tests / sizeof tests[0]; ++i) {
421 rtl::OUString input(rtl::OUString::createFromAscii(tests[i].input));
422 xub_StrLen begin = 0;
423 xub_StrLen end = static_cast< xub_StrLen >(input.getLength());
424 rtl::OUString result(
425 URIHelper::FindFirstURLInText(input, begin, end, charClass));
426 bool ok = tests[i].result == 0
427 ? (result.getLength() == 0 && begin == input.getLength()
428 && end == input.getLength())
429 : (result.equalsAscii(tests[i].result) && begin == tests[i].begin
430 && end == tests[i].end);
431 rtl::OString msg;
432 if (!ok) {
433 rtl::OStringBuffer buf;
434 buf.append('"');
435 buf.append(tests[i].input);
436 buf.append(RTL_CONSTASCII_STRINGPARAM("\" -> "));
437 buf.append(tests[i].result == 0 ? "none" : tests[i].result);
438 buf.append(RTL_CONSTASCII_STRINGPARAM(" ("));
439 buf.append(static_cast< sal_Int32 >(tests[i].begin));
440 buf.append(RTL_CONSTASCII_STRINGPARAM(", "));
441 buf.append(static_cast< sal_Int32 >(tests[i].end));
442 buf.append(')');
443 buf.append(RTL_CONSTASCII_STRINGPARAM(" != "));
444 buf.append(rtl::OUStringToOString(result, RTL_TEXTENCODING_UTF8));
445 buf.append(RTL_CONSTASCII_STRINGPARAM(" ("));
446 buf.append(static_cast< sal_Int32 >(begin));
447 buf.append(RTL_CONSTASCII_STRINGPARAM(", "));
448 buf.append(static_cast< sal_Int32 >(end));
449 buf.append(')');
450 msg = buf.makeStringAndClear();
452 CPPUNIT_ASSERT_MESSAGE(msg.getStr(), ok);
456 css::uno::Reference< css::uno::XComponentContext > Test::m_context;
458 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
462 NOADDITIONAL;