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