xref: /AOO42X/main/qadevOOo/tests/java/mod/_sc/ScTableConditionalEntry.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 import util.utils;
38 
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.container.XIndexAccess;
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.sheet.ConditionOperator;
45 import com.sun.star.sheet.XSheetConditionalEntries;
46 import com.sun.star.sheet.XSheetConditionalEntry;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.sheet.XSpreadsheets;
50 import com.sun.star.table.CellAddress;
51 import com.sun.star.uno.AnyConverter;
52 import com.sun.star.uno.Type;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 
56 /**
57 * Test for object which is represented by service
58 * <code>com.sun.star.sheet.TableConditionalEntry</code>. <p>
59 * Object implements the following interfaces :
60 * <ul>
61 *  <li> <code>com::sun::star::sheet::XSheetConditionalEntry</code></li>
62 *  <li> <code>com::sun::star::sheet::XSheetCondition</code></li>
63 * </ul>
64 * @see com.sun.star.sheet.TableConditionalEntry
65 * @see com.sun.star.sheet.XSheetConditionalEntry
66 * @see com.sun.star.sheet.XSheetCondition
67 * @see ifc.sheet._XSheetConditionalEntry
68 * @see ifc.sheet._XSheetCondition
69 */
70 public class ScTableConditionalEntry extends TestCase {
71     static XSpreadsheetDocument xSpreadsheetDoc = null;
72 
73     /**
74     * Creates Spreadsheet document.
75     */
76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         // get a soffice factory object
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
79 
80         try {
81             log.println( "creating a sheetdocument" );
82             xSpreadsheetDoc = SOF.createCalcDoc(null);;
83         } catch (com.sun.star.uno.Exception e) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91     * Disposes Spreadsheet document.
92     */
93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xSheetDoc " );
95         XComponent oComp = (XComponent)
96             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
97         util.DesktopTools.closeDoc(oComp);
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Retrieves a collection of spreadsheets from the document and takes one of
103     * them. Fills some cells of the spreadsheet. Retrieves value of the property
104     * <code>'ConditionalFormat'</code> that is the collection of the conditions
105     * of a conditional format. Adds new conditional entry to the collection
106     * using the interface <code>XSheetConditionalEntries</code>. Sets new value
107     * of the property <code>'ConditionalFormat'</code>. Obtains the conditional
108     * entry with index 0 that is the instance of the service
109     * <code>com.sun.star.sheet.TableConditionalEntry</code>.
110     * @see com.sun.star.sheet.XSheetConditionalEntries
111     * @see com.sun.star.sheet.TableConditionalEntry
112     */
113     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
114 
115         XInterface oObj = null;
116         log.println("getting sheets");
117         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
118 
119         log.println("getting a sheet");
120         XSpreadsheet oSheet = null;
121         XIndexAccess oIndexAccess = (XIndexAccess)
122             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
123 
124         try {
125             oSheet = (XSpreadsheet) AnyConverter.toObject(
126                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
127         } catch (com.sun.star.lang.WrappedTargetException e) {
128             e.printStackTrace(log);
129             throw new StatusException( "Couldn't get a spreadsheet", e);
130         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
131             e.printStackTrace(log);
132             throw new StatusException( "Couldn't get a spreadsheet", e);
133         } catch (com.sun.star.lang.IllegalArgumentException e) {
134             e.printStackTrace(log);
135             throw new StatusException( "Couldn't get a spreadsheet", e);
136         }
137 
138         log.println("filling some cells");
139         try {
140             oSheet.getCellByPosition(5, 5).setValue(15);
141             oSheet.getCellByPosition(1, 4).setValue(10);
142             oSheet.getCellByPosition(2, 0).setValue(-5.15);
143         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
144             log.println("Exception occurred while filling cells");
145             e.printStackTrace(log);
146         }
147 
148         Object CFormat = null;
149         XPropertySet Props = null;
150 
151         try {
152             Props = (XPropertySet)
153                 UnoRuntime.queryInterface(XPropertySet.class, oSheet);
154             CFormat = Props.getPropertyValue("ConditionalFormat");
155             if (utils.isVoid(CFormat)) {
156                 log.println("Property 'ConditionalFormat' is void");
157             }
158         } catch (com.sun.star.lang.WrappedTargetException e){
159             e.printStackTrace(log);
160             throw new StatusException("Couldn't create instance", e);
161         } catch (com.sun.star.beans.UnknownPropertyException e){
162             e.printStackTrace(log);
163             throw new StatusException("Couldn't create instance", e);
164         }
165 
166         try {
167             XSheetConditionalEntries xSCE = (XSheetConditionalEntries)
168                 UnoRuntime.queryInterface(XSheetConditionalEntries.class, CFormat);
169             xSCE.addNew(Conditions());
170             Props.setPropertyValue("ConditionalFormat", xSCE);
171             oObj = (XSheetConditionalEntry) AnyConverter.toObject(
172                     new Type(XSheetConditionalEntry.class),xSCE.getByIndex(0));
173         } catch (com.sun.star.lang.WrappedTargetException e) {
174             e.printStackTrace(log);
175             throw new StatusException(
176                 "Exception occurred while getting Entry", e);
177         } catch (com.sun.star.lang.IllegalArgumentException e) {
178             e.printStackTrace(log);
179             throw new StatusException(
180                 "Exception occurred while getting Entry", e);
181         } catch (com.sun.star.beans.PropertyVetoException e) {
182             e.printStackTrace(log);
183             throw new StatusException(
184                 "Exception occurred while getting Entry", e);
185         } catch (com.sun.star.beans.UnknownPropertyException e) {
186             e.printStackTrace(log);
187             throw new StatusException(
188                 "Exception occurred while getting Entry", e);
189         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
190             e.printStackTrace(log);
191             throw new StatusException(
192                 "Exception occurred while getting Entry", e);
193         }
194 
195         log.println("creating a new environment for object");
196         TestEnvironment tEnv = new TestEnvironment(oObj);
197 
198         return tEnv;
199     } // finish method getTestEnvironment
200 
201     /**
202     * Returns the array of the property values that was filled by condition
203     * values.
204     */
205     protected PropertyValue[] Conditions() {
206         PropertyValue[] con = new PropertyValue[5];
207         CellAddress ca = new CellAddress();
208         ca.Column = 1;
209         ca.Row = 5;
210         ca.Sheet = 0;
211         con[0] = new PropertyValue();
212         con[0].Name = "StyleName";
213         con[0].Value = "Result2";
214         con[1] = new PropertyValue();
215         con[1].Name = "Formula1";
216         con[1].Value = "$Sheet1.$B$5";
217         con[2] = new PropertyValue();
218         con[2].Name = "Formula2";
219         con[2].Value = "";
220         con[3] = new PropertyValue();
221         con[3].Name = "Operator";
222         con[3].Value = ConditionOperator.EQUAL;
223         con[4] = new PropertyValue();
224         con[4].Name = "SourcePosition";
225         con[4].Value = ca;
226         return con;
227     }
228 
229 }    // finish class ScTableConditionalEntry
230 
231