bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / ParagraphStyle.java
blob0c9110f521c83c8698b338570f092ddd4fea26c7
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 .
18 package mod._sw;
20 import com.sun.star.beans.PropertyAttribute;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XNameAccess;
24 import com.sun.star.container.XNameContainer;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.style.XStyle;
27 import com.sun.star.style.XStyleFamiliesSupplier;
28 import com.sun.star.text.XText;
29 import com.sun.star.text.XTextCursor;
30 import com.sun.star.text.XTextDocument;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XInterface;
33 import java.io.PrintWriter;
34 import lib.StatusException;
35 import lib.TestCase;
36 import lib.TestEnvironment;
37 import lib.TestParameters;
38 import util.DesktopTools;
39 import util.SOfficeFactory;
40 import util.utils;
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.style.ParagraphStyle</code>. <p>
46 * @see com.sun.star.style.ParagraphStyle
48 public class ParagraphStyle extends TestCase {
50 XTextDocument xTextDoc;
51 SOfficeFactory SOF = null;
53 /**
54 * Creates text document.
56 protected void initialize( TestParameters tParam, PrintWriter log ) {
57 SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
58 try {
59 log.println( "creating a textdocument" );
60 xTextDoc = SOF.createTextDoc( null );
61 } catch ( com.sun.star.uno.Exception e ) {
62 e.printStackTrace( log );
63 throw new StatusException( "Couldn't create document", e );
67 /**
68 * Disposes text document.
70 protected void cleanup( TestParameters tParam, PrintWriter log ) {
71 log.println( " disposing xTextDoc " );
72 DesktopTools.closeDoc(xTextDoc);
75 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
76 TestEnvironment tEnv = null;
77 XNameAccess oSFNA = null;
78 XStyle oStyle = null;
79 XStyle oMyStyle = null;
81 log.println("creating a test environment");
83 try {
84 log.println("getting style");
85 XStyleFamiliesSupplier oSFS = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
86 xTextDoc);
87 XNameAccess oSF = oSFS.getStyleFamilies();
88 oSFNA = UnoRuntime.queryInterface(
89 XNameAccess.class,oSF.getByName("ParagraphStyles"));
90 XIndexAccess oSFIA = UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
91 oSFNA.getElementNames();
92 oStyle = UnoRuntime.queryInterface(
93 XStyle.class,oSFIA.getByIndex(1));
94 } catch ( com.sun.star.lang.WrappedTargetException e ) {
95 log.println("Error: exception occurred.");
96 e.printStackTrace(log);
97 throw new StatusException( "Couldn't create environment ", e );
98 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
99 log.println("Error: exception occurred.");
100 e.printStackTrace(log);
101 throw new StatusException( "Couldn't create environment ", e );
102 } catch ( com.sun.star.container.NoSuchElementException e ) {
103 log.println("Error: exception occurred.");
104 e.printStackTrace(log);
105 throw new StatusException( "Couldn't create environment ", e );
108 try {
109 log.print("Creating a user-defined style... ");
110 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
111 XInterface oInt = (XInterface)
112 oMSF.createInstance("com.sun.star.style.ParagraphStyle");
113 oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
114 } catch ( com.sun.star.uno.Exception e ) {
115 log.println("Error: exception occurred.");
116 e.printStackTrace(log);
117 throw new StatusException( "Couldn't create environment ", e );
121 if (oMyStyle == null)
122 log.println("FAILED");
123 else
124 log.println("OK");
125 XNameContainer oSFNC = UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
127 try {
128 if ( oSFNC.hasByName("My Style") )
129 oSFNC.removeByName("My Style");
130 oSFNC.insertByName("My Style", oMyStyle);
131 } catch ( com.sun.star.lang.WrappedTargetException e ) {
132 e.printStackTrace(log);
133 throw new StatusException( "Couldn't create environment ", e );
134 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
135 e.printStackTrace(log);
136 throw new StatusException( "Couldn't create environment ", e );
137 } catch ( com.sun.star.container.NoSuchElementException e ) {
138 e.printStackTrace(log);
139 throw new StatusException( "Couldn't create environment ", e );
140 } catch ( com.sun.star.container.ElementExistException e ) {
141 e.printStackTrace(log);
142 throw new StatusException( "Couldn't create environment ", e );
145 XText oText = xTextDoc.getText();
146 XTextCursor oCursor = oText.createTextCursor();
147 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, oCursor);
149 try {
150 xProp.setPropertyValue("ParaStyleName", oMyStyle.getName());
151 } catch ( com.sun.star.lang.WrappedTargetException e ) {
152 e.printStackTrace( log );
153 throw new StatusException( "Couldn't create environment ", e );
154 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
155 e.printStackTrace( log );
156 throw new StatusException( "Couldn't create environment ", e );
157 } catch ( com.sun.star.beans.PropertyVetoException e ) {
158 e.printStackTrace( log );
159 throw new StatusException( "Couldn't create environment ", e );
160 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
161 e.printStackTrace( log );
162 throw new StatusException( "Couldn't create environment ", e );
165 log.println("creating a new environment for object");
166 tEnv = new TestEnvironment(oMyStyle);
167 tEnv.addObjRelation("PoolStyle", oStyle);
169 XPropertySet xStyleProp = UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
170 short exclude = PropertyAttribute.READONLY;
171 String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude);
172 tEnv.addObjRelation("PropertyNames", names);
174 return tEnv;