merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScStyleObj.java
blob0d0579cc2afee49e8bb56ba979bb4129c3450fab
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScStyleObj.java,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod._sc;
33 import java.io.PrintWriter;
34 import java.util.Vector;
36 import lib.StatusException;
37 import lib.TestCase;
38 import lib.TestEnvironment;
39 import lib.TestParameters;
40 import util.SOfficeFactory;
42 import com.sun.star.beans.Property;
43 import com.sun.star.beans.PropertyAttribute;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.container.XIndexAccess;
46 import com.sun.star.container.XNameAccess;
47 import com.sun.star.container.XNameContainer;
48 import com.sun.star.lang.XComponent;
49 import com.sun.star.lang.XMultiServiceFactory;
50 import com.sun.star.sheet.XSpreadsheet;
51 import com.sun.star.sheet.XSpreadsheetDocument;
52 import com.sun.star.sheet.XSpreadsheets;
53 import com.sun.star.style.XStyle;
54 import com.sun.star.style.XStyleFamiliesSupplier;
55 import com.sun.star.table.XCell;
56 import com.sun.star.uno.AnyConverter;
57 import com.sun.star.uno.Type;
58 import com.sun.star.uno.UnoRuntime;
59 import com.sun.star.uno.XInterface;
61 /**
62 * Test for object which is represented by service
63 * <code>com.sun.star.style.Style</code>. <p>
64 * Object implements the following interfaces :
65 * <ul>
66 * <li> <code>com::sun::star::container::XNamed</code></li>
67 * <li> <code>com::sun::star::style::Style</code></li>
68 * <li> <code>com::sun::star::style::XStyle</code></li>
69 * </ul>
70 * @see com.sun.star.style.Style
71 * @see com.sun.star.container.XNamed
72 * @see com.sun.star.style.Style
73 * @see com.sun.star.style.XStyle
74 * @see ifc.container._XNamed
75 * @see ifc.style._Style
76 * @see ifc.style._XStyle
78 public class ScStyleObj extends TestCase {
79 static XSpreadsheetDocument xSpreadsheetDoc = null;
81 /**
82 * Creates Spreadsheet document.
84 protected void initialize( TestParameters tParam, PrintWriter log ) {
85 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
87 try {
88 log.println( "creating a Spreadsheet document" );
89 xSpreadsheetDoc = SOF.createCalcDoc(null);
90 } catch ( com.sun.star.uno.Exception e ) {
91 // Some exception occures.FAILED
92 e.printStackTrace( log );
93 throw new StatusException( "Couldn't create document", e );
97 /**
98 * Disposes Spreadsheet document.
100 protected void cleanup( TestParameters tParam, PrintWriter log ) {
101 log.println( " disposing xSheetDoc " );
102 XComponent oComp = (XComponent)
103 UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
104 util.DesktopTools.closeDoc(oComp);
108 * Creating a Testenvironment for the interfaces to be tested.
109 * Retrieves the collection of style families from the document
110 * using the interface <code>XStyleFamiliesSupplier</code>.
111 * Obtains style family with index 0 from the collection and obtains
112 * style with index 0 from this style family. Creates the instance of the
113 * service <code>com.sun.star.style.CellStyle</code>. Inserts the created
114 * style to the obtained style family. Retrieves a collection of spreadsheets
115 * from the document and takes one of them. Gets a cell from the spreadsheet
116 * and sets the value of property <code>'CellStyle'</code> to the created
117 * style. The created style is the instance of the service
118 * <code>com.sun.star.style.Style</code> also.
119 * Object relations created :
120 * <ul>
121 * <li> <code>'PoolStyle'</code> for
122 * {@link ifc.style._XStyle}(the style with index 0 that was obtained
123 * from the collection)</li>
124 * </ul>
125 * @see com.sun.star.style.CellStyle
126 * @see com.sun.star.style.Style
127 * @see com.sun.star.style.XStyleFamiliesSupplier
129 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
132 // creation of the testobject here
133 // first we write what we are intend to do to log file
135 log.println("creating a test environment");
137 log.println("getting style");
138 XStyleFamiliesSupplier oStyleFamiliesSupplier = (XStyleFamiliesSupplier)
139 UnoRuntime.queryInterface(
140 XStyleFamiliesSupplier.class, xSpreadsheetDoc);
141 XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
142 XIndexAccess oStyleFamiliesIndexAccess = (XIndexAccess)
143 UnoRuntime.queryInterface(XIndexAccess.class, oStyleFamilies);
144 XNameAccess oStyleFamilyNameAccess = null;
145 XStyle oStyle = null;
146 try {
147 oStyleFamilyNameAccess = (XNameAccess) AnyConverter.toObject(
148 new Type(XNameAccess.class),
149 oStyleFamiliesIndexAccess.getByIndex(0));
151 XIndexAccess oStyleFamilyIndexAccess = (XIndexAccess)
152 UnoRuntime.queryInterface(XIndexAccess.class,
153 oStyleFamilyNameAccess);
154 oStyle = (XStyle) AnyConverter.toObject(
155 new Type(XStyle.class),oStyleFamilyIndexAccess.getByIndex(0));
156 } catch(com.sun.star.lang.WrappedTargetException e) {
157 e.printStackTrace(log);
158 throw new StatusException("Couldn't get by index", e);
159 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
160 e.printStackTrace(log);
161 throw new StatusException("Couldn't get by index", e);
162 } catch(com.sun.star.lang.IllegalArgumentException e) {
163 e.printStackTrace(log);
164 throw new StatusException("Couldn't get by index", e);
167 log.println("Creating a user-defined style");
168 XMultiServiceFactory oMSF = (XMultiServiceFactory)
169 UnoRuntime.queryInterface(
170 XMultiServiceFactory.class, xSpreadsheetDoc);
172 XInterface oInt = null;
173 try {
174 oInt = (XInterface)
175 oMSF.createInstance("com.sun.star.style.CellStyle");
176 } catch(com.sun.star.uno.Exception e) {
177 e.printStackTrace(log);
178 throw new StatusException("Couldn't create instance", e);
180 XStyle oMyStyle = (XStyle)UnoRuntime.queryInterface(XStyle.class, oInt);
182 XNameContainer oStyleFamilyNameContainer = (XNameContainer)UnoRuntime.
183 queryInterface(XNameContainer.class, oStyleFamilyNameAccess);
185 try {
186 if (oStyleFamilyNameContainer.hasByName("My Style")) {
187 oStyleFamilyNameContainer.removeByName("My Style");
190 oStyleFamilyNameContainer.insertByName("My Style", oMyStyle);
191 } catch(com.sun.star.lang.WrappedTargetException e) {
192 e.printStackTrace(log);
193 throw new StatusException("Couldn't create test environment", e);
194 } catch(com.sun.star.container.NoSuchElementException e) {
195 e.printStackTrace(log);
196 throw new StatusException("Couldn't create test environment", e);
197 } catch(com.sun.star.container.ElementExistException e) {
198 e.printStackTrace(log);
199 throw new StatusException("Couldn't create test environment", e);
200 } catch(com.sun.star.lang.IllegalArgumentException e) {
201 e.printStackTrace(log);
202 throw new StatusException("Couldn't create test environment", e);
206 //using the style
207 log.println("Getting spreadsheet") ;
208 XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
209 XIndexAccess oIndexSheets = (XIndexAccess)
210 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
212 XCell aCell = null;
213 try {
214 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
215 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
216 log.println("Getting a cell from sheet") ;
217 aCell = oSheet.getCellByPosition(2,3) ;
218 } catch(com.sun.star.lang.WrappedTargetException e) {
219 e.printStackTrace(log);
220 throw new StatusException("Couldn't get spreadsheet by index", e);
221 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
222 e.printStackTrace(log);
223 throw new StatusException("Couldn't get spreadsheet by index", e);
224 } catch(com.sun.star.lang.IllegalArgumentException e) {
225 e.printStackTrace(log);
226 throw new StatusException("Couldn't get spreadsheet by index", e);
229 XPropertySet xProp = (XPropertySet)
230 UnoRuntime.queryInterface(XPropertySet.class, aCell);
232 try {
233 xProp.setPropertyValue("CellStyle", oMyStyle.getName());
234 } catch(com.sun.star.lang.WrappedTargetException e) {
235 e.printStackTrace(log);
236 throw new StatusException("Couldn't set property CellStyle", e);
237 } catch(com.sun.star.lang.IllegalArgumentException e) {
238 e.printStackTrace(log);
239 throw new StatusException("Couldn't set property CellStyle", e);
240 } catch(com.sun.star.beans.PropertyVetoException e) {
241 e.printStackTrace(log);
242 throw new StatusException("Couldn't set property CellStyle", e);
243 } catch(com.sun.star.beans.UnknownPropertyException e) {
244 e.printStackTrace(log);
245 throw new StatusException("Couldn't set property CellStyle", e);
248 log.println("creating a new environment for object");
249 TestEnvironment tEnv = new TestEnvironment(oMyStyle);
251 tEnv.addObjRelation("PoolStyle", oStyle);
253 tEnv.addObjRelation("PropertyNames", getPropertyNames
254 ((XPropertySet) UnoRuntime.queryInterface
255 (XPropertySet.class, oMyStyle)));
257 return tEnv;
260 public String[] getPropertyNames(XPropertySet props) {
261 Property[] the_props = props.getPropertySetInfo().getProperties();
262 Vector names = new Vector() ;
264 for (int i=0;i<the_props.length;i++) {
265 boolean isWritable =
266 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
267 if (isWritable) {
268 names.add(the_props[i].Name);
271 return (String[]) names.toArray(new String[names.size()]) ;
273 } // finish class ScStyleObj