Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * [ insert your name / company etc. here eg. Jim Bob <jim@bob.org> ]
17 : * Portions created by the Initial Developer are Copyright (C) 2010 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s): Florian Reuter <freuter@novell.com>
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include <string>
30 :
31 : #include <sal/types.h>
32 : #include "cppunit/TestFixture.h"
33 : #include <cppunit/extensions/HelperMacros.h>
34 : #include "tools/urlobj.hxx"
35 :
36 : #define OUSTR_TO_STDSTR( oustr ) std::string( rtl::OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() )
37 :
38 : CPPUNIT_NS_BEGIN
39 :
40 : template<> struct assertion_traits<INetProtocol>
41 : {
42 2 : static bool equal( const INetProtocol& x, const INetProtocol& y )
43 : {
44 2 : return x == y;
45 : }
46 :
47 0 : static std::string toString( const INetProtocol& x )
48 : {
49 0 : OStringStream ost;
50 0 : ost << static_cast<unsigned int>(x);
51 0 : return ost.str();
52 : }
53 : };
54 :
55 : CPPUNIT_NS_END
56 :
57 : namespace tools_urlobj
58 : {
59 :
60 21 : class urlobjTest:public CppUnit::TestFixture
61 : {
62 :
63 : public:
64 : // initialise your test code values here.
65 7 : void setUp( )
66 : {
67 7 : }
68 :
69 7 : void tearDown( )
70 : {
71 7 : }
72 :
73 : // insert your test code here.
74 : // this is only demonstration code
75 1 : void urlobjTest_001( )
76 : {
77 : INetURLObject aUrl( rtl::
78 : OUString( RTL_CONSTASCII_USTRINGPARAM
79 1 : ( "file://10.10.1.1/sampledir/sample.file" ) ) );
80 : #ifdef LINUX
81 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
82 : compareToAscii
83 : ( "smb://10.10.1.1/sampledir/sample.file" ) ==
84 1 : 0 );
85 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
86 : #endif
87 : #ifdef WIN
88 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
89 : compareToAscii
90 : ( "file://10.10.1.1/sampledir/sample.file" ) ==
91 : 0 );
92 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
93 : #endif
94 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
95 1 : compareToAscii( "10.10.1.1" ) == 0 );
96 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
97 1 : compareToAscii( "/sampledir/sample.file" ) == 0 );
98 2 : CPPUNIT_ASSERT( aUrl.getName( ).
99 1 : compareToAscii( "sample.file" ) == 0 );
100 2 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "sample" ) ==
101 1 : 0 );
102 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "file" ) ==
103 2 : 0 );
104 1 : }
105 :
106 1 : void urlobjTest_002( )
107 : {
108 1 : INetURLObject aUrl;
109 : aUrl.
110 : setFSysPath( rtl::
111 : OUString( RTL_CONSTASCII_USTRINGPARAM
112 : ( "\\\\137.65.170.24\\c$\\Img0001.jpg" ) ),
113 1 : INetURLObject::FSYS_DETECT );
114 : #ifdef LINUX
115 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
116 : compareToAscii
117 1 : ( "smb://137.65.170.24/c$/Img0001.jpg" ) == 0 );
118 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
119 : #endif
120 : #ifdef WIN
121 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
122 : compareToAscii
123 : ( "file://137.65.170.24/c$/Img0001.jpg" ) == 0 );
124 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
125 : #endif
126 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
127 1 : compareToAscii( "137.65.170.24" ) == 0 );
128 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
129 1 : compareToAscii( "/c$/Img0001.jpg" ) == 0 );
130 2 : CPPUNIT_ASSERT( aUrl.getName( ).
131 1 : compareToAscii( "Img0001.jpg" ) == 0 );
132 2 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "Img0001" ) ==
133 1 : 0 );
134 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "jpg" ) ==
135 2 : 0 );
136 1 : }
137 :
138 :
139 1 : void urlobjTest_003( )
140 : {
141 1 : INetURLObject aUrl;
142 : aUrl.
143 : setFSysPath( rtl::
144 : OUString( RTL_CONSTASCII_USTRINGPARAM
145 : ( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ) ),
146 1 : INetURLObject::FSYS_DETECT );
147 : #ifdef LINUX
148 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
149 : compareToAscii
150 : ( "smb://hive-winxp-x86/pmladek/test2.odt" ) ==
151 1 : 0 );
152 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
153 : #endif
154 : #ifdef WIN
155 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
156 : compareToAscii
157 : ( "file://hive-winxp-x86/pmladek/test2.odt" ) ==
158 : 0 );
159 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
160 : #endif
161 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
162 1 : compareToAscii( "hive-winxp-x86" ) == 0 );
163 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
164 2 : compareToAscii( "/pmladek/test2.odt" ) == 0 );
165 1 : }
166 :
167 1 : void urlobjTest_004( )
168 : {
169 : INetURLObject aUrl( rtl::
170 : OUString( RTL_CONSTASCII_USTRINGPARAM
171 1 : ( "smb://10.10.1.1/sampledir/sample.file" ) ) );
172 : #ifdef LINUX
173 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
174 : compareToAscii
175 : ( "smb://10.10.1.1/sampledir/sample.file" ) ==
176 1 : 0 );
177 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
178 : #endif
179 : #ifdef WIN
180 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
181 : compareToAscii
182 : ( "file://10.10.1.1/sampledir/sample.file" ) ==
183 : 0 );
184 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
185 : #endif
186 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
187 1 : compareToAscii( "10.10.1.1" ) == 0 );
188 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
189 1 : compareToAscii( "/sampledir/sample.file" ) == 0 );
190 2 : CPPUNIT_ASSERT( aUrl.getName( ).
191 1 : compareToAscii( "sample.file" ) == 0 );
192 2 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "sample" ) ==
193 1 : 0 );
194 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "file" ) ==
195 2 : 0 );
196 1 : }
197 :
198 1 : void urlobjTest_005( )
199 : {
200 1 : INetURLObject aUrl;
201 : aUrl.setFSysPath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "//137.65.170.24/c$/Img0001.jpg" ) ),
202 1 : INetURLObject::FSYS_DETECT );
203 : #ifdef LINUX
204 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
205 1 : compareToAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) == 0 );
206 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
207 : #endif
208 : #ifdef WIN
209 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
210 : compareToAscii( "file://137.65.170.24/c$/Img0001.jpg" ) == 0 );
211 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
212 : #endif
213 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
214 1 : compareToAscii( "137.65.170.24" ) == 0 );
215 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
216 1 : compareToAscii( "/c$/Img0001.jpg" ) == 0 );
217 1 : CPPUNIT_ASSERT( aUrl.getName( ).compareToAscii( "Img0001.jpg" ) == 0 );
218 1 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "Img0001" ) == 0 );
219 1 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "jpg" ) == 0 );
220 1 : }
221 :
222 :
223 1 : void urlobjTest_006( )
224 : {
225 1 : INetURLObject aUrl;
226 : aUrl.setFSysPath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "//hive-winxp-x86/pmladek/test2.odt" ) ),
227 1 : INetURLObject::FSYS_DETECT );
228 : #ifdef LINUX
229 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
230 1 : compareToAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) == 0 );
231 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
232 : #endif
233 : #ifdef WIN
234 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
235 : compareToAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) == 0 );
236 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
237 : #endif
238 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
239 1 : compareToAscii( "hive-winxp-x86" ) == 0 );
240 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
241 2 : compareToAscii( "/pmladek/test2.odt" ) == 0 );
242 1 : }
243 :
244 1 : void urlobjCmisTest( )
245 : {
246 : // Test with a username part
247 : {
248 : INetURLObject aUrl( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
249 1 : "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) ) );
250 2 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
251 1 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
252 1 : CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
253 2 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
254 1 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
255 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol( ) );
256 : }
257 :
258 : // Test without a username part
259 : {
260 : INetURLObject aUrl( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
261 1 : "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) ) );
262 2 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
263 1 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
264 1 : CPPUNIT_ASSERT( !aUrl.HasUserData() );
265 2 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
266 1 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
267 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol( ) );
268 : }
269 1 : }
270 :
271 : // Change the following lines only, if you add, remove or rename
272 : // member functions of the current class,
273 : // because these macros are need by auto register mechanism.
274 :
275 2 : CPPUNIT_TEST_SUITE( urlobjTest );
276 1 : CPPUNIT_TEST( urlobjTest_001 );
277 1 : CPPUNIT_TEST( urlobjTest_002 );
278 1 : CPPUNIT_TEST( urlobjTest_003 );
279 1 : CPPUNIT_TEST( urlobjTest_004 );
280 1 : CPPUNIT_TEST( urlobjTest_005 );
281 1 : CPPUNIT_TEST( urlobjTest_006 );
282 1 : CPPUNIT_TEST( urlobjCmisTest );
283 2 : CPPUNIT_TEST_SUITE_END( );
284 : }; // class createPool
285 :
286 :
287 1 : CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
288 3 : } // namespace rtl_random
289 :
290 :
291 : // -----------------------------------------------------------------------------
292 :
293 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|