2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.SOfficeFactory
;
29 import com
.sun
.star
.container
.XIndexAccess
;
30 import com
.sun
.star
.container
.XNameAccess
;
31 import com
.sun
.star
.container
.XNameContainer
;
32 import com
.sun
.star
.lang
.XComponent
;
33 import com
.sun
.star
.lang
.XMultiServiceFactory
;
34 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
35 import com
.sun
.star
.text
.XTextDocument
;
36 import com
.sun
.star
.uno
.AnyConverter
;
37 import com
.sun
.star
.uno
.Type
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import com
.sun
.star
.uno
.XInterface
;
42 * Test for object which is represented by service
43 * <code>com.sun.star.style.StyleFamily</code>. <p>
44 * Object implements the following interfaces :
46 * <li> <code>com::sun::star::container::XNameContainer</code></li>
47 * <li> <code>com::sun::star::container::XNameAccess</code></li>
48 * <li> <code>com::sun::star::container::XElementAccess</code></li>
49 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
50 * <li> <code>com::sun::star::container::XNameReplace</code></li>
52 * This object test <b> is NOT </b> designed to be run in several
53 * threads concurently.
54 * @see com.sun.star.container.XNameContainer
55 * @see com.sun.star.container.XNameAccess
56 * @see com.sun.star.container.XElementAccess
57 * @see com.sun.star.container.XIndexAccess
58 * @see com.sun.star.container.XNameReplace
59 * @see ifc.container._XNameContainer
60 * @see ifc.container._XNameAccess
61 * @see ifc.container._XElementAccess
62 * @see ifc.container._XIndexAccess
63 * @see ifc.container._XNameReplace
65 public class SwXStyleFamily
extends TestCase
{
66 XTextDocument xTextDoc
;
69 * Creates text document.
71 protected void initialize( TestParameters tParam
, PrintWriter log
) {
72 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
74 log
.println( "creating a textdocument" );
75 xTextDoc
= SOF
.createTextDoc( null );
76 } catch ( com
.sun
.star
.uno
.Exception e
) {
77 e
.printStackTrace( log
);
78 throw new StatusException( "Couldn't create document", e
);
83 * Disposes text document.
85 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
86 log
.println( " disposing xTextDoc " );
87 util
.DesktopTools
.closeDoc(xTextDoc
);
91 * Creating a Testenvironment for the interfaces to be tested. At first,
92 * style families of text document are gotten using
93 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
94 * gotten from previously obtained style families collection using
95 * <code>XIndexAccess</code> interface. Finally, method creates an instance
96 * of the service <code>com.sun.star.style.CharacterStyle</code> and inserts
97 * it to a gotten style family as 'SwXStyleFamily' using
98 * <code>XNameContainer</code> interface.
99 * Object relations created :
101 * <li> <code>'XNameReplaceINDEX'</code> for
102 * {@link ifc.container._XNameReplace} : number of last taken instance
103 * of <code>com.sun.star.style.CharacterStyle</code>, when multithread
104 * testing is going.</li>
105 * <li> <code>'NAMEREPLACE'</code> for
106 * {@link ifc.container._XNameReplace} : name of style family, inserted
107 * to style families of a text document.</li>
108 * <li> <code>'INSTANCEn'</code> for
109 * {@link ifc.container._XIndexContainer},
110 * {@link ifc.container._XIndexReplace},
111 * {@link ifc.container._XNameContainer},
112 * {@link ifc.container._XNameReplace} : several relations, which are
113 * represented by instances of service
114 * <code>com.sun.star.style.CharacterStyle</code>.</li>
117 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
119 XNameAccess oSFNA
= null;
121 log
.println( "Creating Test Environment..." );
123 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)Param
.getMSF());
124 XComponent xComp
= UnoRuntime
.queryInterface(XComponent
.class, xTextDoc
);
125 XInterface oInstance
= (XInterface
)
126 SOF
.createInstance(xComp
, "com.sun.star.style.CharacterStyle");
127 XStyleFamiliesSupplier oSFsS
= UnoRuntime
.queryInterface(XStyleFamiliesSupplier
.class, xTextDoc
);
128 XNameAccess oSF
= oSFsS
.getStyleFamilies();
129 XIndexAccess oSFIA
= UnoRuntime
.queryInterface(XIndexAccess
.class, oSF
);
132 oSFNA
= (XNameAccess
) AnyConverter
.toObject(
133 new Type(XNameAccess
.class),oSFIA
.getByIndex(0));
134 } catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
135 e
.printStackTrace(log
);
136 throw new StatusException("Unexpected exception. ", e
);
137 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
138 e
.printStackTrace(log
);
139 throw new StatusException("Unexpected exception. ", e
);
140 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
141 e
.printStackTrace(log
);
142 throw new StatusException("Unexpected exception. ", e
);
145 XNameContainer oContainer
= UnoRuntime
.queryInterface(XNameContainer
.class, oSFNA
);
147 // insert a Style which can be replaced by name
149 oContainer
.insertByName("SwXStyleFamily",oInstance
);
150 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
151 log
.println("Could not insert style family.");
152 e
.printStackTrace(log
);
153 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
154 log
.println("Could not insert style family.");
155 e
.printStackTrace(log
);
156 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
157 log
.println("Could not insert style family.");
158 e
.printStackTrace(log
);
161 TestEnvironment tEnv
= new TestEnvironment(oSFNA
);
164 if ((String
)Param
.get("THRCNT") != null) {
165 THRCNT
= Integer
.parseInt((String
)Param
.get("THRCNT"));
167 String nr
= new Integer(THRCNT
+1).toString();
169 log
.println( "adding NameReplaceIndex as mod relation to environment" );
170 tEnv
.addObjRelation("XNameReplaceINDEX", nr
);
172 for (int n
=1; n
<(THRCNT
+3); n
++ ) {
173 log
.println( "adding INSTANCE"+n
+" as mod relation to environment");
174 tEnv
.addObjRelation("INSTANCE"+n
,
175 SOF
.createInstance(xComp
,"com.sun.star.style.CharacterStyle"));
178 log
.println("adding NAMEREPLACE as mod relation to environment");
179 tEnv
.addObjRelation("NAMEREPLACE", "SwXStyleFamily");
184 } // finish class SwXStyleFamily