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
;
24 import lib
.TestEnvironment
;
25 import lib
.TestParameters
;
26 import util
.SOfficeFactory
;
28 import com
.sun
.star
.container
.XIndexAccess
;
29 import com
.sun
.star
.container
.XNameAccess
;
30 import com
.sun
.star
.container
.XNameContainer
;
31 import com
.sun
.star
.lang
.XComponent
;
32 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
33 import com
.sun
.star
.text
.XTextDocument
;
34 import com
.sun
.star
.uno
.AnyConverter
;
35 import com
.sun
.star
.uno
.Type
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
37 import com
.sun
.star
.uno
.XInterface
;
40 * Test for object which is represented by service
41 * <code>com.sun.star.style.StyleFamily</code>. <p>
42 * Object implements the following interfaces :
44 * <li> <code>com::sun::star::container::XNameContainer</code></li>
45 * <li> <code>com::sun::star::container::XNameAccess</code></li>
46 * <li> <code>com::sun::star::container::XElementAccess</code></li>
47 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
48 * <li> <code>com::sun::star::container::XNameReplace</code></li>
50 * This object test <b> is NOT </b> designed to be run in several
51 * threads concurrently.
52 * @see com.sun.star.container.XNameContainer
53 * @see com.sun.star.container.XNameAccess
54 * @see com.sun.star.container.XElementAccess
55 * @see com.sun.star.container.XIndexAccess
56 * @see com.sun.star.container.XNameReplace
57 * @see ifc.container._XNameContainer
58 * @see ifc.container._XNameAccess
59 * @see ifc.container._XElementAccess
60 * @see ifc.container._XIndexAccess
61 * @see ifc.container._XNameReplace
63 public class SwXStyleFamily
extends TestCase
{
64 XTextDocument xTextDoc
;
67 * Creates text document.
70 protected void initialize( TestParameters tParam
, PrintWriter log
) throws Exception
{
71 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
72 log
.println( "creating a textdocument" );
73 xTextDoc
= SOF
.createTextDoc( null );
77 * Disposes text document.
80 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
81 log
.println( " disposing xTextDoc " );
82 util
.DesktopTools
.closeDoc(xTextDoc
);
86 * Creating a TestEnvironment for the interfaces to be tested. At first,
87 * style families of text document are gotten using
88 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
89 * gotten from previously obtained style families collection using
90 * <code>XIndexAccess</code> interface. Finally, method creates an instance
91 * of the service <code>com.sun.star.style.CharacterStyle</code> and inserts
92 * it to a gotten style family as 'SwXStyleFamily' using
93 * <code>XNameContainer</code> interface.
94 * Object relations created :
96 * <li> <code>'XNameReplaceINDEX'</code> for
97 * {@link ifc.container._XNameReplace} : number of last taken instance
98 * of <code>com.sun.star.style.CharacterStyle</code>, when multithread
99 * testing is going.</li>
100 * <li> <code>'NAMEREPLACE'</code> for
101 * {@link ifc.container._XNameReplace} : name of style family, inserted
102 * to style families of a text document.</li>
103 * <li> <code>'INSTANCEn'</code> for
104 * {@link ifc.container._XIndexContainer},
105 * {@link ifc.container._XIndexReplace},
106 * {@link ifc.container._XNameContainer},
107 * {@link ifc.container._XNameReplace} : several relations, which are
108 * represented by instances of service
109 * <code>com.sun.star.style.CharacterStyle</code>.</li>
113 protected TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) throws Exception
{
115 XNameAccess oSFNA
= null;
117 log
.println( "Creating Test Environment..." );
119 SOfficeFactory SOF
= SOfficeFactory
.getFactory( Param
.getMSF());
120 XComponent xComp
= UnoRuntime
.queryInterface(XComponent
.class, xTextDoc
);
121 XInterface oInstance
= (XInterface
)
122 SOF
.createInstance(xComp
, "com.sun.star.style.CharacterStyle");
123 XStyleFamiliesSupplier oSFsS
= UnoRuntime
.queryInterface(XStyleFamiliesSupplier
.class, xTextDoc
);
124 XNameAccess oSF
= oSFsS
.getStyleFamilies();
125 XIndexAccess oSFIA
= UnoRuntime
.queryInterface(XIndexAccess
.class, oSF
);
127 oSFNA
= (XNameAccess
) AnyConverter
.toObject(
128 new Type(XNameAccess
.class),oSFIA
.getByIndex(0));
130 XNameContainer oContainer
= UnoRuntime
.queryInterface(XNameContainer
.class, oSFNA
);
132 // insert a Style which can be replaced by name
134 oContainer
.insertByName("SwXStyleFamily",oInstance
);
135 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
136 log
.println("Could not insert style family.");
137 e
.printStackTrace(log
);
138 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
139 log
.println("Could not insert style family.");
140 e
.printStackTrace(log
);
141 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
142 log
.println("Could not insert style family.");
143 e
.printStackTrace(log
);
146 TestEnvironment tEnv
= new TestEnvironment(oSFNA
);
149 if ((String
)Param
.get("THRCNT") != null) {
150 THRCNT
= Integer
.parseInt((String
)Param
.get("THRCNT"));
152 String nr
= Integer
.toString(THRCNT
+1);
154 log
.println( "adding NameReplaceIndex as mod relation to environment" );
155 tEnv
.addObjRelation("XNameReplaceINDEX", nr
);
157 for (int n
=1; n
<(THRCNT
+3); n
++ ) {
158 log
.println( "adding INSTANCE"+n
+" as mod relation to environment");
159 tEnv
.addObjRelation("INSTANCE"+n
,
160 SOF
.createInstance(xComp
,"com.sun.star.style.CharacterStyle"));
163 log
.println("adding NAMEREPLACE as mod relation to environment");
164 tEnv
.addObjRelation("NAMEREPLACE", "SwXStyleFamily");
169 } // finish class SwXStyleFamily