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._sw;
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.style.XStyleFamiliesSupplier;
40 import com.sun.star.text.XTextDocument;
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> <p>
57  * This object test <b> is NOT </b> designed to be run in several
58  * threads concurently.
59  * @see com.sun.star.container.XNameContainer
60  * @see com.sun.star.container.XNameAccess
61  * @see com.sun.star.container.XElementAccess
62  * @see com.sun.star.container.XIndexAccess
63  * @see com.sun.star.container.XNameReplace
64  * @see ifc.container._XNameContainer
65  * @see ifc.container._XNameAccess
66  * @see ifc.container._XElementAccess
67  * @see ifc.container._XIndexAccess
68  * @see ifc.container._XNameReplace
69  */
70 public class SwXStyleFamily extends TestCase {
71     XTextDocument xTextDoc;
72 
73     /**
74     * Creates text document.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78         try {
79             log.println( "creating a textdocument" );
80             xTextDoc = SOF.createTextDoc( null );
81         } catch ( com.sun.star.uno.Exception e ) {
82             e.printStackTrace( log );
83             throw new StatusException( "Couldn't create document", e );
84         }
85     }
86 
87     /**
88     * Disposes text document.
89     */
cleanup( TestParameters tParam, PrintWriter log )90     protected void cleanup( TestParameters tParam, PrintWriter log ) {
91         log.println( "    disposing xTextDoc " );
92         util.DesktopTools.closeDoc(xTextDoc);
93     }
94 
95     /**
96     * Creating a Testenvironment for the interfaces to be tested. At first,
97     * style families of text document are gotten using
98     * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
99     * gotten from previously obtained style families collection using
100     * <code>XIndexAccess</code> interface. Finally, method creates an instance
101     * of the service <code>com.sun.star.style.CharacterStyle</code> and inserts
102     * it to a gotten style family as 'SwXStyleFamily' using
103     * <code>XNameContainer</code> interface.
104     *     Object relations created :
105     * <ul>
106     *  <li> <code>'XNameReplaceINDEX'</code> for
107     *      {@link ifc.container._XNameReplace} : number of last taken instance
108     *      of <code>com.sun.star.style.CharacterStyle</code>, when multithread
109     *      testing is going.</li>
110     *  <li> <code>'NAMEREPLACE'</code> for
111     *      {@link ifc.container._XNameReplace} : name of style family, inserted
112     *      to style families of a text document.</li>
113     *  <li> <code>'INSTANCEn'</code> for
114     *      {@link ifc.container._XIndexContainer},
115     *      {@link ifc.container._XIndexReplace},
116     *      {@link ifc.container._XNameContainer},
117     *      {@link ifc.container._XNameReplace} : several relations, which are
118     *      represented by instances of service
119     *      <code>com.sun.star.style.CharacterStyle</code>.</li>
120     * </ul>
121     */
createTestEnvironment(TestParameters Param, PrintWriter log)122     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
123 
124         XNameAccess oSFNA = null;
125 
126         log.println( "Creating Test Environment..." );
127 
128         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
129         XComponent xComp = (XComponent)
130             UnoRuntime.queryInterface(XComponent.class, xTextDoc);
131         XInterface oInstance = (XInterface)
132             SOF.createInstance(xComp, "com.sun.star.style.CharacterStyle");
133         XStyleFamiliesSupplier oSFsS = (XStyleFamiliesSupplier)
134                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDoc);
135         XNameAccess oSF = oSFsS.getStyleFamilies();
136         XIndexAccess oSFIA = (XIndexAccess)
137                 UnoRuntime.queryInterface(XIndexAccess.class, oSF);
138 
139         try {
140             oSFNA = (XNameAccess) AnyConverter.toObject(
141                         new Type(XNameAccess.class),oSFIA.getByIndex(0));
142         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
143             e.printStackTrace(log);
144             throw new StatusException("Unexpected exception. ", e);
145         } catch ( com.sun.star.lang.WrappedTargetException e ) {
146             e.printStackTrace(log);
147             throw new StatusException("Unexpected exception. ", e);
148         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
149             e.printStackTrace(log);
150             throw new StatusException("Unexpected exception. ", e);
151         }
152 
153         XNameContainer oContainer = (XNameContainer)
154             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
155 
156         // insert a Style which can be replaced by name
157         try {
158             oContainer.insertByName("SwXStyleFamily",oInstance);
159         } catch ( com.sun.star.lang.WrappedTargetException e ) {
160             log.println("Could not insert style family.");
161             e.printStackTrace(log);
162         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
163             log.println("Could not insert style family.");
164             e.printStackTrace(log);
165         } catch ( com.sun.star.container.ElementExistException e ) {
166             log.println("Could not insert style family.");
167             e.printStackTrace(log);
168         }
169 
170         TestEnvironment tEnv = new TestEnvironment(oSFNA);
171 
172         int THRCNT = 1;
173         if ((String)Param.get("THRCNT") != null) {
174             THRCNT = Integer.parseInt((String)Param.get("THRCNT"));
175         }
176         String nr = new Integer(THRCNT+1).toString();
177 
178         log.println( "adding NameReplaceIndex as mod relation to environment" );
179         tEnv.addObjRelation("XNameReplaceINDEX", nr);
180 
181         for (int n=1; n<(THRCNT+3); n++ ) {
182             log.println( "adding INSTANCE"+n+" as mod relation to environment");
183             tEnv.addObjRelation("INSTANCE"+n,
184                 SOF.createInstance(xComp,"com.sun.star.style.CharacterStyle"));
185         }
186 
187         log.println("adding NAMEREPLACE as mod relation to environment");
188         tEnv.addObjRelation("NAMEREPLACE", "SwXStyleFamily");
189 
190         return tEnv;
191     }
192 
193 }    // finish class SwXStyleFamily
194