Bump for 3.6-28
[LibreOffice.git] / connectivity / workben / testmoz / mozthread.cxx
blobee690649313fb316bee47f2f5c0648437c9b7b7a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <connectivity/sqlparse.hxx>
30 #include "connectivity/sqliterator.hxx"
31 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
32 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/beans/PropertyState.hpp>
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <com/sun/star/sdbc/XConnection.hpp>
42 #include <com/sun/star/sdbc/XDriver.hpp>
43 #include <com/sun/star/sdbc/XDriverAccess.hpp>
44 #include <com/sun/star/sdbcx/XRowLocate.hpp>
45 #include <com/sun/star/sdbc/XRowUpdate.hpp>
46 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
47 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
48 #include <com/sun/star/sdbc/XCloseable.hpp>
49 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
50 #include <com/sun/star/uno/Any.hxx>
52 #include "connectivity/sqlnode.hxx"
53 #include <ucbhelper/contentbroker.hxx>
54 #include <rtl/ustring.hxx>
55 #include <rtl/ustrbuf.hxx>
56 #include <osl/process.h>
58 #include <cppuhelper/bootstrap.hxx>
59 #include <com/sun/star/lang/XComponent.hpp>
60 #include <com/sun/star/registry/XImplementationRegistration.hpp>
61 #include <com/sun/star/ucb/XContentProviderManager.hpp>
63 #include <ucbhelper/content.hxx>
64 #include <osl/module.h>
66 #include <stdio.h>
67 #include <osl/thread.hxx>
68 #include <osl/diagnose.h>
69 #include <osl/conditn.hxx>
70 #include <time.h>
73 using namespace comphelper;
74 using namespace cppu;
75 using namespace com::sun::star::uno;
76 using namespace com::sun::star::lang;
77 using namespace com::sun::star::registry;
78 using namespace com::sun::star::ucb;
79 using namespace com::sun::star::beans;
81 using namespace connectivity;
82 using namespace com::sun::star::sdb;
83 using namespace com::sun::star::sdbc;
84 using namespace com::sun::star::sdbcx;
85 using namespace ::com::sun::star::container;
86 using namespace com::sun::star::registry;
88 using ::rtl::OUString;
89 using ::rtl::OUStringToOString;
91 extern Reference< XMultiServiceFactory > InitializeFac( void );
92 Reference< XMultiServiceFactory > mMgr;
94 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
95 #define PRINTSTR(x) printf("%s",x);
96 #define PRINTLN(x) printf("%s\n",x);
98 const int testLDAP=0;
99 const int testMozilla=1;
100 const int testOp=2;
101 const int testOe=3;
103 static int testCount = 0;
104 static int testList[4] = {0,0,0,0};
106 int autoTest(Reference<XResultSet> &xRes);
108 void printColumns( Reference<XResultSet> &xRes )
110 if(xRes.is())
112 char* aPat = " %-22s ";
113 char* aPat_Short = " %-12s ";
114 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
115 OSL_TRACE( "ColumnCount = %d", xMeta->getColumnCount());
116 for(sal_Int32 i=1;i<=xMeta->getColumnCount();++i)
118 const char *str = OUtoCStr(xMeta->getColumnName(i));
119 OSL_TRACE( aPat, str );
121 OSL_TRACE("\n");
122 OSL_TRACE("------------------------------------------------------------------------------------------");
124 else
126 OSL_TRACE(": FAILED to get a ResultSet");
129 void printXResultSet( Reference<XResultSet> &xRes )
131 if(xRes.is())
133 char* aPat = " %-22s ";
134 char* aPat_Short = " %-12s ";
135 Reference<XRow> xRow(xRes,UNO_QUERY);
136 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
137 for(sal_Int32 j=1;j<=xMeta->getColumnCount();++j)
141 const char *str = OUtoCStr(xRow->getString(j));
142 OSL_TRACE( aPat_Short, str );
143 } catch (...) {
144 OSL_TRACE(" Ex ");
147 OSL_TRACE("\n");
149 else
150 OSL_TRACE("FAILED to get a ResultSet");
153 void printXResultSets( Reference<XResultSet> &xRes )
155 if(xRes.is())
157 printColumns(xRes);
158 sal_Int32 nRows = 0;
159 while( xRes.is() && xRes->next())
161 printXResultSet(xRes);
162 nRows++;
164 OSL_TRACE( "%d Row(s)", nRows);
165 }else
166 OSL_TRACE("FAILED to get a ResultSet");
171 int TestMetaData(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
173 // Test some metadata
174 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
175 if ( xDmd.is() )
177 OSL_TRACE(": got DatabaseMetaData");
179 OUString sQuoteStr = xDmd->getIdentifierQuoteString();
180 OSL_TRACE( "Quote String : '%s'", OUtoCStr( sQuoteStr ) );
182 OUString sSQLCmds = xDmd->getSQLKeywords();
183 OSL_TRACE( "SQL Commands : '%s'", OUtoCStr( sSQLCmds ) );
185 OSL_TRACE("Testing getColumns() : START");
187 Reference<XResultSet> xRes = xDmd->getColumns(
188 makeAny(OUString("")), // Catalog
189 OUString("%"), // Schema
190 OUString("%"), // TabName
191 OUString("%")
193 printXResultSets( xRes );
195 OSL_TRACE("Testing getColumns() : END");
197 OSL_TRACE("Testing getTypeInfo() : START");
199 Reference<XResultSet> xRes = xDmd-> getTypeInfo();
200 printXResultSets( xRes );
202 OSL_TRACE("Testing getTypeInfo() : END");
204 OSL_TRACE("Testing getTables() : START");
206 Reference<XResultSet> xRes = xDmd->getTables(
207 makeAny(OUString("")), // Catalog
208 OUString("%"), // Schema
209 OUString("%"), // TabName
210 Sequence<rtl::OUString>() );
211 printXResultSets( xRes );
213 OSL_TRACE("Testing getTables() : END");
216 else
217 OSL_TRACE(": FAILED to get DatabaseMetaData");
218 return 0;
221 void TestQuery(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
223 // Try a query
224 OSL_TRACE("Testing createStatement() & executeQuery() : START");
225 Reference<XStatement> xStmt = pConnection->createStatement();
226 Reference<XResultSet> xRes;
227 if(xStmt.is())
229 OSL_TRACE(": got statement");
230 OSL_TRACE(": excuteQuery() : START \n");
231 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
232 OUString sqlPrefix("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
235 sal_Int32 times=0;
236 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
237 if ( xDmd.is() )
239 OSL_TRACE("getTables() : START");
240 OUString qut = xDmd->getIdentifierQuoteString();
242 Reference<XResultSet> xRes = xDmd->getTables(
243 makeAny(OUString("")), // Catalog
244 OUString("%"), // Schema
245 OUString("%"), // TabName
246 Sequence<rtl::OUString>() );
247 sal_Int32 nTables = 0;
248 while( xRes.is() && xRes->next())
250 Reference<XRow> xRow(xRes,UNO_QUERY);
251 const char *strTableName = OUtoCStr(xRow->getString(3));
252 OSL_TRACE("Testing Table:%s",strTableName);
254 Reference<XResultSet> tmpRes =
255 xStmt->executeQuery(sqlPrefix + qut + xRow->getString(3) + qut);
256 autoTest( tmpRes );
257 Reference<XCloseable> clsRes(tmpRes,UNO_QUERY);
258 clsRes->close();
259 nTables++;
261 OSL_TRACE("Tested Tables:%d",nTables);
263 } catch ( Exception &e ) {
264 OSL_TRACE( "Exception caught : %s", OUtoCStr( e.Message) );
266 // catch (...) {
267 // OSL_TRACE( "Non-UNO Exception caught\n" );
268 // }
269 OSL_TRACE("excuteQuery() : END");
271 else
273 OSL_TRACE(": FAILED to get statement");
275 OSL_TRACE("Testing createStatement() & executeQuery() : END");
277 Reference< ::com::sun::star::sdbc::XConnection> TestConnected
278 (Reference< ::com::sun::star::sdbc::XDriver> &pDriver)
280 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> pConnection;
281 OSL_TRACE("Begin Connect!");
282 OUString url;
283 Sequence<PropertyValue> aValue;
284 int nType=0;
285 srand( (unsigned)time( NULL ) );
286 nType = rand() % testCount + 1;
287 int nIndex=0,nCount=0;
288 for ( nIndex = 0; nIndex< 4;nIndex++)
290 if (testList[nIndex])
291 nCount++;
292 if (nCount == nType)
293 break;
295 switch( nIndex)
297 case testLDAP:
298 url=OUString("sdbc:address:ldap://");
299 aValue.realloc(2);
300 aValue[0].Name = ::rtl::OUString("HostName");
301 aValue[0].Value <<= rtl::OUString("sun-ds");
302 aValue[1].Name = ::rtl::OUString("BaseDN");
303 aValue[1].Value <<= rtl::OUString("dc=sun,dc=com");
304 break;
305 case testMozilla:
306 url=OUString("sdbc:address:mozilla://");
307 break;
308 case testOp:
309 url=OUString("sdbc:address:outlook://");
310 break;
311 case testOe:
312 url=OUString("sdbc:address:outlookexp://");
313 break;
314 default:
315 url=OUString("sdbc:address:mozilla://");
316 break;
318 pConnection =
319 pDriver->connect(url,aValue);
320 return pConnection;
323 int autoTest(Reference<XResultSet> &xRes)
325 sal_Int32 nRows = 0;
326 printColumns(xRes);
327 if(xRes.is())
329 while( xRes.is() && xRes->next())
331 nRows++;
333 OSL_TRACE( "%d Row(s)", nRows);
334 sal_Int32 times;
335 sal_Int32 pos;
336 if (nRows)
338 for(times = 1;times < 10; times ++)
340 pos= rand() % nRows+1;
341 OSL_TRACE("pos:%d",pos);
342 xRes->absolute(pos);
343 printXResultSet(xRes);
347 else
349 OSL_TRACE(": FAILED to get a ResultSet");
351 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
352 osl_waitThread(&timeValue);
353 return 0;
355 void SAL_CALL mozThread(void*)
357 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
360 Reference< ::com::sun::star::sdbc::XDriver>
361 m_xDriver(mMgr->createInstance(
362 OUString("com.sun.star.comp.sdbc.MozabDriver")),
363 UNO_QUERY);
364 if(m_xDriver.is())
367 m_xConnection = TestConnected(m_xDriver);
368 if(m_xConnection.is())
370 if (!TestMetaData(m_xConnection))
372 TestQuery(m_xConnection);
375 else
376 OSL_TRACE("Can't connected!");
379 else
381 OSL_TRACE("No driver!");
383 } catch ( Exception &e ) {
384 OSL_TRACE( "Exception caught : %s", OUtoCStr( e.Message) );
386 // catch (...) {
387 // OSL_TRACE( "Non-UNO Exception caught\n" );
388 // }
390 const int THREAD_COUNT=100;
393 void usage()
395 PRINTLN("mozThread [opts] threadcount");
396 PRINTLN("opts:");
397 PRINTLN("-l test ldap");
398 PRINTLN("-m test mozilla");
399 PRINTLN("-p test outlook");
400 PRINTLN("-e test outlook express");
401 PRINTLN("0 < threadcount <= 100, default 100");
403 #if (defined UNX)
404 int main( int argc, char * argv[] )
405 #else
406 int _cdecl main( int argc, char * argv[] )
407 #endif
410 OSL_TRACE("Init UNO");
411 Reference< XMultiServiceFactory > xMgr =InitializeFac();
412 int threadCount=THREAD_COUNT;
413 int nAc;
414 for (nAc = 1; nAc < argc; nAc ++)
416 if (strcmp(argv[nAc],"-l") ==0)
418 testList[testLDAP] = 1;
419 }else if(strcmp(argv[nAc],"-m") ==0)
421 testList[testMozilla]=1;
422 }else if(strcmp(argv[nAc],"-p") ==0)
424 testList[testOp]=1;
425 }else if(strcmp(argv[nAc],"-e") ==0)
427 testList[testOe]=1;
428 }else if(strcmp(argv[nAc],"-h") ==0 || strcmp(argv[nAc],"--help") ==0)
430 usage();
431 return 0;
432 }else
434 int tmpCount = atoi(argv[nAc]);
435 if (tmpCount > 0 && tmpCount < threadCount)
437 threadCount = tmpCount;
441 testCount = testList[testLDAP] + testList[testMozilla] + testList[testOp] + testList[testOe];
442 if ( testCount == 0)
444 testList[testLDAP] = 1;
445 testCount = 1;
448 if (!xMgr.is())
450 OSL_TRACE("Error init UNO");
451 return 1;
453 else
454 OSL_TRACE("UNO initted");
456 mMgr = xMgr;
457 oslThread xThreads[THREAD_COUNT];
458 int index=0;
459 for(index=0;index < threadCount; index++)
461 xThreads[index] = osl_createThread(mozThread,(void*)NULL);
462 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
463 osl_waitThread(&timeValue);
465 for(index=0;index < threadCount; index++)
467 if (osl_isThreadRunning(xThreads[index]))
468 osl_joinWithThread(xThreads[index]);
470 OSL_TRACE("Exiting...");
471 return 0;
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */