fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / connectivity / workben / testmoz / mozthread.cxx
blobdd649a8a1b9890715993756dfedc456004bb8ba6
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;
78 extern Reference< XMultiServiceFactory > InitializeFac( void );
79 Reference< XMultiServiceFactory > mMgr;
81 #define OUtoCStr( x ) (OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US ).getStr())
82 #define PRINTSTR(x) printf("%s",x);
83 #define PRINTLN(x) printf("%s\n",x);
85 const int testLDAP=0;
86 const int testMozilla=1;
87 const int testOp=2;
88 const int testOe=3;
90 static int testCount = 0;
91 static int testList[4] = {0,0,0,0};
93 int autoTest(Reference<XResultSet> &xRes);
95 void printColumns( Reference<XResultSet> &xRes )
97 if(xRes.is())
99 char* aPat = " %-22s ";
100 char* aPat_Short = " %-12s ";
101 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
102 OSL_TRACE( "ColumnCount = %d", xMeta->getColumnCount());
103 for(sal_Int32 i=1;i<=xMeta->getColumnCount();++i)
105 const char *str = OUtoCStr(xMeta->getColumnName(i));
106 OSL_TRACE( aPat, str );
108 OSL_TRACE("\n");
109 OSL_TRACE("------------------------------------------------------------------------------------------");
111 else
113 OSL_TRACE(": FAILED to get a ResultSet");
116 void printXResultSet( Reference<XResultSet> &xRes )
118 if(xRes.is())
120 char* aPat = " %-22s ";
121 char* aPat_Short = " %-12s ";
122 Reference<XRow> xRow(xRes,UNO_QUERY);
123 Reference<XResultSetMetaData> xMeta = Reference<XResultSetMetaDataSupplier>(xRes,UNO_QUERY)->getMetaData();
124 for(sal_Int32 j=1;j<=xMeta->getColumnCount();++j)
128 const char *str = OUtoCStr(xRow->getString(j));
129 OSL_TRACE( aPat_Short, str );
130 } catch (...) {
131 OSL_TRACE(" Ex ");
134 OSL_TRACE("\n");
136 else
137 OSL_TRACE("FAILED to get a ResultSet");
140 void printXResultSets( Reference<XResultSet> &xRes )
142 if(xRes.is())
144 printColumns(xRes);
145 sal_Int32 nRows = 0;
146 while( xRes.is() && xRes->next())
148 printXResultSet(xRes);
149 nRows++;
151 OSL_TRACE( "%d Row(s)", nRows);
152 }else
153 OSL_TRACE("FAILED to get a ResultSet");
158 int TestMetaData(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
160 // Test some metadata
161 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
162 if ( xDmd.is() )
164 OSL_TRACE(": got DatabaseMetaData");
166 OUString sQuoteStr = xDmd->getIdentifierQuoteString();
167 OSL_TRACE( "Quote String : '%s'", OUtoCStr( sQuoteStr ) );
169 OUString sSQLCmds = xDmd->getSQLKeywords();
170 OSL_TRACE( "SQL Commands : '%s'", OUtoCStr( sSQLCmds ) );
172 OSL_TRACE("Testing getColumns() : START");
174 Reference<XResultSet> xRes = xDmd->getColumns(
175 makeAny(OUString("")), // Catalog
176 OUString("%"), // Schema
177 OUString("%"), // TabName
178 OUString("%")
180 printXResultSets( xRes );
182 OSL_TRACE("Testing getColumns() : END");
184 OSL_TRACE("Testing getTypeInfo() : START");
186 Reference<XResultSet> xRes = xDmd-> getTypeInfo();
187 printXResultSets( xRes );
189 OSL_TRACE("Testing getTypeInfo() : END");
191 OSL_TRACE("Testing getTables() : START");
193 Reference<XResultSet> xRes = xDmd->getTables(
194 makeAny(OUString("")), // Catalog
195 OUString("%"), // Schema
196 OUString("%"), // TabName
197 Sequence<OUString>() );
198 printXResultSets( xRes );
200 OSL_TRACE("Testing getTables() : END");
203 else
204 OSL_TRACE(": FAILED to get DatabaseMetaData");
205 return 0;
208 void TestQuery(Reference< ::com::sun::star::sdbc::XConnection> &pConnection)
210 // Try a query
211 OSL_TRACE("Testing createStatement() & executeQuery() : START");
212 Reference<XStatement> xStmt = pConnection->createStatement();
213 Reference<XResultSet> xRes;
214 if(xStmt.is())
216 OSL_TRACE(": got statement");
217 OSL_TRACE(": excuteQuery() : START \n");
218 // SELECT "First Name", "Display Name", "E-mail" FROM tablename
219 OUString sqlPrefix("SELECT \"First Name\", \"Display Name\", \"E-mail\" FROM ");
222 sal_Int32 times=0;
223 Reference< XDatabaseMetaData > xDmd = pConnection->getMetaData();
224 if ( xDmd.is() )
226 OSL_TRACE("getTables() : START");
227 OUString qut = xDmd->getIdentifierQuoteString();
229 Reference<XResultSet> xRes = xDmd->getTables(
230 makeAny(OUString("")), // Catalog
231 OUString("%"), // Schema
232 OUString("%"), // TabName
233 Sequence<OUString>() );
234 sal_Int32 nTables = 0;
235 while( xRes.is() && xRes->next())
237 Reference<XRow> xRow(xRes,UNO_QUERY);
238 const char *strTableName = OUtoCStr(xRow->getString(3));
239 OSL_TRACE("Testing Table:%s",strTableName);
241 Reference<XResultSet> tmpRes =
242 xStmt->executeQuery(sqlPrefix + qut + xRow->getString(3) + qut);
243 autoTest( tmpRes );
244 Reference<XCloseable> clsRes(tmpRes,UNO_QUERY);
245 clsRes->close();
246 nTables++;
248 OSL_TRACE("Tested Tables:%d",nTables);
250 } catch ( Exception &e ) {
251 OSL_TRACE( "Exception caught : %s", OUtoCStr( e.Message) );
253 // catch (...) {
254 // OSL_TRACE( "Non-UNO Exception caught\n" );
255 // }
256 OSL_TRACE("excuteQuery() : END");
258 else
260 OSL_TRACE(": FAILED to get statement");
262 OSL_TRACE("Testing createStatement() & executeQuery() : END");
264 Reference< ::com::sun::star::sdbc::XConnection> TestConnected
265 (Reference< ::com::sun::star::sdbc::XDriver> &pDriver)
267 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> pConnection;
268 OSL_TRACE("Begin Connect!");
269 OUString url;
270 Sequence<PropertyValue> aValue;
271 int nType=0;
272 srand( (unsigned)time( NULL ) );
273 nType = rand() % testCount + 1;
274 int nIndex=0,nCount=0;
275 for ( nIndex = 0; nIndex< 4;nIndex++)
277 if (testList[nIndex])
278 nCount++;
279 if (nCount == nType)
280 break;
282 switch( nIndex)
284 case testLDAP:
285 url=OUString("sdbc:address:ldap://");
286 aValue.realloc(2);
287 aValue[0].Name = OUString("HostName");
288 aValue[0].Value <<= OUString("sun-ds");
289 aValue[1].Name = OUString("BaseDN");
290 aValue[1].Value <<= OUString("dc=sun,dc=com");
291 break;
292 case testMozilla:
293 url=OUString("sdbc:address:mozilla://");
294 break;
295 case testOp:
296 url=OUString("sdbc:address:outlook://");
297 break;
298 case testOe:
299 url=OUString("sdbc:address:outlookexp://");
300 break;
301 default:
302 url=OUString("sdbc:address:mozilla://");
303 break;
305 pConnection =
306 pDriver->connect(url,aValue);
307 return pConnection;
310 int autoTest(Reference<XResultSet> &xRes)
312 sal_Int32 nRows = 0;
313 printColumns(xRes);
314 if(xRes.is())
316 while( xRes.is() && xRes->next())
318 nRows++;
320 OSL_TRACE( "%d Row(s)", nRows);
321 sal_Int32 times;
322 sal_Int32 pos;
323 if (nRows)
325 for(times = 1;times < 10; times ++)
327 pos= rand() % nRows+1;
328 OSL_TRACE("pos:%d",pos);
329 xRes->absolute(pos);
330 printXResultSet(xRes);
334 else
336 OSL_TRACE(": FAILED to get a ResultSet");
338 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
339 osl_waitThread(&timeValue);
340 return 0;
342 void SAL_CALL mozThread(void*)
344 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
347 Reference< ::com::sun::star::sdbc::XDriver>
348 m_xDriver(mMgr->createInstance(
349 OUString("com.sun.star.comp.sdbc.MozabDriver")),
350 UNO_QUERY);
351 if(m_xDriver.is())
354 m_xConnection = TestConnected(m_xDriver);
355 if(m_xConnection.is())
357 if (!TestMetaData(m_xConnection))
359 TestQuery(m_xConnection);
362 else
363 OSL_TRACE("Can't connected!");
366 else
368 OSL_TRACE("No driver!");
370 } catch ( Exception &e ) {
371 OSL_TRACE( "Exception caught : %s", OUtoCStr( e.Message) );
373 // catch (...) {
374 // OSL_TRACE( "Non-UNO Exception caught\n" );
375 // }
377 const int THREAD_COUNT=100;
380 void usage()
382 PRINTLN("mozThread [opts] threadcount");
383 PRINTLN("opts:");
384 PRINTLN("-l test ldap");
385 PRINTLN("-m test mozilla");
386 PRINTLN("-p test outlook");
387 PRINTLN("-e test outlook express");
388 PRINTLN("0 < threadcount <= 100, default 100");
390 #if (defined UNX)
391 int main( int argc, char * argv[] )
392 #else
393 int _cdecl main( int argc, char * argv[] )
394 #endif
397 OSL_TRACE("Init UNO");
398 Reference< XMultiServiceFactory > xMgr =InitializeFac();
399 int threadCount=THREAD_COUNT;
400 int nAc;
401 for (nAc = 1; nAc < argc; nAc ++)
403 if (strcmp(argv[nAc],"-l") ==0)
405 testList[testLDAP] = 1;
406 }else if(strcmp(argv[nAc],"-m") ==0)
408 testList[testMozilla]=1;
409 }else if(strcmp(argv[nAc],"-p") ==0)
411 testList[testOp]=1;
412 }else if(strcmp(argv[nAc],"-e") ==0)
414 testList[testOe]=1;
415 }else if(strcmp(argv[nAc],"-h") ==0 || strcmp(argv[nAc],"--help") ==0)
417 usage();
418 return 0;
419 }else
421 int tmpCount = atoi(argv[nAc]);
422 if (tmpCount > 0 && tmpCount < threadCount)
424 threadCount = tmpCount;
428 testCount = testList[testLDAP] + testList[testMozilla] + testList[testOp] + testList[testOe];
429 if ( testCount == 0)
431 testList[testLDAP] = 1;
432 testCount = 1;
435 if (!xMgr.is())
437 OSL_TRACE("Error init UNO");
438 return 1;
440 else
441 OSL_TRACE("UNO initted");
443 mMgr = xMgr;
444 oslThread xThreads[THREAD_COUNT];
445 int index=0;
446 for(index=0;index < threadCount; index++)
448 xThreads[index] = osl_createThread(mozThread,(void*)NULL);
449 TimeValue timeValue = { 1, 0 }; //sleep 1 Seconds to give time to other threads
450 osl_waitThread(&timeValue);
452 for(index=0;index < threadCount; index++)
454 if (osl_isThreadRunning(xThreads[index]))
455 osl_joinWithThread(xThreads[index]);
457 OSL_TRACE("Exiting...");
458 return 0;
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */