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