xref: /trunk/main/stoc/test/excomp/excomp.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647a425cSAndrew Rist  * distributed with this work for additional information
6*647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9*647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647a425cSAndrew Rist  * software distributed under the License is distributed on an
15*647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647a425cSAndrew Rist  * specific language governing permissions and limitations
18*647a425cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647a425cSAndrew Rist  *************************************************************/
21*647a425cSAndrew Rist 
22*647a425cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_stoc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <stdlib.h>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sal/main.h>
32cdf0e10cSrcweir #include <osl/diagnose.h>
33cdf0e10cSrcweir #include <osl/process.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <example/XTest.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
39cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
40cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
41cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace com::sun::star::uno;
45cdf0e10cSrcweir using namespace com::sun::star::registry;
46cdf0e10cSrcweir using namespace com::sun::star::lang;
47cdf0e10cSrcweir using namespace example;
48cdf0e10cSrcweir using namespace cppu;
49cdf0e10cSrcweir using namespace rtl;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
52cdf0e10cSrcweir #define TEST_ENSHURE(c, m)   OSL_ENSURE(c, m)
53cdf0e10cSrcweir #else
54cdf0e10cSrcweir #define TEST_ENSHURE(c, m)   OSL_VERIFY(c)
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir 
getExePath()57cdf0e10cSrcweir OUString getExePath()
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     OUString        exe;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     OSL_VERIFY(osl_getExecutableFile( &exe.pData) == osl_Process_E_None);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir #if defined(WIN32) || defined(__OS2__) || defined(WNT)
64cdf0e10cSrcweir     exe = exe.copy(0, exe.getLength() - 10);
65cdf0e10cSrcweir #else
66cdf0e10cSrcweir     exe = exe.copy(0, exe.getLength() - 6);
67cdf0e10cSrcweir #endif
68cdf0e10cSrcweir     return exe;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
SAL_IMPLEMENT_MAIN()71cdf0e10cSrcweir SAL_IMPLEMENT_MAIN()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir #ifdef UNX
74cdf0e10cSrcweir     OUString compName1(RTL_CONSTASCII_USTRINGPARAM("libexcomp1.so"));
75cdf0e10cSrcweir     OUString compName2(RTL_CONSTASCII_USTRINGPARAM("libexcomp2.so"));
76cdf0e10cSrcweir #else
77cdf0e10cSrcweir     OUString compName1(RTL_CONSTASCII_USTRINGPARAM("excomp1"));
78cdf0e10cSrcweir     OUString compName2(RTL_CONSTASCII_USTRINGPARAM("excomp2"));
79cdf0e10cSrcweir #endif
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     OUString exePath( getExePath() );
82cdf0e10cSrcweir     OUString excompRdb(exePath);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     excompRdb += OUString::createFromAscii("excomp.rdb");
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     Reference< XMultiServiceFactory > xSMgr  = ::cppu::createRegistryServiceFactory( excompRdb );
87cdf0e10cSrcweir     TEST_ENSHURE( xSMgr.is(), "excomp error 0" );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     typelib_TypeDescription* pTypeDesc = NULL;
90cdf0e10cSrcweir     OUString sType = OUString::createFromAscii("com.sun.star.text.XTextDocument");
91cdf0e10cSrcweir     typelib_typedescription_getByName( &pTypeDesc, sType.pData);
92cdf0e10cSrcweir //  typelib_InterfaceTypeDescription* pInterDesc = (typelib_InterfaceTypeDescription*)pTypeDesc;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     Reference< XInterface > xIFace = xSMgr->createInstance(OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration"));
95cdf0e10cSrcweir     Reference< XImplementationRegistration > xImpReg( xIFace, UNO_QUERY);
96cdf0e10cSrcweir     TEST_ENSHURE( xImpReg.is(), "excomp error 1" );
97cdf0e10cSrcweir     try
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         xImpReg->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"),
100cdf0e10cSrcweir                                         compName1,
101cdf0e10cSrcweir                                         Reference< XSimpleRegistry >() );
102cdf0e10cSrcweir         xImpReg->registerImplementation(OUString::createFromAscii("com.sun.star.loader.SharedLibrary"),
103cdf0e10cSrcweir                                         compName2,
104cdf0e10cSrcweir                                         Reference< XSimpleRegistry >() );
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir     catch( CannotRegisterImplementationException& e)
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         TEST_ENSHURE( e.Message.getLength(), OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     Reference< XTest > xTest1( xSMgr->createInstance(OUString::createFromAscii("example.ExampleComponent1")),
112cdf0e10cSrcweir                                UNO_QUERY);
113cdf0e10cSrcweir     TEST_ENSHURE( xTest1.is(), "excomp error 2" );
114cdf0e10cSrcweir     Reference< XTest > xTest2( xSMgr->createInstance(OUString::createFromAscii("example.ExampleComponent2")),
115cdf0e10cSrcweir                                UNO_QUERY);
116cdf0e10cSrcweir     TEST_ENSHURE( xTest2.is(), "excomp error 3" );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     OUString m1 = xTest1->getMessage();
119cdf0e10cSrcweir     OUString m2 = xTest2->getMessage();
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     fprintf(stdout, "ExampleComponent1, Message = \"%s\"\n", OUStringToOString(m1, RTL_TEXTENCODING_ASCII_US).getStr());
122cdf0e10cSrcweir     fprintf(stdout, "ExampleComponent2, Message = \"%s\"\n", OUStringToOString(m2, RTL_TEXTENCODING_ASCII_US).getStr());
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     xImpReg->revokeImplementation(compName1, Reference< XSimpleRegistry >() );
125cdf0e10cSrcweir     xImpReg->revokeImplementation(compName2, Reference< XSimpleRegistry >() );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     Reference< XComponent >( xSMgr, UNO_QUERY )->dispose();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     return(0);
130cdf0e10cSrcweir }
131