summaryrefslogtreecommitdiff
path: root/extensions/coooder/source/org/libreoffice/coooder/comp/test/base
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/coooder/source/org/libreoffice/coooder/comp/test/base')
-rw-r--r--extensions/coooder/source/org/libreoffice/coooder/comp/test/base/Bootstrap.java337
-rw-r--r--extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestCase.java69
-rw-r--r--extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestSuite.java144
3 files changed, 0 insertions, 550 deletions
diff --git a/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/Bootstrap.java b/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/Bootstrap.java
deleted file mode 100644
index 839cf14cb..000000000
--- a/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/Bootstrap.java
+++ /dev/null
@@ -1,337 +0,0 @@
-/*************************************************************************
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * Copyright 2008 by Sun Microsystems, Inc.
- *
- * OpenOffice.org - a multi-platform office productivity suite
- *
- * $RCSfile: Bootstrap.java,v $
- * $Revision: 1.16 $
- *
- * This file is part of OpenOffice.org.
- *
- * OpenOffice.org is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License version 3
- * only, as published by the Free Software Foundation.
- *
- * OpenOffice.org is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License version 3 for more details
- * (a copy is included in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU Lesser General Public License
- * version 3 along with OpenOffice.org. If not, see
- * <http://www.openoffice.org/license.html>
- * for a copy of the LGPLv3 License.
- *
- ************************************************************************/
-
-package org.libreoffice.coooder.comp.test.base;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.net.URLClassLoader;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Random;
-
-import com.sun.star.bridge.UnoUrlResolver;
-import com.sun.star.bridge.XUnoUrlResolver;
-import com.sun.star.comp.helper.BootstrapException;
-import com.sun.star.comp.helper.ComponentContext;
-import com.sun.star.comp.helper.ComponentContextEntry;
-import com.sun.star.comp.loader.JavaLoader;
-import com.sun.star.container.XSet;
-import com.sun.star.lang.XInitialization;
-import com.sun.star.lang.XMultiComponentFactory;
-import com.sun.star.lang.XMultiServiceFactory;
-import com.sun.star.lang.XSingleComponentFactory;
-import com.sun.star.lib.util.NativeLibraryLoader;
-import com.sun.star.loader.XImplementationLoader;
-import com.sun.star.uno.UnoRuntime;
-import com.sun.star.uno.XComponentContext;
-
-/** Bootstrap offers functionality to obtain a context or simply
- a service manager.
- The service manager can create a few basic services, whose implementations are:
- <ul>
- <li>com.sun.star.comp.loader.JavaLoader</li>
- <li>com.sun.star.comp.urlresolver.UrlResolver</li>
- <li>com.sun.star.comp.bridgefactory.BridgeFactory</li>
- <li>com.sun.star.comp.connections.Connector</li>
- <li>com.sun.star.comp.connections.Acceptor</li>
- <li>com.sun.star.comp.servicemanager.ServiceManager</li>
- </ul>
-
- Other services can be inserted into the service manager by
- using its XSet interface:
- <pre>
- XSet xSet = (XSet)UnoRuntime.queryInterface( XSet.class, aMultiComponentFactory );
- // insert the service manager
- xSet.insert( aSingleComponentFactory );
- </pre>
-
- @author cbosdo Changed the class for a better control of the bootstrap using a custom classloader
-*/
-public class Bootstrap {
-
- private static void insertBasicFactories(
- XSet xSet, XImplementationLoader xImpLoader )
- throws Exception
- {
- // insert the factory of the loader
- xSet.insert( xImpLoader.activate(
- "com.sun.star.comp.loader.JavaLoader", null, null, null ) );
-
- // insert the factory of the URLResolver
- xSet.insert( xImpLoader.activate(
- "com.sun.star.comp.urlresolver.UrlResolver", null, null, null ) );
-
- // insert the bridgefactory
- xSet.insert( xImpLoader.activate(
- "com.sun.star.comp.bridgefactory.BridgeFactory", null, null, null ) );
-
- // insert the connector
- xSet.insert( xImpLoader.activate(
- "com.sun.star.comp.connections.Connector", null, null, null ) );
-
- // insert the acceptor
- xSet.insert( xImpLoader.activate(
- "com.sun.star.comp.connections.Acceptor", null, null, null ) );
- }
-
- /** Bootstraps an initial component context with service manager and basic
- jurt components inserted.
- @param context_entries the hash table contains mappings of entry names (type string) to
- context entries (type class ComponentContextEntry).
- @return a new context.
- */
- static public XComponentContext createInitialComponentContext( Hashtable context_entries )
- throws Exception
- {
- XImplementationLoader xImpLoader = (XImplementationLoader)UnoRuntime.queryInterface(
- XImplementationLoader.class, new JavaLoader() );
-
- // Get the factory of the ServiceManager
- XSingleComponentFactory smgr_fac = (XSingleComponentFactory)UnoRuntime.queryInterface(
- XSingleComponentFactory.class, xImpLoader.activate(
- "com.sun.star.comp.servicemanager.ServiceManager", null, null, null ) );
-
- // Create an instance of the ServiceManager
- XMultiComponentFactory xSMgr = (XMultiComponentFactory)UnoRuntime.queryInterface(
- XMultiComponentFactory.class, smgr_fac.createInstanceWithContext( null ) );
-
- // post init loader
- XInitialization xInit = (XInitialization)UnoRuntime.queryInterface(
- XInitialization.class, xImpLoader );
- Object[] args = new Object [] { xSMgr };
- xInit.initialize( args );
-
- // initial component context
- if (context_entries == null)
- context_entries = new Hashtable( 1 );
- // add smgr
- context_entries.put(
- "/singletons/com.sun.star.lang.theServiceManager",
- new ComponentContextEntry( null, xSMgr ) );
- // ... xxx todo: add standard entries
- XComponentContext xContext = new ComponentContext( context_entries, null );
-
- // post init smgr
- xInit = (XInitialization)UnoRuntime.queryInterface(
- XInitialization.class, xSMgr );
- args = new Object [] { null, xContext }; // no registry, default context
- xInit.initialize( args );
-
- XSet xSet = (XSet)UnoRuntime.queryInterface( XSet.class, xSMgr );
- // insert the service manager
- xSet.insert( smgr_fac );
- // and basic jurt factories
- insertBasicFactories( xSet, xImpLoader );
-
- return xContext;
- }
-
- /**
- * Bootstraps a servicemanager with the jurt base components registered.
- * <p>
- * @return a freshly boostrapped service manager
- * @see com.sun.star.lang.ServiceManager
- */
- static public XMultiServiceFactory createSimpleServiceManager() throws Exception
- {
- return (XMultiServiceFactory)UnoRuntime.queryInterface(
- XMultiServiceFactory.class, createInitialComponentContext( null ).getServiceManager() );
- }
-
-
- /** Bootstraps the initial component context from a native UNO installation.
-
- @see cppuhelper/defaultBootstrap_InitialComponentContext()
- */
- static public final XComponentContext defaultBootstrap_InitialComponentContext()
- throws Exception
- {
- return defaultBootstrap_InitialComponentContext( null, null );
- }
- /** Bootstraps the initial component context from a native UNO installation.
-
- @param ini_file
- ini_file (may be null: uno.rc besides cppuhelper lib)
- @param bootstrap_parameters
- bootstrap parameters (maybe null)
-
- @see cppuhelper/defaultBootstrap_InitialComponentContext()
- */
- static public final XComponentContext defaultBootstrap_InitialComponentContext(
- String ini_file, Hashtable bootstrap_parameters )
- throws Exception
- {
- // jni convenience: easier to iterate over array than calling Hashtable
- String pairs [] = null;
- if (null != bootstrap_parameters)
- {
- pairs = new String [ 2 * bootstrap_parameters.size() ];
- Enumeration keys = bootstrap_parameters.keys();
- int n = 0;
- while (keys.hasMoreElements())
- {
- String name = (String)keys.nextElement();
- pairs[ n++ ] = name;
- pairs[ n++ ] = (String)bootstrap_parameters.get( name );
- }
- }
-
- if (! m_loaded_juh)
- {
- System.loadLibrary( "juh" );
- m_loaded_juh = true;
- }
- return (XComponentContext)UnoRuntime.queryInterface(
- XComponentContext.class,
- cppuhelper_bootstrap(
- ini_file, pairs, Bootstrap.class.getClassLoader() ) );
- }
-
- static private boolean m_loaded_juh = false;
- static private native Object cppuhelper_bootstrap(
- String ini_file, String bootstrap_parameters [], ClassLoader loader )
- throws Exception;
-
- /**
- * Bootstraps the component context from a UNO installation.
- *
- * @return a bootstrapped component context.
- *
- * @since UDK 3.1.0
- */
- public static final XComponentContext bootstrap(URLClassLoader loader)
- throws BootstrapException {
-
- XComponentContext xContext = null;
-
- try {
- // create default local component context
- XComponentContext xLocalContext =
- createInitialComponentContext( null );
- if ( xLocalContext == null )
- throw new BootstrapException( "no local component context!" );
-
- // find office executable relative to this class's class loader
- String sOffice =
- System.getProperty( "os.name" ).startsWith( "Windows" ) ?
- "soffice.exe" : "soffice";
- File fOffice = NativeLibraryLoader.getResource( loader, sOffice );
- if ( fOffice == null )
- throw new BootstrapException( "no office executable found!" );
-
- // create random pipe name
- String sPipeName = "uno" +
- Long.toString( (new Random()).nextLong() & 0x7fffffffffffffffL );
-
- // create call with arguments
- String[] cmdArray = new String[7];
- cmdArray[0] = fOffice.getPath();
- cmdArray[1] = "-nologo";
- cmdArray[2] = "-nodefault";
- cmdArray[3] = "-norestore";
- cmdArray[4] = "-nocrashreport";
- cmdArray[5] = "-nolockcheck";
- cmdArray[6] = "-accept=pipe,name=" + sPipeName + ";urp;";
-
- // start office process
- Process p = Runtime.getRuntime().exec( cmdArray );
- pipe( p.getInputStream(), System.out, "CO> " );
- pipe( p.getErrorStream(), System.err, "CE> " );
-
- // initial service manager
- XMultiComponentFactory xLocalServiceManager =
- xLocalContext.getServiceManager();
- if ( xLocalServiceManager == null )
- throw new BootstrapException( "no initial service manager!" );
-
- // create a URL resolver
- XUnoUrlResolver xUrlResolver =
- UnoUrlResolver.create( xLocalContext );
-
- // connection string
- String sConnect = "uno:pipe,name=" + sPipeName +
- ";urp;StarOffice.ComponentContext";
-
- // wait until office is started
- for (int i = 0;; ++i) {
- try {
- // try to connect to office
- Object context = xUrlResolver.resolve( sConnect );
- xContext = (XComponentContext) UnoRuntime.queryInterface(
- XComponentContext.class, context);
- if ( xContext == null )
- throw new BootstrapException( "no component context!" );
- break;
- } catch ( com.sun.star.connection.NoConnectException ex ) {
- // Wait 500 ms, then try to connect again, but do not wait
- // longer than 5 min (= 600 * 500 ms) total:
- if (i == 600) {
- throw new BootstrapException(ex.toString());
- }
- Thread.sleep( 500 );
- }
- }
- } catch ( BootstrapException e ) {
- throw e;
- } catch ( java.lang.RuntimeException e ) {
- throw e;
- } catch ( java.lang.Exception e ) {
- throw new BootstrapException( e );
- }
-
- return xContext;
- }
-
- private static void pipe(
- final InputStream in, final PrintStream out, final String prefix ) {
-
- new Thread( "Pipe: " + prefix) {
- public void run() {
- BufferedReader r = new BufferedReader(
- new InputStreamReader( in ) );
- try {
- for ( ; ; ) {
- String s = r.readLine();
- if ( s == null ) {
- break;
- }
- out.println( prefix + s );
- }
- } catch ( java.io.IOException e ) {
- e.printStackTrace( System.err );
- }
- }
- }.start();
- }
-}
diff --git a/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestCase.java b/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestCase.java
deleted file mode 100644
index 187deaca7..000000000
--- a/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestCase.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * LibreOffice extension for syntax highlighting
- * Copyright (C) 2008 Cédric Bosdonnat cedric.bosdonnat.ooo@free.fr
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation;
- * version 2 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-package org.libreoffice.coooder.comp.test.base;
-
-import junit.framework.TestCase;
-
-import com.sun.star.uno.XComponentContext;
-
-/**
- * Base for all test cases to use along with {@link UnoTestSuite}.
- *
- * @author cbosdo
- *
- */
-public abstract class UnoTestCase extends TestCase {
-
- /**
- * Default constructor.
- */
- public UnoTestCase() {
- super();
- }
-
- /**
- * Constructor to use to run only one test
- *
- * @param pName the test name
- */
- public UnoTestCase(String pName) {
- super(pName);
- }
-
-
- private XComponentContext mContext;
-
- /**
- * Set the office context to use in the tests.
- *
- * @param pContext the remote office context
- */
- public void setContext(XComponentContext pContext) {
- mContext = pContext;
- }
-
- /**
- * Get the office context to use in the tests.
- *
- * @return the remote office context
- */
- protected XComponentContext getContext() {
- return mContext;
- }
-}
diff --git a/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestSuite.java b/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestSuite.java
deleted file mode 100644
index d76a71a64..000000000
--- a/extensions/coooder/source/org/libreoffice/coooder/comp/test/base/UnoTestSuite.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * LibreOffice extension for syntax highlighting
- * Copyright (C) 2008 Cédric Bosdonnat cedric.bosdonnat.ooo@free.fr
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation;
- * version 2 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */package org.libreoffice.coooder.comp.test.base;
-
-
-import java.io.File;
-import java.net.URL;
-import java.net.URLClassLoader;
-
-import junit.framework.Test;
-import junit.framework.TestResult;
-import junit.framework.TestSuite;
-
-import com.sun.star.frame.XDesktop;
-import com.sun.star.lang.XMultiComponentFactory;
-import com.sun.star.uno.UnoRuntime;
-import com.sun.star.uno.XComponentContext;
-
-/**
- * This is a test suite for an OOo extension unit tests:
- * it bootstraps an OOo instance and handles the connection with it.
- *
- * The extension to test has to be already installed on OOo for the
- * current user.
- *
- * @author cbosdo
- *
- */
-public class UnoTestSuite extends TestSuite {
-
- private XComponentContext mContext;
- private boolean mHandleConnection = false;
-
-
- public UnoTestSuite() {
- super();
- }
-
- public UnoTestSuite(Class pTheClass, String pName) {
- super(pTheClass, pName);
- }
-
- public UnoTestSuite(Class pTheClass) {
- super(pTheClass);
- }
-
- public UnoTestSuite(Class[] pClasses, String pName) {
- super(pClasses, pName);
- }
-
- public UnoTestSuite(Class[] pClasses) {
- // Override to create UnoTestSuites instead of TestSuites
- for (int i= 0; i < pClasses.length; i++) {
- addTest(new UnoTestSuite(pClasses[i]));
- }
- }
-
- public UnoTestSuite(String pName) {
- super(pName);
- }
-
- public void run(TestResult pResult) {
-
- try {
- startOffice();
-
- super.run(pResult);
- } catch (Exception e) {
- System.err.println("Unable to run the tests");
- e.printStackTrace();
- }
-
- stopOffice();
- }
-
- public void runTest(Test pTest, TestResult pResult) {
- // Try to set the context on the test
- if (pTest instanceof UnoTestCase) {
- ((UnoTestCase)pTest).setContext(mContext);
- } else if (pTest instanceof UnoTestSuite) {
- UnoTestSuite suite = (UnoTestSuite)pTest;
- suite.setContext(mContext);
- }
-
- super.runTest(pTest, pResult);
- }
-
- protected void setContext(XComponentContext pContext) {
- mContext = pContext;
- }
-
- protected void setHandleConnection(boolean pHandle) {
- mHandleConnection = pHandle;
- }
-
- /**
- * Starts LibreOffice only if the context isn't set.
- *
- * @throws Exception if anything wrong happens when starting the office
- */
- private void startOffice() throws Exception {
- if (mContext == null) {
- String path = System.getProperty("libreoffice.program.path");
- if (path == null) {
- throw new Exception("The libreoffice.install.path variable is missing");
- }
-
- File baseDir = new File(path);
- URLClassLoader classLoader = new URLClassLoader(new URL[]{baseDir.toURI().toURL()});
- mContext = Bootstrap.bootstrap(classLoader);
- mHandleConnection = true;
- }
- }
-
- private void stopOffice() {
- try {
- if (mContext != null && mHandleConnection) {
- // Only the uno test suite which started the office can stop it
- XMultiComponentFactory xMngr = mContext.getServiceManager();
- Object oDesktop = xMngr.createInstanceWithContext("com.sun.star.frame.Desktop", mContext);
- XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, oDesktop);
-
- xDesktop.terminate();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-}