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 package mod._sc;
25 
26 import java.io.PrintWriter;
27 import java.util.Random;
28 
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.SOfficeFactory;
34 
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 
42 public class ScFunctionDescriptionObj extends TestCase {
43     static XSpreadsheetDocument xSheetDoc = null;
44 
initialize( TestParameters tParam, PrintWriter log )45     protected void initialize( TestParameters tParam, PrintWriter log ) {
46         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) tParam.getMSF() );
47 
48         try {
49             log.println( "creating a Spreadsheet document" );
50             xSheetDoc = SOF.createCalcDoc(null);
51         } catch ( com.sun.star.uno.Exception e ) {
52             // Some exception occured.FAILED
53             e.printStackTrace( log );
54             throw new StatusException( "Couldn't create document", e );
55         }
56 
57     }
58 
cleanup( TestParameters tParam, PrintWriter log )59     protected void cleanup( TestParameters tParam, PrintWriter log ) {
60         log.println( "    disposing xSheetDoc " );
61         XComponent oComp = (XComponent) UnoRuntime.
62                         queryInterface (XComponent.class, xSheetDoc) ;
63         util.DesktopTools.closeDoc(oComp);
64     }
65 
66 
67     /**
68      *    creating a Testenvironment for the interfaces to be tested
69      */
createTestEnvironment( TestParameters Param, PrintWriter log )70     public synchronized TestEnvironment createTestEnvironment(
71                                     TestParameters Param, PrintWriter log )
72                                                        throws StatusException {
73 
74         XInterface oObj = null;
75 
76         // creation of testobject here
77         // first we write what we are intend to do to log file
78         log.println( "Creating a test environment" );
79 
80         try {
81             log.println("Getting test object ") ;
82 
83             XMultiServiceFactory oDocMSF =  (XMultiServiceFactory) Param.getMSF();
84 
85             XInterface FDs = (XInterface)oDocMSF.
86                     createInstance("com.sun.star.sheet.FunctionDescriptions");
87             XNameAccess NA = (XNameAccess)UnoRuntime.queryInterface
88                 (XNameAccess.class, FDs);
89 
90             String names[] = NA.getElementNames();
91             Random rnd = new Random();
92             int idx = rnd.nextInt(names.length);
93 
94             oObj = (XInterface)NA.getByName(names[idx]);
95 
96             log.println("Creating object - " +
97                                         ((oObj == null) ? "FAILED" : "OK"));
98 
99         } catch (Exception e) {
100             e.printStackTrace(log) ;
101             throw new StatusException
102                 ("Error getting test object from spreadsheet document",e) ;
103         }
104 
105         TestEnvironment tEnv = new TestEnvironment( oObj );
106 
107         // Other parameters required for interface tests
108 
109         return tEnv;
110     }
111 
112 }
113 
114 
115