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 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.container.XIndexAccess;
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.XSpreadsheet;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.sheet.XSpreadsheets;
45 import com.sun.star.table.XColumnRowRange;
46 import com.sun.star.table.XTableColumns;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 
52 /**
53 * Test for object which is represented by service
54 * <code>com.sun.star.table.TableColumn</code>. <p>
55 * Object implements the following interfaces :
56 * <ul>
57 *  <li> <code>com::sun::star::container::XNamed</code></li>
58 *  <li> <code>com::sun::star::table::TableColumn</code></li>
59 *  <li> <code>com::sun::star::table::XCellRange</code></li>
60 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
61 * </ul>
62 * @see com.sun.star.container.XNamed
63 * @see com.sun.star.table.TableColumn
64 * @see com.sun.star.table.XCellRange
65 * @see com.sun.star.beans.XPropertySet
66 * @see ifc.container._XNamed
67 * @see ifc.table._TableColumn
68 * @see ifc.table._XCellRange
69 * @see ifc.beans._XPropertySet
70 */
71 public class ScTableColumnObj extends TestCase {
72     static XSpreadsheetDocument xSheetDoc = null;
73 
74     /**
75     * Creates Spreadsheet document.
76     */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         // get a soffice factory object
79         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
80 
81         try {
82             log.println( "creating a sheetdocument" );
83             xSheetDoc = SOF.createCalcDoc(null);;
84         } catch (com.sun.star.uno.Exception e) {
85             // Some exception occures.FAILED
86             e.printStackTrace( log );
87             throw new StatusException( "Couldn't create document", e );
88         }
89     }
90 
91     /**
92     * Disposes Spreadsheet document.
93     */
94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing xSheetDoc " );
96         XComponent oComp = (XComponent)
97             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
98         util.DesktopTools.closeDoc(oComp);
99     }
100 
101     /**
102     * Creating a Testenvironment for the interfaces to be tested.
103     * Retrieves a collection of spreadsheets from the document and takes one of
104     * them. Obtaines the collection of columns in the range using the interface
105     * <code>XColumnRowRange</code>. Retrieves the column with index 10 that is
106     * the instance of the service <code>com.sun.star.table.TableColumn</code>.
107     * Object relations created :
108     * <ul>
109     *  <li> <code>'setName'</code> for
110     *      {@link ifc.container._XNamed} </li>
111     *  <li> <code>'ValidRange'</code> for
112     *      {@link ifc.table._XCellRange} </li>
113     * </ul>
114     * @see com.sun.star.table.XColumnRowRange
115     * @see com.sun.star.table.TableColumn
116     */
117     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
118 
119         XInterface oObj = null;
120 
121         // creation of the testobject here
122         // first we write what we are intend to do to log file
123         log.println("creating a test environment");
124 
125         XSpreadsheet xSpreadsheet = null;
126 
127         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
128         XNameAccess oNames = (XNameAccess)
129             UnoRuntime.queryInterface( XNameAccess.class, xSpreadsheets );
130         try {
131             xSpreadsheet = (XSpreadsheet) AnyConverter.toObject(
132                 new Type(XSpreadsheet.class),
133                     oNames.getByName(oNames.getElementNames()[0]));
134         } catch (com.sun.star.lang.WrappedTargetException e) {
135             e.printStackTrace(log);
136             throw new StatusException("Couldn't get element by name", e);
137         } catch (com.sun.star.container.NoSuchElementException e) {
138             e.printStackTrace(log);
139             throw new StatusException("Couldn't get element by name", e);
140         } catch (com.sun.star.lang.IllegalArgumentException e) {
141             e.printStackTrace(log);
142             throw new StatusException("Couldn't get element by name", e);
143         }
144 
145         XColumnRowRange oColumnRowRange = (XColumnRowRange)
146             UnoRuntime.queryInterface(XColumnRowRange.class, xSpreadsheet);
147         XTableColumns oColumns = (XTableColumns) oColumnRowRange.getColumns();
148         XIndexAccess oIndexAccess = (XIndexAccess)
149             UnoRuntime.queryInterface(XIndexAccess.class, oColumns);
150         try {
151             oObj = (XInterface) AnyConverter.toObject(
152                     new Type(XInterface.class),oIndexAccess.getByIndex(10));
153         } catch (com.sun.star.lang.WrappedTargetException e) {
154             e.printStackTrace(log);
155             throw new StatusException("Couldn't get by index", e);
156         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
157             e.printStackTrace(log);
158             throw new StatusException("Couldn't get by index", e);
159         } catch (com.sun.star.lang.IllegalArgumentException e) {
160             e.printStackTrace(log);
161             throw new StatusException("Couldn't get by index", e);
162         }
163 
164         log.println("creating a new environment for object");
165         TestEnvironment tEnv = new TestEnvironment(oObj);
166 
167         // a valid Range for XCellRange
168         tEnv.addObjRelation("ValidRange","K1:K1");
169 
170         //since TableColumnsNames are fixed XNamed::setName() should always be OK
171         tEnv.addObjRelation("setName",new Boolean(true));
172         return tEnv;
173     }
174 }
175 
176 
177