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 com
.sun
.star
.beans
.PropertyAttribute
;
22 import com
.sun
.star
.beans
.XPropertySet
;
23 import com
.sun
.star
.container
.XIndexAccess
;
24 import com
.sun
.star
.container
.XNameAccess
;
25 import com
.sun
.star
.container
.XNameContainer
;
26 import com
.sun
.star
.lang
.XMultiServiceFactory
;
27 import com
.sun
.star
.style
.XStyle
;
28 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
29 import com
.sun
.star
.text
.XText
;
30 import com
.sun
.star
.text
.XTextCursor
;
31 import com
.sun
.star
.text
.XTextDocument
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
33 import com
.sun
.star
.uno
.XInterface
;
34 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
39 import util
.DesktopTools
;
40 import util
.SOfficeFactory
;
44 * Test for object which is represented by service
45 * <code>com.sun.star.style.CharacterStyle</code>. <p>
46 * @see com.sun.star.style.CharacterStyle
48 public class CharacterStyle
extends TestCase
{
49 private XTextDocument xTextDoc
;
52 * Creates text document.
54 protected void initialize( TestParameters tParam
, PrintWriter log
) {
55 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
57 log
.println( "creating a textdocument" );
58 xTextDoc
= SOF
.createTextDoc( null );
59 } catch ( com
.sun
.star
.uno
.Exception e
) {
60 e
.printStackTrace( log
);
61 throw new StatusException( "Couldn't create document", e
);
66 * Disposes text document.
68 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
69 log
.println( " disposing xTextDoc " );
70 DesktopTools
.closeDoc(xTextDoc
);
74 * Creating a Testenvironment for the interfaces to be tested.
75 * At first style families are gotten from a text document using
76 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
77 * gotten from this style family using <code>XIndexAccess</code> interface.
78 * Next, method creates an instance of the service
79 * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
80 * previously obtained style family using <code>XNameContainer</code>
81 * interface. Finally, method creates a cursor of a major text of text
82 * document and sets it's property 'CharStyleName' value to the name of
83 * previously created style's name.
84 * Object relations created :
86 * <li> <code>'PoolStyle'</code> for
87 * {@link ifc.style._XStyle} : style indexed '10' obtained from
88 * StyleFamily indexed '0' from text document using
89 * <code>XIndexAccess</code> interface.</li>
92 protected TestEnvironment
createTestEnvironment(TestParameters tParam
, PrintWriter log
) {
94 TestEnvironment tEnv
= null;
95 XNameAccess oSFNA
= null;
97 XStyle oMyStyle
= null;
99 log
.println("creating a test environment");
102 log
.println("getting style");
103 XStyleFamiliesSupplier oSFS
= UnoRuntime
.queryInterface(XStyleFamiliesSupplier
.class,
105 XNameAccess oSF
= oSFS
.getStyleFamilies();
106 XIndexAccess oSFsIA
= UnoRuntime
.queryInterface(XIndexAccess
.class, oSF
);
107 oSFNA
= UnoRuntime
.queryInterface(
108 XNameAccess
.class,oSFsIA
.getByIndex(0));
109 XIndexAccess oSFIA
= UnoRuntime
.queryInterface(XIndexAccess
.class, oSFNA
);
110 oStyle
= UnoRuntime
.queryInterface(
111 XStyle
.class,oSFIA
.getByIndex(0));
112 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
113 log
.println("Error: exception occurred.");
114 e
.printStackTrace(log
);
115 throw new StatusException( "Couldn't create environment ", e
);
116 } catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
117 log
.println("Error: exception occurred.");
118 e
.printStackTrace(log
);
119 throw new StatusException( "Couldn't create environment ", e
);
123 log
.print("Creating a user-defined style... ");
124 XMultiServiceFactory oMSF
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, xTextDoc
);
125 XInterface oInt
= (XInterface
)
126 oMSF
.createInstance("com.sun.star.style.CharacterStyle");
127 oMyStyle
= UnoRuntime
.queryInterface(XStyle
.class, oInt
);
128 } catch ( com
.sun
.star
.uno
.Exception e
) {
129 log
.println("Error: exception occurred.");
130 e
.printStackTrace(log
);
131 throw new StatusException( "Couldn't create environment ", e
);
135 if (oMyStyle
== null)
136 log
.println("FAILED");
139 XNameContainer oSFNC
= UnoRuntime
.queryInterface(XNameContainer
.class, oSFNA
);
142 if ( oSFNC
.hasByName("My Style") )
143 oSFNC
.removeByName("My Style");
144 oSFNC
.insertByName("My Style", oMyStyle
);
145 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
146 e
.printStackTrace(log
);
147 throw new StatusException( "Couldn't create environment ", e
);
148 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
149 e
.printStackTrace(log
);
150 throw new StatusException( "Couldn't create environment ", e
);
151 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
152 e
.printStackTrace(log
);
153 throw new StatusException( "Couldn't create environment ", e
);
154 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
155 e
.printStackTrace(log
);
156 throw new StatusException( "Couldn't create environment ", e
);
159 XText oText
= xTextDoc
.getText();
160 XTextCursor oCursor
= oText
.createTextCursor();
161 XPropertySet xProp
= UnoRuntime
.queryInterface(XPropertySet
.class, oCursor
);
164 xProp
.setPropertyValue("CharStyleName", oMyStyle
.getName());
165 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
166 e
.printStackTrace( log
);
167 throw new StatusException( "Couldn't create environment ", e
);
168 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
169 e
.printStackTrace( log
);
170 throw new StatusException( "Couldn't create environment ", e
);
171 } catch ( com
.sun
.star
.beans
.PropertyVetoException e
) {
172 e
.printStackTrace( log
);
173 throw new StatusException( "Couldn't create environment ", e
);
174 } catch ( com
.sun
.star
.beans
.UnknownPropertyException e
) {
175 e
.printStackTrace( log
);
176 throw new StatusException( "Couldn't create environment ", e
);
179 log
.println("creating a new environment for object");
180 tEnv
= new TestEnvironment(oMyStyle
);
181 tEnv
.addObjRelation("PoolStyle", oStyle
);
183 XPropertySet xStyleProp
= UnoRuntime
.queryInterface(XPropertySet
.class, oMyStyle
);
185 short exclude
= PropertyAttribute
.READONLY
;
186 tEnv
.addObjRelation("PropertyNames",utils
.getFilteredPropertyNames(xStyleProp
, (short)0, exclude
));