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