update dev300-m58
[ooovba.git] / connectivity / workben / testmoz / mozthread.cxx
blobbab014cb978d3d17eb148c98d44c0f39b4c229e6
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: mozthread.cxx,v $
10 * $Revision: 1.6 $
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_connectivity.hxx"
33 #include <connectivity/sqlparse.hxx>
34 #include "connectivity/sqliterator.hxx"
35 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
36 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/beans/PropertyState.hpp>
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #include <unotools/processfactory.hxx>
45 #include <cppuhelper/servicefactory.hxx>
46 #include <com/sun/star/sdbc/XConnection.hpp>
47 #include <com/sun/star/sdbc/XDriver.hpp>
48 #include <com/sun/star/sdbc/XDriverAccess.hpp>
49 #include <com/sun/star/sdbcx/XRowLocate.hpp>
50 #include <com/sun/star/sdbc/XRowUpdate.hpp>
51 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
52 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
53 #include <com/sun/star/sdbc/XCloseable.hpp>
54 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
55 #include <com/sun/star/uno/Any.hxx>
57 #include "connectivity/sqlnode.hxx"
58 #include <ucbhelper/contentbroker.hxx>
59 #include <comphelper/regpathhelper.hxx>
60 #include <rtl/ustring.hxx>
61 #include <rtl/ustrbuf.hxx>
62 #include <osl/process.h>
64 #include <cppuhelper/bootstrap.hxx>
65 #include <cppuhelper/servicefactory.hxx>
66 #include <com/sun/star/lang/XComponent.hpp>
67 #include <com/sun/star/registry/XImplementationRegistration.hpp>
68 #include <com/sun/star/ucb/XContentProviderManager.hpp>
70 #include <ucbhelper/content.hxx>
71 #include <osl/module.h>
72 #include <tools/config.hxx>
74 #include <stdio.h>
75 #include <osl/thread.hxx>
76 #include <osl/diagnose.h>
77 #include <osl/conditn.hxx>
78 #include <time.h>
81 using namespace comphelper;
82 using namespace cppu;
83 using namespace rtl;
84 using namespace com::sun::star::uno;
85 using namespace com::sun::star::lang;
86 using namespace com::sun::star::registry;
87 using namespace com::sun::star::ucb;
88 using namespace com::sun::star::beans;
90 //using namespace com::sun::star;
91 using namespace connectivity;
92 using namespace com::sun::star::sdb;
93 using namespace com::sun::star::sdbc;
94 using namespace com::sun::star::sdbcx;
95 using namespace ::com::sun::star::container;
96 using namespace com::sun::star::registry;
98 extern Reference< XMultiServiceFactory > InitializeFac( void );
99 Reference< XMultiServiceFactory > mMgr;
101 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
102 #define PRINTSTR(x) printf("%s",x);
103 #define PRINTLN(x) printf("%s\n",x);
105 const int testLDAP=0;
106 const int testMozilla=1;
107 const int testOp=2;
108 const int testOe=3;
110 static int testCount = 0;
111 static int testList[4] = {0,0,0,0};
113 int autoTest(Reference<XResultSet> &xRes);
115 void printColumns( Reference<XResultSet> &xRes )
117 if(xRes.is())
119 char* aPat = " %-22s ";
120 char* aPat_Short = " %-12s ";
121 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
122 OSL_TRACE( "ColumnCount = %d\n", xMeta->getColumnCount());
123 for(sal_Int32 i=1;i<=xMeta->getColumnCount();++i)
125 const char *str = OUtoCStr(xMeta->getColumnName(i));
126 OSL_TRACE( aPat, str );
128 OSL_TRACE("\n");
129 OSL_TRACE("------------------------------------------------------------------------------------------\n");
131 else
133 OSL_TRACE(": FAILED to get a ResultSet \n");
136 void printXResultSet( Reference<XResultSet> &xRes )
138 if(xRes.is())
140 char* aPat = " %-22s ";
141 char* aPat_Short = " %-12s ";
142 Reference<XRow> xRow(xRes,UNO_QUERY);
143 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
144 for(sal_Int32 j=1;j<=xMeta->getColumnCount();++j)
148 const char *str = OUtoCStr(xRow->getString(j));
149 OSL_TRACE( aPat_Short, str );
150 } catch (...) {
151 OSL_TRACE(" Ex ");
154 OSL_TRACE("\n");
156 else
157 OSL_TRACE("FAILED to get a ResultSet \n");
160 void printXResultSets( Reference<XResultSet> &xRes )
162 if(xRes.is())
164 printColumns(xRes);
165 sal_Int32 nRows = 0;
166 while( xRes.is() && xRes->next())
168 printXResultSet(xRes);
169 nRows++;
171 OSL_TRACE( "%d Row(s)\n", nRows);
172 }else
173 OSL_TRACE("FAILED to get a ResultSet \n");
178 int TestMetaData(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
180 // Test some metadata
181 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
182 if ( xDmd.is() )
184 OSL_TRACE(": got DatabaseMetaData \n");
186 OUString sQuoteStr = xDmd->getIdentifierQuoteString();
187 OSL_TRACE( "Quote String : '%s'\n", OUtoCStr( sQuoteStr ) );
189 OUString sSQLCmds = xDmd->getSQLKeywords();
190 OSL_TRACE( "SQL Commands : '%s'\n", OUtoCStr( sSQLCmds ) );
192 OSL_TRACE("Testing getColumns() : START\n");
194 Reference<XResultSet> xRes = xDmd->getColumns(
195 makeAny(OUString::createFromAscii("")), // Catalog
196 OUString::createFromAscii("%"), // Schema
197 OUString::createFromAscii("%"), // TabName
198 OUString::createFromAscii("%")
200 printXResultSets( xRes );
202 OSL_TRACE("Testing getColumns() : END\n");
204 OSL_TRACE("Testing getTypeInfo() : START\n");
206 Reference<XResultSet> xRes = xDmd-> getTypeInfo();
207 printXResultSets( xRes );
209 OSL_TRACE("Testing getTypeInfo() : END\n");
211 OSL_TRACE("Testing getTables() : START\n");
213 Reference<XResultSet> xRes = xDmd->getTables(
214 makeAny(OUString::createFromAscii("")), // Catalog
215 OUString::createFromAscii("%"), // Schema
216 OUString::createFromAscii("%"), // TabName
217 Sequence<rtl::OUString>() );
218 printXResultSets( xRes );
220 OSL_TRACE("Testing getTables() : END\n");
223 else
224 OSL_TRACE(": FAILED to get DatabaseMetaData \n");
225 return 0;
228 void TestQuery(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
230 // Try a query
231 OSL_TRACE("Testing createStatement() & executeQuery() : START\n");
232 Reference<XStatement> xStmt = pConnection->createStatement();
233 Reference<XResultSet> xRes;
234 if(xStmt.is())
236 OSL_TRACE(": got statement\n");
237 OSL_TRACE(": excuteQuery() : START \n");
238 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
239 OUString sqlPrefix= OUString::createFromAscii("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
242 sal_Int32 times=0;
243 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
244 if ( xDmd.is() )
246 OSL_TRACE("getTables() : START\n");
247 OUString qut = xDmd->getIdentifierQuoteString();
249 Reference<XResultSet> xRes = xDmd->getTables(
250 makeAny(OUString::createFromAscii("")), // Catalog
251 OUString::createFromAscii("%"), // Schema
252 OUString::createFromAscii("%"), // TabName
253 Sequence<rtl::OUString>() );
254 sal_Int32 nTables = 0;
255 while( xRes.is() && xRes->next())
257 Reference<XRow> xRow(xRes,UNO_QUERY);
258 const char *strTableName = OUtoCStr(xRow->getString(3));
259 OSL_TRACE("Testing Table:%s\n",strTableName);
261 Reference<XResultSet> tmpRes =
262 xStmt->executeQuery(sqlPrefix + qut + xRow->getString(3) + qut);
263 autoTest( tmpRes );
264 Reference<XCloseable> clsRes(tmpRes,UNO_QUERY);
265 clsRes->close();
266 nTables++;
268 OSL_TRACE("Tested Tables:%d\n",nTables);
270 } catch ( Exception &e ) {
271 OSL_TRACE( "Exception caught : %s\n", OUtoCStr( e.Message) );
273 // catch (...) {
274 // OSL_TRACE( "Non-UNO Exception caught\n" );
275 // }
276 OSL_TRACE("excuteQuery() : END \n");
278 else
280 OSL_TRACE(": FAILED to get statement\n");
282 OSL_TRACE("Testing createStatement() & executeQuery() : END\n");
284 Reference< ::com::sun::star::sdbc::XConnection> TestConnected
285 (Reference< ::com::sun::star::sdbc::XDriver> &pDriver)
287 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> pConnection;
288 OSL_TRACE("Begin Connect!\n");
289 OUString url;
290 Sequence<PropertyValue> aValue;
291 int nType=0;
292 srand( (unsigned)time( NULL ) );
293 nType = rand() % testCount + 1;
294 int nIndex=0,nCount=0;
295 for ( nIndex = 0; nIndex< 4;nIndex++)
297 if (testList[nIndex])
298 nCount++;
299 if (nCount == nType)
300 break;
302 switch( nIndex)
304 case testLDAP:
305 url=OUString::createFromAscii("sdbc:address:ldap://");
306 aValue.realloc(2);
307 aValue[0].Name = ::rtl::OUString::createFromAscii("HostName");
308 aValue[0].Value <<= rtl::OUString::createFromAscii("sun-ds");
309 aValue[1].Name = ::rtl::OUString::createFromAscii("BaseDN");
310 aValue[1].Value <<= rtl::OUString::createFromAscii("dc=sun,dc=com");
311 break;
312 case testMozilla:
313 url=OUString::createFromAscii("sdbc:address:mozilla://");
314 break;
315 case testOp:
316 url=OUString::createFromAscii("sdbc:address:outlook://");
317 break;
318 case testOe:
319 url=OUString::createFromAscii("sdbc:address:outlookexp://");
320 break;
321 default:
322 url=OUString::createFromAscii("sdbc:address:mozilla://");
323 break;
325 pConnection =
326 pDriver->connect(url,aValue);
327 return pConnection;
330 int autoTest(Reference<XResultSet> &xRes)
332 sal_Int32 nRows = 0;
333 printColumns(xRes);
334 if(xRes.is())
336 while( xRes.is() && xRes->next())
338 nRows++;
340 OSL_TRACE( "%d Row(s)\n", nRows);
341 sal_Int32 times;
342 sal_Int32 pos;
343 if (nRows)
345 for(times = 1;times < 10; times ++)
347 pos= rand() % nRows+1;
348 OSL_TRACE("pos:%d\n",pos);
349 xRes->absolute(pos);
350 printXResultSet(xRes);
354 else
356 OSL_TRACE(": FAILED to get a ResultSet \n");
358 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
359 osl_waitThread(&timeValue);
360 return 0;
362 void SAL_CALL mozThread(void*)
364 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
367 Reference< ::com::sun::star::sdbc::XDriver>
368 m_xDriver(mMgr->createInstance(
369 OUString::createFromAscii("com.sun.star.comp.sdbc.MozabDriver")),
370 UNO_QUERY);
371 if(m_xDriver.is())
374 m_xConnection = TestConnected(m_xDriver);
375 if(m_xConnection.is())
377 if (!TestMetaData(m_xConnection))
379 TestQuery(m_xConnection);
382 else
383 OSL_TRACE("Can't connected!\n");
386 else
388 OSL_TRACE("No driver!\n");
390 } catch ( Exception &e ) {
391 OSL_TRACE( "Exception caught : %s\n", OUtoCStr( e.Message) );
393 // catch (...) {
394 // OSL_TRACE( "Non-UNO Exception caught\n" );
395 // }
397 const int THREAD_COUNT=100;
400 void usage()
402 PRINTLN("mozThread [opts] threadcount");
403 PRINTLN("opts:");
404 PRINTLN("-l test ldap");
405 PRINTLN("-m test mozilla");
406 PRINTLN("-p test outlook");
407 PRINTLN("-e test outlook express");
408 PRINTLN("0 < threadcount <= 100, default 100");
410 #if (defined UNX) || (defined OS2)
411 int main( int argc, char * argv[] )
412 #else
413 int _cdecl main( int argc, char * argv[] )
414 #endif
417 OSL_TRACE("Init UNO\n");
418 Reference< XMultiServiceFactory > xMgr =InitializeFac();
419 int threadCount=THREAD_COUNT;
420 int nAc;
421 for (nAc = 1; nAc < argc; nAc ++)
423 if (strcmp(argv[nAc],"-l") ==0)
425 testList[testLDAP] = 1;
426 }else if(strcmp(argv[nAc],"-m") ==0)
428 testList[testMozilla]=1;
429 }else if(strcmp(argv[nAc],"-p") ==0)
431 testList[testOp]=1;
432 }else if(strcmp(argv[nAc],"-e") ==0)
434 testList[testOe]=1;
435 }else if(strcmp(argv[nAc],"-h") ==0 || strcmp(argv[nAc],"--help") ==0)
437 usage();
438 return 0;
439 }else
441 int tmpCount = atoi(argv[nAc]);
442 if (tmpCount > 0 && tmpCount < threadCount)
444 threadCount = tmpCount;
448 testCount = testList[testLDAP] + testList[testMozilla] + testList[testOp] + testList[testOe];
449 if ( testCount == 0)
451 testList[testLDAP] = 1;
452 testCount = 1;
455 if (!xMgr.is())
457 OSL_TRACE("Error init UNO\n");
458 return 1;
460 else
461 OSL_TRACE("UNO initted\n");
463 mMgr = xMgr;
464 oslThread xThreads[THREAD_COUNT];
465 int index=0;
466 for(index=0;index < threadCount; index++)
468 xThreads[index] = osl_createThread(mozThread,(void*)NULL);
469 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
470 osl_waitThread(&timeValue);
472 for(index=0;index < threadCount; index++)
474 if (osl_isThreadRunning(xThreads[index]))
475 osl_joinWithThread(xThreads[index]);
477 OSL_TRACE("Exiting...\n");
478 return 0;