Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / connectivity / workben / testmoz / mozthread.cxx
blob738582a3bc3274160470ccadb4619faf73f819c1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <connectivity/sqlparse.hxx>
21 #include "connectivity/sqliterator.hxx"
22 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
23 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
24 #include <com/sun/star/sdbc/XResultSet.hpp>
25 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
26 #include <com/sun/star/sdbc/XRow.hpp>
27 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/beans/PropertyState.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <cppuhelper/servicefactory.hxx>
32 #include <com/sun/star/sdbc/XConnection.hpp>
33 #include <com/sun/star/sdbc/XDriver.hpp>
34 #include <com/sun/star/sdbc/XDriverAccess.hpp>
35 #include <com/sun/star/sdbcx/XRowLocate.hpp>
36 #include <com/sun/star/sdbc/XRowUpdate.hpp>
37 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
38 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
39 #include <com/sun/star/sdbc/XCloseable.hpp>
40 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
41 #include <com/sun/star/uno/Any.hxx>
43 #include "connectivity/sqlnode.hxx"
44 #include <rtl/ustring.hxx>
45 #include <rtl/ustrbuf.hxx>
46 #include <osl/process.h>
48 #include <cppuhelper/bootstrap.hxx>
49 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/registry/XImplementationRegistration.hpp>
51 #include <com/sun/star/ucb/XContentProviderManager.hpp>
53 #include <ucbhelper/content.hxx>
54 #include <osl/module.h>
56 #include <stdio.h>
57 #include <osl/thread.hxx>
58 #include <osl/diagnose.h>
59 #include <osl/conditn.hxx>
60 #include <time.h>
63 using namespace comphelper;
64 using namespace cppu;
65 using namespace com::sun::star::uno;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::registry;
68 using namespace com::sun::star::ucb;
69 using namespace com::sun::star::beans;
71 using namespace connectivity;
72 using namespace com::sun::star::sdb;
73 using namespace com::sun::star::sdbc;
74 using namespace com::sun::star::sdbcx;
75 using namespace ::com::sun::star::container;
76 using namespace com::sun::star::registry;
78 using ::rtl::OUString;
79 using ::rtl::OUStringToOString;
81 extern Reference< XMultiServiceFactory > InitializeFac( void );
82 Reference< XMultiServiceFactory > mMgr;
84 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
85 #define PRINTSTR(x) printf("%s",x);
86 #define PRINTLN(x) printf("%s\n",x);
88 const int testLDAP=0;
89 const int testMozilla=1;
90 const int testOp=2;
91 const int testOe=3;
93 static int testCount = 0;
94 static int testList[4] = {0,0,0,0};
96 int autoTest(Reference<XResultSet> &xRes);
98 void printColumns( Reference<XResultSet> &xRes )
100 if(xRes.is())
102 char* aPat = " %-22s ";
103 char* aPat_Short = " %-12s ";
104 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
105 OSL_TRACE( "ColumnCount = %d", xMeta->getColumnCount());
106 for(sal_Int32 i=1;i<=xMeta->getColumnCount();++i)
108 const char *str = OUtoCStr(xMeta->getColumnName(i));
109 OSL_TRACE( aPat, str );
111 OSL_TRACE("\n");
112 OSL_TRACE("------------------------------------------------------------------------------------------");
114 else
116 OSL_TRACE(": FAILED to get a ResultSet");
119 void printXResultSet( Reference<XResultSet> &xRes )
121 if(xRes.is())
123 char* aPat = " %-22s ";
124 char* aPat_Short = " %-12s ";
125 Reference<XRow> xRow(xRes,UNO_QUERY);
126 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
127 for(sal_Int32 j=1;j<=xMeta->getColumnCount();++j)
131 const char *str = OUtoCStr(xRow->getString(j));
132 OSL_TRACE( aPat_Short, str );
133 } catch (...) {
134 OSL_TRACE(" Ex ");
137 OSL_TRACE("\n");
139 else
140 OSL_TRACE("FAILED to get a ResultSet");
143 void printXResultSets( Reference<XResultSet> &xRes )
145 if(xRes.is())
147 printColumns(xRes);
148 sal_Int32 nRows = 0;
149 while( xRes.is() && xRes->next())
151 printXResultSet(xRes);
152 nRows++;
154 OSL_TRACE( "%d Row(s)", nRows);
155 }else
156 OSL_TRACE("FAILED to get a ResultSet");
161 int TestMetaData(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
163 // Test some metadata
164 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
165 if ( xDmd.is() )
167 OSL_TRACE(": got DatabaseMetaData");
169 OUString sQuoteStr = xDmd->getIdentifierQuoteString();
170 OSL_TRACE( "Quote String : '%s'", OUtoCStr( sQuoteStr ) );
172 OUString sSQLCmds = xDmd->getSQLKeywords();
173 OSL_TRACE( "SQL Commands : '%s'", OUtoCStr( sSQLCmds ) );
175 OSL_TRACE("Testing getColumns() : START");
177 Reference<XResultSet> xRes = xDmd->getColumns(
178 makeAny(OUString("")), // Catalog
179 OUString("%"), // Schema
180 OUString("%"), // TabName
181 OUString("%")
183 printXResultSets( xRes );
185 OSL_TRACE("Testing getColumns() : END");
187 OSL_TRACE("Testing getTypeInfo() : START");
189 Reference<XResultSet> xRes = xDmd-> getTypeInfo();
190 printXResultSets( xRes );
192 OSL_TRACE("Testing getTypeInfo() : END");
194 OSL_TRACE("Testing getTables() : START");
196 Reference<XResultSet> xRes = xDmd->getTables(
197 makeAny(OUString("")), // Catalog
198 OUString("%"), // Schema
199 OUString("%"), // TabName
200 Sequence<rtl::OUString>() );
201 printXResultSets( xRes );
203 OSL_TRACE("Testing getTables() : END");
206 else
207 OSL_TRACE(": FAILED to get DatabaseMetaData");
208 return 0;
211 void TestQuery(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
213 // Try a query
214 OSL_TRACE("Testing createStatement() & executeQuery() : START");
215 Reference<XStatement> xStmt = pConnection->createStatement();
216 Reference<XResultSet> xRes;
217 if(xStmt.is())
219 OSL_TRACE(": got statement");
220 OSL_TRACE(": excuteQuery() : START \n");
221 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
222 OUString sqlPrefix("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
225 sal_Int32 times=0;
226 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
227 if ( xDmd.is() )
229 OSL_TRACE("getTables() : START");
230 OUString qut = xDmd->getIdentifierQuoteString();
232 Reference<XResultSet> xRes = xDmd->getTables(
233 makeAny(OUString("")), // Catalog
234 OUString("%"), // Schema
235 OUString("%"), // TabName
236 Sequence<rtl::OUString>() );
237 sal_Int32 nTables = 0;
238 while( xRes.is() && xRes->next())
240 Reference<XRow> xRow(xRes,UNO_QUERY);
241 const char *strTableName = OUtoCStr(xRow->getString(3));
242 OSL_TRACE("Testing Table:%s",strTableName);
244 Reference<XResultSet> tmpRes =
245 xStmt->executeQuery(sqlPrefix + qut + xRow->getString(3) + qut);
246 autoTest( tmpRes );
247 Reference<XCloseable> clsRes(tmpRes,UNO_QUERY);
248 clsRes->close();
249 nTables++;
251 OSL_TRACE("Tested Tables:%d",nTables);
253 } catch ( Exception &e ) {
254 OSL_TRACE( "Exception caught : %s", OUtoCStr( e.Message) );
256 // catch (...) {
257 // OSL_TRACE( "Non-UNO Exception caught\n" );
258 // }
259 OSL_TRACE("excuteQuery() : END");
261 else
263 OSL_TRACE(": FAILED to get statement");
265 OSL_TRACE("Testing createStatement() & executeQuery() : END");
267 Reference< ::com::sun::star::sdbc::XConnection> TestConnected
268 (Reference< ::com::sun::star::sdbc::XDriver> &pDriver)
270 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> pConnection;
271 OSL_TRACE("Begin Connect!");
272 OUString url;
273 Sequence<PropertyValue> aValue;
274 int nType=0;
275 srand( (unsigned)time( NULL ) );
276 nType = rand() % testCount + 1;
277 int nIndex=0,nCount=0;
278 for ( nIndex = 0; nIndex< 4;nIndex++)
280 if (testList[nIndex])
281 nCount++;
282 if (nCount == nType)
283 break;
285 switch( nIndex)
287 case testLDAP:
288 url=OUString("sdbc:address:ldap://");
289 aValue.realloc(2);
290 aValue[0].Name = ::rtl::OUString("HostName");
291 aValue[0].Value <<= rtl::OUString("sun-ds");
292 aValue[1].Name = ::rtl::OUString("BaseDN");
293 aValue[1].Value <<= rtl::OUString("dc=sun,dc=com");
294 break;
295 case testMozilla:
296 url=OUString("sdbc:address:mozilla://");
297 break;
298 case testOp:
299 url=OUString("sdbc:address:outlook://");
300 break;
301 case testOe:
302 url=OUString("sdbc:address:outlookexp://");
303 break;
304 default:
305 url=OUString("sdbc:address:mozilla://");
306 break;
308 pConnection =
309 pDriver->connect(url,aValue);
310 return pConnection;
313 int autoTest(Reference<XResultSet> &xRes)
315 sal_Int32 nRows = 0;
316 printColumns(xRes);
317 if(xRes.is())
319 while( xRes.is() && xRes->next())
321 nRows++;
323 OSL_TRACE( "%d Row(s)", nRows);
324 sal_Int32 times;
325 sal_Int32 pos;
326 if (nRows)
328 for(times = 1;times < 10; times ++)
330 pos= rand() % nRows+1;
331 OSL_TRACE("pos:%d",pos);
332 xRes->absolute(pos);
333 printXResultSet(xRes);
337 else
339 OSL_TRACE(": FAILED to get a ResultSet");
341 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
342 osl_waitThread(&timeValue);
343 return 0;
345 void SAL_CALL mozThread(void*)
347 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
350 Reference< ::com::sun::star::sdbc::XDriver>
351 m_xDriver(mMgr->createInstance(
352 OUString("com.sun.star.comp.sdbc.MozabDriver")),
353 UNO_QUERY);
354 if(m_xDriver.is())
357 m_xConnection = TestConnected(m_xDriver);
358 if(m_xConnection.is())
360 if (!TestMetaData(m_xConnection))
362 TestQuery(m_xConnection);
365 else
366 OSL_TRACE("Can't connected!");
369 else
371 OSL_TRACE("No driver!");
373 } catch ( Exception &e ) {
374 OSL_TRACE( "Exception caught : %s", OUtoCStr( e.Message) );
376 // catch (...) {
377 // OSL_TRACE( "Non-UNO Exception caught\n" );
378 // }
380 const int THREAD_COUNT=100;
383 void usage()
385 PRINTLN("mozThread [opts] threadcount");
386 PRINTLN("opts:");
387 PRINTLN("-l test ldap");
388 PRINTLN("-m test mozilla");
389 PRINTLN("-p test outlook");
390 PRINTLN("-e test outlook express");
391 PRINTLN("0 < threadcount <= 100, default 100");
393 #if (defined UNX)
394 int main( int argc, char * argv[] )
395 #else
396 int _cdecl main( int argc, char * argv[] )
397 #endif
400 OSL_TRACE("Init UNO");
401 Reference< XMultiServiceFactory > xMgr =InitializeFac();
402 int threadCount=THREAD_COUNT;
403 int nAc;
404 for (nAc = 1; nAc < argc; nAc ++)
406 if (strcmp(argv[nAc],"-l") ==0)
408 testList[testLDAP] = 1;
409 }else if(strcmp(argv[nAc],"-m") ==0)
411 testList[testMozilla]=1;
412 }else if(strcmp(argv[nAc],"-p") ==0)
414 testList[testOp]=1;
415 }else if(strcmp(argv[nAc],"-e") ==0)
417 testList[testOe]=1;
418 }else if(strcmp(argv[nAc],"-h") ==0 || strcmp(argv[nAc],"--help") ==0)
420 usage();
421 return 0;
422 }else
424 int tmpCount = atoi(argv[nAc]);
425 if (tmpCount > 0 && tmpCount < threadCount)
427 threadCount = tmpCount;
431 testCount = testList[testLDAP] + testList[testMozilla] + testList[testOp] + testList[testOe];
432 if ( testCount == 0)
434 testList[testLDAP] = 1;
435 testCount = 1;
438 if (!xMgr.is())
440 OSL_TRACE("Error init UNO");
441 return 1;
443 else
444 OSL_TRACE("UNO initted");
446 mMgr = xMgr;
447 oslThread xThreads[THREAD_COUNT];
448 int index=0;
449 for(index=0;index < threadCount; index++)
451 xThreads[index] = osl_createThread(mozThread,(void*)NULL);
452 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
453 osl_waitThread(&timeValue);
455 for(index=0;index < threadCount; index++)
457 if (osl_isThreadRunning(xThreads[index]))
458 osl_joinWithThread(xThreads[index]);
460 OSL_TRACE("Exiting...");
461 return 0;
464 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */