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