1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._sc;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import lib.TestCase;
30cdf0e10cSrcweir import lib.TestEnvironment;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir import util.SOfficeFactory;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
35cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
36cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
37cdf0e10cSrcweir import com.sun.star.lang.XComponent;
38cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
39cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
40cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
41cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
42cdf0e10cSrcweir import com.sun.star.uno.Type;
43cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44cdf0e10cSrcweir import com.sun.star.uno.XInterface;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /**
47cdf0e10cSrcweir * Test for object which is represented by service
48cdf0e10cSrcweir * <code>com.sun.star.style.StyleFamily</code>. <p>
49cdf0e10cSrcweir * Object implements the following interfaces :
50cdf0e10cSrcweir * <ul>
51cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNameContainer</code></li>
52cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNameAccess</code></li>
53cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XElementAccess</code></li>
54cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
55cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNameReplace</code></li>
56cdf0e10cSrcweir * </ul>
57cdf0e10cSrcweir * @see com.sun.star.style.StyleFamily
58cdf0e10cSrcweir * @see com.sun.star.container.XNameContainer
59cdf0e10cSrcweir * @see com.sun.star.container.XNameAccess
60cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess
61cdf0e10cSrcweir * @see com.sun.star.container.XIndexAccess
62cdf0e10cSrcweir * @see com.sun.star.container.XNameReplace
63cdf0e10cSrcweir * @see ifc.container._XNameContainer
64cdf0e10cSrcweir * @see ifc.container._XNameAccess
65cdf0e10cSrcweir * @see ifc.container._XElementAccess
66cdf0e10cSrcweir * @see ifc.container._XIndexAccess
67cdf0e10cSrcweir * @see ifc.container._XNameReplace
68cdf0e10cSrcweir */
69cdf0e10cSrcweir public class ScStyleFamilyObj extends TestCase {
70cdf0e10cSrcweir     static XSpreadsheetDocument xSpreadsheetDoc = null;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     /**
73cdf0e10cSrcweir     * Creates Spreadsheet document.
74cdf0e10cSrcweir     */
initialize( TestParameters tParam, PrintWriter log )75cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
76cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         try {
79cdf0e10cSrcweir             log.println( "creating a Spreadsheet document" );
80cdf0e10cSrcweir             xSpreadsheetDoc = SOF.createCalcDoc(null);
81cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
82cdf0e10cSrcweir             // Some exception occures.FAILED
83cdf0e10cSrcweir             e.printStackTrace( log );
84cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir     * Disposes Spreadsheet document.
90cdf0e10cSrcweir     */
cleanup( TestParameters tParam, PrintWriter log )91cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
92cdf0e10cSrcweir         log.println( "    disposing xSheetDoc " );
93cdf0e10cSrcweir         XComponent oComp = (XComponent)
94cdf0e10cSrcweir             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
95cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     /**
99cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
100cdf0e10cSrcweir     * Retrieves the collection of style families from the document
101cdf0e10cSrcweir     * using the interface <code>XStyleFamiliesSupplier</code>.
102cdf0e10cSrcweir     * Obtains style family with index 0 from the collection.
103cdf0e10cSrcweir     * Creates the two instance of the service <code>com.sun.star.style.CellStyle</code>.
104cdf0e10cSrcweir     * One of the instance uses for inserting of new style to the obtained style
105cdf0e10cSrcweir     * family that is the instance of the service
106cdf0e10cSrcweir     * <code>com.sun.star.style.StyleFamily</code>.
107cdf0e10cSrcweir     * Object relations created :
108cdf0e10cSrcweir     * <ul>
109cdf0e10cSrcweir     *  <li> <code>'SecondInstance'</code> for
110cdf0e10cSrcweir     *      {@link ifc.container._XNameContainer}(the second created instance of
111cdf0e10cSrcweir     *      the service <code>com.sun.star.style.CellStyle</code>)</li>
112cdf0e10cSrcweir     *  <li> <code>'XNameReplaceINDEX'</code> for
113cdf0e10cSrcweir     *      {@link ifc.container._XNameContainer}(the number of the current
114cdf0e10cSrcweir     *      running threads multiplied by two)</li>
115cdf0e10cSrcweir     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
116cdf0e10cSrcweir     *      {@link ifc.container._XNameContainer}(the created instances of
117cdf0e10cSrcweir     *      the service <code>com.sun.star.style.CellStyle</code>)</li>
118cdf0e10cSrcweir     * </ul>
119cdf0e10cSrcweir     * @see com.sun.star.style.CellStyle
120cdf0e10cSrcweir     */
createTestEnvironment(TestParameters tParam, PrintWriter log)121cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         // creation of testobject here
124cdf0e10cSrcweir         // first we write what we are intend to do to log file
125cdf0e10cSrcweir         log.println( "creating a test environment" );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         XNameAccess oStyleFamilyNameAccess = null;
128cdf0e10cSrcweir         // create testobject here
129cdf0e10cSrcweir         log.println("getting style");
130cdf0e10cSrcweir         XStyleFamiliesSupplier oStyleFamiliesSupplier = (XStyleFamiliesSupplier)
131cdf0e10cSrcweir             UnoRuntime.queryInterface(
132cdf0e10cSrcweir                 XStyleFamiliesSupplier.class, xSpreadsheetDoc);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
135cdf0e10cSrcweir         XIndexAccess oStyleFamiliesIndexAccess = (XIndexAccess)
136cdf0e10cSrcweir             UnoRuntime.queryInterface(XIndexAccess.class, oStyleFamilies);
137cdf0e10cSrcweir         try {
138cdf0e10cSrcweir             oStyleFamilyNameAccess = (XNameAccess) AnyConverter.toObject(
139cdf0e10cSrcweir                 new Type(XNameAccess.class),
140cdf0e10cSrcweir                     oStyleFamiliesIndexAccess.getByIndex(0));
141cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
142cdf0e10cSrcweir             e.printStackTrace(log);
143cdf0e10cSrcweir             throw new StatusException(
144cdf0e10cSrcweir                 "Exception occured while getting StyleFamily", e);
145cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
146cdf0e10cSrcweir             e.printStackTrace(log);
147cdf0e10cSrcweir             throw new StatusException(
148cdf0e10cSrcweir                 "Exception occured while getting StyleFamily", e);
149cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
150cdf0e10cSrcweir             e.printStackTrace(log);
151cdf0e10cSrcweir             throw new StatusException(
152cdf0e10cSrcweir                 "Exception occured while getting StyleFamily", e);
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
156cdf0e10cSrcweir         XComponent xComp = (XComponent)
157cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         XInterface oInstance = (XInterface)
160cdf0e10cSrcweir             SOF.createInstance(xComp,"com.sun.star.style.CellStyle");
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         // insert a Style which can be replaced by name
163cdf0e10cSrcweir         XNameContainer oContainer = (XNameContainer)
164cdf0e10cSrcweir             UnoRuntime.queryInterface(
165cdf0e10cSrcweir                 XNameContainer.class, oStyleFamilyNameAccess);
166cdf0e10cSrcweir         try {
167cdf0e10cSrcweir             oContainer.insertByName("ScStyleFamilyObj", oInstance);
168cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
169cdf0e10cSrcweir             e.printStackTrace(log);
170cdf0e10cSrcweir             throw new StatusException("Couldn't insert new style family", e);
171cdf0e10cSrcweir         } catch (com.sun.star.container.ElementExistException e) {
172cdf0e10cSrcweir             e.printStackTrace(log);
173cdf0e10cSrcweir             throw new StatusException("Couldn't insert new style family", e);
174cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
175cdf0e10cSrcweir             e.printStackTrace(log);
176cdf0e10cSrcweir             throw new StatusException("Couldn't insert new style family", e);
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oStyleFamilyNameAccess);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         oInstance = (XInterface)
182cdf0e10cSrcweir             SOF.createInstance(xComp, "com.sun.star.style.CellStyle");
183cdf0e10cSrcweir 
184cdf0e10cSrcweir         //second instance for insertByName in XNameContainer
185cdf0e10cSrcweir         tEnv.addObjRelation("SecondInstance", oInstance);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         int THRCNT = 1;
188cdf0e10cSrcweir         if ((String)tParam.get("THRCNT") != null) {
189cdf0e10cSrcweir             THRCNT = Integer.parseInt((String)tParam.get("THRCNT"));
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         log.println( "adding XNameReplaceINDEX as mod relation to environment" );
193cdf0e10cSrcweir         tEnv.addObjRelation("XNameReplaceINDEX", new Integer(2*THRCNT).toString());
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         // INSTANCEn : _XNameContainer; _XNameReplace
196cdf0e10cSrcweir         log.println( "adding INSTANCEn as mod relation to environment" );
197cdf0e10cSrcweir         for (int n = 1; n < 2*(THRCNT+1) ;n++ ) {
198cdf0e10cSrcweir             log.println( "adding INSTANCE" + n +
199cdf0e10cSrcweir                                             " as mod relation to environment" );
200cdf0e10cSrcweir             tEnv.addObjRelation("INSTANCE" + n, SOF.createInstance(
201cdf0e10cSrcweir                                     xComp,"com.sun.star.style.CellStyle"));
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir         // NAMEREPLACE : _XNameReplace
204cdf0e10cSrcweir         log.println("adding NAMEREPLACE as mod relation to environment");
205cdf0e10cSrcweir         String cName = "ScStyleFamilyObj";
206cdf0e10cSrcweir         tEnv.addObjRelation("NAMEREPLACE", cName);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         return tEnv;
209cdf0e10cSrcweir     } // finish method getTestEnvironment
210cdf0e10cSrcweir 
211cdf0e10cSrcweir }    // finish class ScStyleFamilyObj
212cdf0e10cSrcweir 
213