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.container.XNameContainer;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.style.XStyleFamiliesSupplier;
45 import com.sun.star.uno.AnyConverter;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.style.StyleFamily</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 *  <li> <code>com::sun::star::container::XNameContainer</code></li>
56 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
57 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
58 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
59 *  <li> <code>com::sun::star::container::XNameReplace</code></li>
60 * </ul>
61 * @see com.sun.star.style.StyleFamily
62 * @see com.sun.star.container.XNameContainer
63 * @see com.sun.star.container.XNameAccess
64 * @see com.sun.star.container.XElementAccess
65 * @see com.sun.star.container.XIndexAccess
66 * @see com.sun.star.container.XNameReplace
67 * @see ifc.container._XNameContainer
68 * @see ifc.container._XNameAccess
69 * @see ifc.container._XElementAccess
70 * @see ifc.container._XIndexAccess
71 * @see ifc.container._XNameReplace
72 */
73 public class ScStyleFamilyObj extends TestCase {
74     static XSpreadsheetDocument xSpreadsheetDoc = null;
75 
76     /**
77     * Creates Spreadsheet document.
78     */
79     protected void initialize( TestParameters tParam, PrintWriter log ) {
80         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
81 
82         try {
83             log.println( "creating a Spreadsheet document" );
84             xSpreadsheetDoc = SOF.createCalcDoc(null);
85         } catch ( com.sun.star.uno.Exception e ) {
86             // Some exception occures.FAILED
87             e.printStackTrace( log );
88             throw new StatusException( "Couldn't create document", e );
89         }
90     }
91 
92     /**
93     * Disposes Spreadsheet document.
94     */
95     protected void cleanup( TestParameters tParam, PrintWriter log ) {
96         log.println( "    disposing xSheetDoc " );
97         XComponent oComp = (XComponent)
98             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
99         util.DesktopTools.closeDoc(oComp);
100     }
101 
102     /**
103     * Creating a Testenvironment for the interfaces to be tested.
104     * Retrieves the collection of style families from the document
105     * using the interface <code>XStyleFamiliesSupplier</code>.
106     * Obtains style family with index 0 from the collection.
107     * Creates the two instance of the service <code>com.sun.star.style.CellStyle</code>.
108     * One of the instance uses for inserting of new style to the obtained style
109     * family that is the instance of the service
110     * <code>com.sun.star.style.StyleFamily</code>.
111     * Object relations created :
112     * <ul>
113     *  <li> <code>'SecondInstance'</code> for
114     *      {@link ifc.container._XNameContainer}(the second created instance of
115     *      the service <code>com.sun.star.style.CellStyle</code>)</li>
116     *  <li> <code>'XNameReplaceINDEX'</code> for
117     *      {@link ifc.container._XNameContainer}(the number of the current
118     *      running threads multiplied by two)</li>
119     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
120     *      {@link ifc.container._XNameContainer}(the created instances of
121     *      the service <code>com.sun.star.style.CellStyle</code>)</li>
122     * </ul>
123     * @see com.sun.star.style.CellStyle
124     */
125     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
126 
127         // creation of testobject here
128         // first we write what we are intend to do to log file
129         log.println( "creating a test environment" );
130 
131         XNameAccess oStyleFamilyNameAccess = null;
132         // create testobject here
133         log.println("getting style");
134         XStyleFamiliesSupplier oStyleFamiliesSupplier = (XStyleFamiliesSupplier)
135             UnoRuntime.queryInterface(
136                 XStyleFamiliesSupplier.class, xSpreadsheetDoc);
137 
138         XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
139         XIndexAccess oStyleFamiliesIndexAccess = (XIndexAccess)
140             UnoRuntime.queryInterface(XIndexAccess.class, oStyleFamilies);
141         try {
142             oStyleFamilyNameAccess = (XNameAccess) AnyConverter.toObject(
143                 new Type(XNameAccess.class),
144                     oStyleFamiliesIndexAccess.getByIndex(0));
145         } catch (com.sun.star.lang.WrappedTargetException e) {
146             e.printStackTrace(log);
147             throw new StatusException(
148                 "Exception occured while getting StyleFamily", e);
149         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
150             e.printStackTrace(log);
151             throw new StatusException(
152                 "Exception occured while getting StyleFamily", e);
153         } catch (com.sun.star.lang.IllegalArgumentException e) {
154             e.printStackTrace(log);
155             throw new StatusException(
156                 "Exception occured while getting StyleFamily", e);
157         }
158 
159         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
160         XComponent xComp = (XComponent)
161             UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
162 
163         XInterface oInstance = (XInterface)
164             SOF.createInstance(xComp,"com.sun.star.style.CellStyle");
165 
166         // insert a Style which can be replaced by name
167         XNameContainer oContainer = (XNameContainer)
168             UnoRuntime.queryInterface(
169                 XNameContainer.class, oStyleFamilyNameAccess);
170         try {
171             oContainer.insertByName("ScStyleFamilyObj", oInstance);
172         } catch (com.sun.star.lang.WrappedTargetException e) {
173             e.printStackTrace(log);
174             throw new StatusException("Couldn't insert new style family", e);
175         } catch (com.sun.star.container.ElementExistException e) {
176             e.printStackTrace(log);
177             throw new StatusException("Couldn't insert new style family", e);
178         } catch (com.sun.star.lang.IllegalArgumentException e) {
179             e.printStackTrace(log);
180             throw new StatusException("Couldn't insert new style family", e);
181         }
182 
183         TestEnvironment tEnv = new TestEnvironment(oStyleFamilyNameAccess);
184 
185         oInstance = (XInterface)
186             SOF.createInstance(xComp, "com.sun.star.style.CellStyle");
187 
188         //second instance for insertByName in XNameContainer
189         tEnv.addObjRelation("SecondInstance", oInstance);
190 
191         int THRCNT = 1;
192         if ((String)tParam.get("THRCNT") != null) {
193             THRCNT = Integer.parseInt((String)tParam.get("THRCNT"));
194         }
195 
196         log.println( "adding XNameReplaceINDEX as mod relation to environment" );
197         tEnv.addObjRelation("XNameReplaceINDEX", new Integer(2*THRCNT).toString());
198 
199         // INSTANCEn : _XNameContainer; _XNameReplace
200         log.println( "adding INSTANCEn as mod relation to environment" );
201         for (int n = 1; n < 2*(THRCNT+1) ;n++ ) {
202             log.println( "adding INSTANCE" + n +
203                                             " as mod relation to environment" );
204             tEnv.addObjRelation("INSTANCE" + n, SOF.createInstance(
205                                     xComp,"com.sun.star.style.CellStyle"));
206         }
207         // NAMEREPLACE : _XNameReplace
208         log.println("adding NAMEREPLACE as mod relation to environment");
209         String cName = "ScStyleFamilyObj";
210         tEnv.addObjRelation("NAMEREPLACE", cName);
211 
212         return tEnv;
213     } // finish method getTestEnvironment
214 
215 }    // finish class ScStyleFamilyObj
216 
217