1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 import java.util.Random;
32 
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.SOfficeFactory;
38 
39 import com.sun.star.container.XNameAccess;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.sheet.XSpreadsheetDocument;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 
46 public class ScFunctionDescriptionObj extends TestCase {
47     static XSpreadsheetDocument xSheetDoc = null;
48 
49     protected void initialize( TestParameters tParam, PrintWriter log ) {
50         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) tParam.getMSF() );
51 
52         try {
53             log.println( "creating a Spreadsheet document" );
54             xSheetDoc = SOF.createCalcDoc(null);
55         } catch ( com.sun.star.uno.Exception e ) {
56             // Some exception occures.FAILED
57             e.printStackTrace( log );
58             throw new StatusException( "Couldn't create document", e );
59         }
60 
61     }
62 
63     protected void cleanup( TestParameters tParam, PrintWriter log ) {
64         log.println( "    disposing xSheetDoc " );
65         XComponent oComp = (XComponent) UnoRuntime.
66                         queryInterface (XComponent.class, xSheetDoc) ;
67         util.DesktopTools.closeDoc(oComp);
68     }
69 
70 
71     /**
72      *    creating a Testenvironment for the interfaces to be tested
73      */
74     public synchronized TestEnvironment createTestEnvironment(
75                                     TestParameters Param, PrintWriter log )
76                                                        throws StatusException {
77 
78         XInterface oObj = null;
79 
80         // creation of testobject here
81         // first we write what we are intend to do to log file
82         log.println( "Creating a test environment" );
83 
84         try {
85             log.println("Getting test object ") ;
86 
87             XMultiServiceFactory oDocMSF =  (XMultiServiceFactory) Param.getMSF();
88 
89             XInterface FDs = (XInterface)oDocMSF.
90                     createInstance("com.sun.star.sheet.FunctionDescriptions");
91             XNameAccess NA = (XNameAccess)UnoRuntime.queryInterface
92                 (XNameAccess.class, FDs);
93 
94             String names[] = NA.getElementNames();
95             Random rnd = new Random();
96             int idx = rnd.nextInt(names.length);
97 
98             oObj = (XInterface)NA.getByName(names[idx]);
99 
100             log.println("Creating object - " +
101                                         ((oObj == null) ? "FAILED" : "OK"));
102 
103         } catch (Exception e) {
104             e.printStackTrace(log) ;
105             throw new StatusException
106                 ("Error getting test object from spreadsheet document",e) ;
107         }
108 
109         TestEnvironment tEnv = new TestEnvironment( oObj );
110 
111         // Other parameters required for interface tests
112 
113         return tEnv;
114     }
115 
116 }
117 
118 
119