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