Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXStyleFamily.java
blobcf84e00af6b84c4e746485f29a5720dfd63984b7
1 /*
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 .
19 package mod._sw;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
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.style.XStyleFamiliesSupplier;
34 import com.sun.star.text.XTextDocument;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.Type;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
40 /**
41 * Test for object which is represented by service
42 * <code>com.sun.star.style.StyleFamily</code>. <p>
43 * Object implements the following interfaces :
44 * <ul>
45 * <li> <code>com::sun::star::container::XNameContainer</code></li>
46 * <li> <code>com::sun::star::container::XNameAccess</code></li>
47 * <li> <code>com::sun::star::container::XElementAccess</code></li>
48 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
49 * <li> <code>com::sun::star::container::XNameReplace</code></li>
50 * </ul> <p>
51 * This object test <b> is NOT </b> designed to be run in several
52 * threads concurently.
53 * @see com.sun.star.container.XNameContainer
54 * @see com.sun.star.container.XNameAccess
55 * @see com.sun.star.container.XElementAccess
56 * @see com.sun.star.container.XIndexAccess
57 * @see com.sun.star.container.XNameReplace
58 * @see ifc.container._XNameContainer
59 * @see ifc.container._XNameAccess
60 * @see ifc.container._XElementAccess
61 * @see ifc.container._XIndexAccess
62 * @see ifc.container._XNameReplace
64 public class SwXStyleFamily extends TestCase {
65 XTextDocument xTextDoc;
67 /**
68 * Creates text document.
70 @Override
71 protected void initialize( TestParameters tParam, PrintWriter log ) {
72 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
73 try {
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 );
82 /**
83 * Disposes text document.
85 @Override
86 protected void cleanup( TestParameters tParam, PrintWriter log ) {
87 log.println( " disposing xTextDoc " );
88 util.DesktopTools.closeDoc(xTextDoc);
91 /**
92 * Creating a Testenvironment for the interfaces to be tested. At first,
93 * style families of text document are gotten using
94 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
95 * gotten from previously obtained style families collection using
96 * <code>XIndexAccess</code> interface. Finally, method creates an instance
97 * of the service <code>com.sun.star.style.CharacterStyle</code> and inserts
98 * it to a gotten style family as 'SwXStyleFamily' using
99 * <code>XNameContainer</code> interface.
100 * Object relations created :
101 * <ul>
102 * <li> <code>'XNameReplaceINDEX'</code> for
103 * {@link ifc.container._XNameReplace} : number of last taken instance
104 * of <code>com.sun.star.style.CharacterStyle</code>, when multithread
105 * testing is going.</li>
106 * <li> <code>'NAMEREPLACE'</code> for
107 * {@link ifc.container._XNameReplace} : name of style family, inserted
108 * to style families of a text document.</li>
109 * <li> <code>'INSTANCEn'</code> for
110 * {@link ifc.container._XIndexContainer},
111 * {@link ifc.container._XIndexReplace},
112 * {@link ifc.container._XNameContainer},
113 * {@link ifc.container._XNameReplace} : several relations, which are
114 * represented by instances of service
115 * <code>com.sun.star.style.CharacterStyle</code>.</li>
116 * </ul>
118 @Override
119 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
121 XNameAccess oSFNA = null;
123 log.println( "Creating Test Environment..." );
125 SOfficeFactory SOF = SOfficeFactory.getFactory( Param.getMSF());
126 XComponent xComp = UnoRuntime.queryInterface(XComponent.class, xTextDoc);
127 XInterface oInstance = (XInterface)
128 SOF.createInstance(xComp, "com.sun.star.style.CharacterStyle");
129 XStyleFamiliesSupplier oSFsS = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDoc);
130 XNameAccess oSF = oSFsS.getStyleFamilies();
131 XIndexAccess oSFIA = UnoRuntime.queryInterface(XIndexAccess.class, oSF);
133 try {
134 oSFNA = (XNameAccess) AnyConverter.toObject(
135 new Type(XNameAccess.class),oSFIA.getByIndex(0));
136 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
137 e.printStackTrace(log);
138 throw new StatusException("Unexpected exception. ", e);
139 } catch ( com.sun.star.lang.WrappedTargetException e ) {
140 e.printStackTrace(log);
141 throw new StatusException("Unexpected exception. ", e);
142 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
143 e.printStackTrace(log);
144 throw new StatusException("Unexpected exception. ", e);
147 XNameContainer oContainer = UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
149 // insert a Style which can be replaced by name
150 try {
151 oContainer.insertByName("SwXStyleFamily",oInstance);
152 } catch ( com.sun.star.lang.WrappedTargetException e ) {
153 log.println("Could not insert style family.");
154 e.printStackTrace(log);
155 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
156 log.println("Could not insert style family.");
157 e.printStackTrace(log);
158 } catch ( com.sun.star.container.ElementExistException e ) {
159 log.println("Could not insert style family.");
160 e.printStackTrace(log);
163 TestEnvironment tEnv = new TestEnvironment(oSFNA);
165 int THRCNT = 1;
166 if ((String)Param.get("THRCNT") != null) {
167 THRCNT = Integer.parseInt((String)Param.get("THRCNT"));
169 String nr = Integer.toString(THRCNT+1);
171 log.println( "adding NameReplaceIndex as mod relation to environment" );
172 tEnv.addObjRelation("XNameReplaceINDEX", nr);
174 for (int n=1; n<(THRCNT+3); n++ ) {
175 log.println( "adding INSTANCE"+n+" as mod relation to environment");
176 tEnv.addObjRelation("INSTANCE"+n,
177 SOF.createInstance(xComp,"com.sun.star.style.CharacterStyle"));
180 log.println("adding NAMEREPLACE as mod relation to environment");
181 tEnv.addObjRelation("NAMEREPLACE", "SwXStyleFamily");
183 return tEnv;
186 } // finish class SwXStyleFamily