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 4 : static bool equal( const INetProtocol& x, const INetProtocol& y )
43 : {
44 4 : 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 42 : class urlobjTest:public CppUnit::TestFixture
61 : {
62 :
63 : public:
64 : // initialise your test code values here.
65 14 : void setUp( )
66 : {
67 14 : }
68 :
69 14 : void tearDown( )
70 : {
71 14 : }
72 :
73 : // insert your test code here.
74 : // this is only demonstration code
75 2 : void urlobjTest_001( )
76 : {
77 : INetURLObject aUrl( rtl::
78 : OUString( RTL_CONSTASCII_USTRINGPARAM
79 2 : ( "file://10.10.1.1/sampledir/sample.file" ) ) );
80 : #ifdef LINUX
81 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
82 : compareToAscii
83 : ( "smb://10.10.1.1/sampledir/sample.file" ) ==
84 2 : 0 );
85 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
95 2 : compareToAscii( "10.10.1.1" ) == 0 );
96 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
97 2 : compareToAscii( "/sampledir/sample.file" ) == 0 );
98 4 : CPPUNIT_ASSERT( aUrl.getName( ).
99 2 : compareToAscii( "sample.file" ) == 0 );
100 4 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "sample" ) ==
101 2 : 0 );
102 4 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "file" ) ==
103 4 : 0 );
104 2 : }
105 :
106 2 : void urlobjTest_002( )
107 : {
108 2 : INetURLObject aUrl;
109 : aUrl.
110 : setFSysPath( rtl::
111 : OUString( RTL_CONSTASCII_USTRINGPARAM
112 : ( "\\\\137.65.170.24\\c$\\Img0001.jpg" ) ),
113 2 : INetURLObject::FSYS_DETECT );
114 : #ifdef LINUX
115 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
116 : compareToAscii
117 2 : ( "smb://137.65.170.24/c$/Img0001.jpg" ) == 0 );
118 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
127 2 : compareToAscii( "137.65.170.24" ) == 0 );
128 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
129 2 : compareToAscii( "/c$/Img0001.jpg" ) == 0 );
130 4 : CPPUNIT_ASSERT( aUrl.getName( ).
131 2 : compareToAscii( "Img0001.jpg" ) == 0 );
132 4 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "Img0001" ) ==
133 2 : 0 );
134 4 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "jpg" ) ==
135 4 : 0 );
136 2 : }
137 :
138 :
139 2 : void urlobjTest_003( )
140 : {
141 2 : INetURLObject aUrl;
142 : aUrl.
143 : setFSysPath( rtl::
144 : OUString( RTL_CONSTASCII_USTRINGPARAM
145 : ( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ) ),
146 2 : INetURLObject::FSYS_DETECT );
147 : #ifdef LINUX
148 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
149 : compareToAscii
150 : ( "smb://hive-winxp-x86/pmladek/test2.odt" ) ==
151 2 : 0 );
152 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
162 2 : compareToAscii( "hive-winxp-x86" ) == 0 );
163 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
164 4 : compareToAscii( "/pmladek/test2.odt" ) == 0 );
165 2 : }
166 :
167 2 : void urlobjTest_004( )
168 : {
169 : INetURLObject aUrl( rtl::
170 : OUString( RTL_CONSTASCII_USTRINGPARAM
171 2 : ( "smb://10.10.1.1/sampledir/sample.file" ) ) );
172 : #ifdef LINUX
173 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
174 : compareToAscii
175 : ( "smb://10.10.1.1/sampledir/sample.file" ) ==
176 2 : 0 );
177 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
187 2 : compareToAscii( "10.10.1.1" ) == 0 );
188 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
189 2 : compareToAscii( "/sampledir/sample.file" ) == 0 );
190 4 : CPPUNIT_ASSERT( aUrl.getName( ).
191 2 : compareToAscii( "sample.file" ) == 0 );
192 4 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "sample" ) ==
193 2 : 0 );
194 4 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "file" ) ==
195 4 : 0 );
196 2 : }
197 :
198 2 : void urlobjTest_005( )
199 : {
200 2 : INetURLObject aUrl;
201 : aUrl.setFSysPath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "//137.65.170.24/c$/Img0001.jpg" ) ),
202 2 : INetURLObject::FSYS_DETECT );
203 : #ifdef LINUX
204 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
205 2 : compareToAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) == 0 );
206 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
214 2 : compareToAscii( "137.65.170.24" ) == 0 );
215 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
216 2 : compareToAscii( "/c$/Img0001.jpg" ) == 0 );
217 2 : CPPUNIT_ASSERT( aUrl.getName( ).compareToAscii( "Img0001.jpg" ) == 0 );
218 2 : CPPUNIT_ASSERT( aUrl.getBase( ).compareToAscii( "Img0001" ) == 0 );
219 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).compareToAscii( "jpg" ) == 0 );
220 2 : }
221 :
222 :
223 2 : void urlobjTest_006( )
224 : {
225 2 : INetURLObject aUrl;
226 : aUrl.setFSysPath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "//hive-winxp-x86/pmladek/test2.odt" ) ),
227 2 : INetURLObject::FSYS_DETECT );
228 : #ifdef LINUX
229 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
230 2 : compareToAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) == 0 );
231 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
239 2 : compareToAscii( "hive-winxp-x86" ) == 0 );
240 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
241 4 : compareToAscii( "/pmladek/test2.odt" ) == 0 );
242 2 : }
243 :
244 2 : void urlobjCmisTest( )
245 : {
246 : // Test with a username part
247 : {
248 : INetURLObject aUrl( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
249 2 : "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) ) );
250 4 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
251 2 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
252 2 : CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
253 4 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
254 2 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
255 2 : 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 2 : "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) ) );
262 4 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
263 2 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
264 2 : CPPUNIT_ASSERT( !aUrl.HasUserData() );
265 4 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
266 2 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
267 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol( ) );
268 : }
269 2 : }
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 4 : CPPUNIT_TEST_SUITE( urlobjTest );
276 2 : CPPUNIT_TEST( urlobjTest_001 );
277 2 : CPPUNIT_TEST( urlobjTest_002 );
278 2 : CPPUNIT_TEST( urlobjTest_003 );
279 2 : CPPUNIT_TEST( urlobjTest_004 );
280 2 : CPPUNIT_TEST( urlobjTest_005 );
281 2 : CPPUNIT_TEST( urlobjTest_006 );
282 2 : CPPUNIT_TEST( urlobjCmisTest );
283 4 : CPPUNIT_TEST_SUITE_END( );
284 : }; // class createPool
285 :
286 :
287 2 : CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
288 6 : } // namespace rtl_random
289 :
290 :
291 : // -----------------------------------------------------------------------------
292 :
293 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|