bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScStyleObj.java
blob159feb1875da9c1c9c81c01022a1fbe37f0fa5f3
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
24 import lib.StatusException;
25 import lib.TestCase;
26 import lib.TestEnvironment;
27 import lib.TestParameters;
28 import util.SOfficeFactory;
30 import com.sun.star.beans.Property;
31 import com.sun.star.beans.PropertyAttribute;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.container.XIndexAccess;
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.container.XNameContainer;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.style.XStyle;
42 import com.sun.star.style.XStyleFamiliesSupplier;
43 import com.sun.star.table.XCell;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.style.Style</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 * <li> <code>com::sun::star::container::XNamed</code></li>
55 * <li> <code>com::sun::star::style::Style</code></li>
56 * <li> <code>com::sun::star::style::XStyle</code></li>
57 * </ul>
58 * @see com.sun.star.style.Style
59 * @see com.sun.star.container.XNamed
60 * @see com.sun.star.style.Style
61 * @see com.sun.star.style.XStyle
62 * @see ifc.container._XNamed
63 * @see ifc.style._Style
64 * @see ifc.style._XStyle
66 public class ScStyleObj extends TestCase {
67 private XSpreadsheetDocument xSpreadsheetDoc = null;
69 /**
70 * Creates Spreadsheet document.
72 protected void initialize( TestParameters tParam, PrintWriter log ) {
73 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
75 try {
76 log.println( "creating a Spreadsheet document" );
77 xSpreadsheetDoc = SOF.createCalcDoc(null);
78 } catch ( com.sun.star.uno.Exception e ) {
79 // Some exception occurs.FAILED
80 e.printStackTrace( log );
81 throw new StatusException( "Couldn't create document", e );
85 /**
86 * Disposes Spreadsheet document.
88 protected void cleanup( TestParameters tParam, PrintWriter log ) {
89 log.println( " disposing xSheetDoc " );
90 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
91 util.DesktopTools.closeDoc(oComp);
94 /**
95 * Creating a Testenvironment for the interfaces to be tested.
96 * Retrieves the collection of style families from the document
97 * using the interface <code>XStyleFamiliesSupplier</code>.
98 * Obtains style family with index 0 from the collection and obtains
99 * style with index 0 from this style family. Creates the instance of the
100 * service <code>com.sun.star.style.CellStyle</code>. Inserts the created
101 * style to the obtained style family. Retrieves a collection of spreadsheets
102 * from the document and takes one of them. Gets a cell from the spreadsheet
103 * and sets the value of property <code>'CellStyle'</code> to the created
104 * style. The created style is the instance of the service
105 * <code>com.sun.star.style.Style</code> also.
106 * Object relations created :
107 * <ul>
108 * <li> <code>'PoolStyle'</code> for
109 * {@link ifc.style._XStyle}(the style with index 0 that was obtained
110 * from the collection)</li>
111 * </ul>
112 * @see com.sun.star.style.CellStyle
113 * @see com.sun.star.style.Style
114 * @see com.sun.star.style.XStyleFamiliesSupplier
116 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
119 // creation of the testobject here
120 // first we write what we are intend to do to log file
122 log.println("creating a test environment");
124 log.println("getting style");
125 XStyleFamiliesSupplier oStyleFamiliesSupplier = UnoRuntime.queryInterface(
126 XStyleFamiliesSupplier.class, xSpreadsheetDoc);
127 XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
128 XIndexAccess oStyleFamiliesIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, oStyleFamilies);
129 XNameAccess oStyleFamilyNameAccess = null;
130 XStyle oStyle = null;
131 try {
132 oStyleFamilyNameAccess = (XNameAccess) AnyConverter.toObject(
133 new Type(XNameAccess.class),
134 oStyleFamiliesIndexAccess.getByIndex(0));
136 XIndexAccess oStyleFamilyIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class,
137 oStyleFamilyNameAccess);
138 oStyle = (XStyle) AnyConverter.toObject(
139 new Type(XStyle.class),oStyleFamilyIndexAccess.getByIndex(0));
140 } catch(com.sun.star.lang.WrappedTargetException e) {
141 e.printStackTrace(log);
142 throw new StatusException("Couldn't get by index", e);
143 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
144 e.printStackTrace(log);
145 throw new StatusException("Couldn't get by index", e);
146 } catch(com.sun.star.lang.IllegalArgumentException e) {
147 e.printStackTrace(log);
148 throw new StatusException("Couldn't get by index", e);
151 log.println("Creating a user-defined style");
152 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(
153 XMultiServiceFactory.class, xSpreadsheetDoc);
155 XInterface oInt = null;
156 try {
157 oInt = (XInterface)
158 oMSF.createInstance("com.sun.star.style.CellStyle");
159 } catch(com.sun.star.uno.Exception e) {
160 e.printStackTrace(log);
161 throw new StatusException("Couldn't create instance", e);
163 XStyle oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
165 XNameContainer oStyleFamilyNameContainer = UnoRuntime.
166 queryInterface(XNameContainer.class, oStyleFamilyNameAccess);
168 try {
169 if (oStyleFamilyNameContainer.hasByName("My Style")) {
170 oStyleFamilyNameContainer.removeByName("My Style");
173 oStyleFamilyNameContainer.insertByName("My Style", oMyStyle);
174 } catch(com.sun.star.lang.WrappedTargetException e) {
175 e.printStackTrace(log);
176 throw new StatusException("Couldn't create test environment", e);
177 } catch(com.sun.star.container.NoSuchElementException e) {
178 e.printStackTrace(log);
179 throw new StatusException("Couldn't create test environment", e);
180 } catch(com.sun.star.container.ElementExistException e) {
181 e.printStackTrace(log);
182 throw new StatusException("Couldn't create test environment", e);
183 } catch(com.sun.star.lang.IllegalArgumentException e) {
184 e.printStackTrace(log);
185 throw new StatusException("Couldn't create test environment", e);
189 //using the style
190 log.println("Getting spreadsheet") ;
191 XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
192 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
194 XCell aCell = null;
195 try {
196 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
197 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
198 log.println("Getting a cell from sheet") ;
199 aCell = oSheet.getCellByPosition(2,3) ;
200 } catch(com.sun.star.lang.WrappedTargetException e) {
201 e.printStackTrace(log);
202 throw new StatusException("Couldn't get spreadsheet by index", e);
203 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
204 e.printStackTrace(log);
205 throw new StatusException("Couldn't get spreadsheet by index", e);
206 } catch(com.sun.star.lang.IllegalArgumentException e) {
207 e.printStackTrace(log);
208 throw new StatusException("Couldn't get spreadsheet by index", e);
211 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, aCell);
213 try {
214 xProp.setPropertyValue("CellStyle", oMyStyle.getName());
215 } catch(com.sun.star.lang.WrappedTargetException e) {
216 e.printStackTrace(log);
217 throw new StatusException("Couldn't set property CellStyle", e);
218 } catch(com.sun.star.lang.IllegalArgumentException e) {
219 e.printStackTrace(log);
220 throw new StatusException("Couldn't set property CellStyle", e);
221 } catch(com.sun.star.beans.PropertyVetoException e) {
222 e.printStackTrace(log);
223 throw new StatusException("Couldn't set property CellStyle", e);
224 } catch(com.sun.star.beans.UnknownPropertyException e) {
225 e.printStackTrace(log);
226 throw new StatusException("Couldn't set property CellStyle", e);
229 log.println("creating a new environment for object");
230 TestEnvironment tEnv = new TestEnvironment(oMyStyle);
232 tEnv.addObjRelation("PoolStyle", oStyle);
234 tEnv.addObjRelation("PropertyNames", getPropertyNames
235 (UnoRuntime.queryInterface
236 (XPropertySet.class, oMyStyle)));
238 return tEnv;
241 public String[] getPropertyNames(XPropertySet props) {
242 Property[] the_props = props.getPropertySetInfo().getProperties();
243 ArrayList<String> names = new ArrayList<String>() ;
245 for (int i=0;i<the_props.length;i++) {
246 boolean isWritable =
247 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
248 if (isWritable) {
249 names.add(the_props[i].Name);
252 return names.toArray(new String[names.size()]) ;
254 } // finish class ScStyleObj