update dev300-m57
[ooovba.git] / sal / qa / rtl / uri / rtl_Uri.cxx
blob9e7c114e8cc683ab20a0e175b9ab662c62a47ae4
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: rtl_Uri.cxx,v $
10 * $Revision: 1.7 $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 #include <stdlib.h>
35 #include <rtl/ustring.hxx>
36 #include <rtl/strbuf.hxx>
37 #include <rtl/uri.hxx>
38 #include <osl/thread.h>
39 #include <osl/file.hxx>
41 #include <cppunit/simpleheader.hxx>
43 // -----------------------------------------------------------------------------
45 namespace Stringtest
47 rtl::OString toHex(unsigned char _c)
49 rtl::OStringBuffer sStrBuf;
50 static char cHex[] = "0123456789ABCDEF";
52 int nhigh = int(_c) >> 4 & 0xf;
53 int nlow = int(_c) & 0xf;
54 sStrBuf.append( cHex[nhigh] );
55 sStrBuf.append( cHex[nlow] );
56 return sStrBuf.makeStringAndClear();
59 rtl::OString escapeString(rtl::OString const& _sStr)
61 rtl::OStringBuffer sStrBuf;
62 sal_Int32 nLength = _sStr.getLength();
63 for(int i=0;i<nLength;++i)
65 unsigned char c = (unsigned char)_sStr[i];
66 if (c > 127)
68 sStrBuf.append("%");
69 sStrBuf.append(toHex(c));
71 else
73 sStrBuf.append((char)c);
76 return sStrBuf.makeStringAndClear();
79 // -----------------------------------------------------------------------------
81 class Convert : public CppUnit::TestFixture
83 rtl::OUString m_aStr;
84 public:
86 rtl::OString toUTF8(rtl::OUString const& _suStr)
88 rtl::OString sStrAsUTF8 = rtl::OUStringToOString(_suStr, RTL_TEXTENCODING_UTF8);
89 t_print("%s\n", escapeString(sStrAsUTF8).getStr());
90 return sStrAsUTF8;
93 rtl::OUString fromUTF8(rtl::OString const& _suStr)
95 rtl::OUString suStr = rtl::OStringToOUString(_suStr, RTL_TEXTENCODING_UTF8);
96 return suStr;
99 rtl::OString convertToOString(rtl::OUString const& _suStr)
101 return rtl::OUStringToOString(_suStr, osl_getThreadTextEncoding()/*RTL_TEXTENCODING_ASCII_US*/);
104 void showContent(rtl::OUString const& _suStr)
106 rtl::OString sStr = convertToOString(_suStr);
107 t_print("%s\n", sStr.getStr());
110 void toUTF8_mech(rtl::OUString const& _suStr, rtl_UriEncodeMechanism _eMechanism)
112 rtl::OUString suStr;
113 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassNone, _eMechanism, RTL_TEXTENCODING_UTF8);
114 showContent(suStr);
115 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUric, _eMechanism, RTL_TEXTENCODING_UTF8);
116 showContent(suStr);
117 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUricNoSlash, _eMechanism, RTL_TEXTENCODING_UTF8);
118 showContent(suStr);
119 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRelSegment, _eMechanism, RTL_TEXTENCODING_UTF8);
120 showContent(suStr);
121 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRegName, _eMechanism, RTL_TEXTENCODING_UTF8);
122 showContent(suStr);
123 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUserinfo, _eMechanism, RTL_TEXTENCODING_UTF8);
124 showContent(suStr);
125 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassPchar, _eMechanism, RTL_TEXTENCODING_UTF8);
126 showContent(suStr);
127 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUnoParamValue, _eMechanism, RTL_TEXTENCODING_UTF8);
128 showContent(suStr);
131 void toUTF8(rtl::OUString const& _suStr)
133 t_print("rtl_UriEncodeIgnoreEscapes \n");
134 toUTF8_mech(_suStr, rtl_UriEncodeIgnoreEscapes);
135 t_print("\n");
136 t_print("# rtl_UriEncodeKeepEscapes\n");
137 toUTF8_mech(_suStr, rtl_UriEncodeKeepEscapes);
138 t_print("\n");
139 t_print("# rtl_UriEncodeCheckEscapes\n");
140 toUTF8_mech(_suStr, rtl_UriEncodeCheckEscapes);
141 t_print("\n");
144 void test_FromUTF8_001()
146 // string --> ustring
147 rtl::OString sStrUTF8("h%C3%A4llo");
148 rtl::OUString suStrUTF8 = rtl::OStringToOUString(sStrUTF8, RTL_TEXTENCODING_ASCII_US);
150 // UTF8 --> real ustring
151 rtl::OUString suStr_UriDecodeToIuri = rtl::Uri::decode(suStrUTF8, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
152 showContent(suStr_UriDecodeToIuri);
154 // string --> ustring
155 rtl::OString sStr("h\xE4llo");
156 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
158 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suString.equals(suStr_UriDecodeToIuri) == sal_True);
160 // ustring --> ustring (UTF8)
161 rtl::OUString suStr2 = rtl::Uri::encode(suStr_UriDecodeToIuri, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
162 showContent(suStr2);
164 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suStr2.equals(suStrUTF8) == sal_True);
165 // suStr should be equal to suStr2
168 // "%C3%84qypten";
169 // testshl2 ../../../unxlngi4.pro/lib/libConvert.so "-onlyerrors"
170 // # Type: 'Directory' file name '%E6%89%8B%E6%9C%BA%E5%8F%B7%E7%A0%81'
171 // # Type: 'Directory' file name '%E6%9C%AA%E5%91%BD%E5%90%8Dzhgb18030'
172 // # Type: 'Regular file' file name '%E5%B7%A5%E4%BD%9C'
173 // # Type: 'Regular file' file name '%E4%BA%8C%E6%89%8B%E6%88%BF%E4%B9%B0%E5%8D%96%E5%90%88%E5%90%8C%E8%8D%89%E7%A8%BF.doc'
174 // ls
175 rtl::OString getFileTypeName(osl::FileStatus const& _aStatus)
177 rtl::OString sType;
178 if (_aStatus.isValid(osl_FileStatus_Mask_Type))
180 osl::FileStatus::Type aType = _aStatus.getFileType();
181 if (aType == osl::FileStatus::Directory)
183 sType = "Directory";
185 else if (aType == osl::FileStatus::Regular)
187 sType = "Regular file";
189 else if (aType == osl::FileStatus::Volume)
191 sType = "Volume";
193 else if (aType == osl::FileStatus::Fifo)
195 sType = "Fifo";
197 else if (aType == osl::FileStatus::Socket)
199 sType = "Socket";
201 else if (aType == osl::FileStatus::Link)
203 sType = "Link";
205 else if (aType == osl::FileStatus::Special)
207 sType = "Special";
209 else if (aType == osl::FileStatus::Unknown)
211 sType = "Unknown";
213 else
215 sType = "Not handled yet";
218 else
220 sType = "ERROR: osl_FileStatus_Mask_Type not set for FileStatus!";
222 return sType;
226 void test_UTF8_files()
228 #ifdef UNX
229 rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/atestdir"));
230 #else /* Windows */
231 rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/atestdir"));
232 #endif
233 osl::Directory aDir(suDirURL);
234 aDir.open();
235 if (aDir.isOpen())
237 osl::DirectoryItem aItem;
238 osl::FileStatus aStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes | osl_FileStatus_Mask_Type);
239 while (aDir.getNextItem(aItem) == ::osl::FileBase::E_None)
241 if (osl::FileBase::E_None == aItem.getFileStatus(aStatus) &&
242 aStatus.isValid(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes))
244 rtl::OString sType = getFileTypeName(aStatus);
246 rtl::OUString suFilename = aStatus.getFileName();
247 // rtl::OUString suFullFileURL;
249 rtl::OUString suStrUTF8 = rtl::Uri::encode(suFilename, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
250 rtl::OString sStrUTF8 = convertToOString(suStrUTF8);
251 t_print("Type: '%s' file name '%s'\n", sType.getStr(), sStrUTF8.getStr());
254 aDir.close();
256 else
258 rtl::OString sStr;
259 sStr = rtl::OUStringToOString(suDirURL, osl_getThreadTextEncoding());
260 t_print("can't open dir:'%s'\n", sStr.getStr());
264 void test_FromUTF8()
266 rtl::OString sStr("h%C3%A4llo");
267 rtl::OUString suStr = rtl::OStringToOUString(sStr, osl_getThreadTextEncoding());
269 // rtl_UriEncodeIgnoreEscapes,
270 // rtl_UriEncodeKeepEscapes,
271 // rtl_UriEncodeCheckEscapes,
272 // rtl::OUString suStr2 = rtl::Uri::encode(suStr, rtl_UriCharClassRegName, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8);
273 rtl::OUString suStr_UriDecodeNone = rtl::Uri::decode(suStr, rtl_UriDecodeNone, RTL_TEXTENCODING_UTF8);
274 showContent(suStr_UriDecodeNone);
275 toUTF8(suStr_UriDecodeNone);
277 rtl::OUString suStr_UriDecodeToIuri = rtl::Uri::decode(suStr, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
278 showContent(suStr_UriDecodeToIuri);
279 toUTF8(suStr_UriDecodeToIuri);
281 rtl::OUString suStr_UriDecodeWithCharset = rtl::Uri::decode(suStr, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
282 showContent(suStr_UriDecodeWithCharset);
283 toUTF8(suStr_UriDecodeWithCharset);
286 CPPUNIT_TEST_SUITE( Convert );
287 CPPUNIT_TEST( test_FromUTF8_001 );
288 // CPPUNIT_TEST( test_UTF8_files );
289 // CPPUNIT_TEST( test_FromUTF8 );
290 CPPUNIT_TEST_SUITE_END( );
296 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Stringtest::Convert, "Stringtest" );
298 // LLA: doku anpassen!!!
300 NOADDITIONAL;