1*f7cf3d52SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f7cf3d52SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f7cf3d52SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f7cf3d52SAndrew Rist  * distributed with this work for additional information
6*f7cf3d52SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f7cf3d52SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f7cf3d52SAndrew Rist  * "License"); you may not use this file except in compliance
9*f7cf3d52SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f7cf3d52SAndrew Rist  *
11*f7cf3d52SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f7cf3d52SAndrew Rist  *
13*f7cf3d52SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f7cf3d52SAndrew Rist  * software distributed under the License is distributed on an
15*f7cf3d52SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f7cf3d52SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f7cf3d52SAndrew Rist  * specific language governing permissions and limitations
18*f7cf3d52SAndrew Rist  * under the License.
19*f7cf3d52SAndrew Rist  *
20*f7cf3d52SAndrew Rist  *************************************************************/
21*f7cf3d52SAndrew Rist 
22*f7cf3d52SAndrew Rist 
23cdf0e10cSrcweir package complex.dbaccess;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.lang.reflect.InvocationTargetException;
26cdf0e10cSrcweir import java.lang.reflect.Method;
27cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
29cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
30cdf0e10cSrcweir import com.sun.star.frame.XModel;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir // import com.sun.star.uno.Exception;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
35cdf0e10cSrcweir import helper.FileTools;
36cdf0e10cSrcweir import java.io.File;
37cdf0e10cSrcweir import java.io.IOException;
38cdf0e10cSrcweir import java.net.URI;
39cdf0e10cSrcweir import java.net.URISyntaxException;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ---------- junit imports -----------------
42cdf0e10cSrcweir import org.junit.AfterClass;
43cdf0e10cSrcweir import org.junit.BeforeClass;
44cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
45cdf0e10cSrcweir import static org.junit.Assert.*;
46cdf0e10cSrcweir // ------------------------------------------
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir public abstract class TestCase
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
getComponentContext()52cdf0e10cSrcweir     protected final XComponentContext getComponentContext()
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         XComponentContext context = null;
55cdf0e10cSrcweir         try
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             final XPropertySet orbProps = UnoRuntime.queryInterface( XPropertySet.class, getMSF() );
58cdf0e10cSrcweir             context = UnoRuntime.queryInterface( XComponentContext.class,
59cdf0e10cSrcweir                 orbProps.getPropertyValue( "DefaultContext" ) );
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir         catch ( Exception ex )
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             fail( "could not retrieve the ComponentContext" );
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir         return context;
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
before()69cdf0e10cSrcweir     public void before() throws java.lang.Exception
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
after()74cdf0e10cSrcweir     public void after() throws java.lang.Exception
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
79cdf0e10cSrcweir     /** returns the URL of a temporary file which can be used during the test.
80cdf0e10cSrcweir      *
81cdf0e10cSrcweir      *  The file will be deleted when the process exits
82cdf0e10cSrcweir      *  @return the URL of a temporary file
83cdf0e10cSrcweir      */
createTempFileURL()84cdf0e10cSrcweir     protected final String createTempFileURL() throws IOException
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         final File documentFile = java.io.File.createTempFile( "dbaccess_test", ".odb" ).getAbsoluteFile();
87cdf0e10cSrcweir         if ( documentFile.exists() )
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             documentFile.delete();
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir         return FileHelper.getOOoCompatibleFileURL( documentFile.toURI().toURL().toString() );
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
95cdf0e10cSrcweir     /**
96cdf0e10cSrcweir      * copies the file given by URL to a temporary file
97cdf0e10cSrcweir      * @return
98cdf0e10cSrcweir      *  the URL of the new file
99cdf0e10cSrcweir      */
copyToTempFile( String _sourceURL )100cdf0e10cSrcweir     protected final String copyToTempFile( String _sourceURL ) throws IOException
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         final String targetURL = createTempFileURL();
103cdf0e10cSrcweir         try
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             FileTools.copyFile( new File( new URI( _sourceURL ) ), new File( new URI( targetURL ) ) );
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir         catch ( URISyntaxException e ) { }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         return FileHelper.getOOoCompatibleFileURL( targetURL );
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
loadDocument( final String _docURL )113cdf0e10cSrcweir     protected final XModel loadDocument( final String _docURL ) throws Exception
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         final XComponentLoader loader = UnoRuntime.queryInterface( XComponentLoader.class,
116cdf0e10cSrcweir             getMSF().createInstance( "com.sun.star.frame.Desktop" ) );
117cdf0e10cSrcweir         return UnoRuntime.queryInterface( XModel.class,
118cdf0e10cSrcweir             loader.loadComponentFromURL( _docURL, "_blank", 0, new PropertyValue[] {} ) );
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
122cdf0e10cSrcweir     /** invokes a given method on a given object, and assures a certain exception is caught
123cdf0e10cSrcweir      * @param _message
124cdf0e10cSrcweir      *          is the message to print when the check fails
125cdf0e10cSrcweir      * @param _object
126cdf0e10cSrcweir      *          is the object to invoke the method on
127cdf0e10cSrcweir      * @param _methodName
128cdf0e10cSrcweir      *          is the name of the method to invoke
129cdf0e10cSrcweir      * @param _methodArgs
130cdf0e10cSrcweir      *          are the arguments to pass to the method.
131cdf0e10cSrcweir      * @param _argClasses
132cdf0e10cSrcweir      *          are the classes to assume for the arguments of the methods
133cdf0e10cSrcweir      * @param _expectedExceptionClass
134cdf0e10cSrcweir      *          is the class of the exception to be caught. If this is null,
135cdf0e10cSrcweir      *          it means that <em>no</em> exception must be throw by invoking the method.
136cdf0e10cSrcweir     */
assureException( final String _message, final Object _object, final String _methodName, final Class[] _argClasses, final Object[] _methodArgs, final Class _expectedExceptionClass )137cdf0e10cSrcweir     protected void assureException( final String _message, final Object _object, final String _methodName,
138cdf0e10cSrcweir         final Class[] _argClasses, final Object[] _methodArgs, final Class _expectedExceptionClass )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         Class objectClass = _object.getClass();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         boolean noExceptionAllowed = ( _expectedExceptionClass == null );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         boolean caughtExpected = noExceptionAllowed ? true : false;
145cdf0e10cSrcweir         try
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             Method method = objectClass.getMethod( _methodName, _argClasses );
148cdf0e10cSrcweir             method.invoke(_object, _methodArgs );
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         catch ( InvocationTargetException e )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             caughtExpected =    noExceptionAllowed
153cdf0e10cSrcweir                             ?   false
154cdf0e10cSrcweir                             :   ( e.getTargetException().getClass().equals( _expectedExceptionClass ) );
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         catch( Exception e )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             caughtExpected = false;
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         assertTrue( _message, caughtExpected );
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     /** invokes a given method on a given object, and assures a certain exception is caught
165cdf0e10cSrcweir      * @param _message is the message to print when the check fails
166cdf0e10cSrcweir      * @param _object is the object to invoke the method on
167cdf0e10cSrcweir      * @param _methodName is the name of the method to invoke
168cdf0e10cSrcweir      * @param _methodArgs are the arguments to pass to the method. Those implicitly define
169cdf0e10cSrcweir      *      the classes of the arguments of the method which is called.
170cdf0e10cSrcweir      * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
171cdf0e10cSrcweir      *          it means that <em>no</em> exception must be throw by invoking the method.
172cdf0e10cSrcweir     */
assureException( final String _message, final Object _object, final String _methodName, final Object[] _methodArgs, final Class _expectedExceptionClass )173cdf0e10cSrcweir     protected void assureException( final String _message, final Object _object, final String _methodName,
174cdf0e10cSrcweir         final Object[] _methodArgs, final Class _expectedExceptionClass )
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         Class[] argClasses = new Class[ _methodArgs.length ];
177cdf0e10cSrcweir         for ( int i=0; i<_methodArgs.length; ++i )
178cdf0e10cSrcweir             argClasses[i] = _methodArgs[i].getClass();
179cdf0e10cSrcweir         assureException( _message, _object, _methodName, argClasses, _methodArgs, _expectedExceptionClass );
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     /** invokes a given method on a given object, and assures a certain exception is caught
183cdf0e10cSrcweir      * @param _object is the object to invoke the method on
184cdf0e10cSrcweir      * @param _methodName is the name of the method to invoke
185cdf0e10cSrcweir      * @param _methodArgs are the arguments to pass to the method. Those implicitly define
186cdf0e10cSrcweir      *      the classes of the arguments of the method which is called.
187cdf0e10cSrcweir      * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
188cdf0e10cSrcweir      *          it means that <em>no</em> exception must be throw by invoking the method.
189cdf0e10cSrcweir     */
assureException( final Object _object, final String _methodName, final Object[] _methodArgs, final Class _expectedExceptionClass )190cdf0e10cSrcweir     protected void assureException( final Object _object, final String _methodName, final Object[] _methodArgs,
191cdf0e10cSrcweir         final Class _expectedExceptionClass )
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         assureException(
194cdf0e10cSrcweir             "did not catch the expected exception (" +
195cdf0e10cSrcweir                 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) +
196cdf0e10cSrcweir                 ") while calling " + _object.getClass().getName() + "." + _methodName,
197cdf0e10cSrcweir             _object, _methodName, _methodArgs, _expectedExceptionClass );
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     /** invokes a given method on a given object, and assures a certain exception is caught
201cdf0e10cSrcweir      * @param _object is the object to invoke the method on
202cdf0e10cSrcweir      * @param _methodName is the name of the method to invoke
203cdf0e10cSrcweir      * @param _methodArgs are the arguments to pass to the method
204cdf0e10cSrcweir      * @param _argClasses are the classes to assume for the arguments of the methods
205cdf0e10cSrcweir      * @param _expectedExceptionClass is the class of the exception to be caught. If this is null,
206cdf0e10cSrcweir      *          it means that <em>no</em> exception must be throw by invoking the method.
207cdf0e10cSrcweir     */
assureException( final Object _object, final String _methodName, final Class[] _argClasses, final Object[] _methodArgs, final Class _expectedExceptionClass )208cdf0e10cSrcweir     protected void assureException( final Object _object, final String _methodName, final Class[] _argClasses,
209cdf0e10cSrcweir         final Object[] _methodArgs, final Class _expectedExceptionClass )
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         assureException(
212cdf0e10cSrcweir             "did not catch the expected exception (" +
213cdf0e10cSrcweir                 ( ( _expectedExceptionClass == null ) ? "none" : _expectedExceptionClass.getName() ) +
214cdf0e10cSrcweir                 ") while calling " + _object.getClass().getName() + "." + _methodName,
215cdf0e10cSrcweir             _object, _methodName, _argClasses, _methodArgs, _expectedExceptionClass );
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
assureException( Object _object, Class _unoInterfaceClass, String _methodName, Object[] _methodArgs, Class _expectedExceptionClass )219cdf0e10cSrcweir     protected void assureException( Object _object, Class _unoInterfaceClass, String _methodName, Object[] _methodArgs,
220cdf0e10cSrcweir         Class _expectedExceptionClass )
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         assureException( UnoRuntime.queryInterface( _unoInterfaceClass, _object ), _methodName,
223cdf0e10cSrcweir             _methodArgs, _expectedExceptionClass );
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
getMSF()227cdf0e10cSrcweir     protected XMultiServiceFactory getMSF()
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
230cdf0e10cSrcweir         return xMSF1;
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
234cdf0e10cSrcweir     // setup and close connections
235cdf0e10cSrcweir     @BeforeClass
setUpConnection()236cdf0e10cSrcweir     public static void setUpConnection() throws Exception
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir         connection.setUp();
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     // --------------------------------------------------------------------------------------------------------
242cdf0e10cSrcweir     @AfterClass
tearDownConnection()243cdf0e10cSrcweir     public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         connection.tearDown();
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
249cdf0e10cSrcweir 
250cdf0e10cSrcweir }
251