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.container.XNameAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.sheet.XSpreadsheets;
40 import com.sun.star.table.XCellRange;
41 import com.sun.star.table.XColumnRowRange;
42 import com.sun.star.table.XTableColumns;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.table.TableColumns</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
54 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
55 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
56 *  <li> <code>com::sun::star::table::XTableColumns</code></li>
57 * </ul>
58 * @see com.sun.star.table.TableColumns
59 * @see com.sun.star.container.XIndexAccess
60 * @see com.sun.star.container.XElementAccess
61 * @see com.sun.star.container.XNameAccess
62 * @see com.sun.star.table.XTableColumns
63 * @see ifc.container._XIndexAccess
64 * @see ifc.container._XElementAccess
65 * @see ifc.container._XNameAccess
66 * @see ifc.table._XTableColumns
67 */
68 public class ScTableColumnsObj extends TestCase {
69     static XSpreadsheetDocument xSheetDoc = null;
70 
71     /**
72     * Creates Spreadsheet document.
73     */
initialize( TestParameters tParam, PrintWriter log )74     protected void initialize( TestParameters tParam, PrintWriter log ) {
75         // get a soffice factory object
76         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
77 
78         try {
79             log.println( "creating a sheetdocument" );
80             xSheetDoc = SOF.createCalcDoc(null);
81         } catch (com.sun.star.uno.Exception e) {
82             // Some exception occures.FAILED
83             e.printStackTrace( log );
84             throw new StatusException( "Couldn't create document", e );
85         }
86     }
87 
88     /**
89     * Disposes Spreadsheet document.
90     */
cleanup( TestParameters tParam, PrintWriter log )91     protected void cleanup( TestParameters tParam, PrintWriter log ) {
92         log.println( "    disposing xSheetDoc " );
93         XComponent oComp = (XComponent)
94             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
95         util.DesktopTools.closeDoc(oComp);
96     }
97 
98     /**
99     * Creating a Testenvironment for the interfaces to be tested.
100     * Retrieves a collection of spreadsheets from the document and takes one of
101     * them. Obtaines the collection of columns in the range using the interface
102     * <code>XColumnRowRange</code>. This collection is the instance of the
103     * service <code>com.sun.star.table.TableColumns</code>.
104     * @see com.sun.star.table.XColumnRowRange
105     * @see com.sun.star.table.TableColumns
106     */
createTestEnvironment(TestParameters Param, PrintWriter log)107     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
108 
109         XInterface oObj = null;
110 
111         // creation of the testobject here
112         // first we write what we are intend to do to log file
113         log.println("craeting a test environment");
114 
115         log.println("getting column");
116         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
117         XNameAccess oNames = (XNameAccess)
118             UnoRuntime.queryInterface( XNameAccess.class, xSpreadsheets );
119         XSpreadsheet xSpreadsheet = null;
120         try {
121             xSpreadsheet = (XSpreadsheet) AnyConverter.toObject(
122                 new Type(XSpreadsheet.class),
123                     oNames.getByName(oNames.getElementNames()[0]));
124         } catch (com.sun.star.lang.WrappedTargetException e) {
125             e.printStackTrace(log);
126             throw new StatusException("Couldn't get spreadsheet", e);
127         } catch (com.sun.star.container.NoSuchElementException e) {
128             e.printStackTrace(log);
129             throw new StatusException("Couldn't get spreadsheet", e);
130         } catch (com.sun.star.lang.IllegalArgumentException e) {
131             e.printStackTrace(log);
132             throw new StatusException("Couldn't get spreadsheet", e);
133         }
134 
135         XColumnRowRange oColumnRowRange = (XColumnRowRange)
136             UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
137         XTableColumns oColumns = (XTableColumns) oColumnRowRange.getColumns();
138         oObj = oColumns;
139 
140         log.println("creating a new environment for object");
141         TestEnvironment tEnv = new TestEnvironment(oObj);
142 
143         // adding relation for XTableColumns
144         tEnv.addObjRelation("XTableColumns.XCellRange",
145             UnoRuntime.queryInterface(XCellRange.class, xSpreadsheet));
146 
147         return tEnv;
148     }
149 }    // finish class ScTableColumnsObj
150