xref: /aoo42x/main/stoc/test/excomp/excomp.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_stoc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <stdlib.h>
33*cdf0e10cSrcweir #include <string.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <sal/main.h>
36*cdf0e10cSrcweir #include <osl/diagnose.h>
37*cdf0e10cSrcweir #include <osl/process.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <example/XTest.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
44*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
45*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace com::sun::star::uno;
49*cdf0e10cSrcweir using namespace com::sun::star::registry;
50*cdf0e10cSrcweir using namespace com::sun::star::lang;
51*cdf0e10cSrcweir using namespace example;
52*cdf0e10cSrcweir using namespace cppu;
53*cdf0e10cSrcweir using namespace rtl;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
56*cdf0e10cSrcweir #define TEST_ENSHURE(c, m)   OSL_ENSURE(c, m)
57*cdf0e10cSrcweir #else
58*cdf0e10cSrcweir #define TEST_ENSHURE(c, m)   OSL_VERIFY(c)
59*cdf0e10cSrcweir #endif
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir OUString getExePath()
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir 	OUString 		exe;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	OSL_VERIFY(osl_getExecutableFile( &exe.pData) == osl_Process_E_None);
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir #if defined(WIN32) || defined(__OS2__) || defined(WNT)
68*cdf0e10cSrcweir 	exe = exe.copy(0, exe.getLength() - 10);
69*cdf0e10cSrcweir #else
70*cdf0e10cSrcweir 	exe = exe.copy(0, exe.getLength() - 6);
71*cdf0e10cSrcweir #endif
72*cdf0e10cSrcweir 	return exe;
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN()
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir #ifdef UNX
78*cdf0e10cSrcweir 	OUString compName1(RTL_CONSTASCII_USTRINGPARAM("libexcomp1.so"));
79*cdf0e10cSrcweir 	OUString compName2(RTL_CONSTASCII_USTRINGPARAM("libexcomp2.so"));
80*cdf0e10cSrcweir #else
81*cdf0e10cSrcweir 	OUString compName1(RTL_CONSTASCII_USTRINGPARAM("excomp1"));
82*cdf0e10cSrcweir 	OUString compName2(RTL_CONSTASCII_USTRINGPARAM("excomp2"));
83*cdf0e10cSrcweir #endif
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	OUString exePath( getExePath() );
86*cdf0e10cSrcweir 	OUString excompRdb(exePath);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	excompRdb += OUString::createFromAscii("excomp.rdb");
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xSMgr  = ::cppu::createRegistryServiceFactory( excompRdb );
91*cdf0e10cSrcweir 	TEST_ENSHURE( xSMgr.is(), "excomp error 0" );
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	typelib_TypeDescription* pTypeDesc = NULL;
94*cdf0e10cSrcweir 	OUString sType = OUString::createFromAscii("com.sun.star.text.XTextDocument");
95*cdf0e10cSrcweir 	typelib_typedescription_getByName( &pTypeDesc, sType.pData);
96*cdf0e10cSrcweir //	typelib_InterfaceTypeDescription* pInterDesc = (typelib_InterfaceTypeDescription*)pTypeDesc;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	Reference< XInterface > xIFace = xSMgr->createInstance(OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration"));
99*cdf0e10cSrcweir 	Reference< XImplementationRegistration > xImpReg( xIFace, UNO_QUERY);
100*cdf0e10cSrcweir 	TEST_ENSHURE( xImpReg.is(), "excomp error 1" );
101*cdf0e10cSrcweir 	try
102*cdf0e10cSrcweir 	{
103*cdf0e10cSrcweir 		xImpReg->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"),
104*cdf0e10cSrcweir 										compName1,
105*cdf0e10cSrcweir 										Reference< XSimpleRegistry >() );
106*cdf0e10cSrcweir 		xImpReg->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"),
107*cdf0e10cSrcweir 										compName2,
108*cdf0e10cSrcweir 										Reference< XSimpleRegistry >() );
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 	catch( CannotRegisterImplementationException& e)
111*cdf0e10cSrcweir 	{
112*cdf0e10cSrcweir 		TEST_ENSHURE( e.Message.getLength(), OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	Reference< XTest > xTest1( xSMgr->createInstance(OUString::createFromAscii("example.ExampleComponent1")),
116*cdf0e10cSrcweir 							   UNO_QUERY);
117*cdf0e10cSrcweir 	TEST_ENSHURE( xTest1.is(), "excomp error 2" );
118*cdf0e10cSrcweir 	Reference< XTest > xTest2( xSMgr->createInstance(OUString::createFromAscii("example.ExampleComponent2")),
119*cdf0e10cSrcweir 							   UNO_QUERY);
120*cdf0e10cSrcweir 	TEST_ENSHURE( xTest2.is(), "excomp error 3" );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	OUString m1 = xTest1->getMessage();
123*cdf0e10cSrcweir 	OUString m2 = xTest2->getMessage();
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	fprintf(stdout, "ExampleComponent1, Message = \"%s\"\n", OUStringToOString(m1, RTL_TEXTENCODING_ASCII_US).getStr());
126*cdf0e10cSrcweir 	fprintf(stdout, "ExampleComponent2, Message = \"%s\"\n", OUStringToOString(m2, RTL_TEXTENCODING_ASCII_US).getStr());
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	xImpReg->revokeImplementation(compName1, Reference< XSimpleRegistry >() );
129*cdf0e10cSrcweir 	xImpReg->revokeImplementation(compName2, Reference< XSimpleRegistry >() );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	Reference< XComponent >( xSMgr, UNO_QUERY )->dispose();
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	return(0);
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 
137