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 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.sheet.XSpreadsheet;
37 import com.sun.star.sheet.XSpreadsheetDocument;
38 import com.sun.star.sheet.XSpreadsheets;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 
42 /**
43 * Test for object which is represented by service
44 * <code>com.sun.star.sheet.Spreadsheets</code>. <p>
45 * Object implements the following interfaces :
46 * <ul>
47 *  <li> <code>com::sun::star::sheet::XSpreadsheets</code></li>
48 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
49 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
50 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
51 *  <li> <code>com::sun::star::container::XNameReplace</code></li>
52 *  <li> <code>com::sun::star::container::XNameContainer</code></li>
53 * </ul>
54 * @see com.sun.star.sheet.Spreadsheets
55 * @see com.sun.star.sheet.XSpreadsheets
56 * @see com.sun.star.container.XNameAccess
57 * @see com.sun.star.container.XIndexAccess
58 * @see com.sun.star.container.XElementAccess
59 * @see com.sun.star.container.XNameReplace
60 * @see com.sun.star.container.XNameContainer
61 * @see ifc.sheet._XSpreadsheets
62 * @see ifc.container._XNameAccess
63 * @see ifc.container._XIndexAccess
64 * @see ifc.container._XElementAccess
65 * @see ifc.container._XNameReplace
66 * @see ifc.container._XNameContainer
67 */
68 public class ScTableSheetsObj extends TestCase {
69     private static XSpreadsheetDocument xSpreadsheetDoc = null;
70 
71     /**
72     * Creates Spreadsheet document.
73     */
initialize( TestParameters Param, PrintWriter log )74     public void initialize( TestParameters Param, PrintWriter log ) {
75         // get a soffice factory object
76 
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
78         try {
79             log.println("creating a spreadsheetdocument");
80             xSpreadsheetDoc = SOF.createCalcDoc(null);
81         } catch (com.sun.star.uno.Exception e) {
82             e.printStackTrace( log );
83             throw new StatusException( "Couldn't create document ", e );
84         }
85     }
86 
87     /**
88     * Disposes Spreadsheet document.
89     */
cleanup( TestParameters tParam, PrintWriter log )90     protected void cleanup( TestParameters tParam, PrintWriter log ) {
91         log.println("disposing xSpreadsheetDocument");
92         XComponent oComp = (XComponent)
93             UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
94         util.DesktopTools.closeDoc(oComp);
95     }
96 
97     /**
98     * Creating a Testenvironment for the interfaces to be tested.
99     * Retrieves the collection of the spreadsheets using the interface
100     * <code>XSpreadsheetDocument</code>. The retrieved collection is the
101     * instance of the service <code>com.sun.star.sheet.Spreadsheets</code>.
102     * Creates instances of the service <code>com.sun.star.sheet.Spreadsheet</code>.
103     * Object relations created :
104     * <ul>
105     *  <li> <code>'SecondInstance'</code> for
106     *      {@link ifc.container._XNameContainer} (the created instance of the
107     *       service <code>com.sun.star.sheet.Spreadsheet</code>) </li>
108     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
109     *      {@link ifc.container._XNameContainer} (the created instances of the
110     *       service <code>com.sun.star.sheet.Spreadsheet</code>) </li>
111     * </ul>
112     * @see com.sun.star.sheet.Spreadsheets
113     * @see com.sun.star.sheet.XSpreadsheetDocument
114     * @see com.sun.star.sheet.Spreadsheet
115     */
createTestEnvironment(TestParameters Param, PrintWriter log)116     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
117 
118         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
119 
120         log.println("getting sheets");
121         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
122 
123         XInterface oObj = (XInterface)
124             UnoRuntime.queryInterface(XInterface.class, xSpreadsheets);
125 
126         log.println("creating a new environment for object");
127         TestEnvironment tEnv = new TestEnvironment(oObj);
128 
129         log.println("creating instance of the sheet object to use it in tests");
130         XSpreadsheet oSecondSheet = null;
131 
132         try {
133             oSecondSheet = SOF.createSpreadsheet(xSpreadsheetDoc);
134         } catch (com.sun.star.uno.Exception e) {
135             e.printStackTrace(log);
136             throw new StatusException("Couldn't create instances", e);
137         }
138 
139         //adding Instance for XNameContainer
140         tEnv.addObjRelation("SecondInstance",oSecondSheet);
141 
142         // INSTANCEn : _XNameContainer; _XNameReplace
143         log.println( "adding INSTANCEn as mod relation to environment" );
144 
145         int THRCNT = 1;
146         if ((String)Param.get("THRCNT") != null) {
147             THRCNT = Integer.parseInt((String)Param.get("THRCNT"));
148         }
149         try {
150             for (int n = 1; n < (THRCNT+1) ;n++ ) {
151                 log.println(
152                     "adding INSTANCE" + n +" as mod relation to environment" );
153                 tEnv.addObjRelation(
154                     "INSTANCE" + n,
155                     SOF.createSpreadsheet(xSpreadsheetDoc) );
156                 }
157         } catch (com.sun.star.uno.Exception e) {
158             e.printStackTrace(log);
159             throw new StatusException(
160                 "Couldn't create instances for object relations", e);
161         }
162 
163         return tEnv;
164     }
165 }
166 
167 
168