1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SwXStyleFamily.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
39 import util
.SOfficeFactory
;
41 import com
.sun
.star
.container
.XIndexAccess
;
42 import com
.sun
.star
.container
.XNameAccess
;
43 import com
.sun
.star
.container
.XNameContainer
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.lang
.XMultiServiceFactory
;
46 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
47 import com
.sun
.star
.text
.XTextDocument
;
48 import com
.sun
.star
.uno
.AnyConverter
;
49 import com
.sun
.star
.uno
.Type
;
50 import com
.sun
.star
.uno
.UnoRuntime
;
51 import com
.sun
.star
.uno
.XInterface
;
54 * Test for object which is represented by service
55 * <code>com.sun.star.style.StyleFamily</code>. <p>
56 * Object implements the following interfaces :
58 * <li> <code>com::sun::star::container::XNameContainer</code></li>
59 * <li> <code>com::sun::star::container::XNameAccess</code></li>
60 * <li> <code>com::sun::star::container::XElementAccess</code></li>
61 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
62 * <li> <code>com::sun::star::container::XNameReplace</code></li>
64 * This object test <b> is NOT </b> designed to be run in several
65 * threads concurently.
66 * @see com.sun.star.container.XNameContainer
67 * @see com.sun.star.container.XNameAccess
68 * @see com.sun.star.container.XElementAccess
69 * @see com.sun.star.container.XIndexAccess
70 * @see com.sun.star.container.XNameReplace
71 * @see ifc.container._XNameContainer
72 * @see ifc.container._XNameAccess
73 * @see ifc.container._XElementAccess
74 * @see ifc.container._XIndexAccess
75 * @see ifc.container._XNameReplace
77 public class SwXStyleFamily
extends TestCase
{
78 XTextDocument xTextDoc
;
81 * Creates text document.
83 protected void initialize( TestParameters tParam
, PrintWriter log
) {
84 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
86 log
.println( "creating a textdocument" );
87 xTextDoc
= SOF
.createTextDoc( null );
88 } catch ( com
.sun
.star
.uno
.Exception e
) {
89 e
.printStackTrace( log
);
90 throw new StatusException( "Couldn't create document", e
);
95 * Disposes text document.
97 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
98 log
.println( " disposing xTextDoc " );
99 util
.DesktopTools
.closeDoc(xTextDoc
);
103 * Creating a Testenvironment for the interfaces to be tested. At first,
104 * style families of text document are gotten using
105 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
106 * gotten from previously obtained style families collection using
107 * <code>XIndexAccess</code> interface. Finally, method creates an instance
108 * of the service <code>com.sun.star.style.CharacterStyle</code> and inserts
109 * it to a gotten style family as 'SwXStyleFamily' using
110 * <code>XNameContainer</code> interface.
111 * Object relations created :
113 * <li> <code>'XNameReplaceINDEX'</code> for
114 * {@link ifc.container._XNameReplace} : number of last taken instance
115 * of <code>com.sun.star.style.CharacterStyle</code>, when multithread
116 * testing is going.</li>
117 * <li> <code>'NAMEREPLACE'</code> for
118 * {@link ifc.container._XNameReplace} : name of style family, inserted
119 * to style families of a text document.</li>
120 * <li> <code>'INSTANCEn'</code> for
121 * {@link ifc.container._XIndexContainer},
122 * {@link ifc.container._XIndexReplace},
123 * {@link ifc.container._XNameContainer},
124 * {@link ifc.container._XNameReplace} : several relations, which are
125 * represented by instances of service
126 * <code>com.sun.star.style.CharacterStyle</code>.</li>
129 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
131 XNameAccess oSFNA
= null;
133 log
.println( "Creating Test Environment..." );
135 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)Param
.getMSF());
136 XComponent xComp
= (XComponent
)
137 UnoRuntime
.queryInterface(XComponent
.class, xTextDoc
);
138 XInterface oInstance
= (XInterface
)
139 SOF
.createInstance(xComp
, "com.sun.star.style.CharacterStyle");
140 XStyleFamiliesSupplier oSFsS
= (XStyleFamiliesSupplier
)
141 UnoRuntime
.queryInterface(XStyleFamiliesSupplier
.class, xTextDoc
);
142 XNameAccess oSF
= oSFsS
.getStyleFamilies();
143 XIndexAccess oSFIA
= (XIndexAccess
)
144 UnoRuntime
.queryInterface(XIndexAccess
.class, oSF
);
147 oSFNA
= (XNameAccess
) AnyConverter
.toObject(
148 new Type(XNameAccess
.class),oSFIA
.getByIndex(0));
149 } catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
150 e
.printStackTrace(log
);
151 throw new StatusException("Unexpected exception. ", e
);
152 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
153 e
.printStackTrace(log
);
154 throw new StatusException("Unexpected exception. ", e
);
155 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
156 e
.printStackTrace(log
);
157 throw new StatusException("Unexpected exception. ", e
);
160 XNameContainer oContainer
= (XNameContainer
)
161 UnoRuntime
.queryInterface(XNameContainer
.class, oSFNA
);
163 // insert a Style which can be replaced by name
165 oContainer
.insertByName("SwXStyleFamily",oInstance
);
166 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
167 log
.println("Could not insert style family.");
168 e
.printStackTrace(log
);
169 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
170 log
.println("Could not insert style family.");
171 e
.printStackTrace(log
);
172 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
173 log
.println("Could not insert style family.");
174 e
.printStackTrace(log
);
177 TestEnvironment tEnv
= new TestEnvironment(oSFNA
);
180 if ((String
)Param
.get("THRCNT") != null) {
181 THRCNT
= Integer
.parseInt((String
)Param
.get("THRCNT"));
183 String nr
= new Integer(THRCNT
+1).toString();
185 log
.println( "adding NameReplaceIndex as mod relation to environment" );
186 tEnv
.addObjRelation("XNameReplaceINDEX", nr
);
188 for (int n
=1; n
<(THRCNT
+3); n
++ ) {
189 log
.println( "adding INSTANCE"+n
+" as mod relation to environment");
190 tEnv
.addObjRelation("INSTANCE"+n
,
191 SOF
.createInstance(xComp
,"com.sun.star.style.CharacterStyle"));
194 log
.println("adding NAMEREPLACE as mod relation to environment");
195 tEnv
.addObjRelation("NAMEREPLACE", "SwXStyleFamily");
200 } // finish class SwXStyleFamily