merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sw / ParagraphStyle.java
blobb0ac00cdeb4d5a2e017ae18be557ae5bf054b8a9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ParagraphStyle.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package mod._sw;
32 import com.sun.star.beans.PropertyAttribute;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.container.XNameContainer;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.style.XStyle;
39 import com.sun.star.style.XStyleFamiliesSupplier;
40 import com.sun.star.text.XText;
41 import com.sun.star.text.XTextCursor;
42 import com.sun.star.text.XTextDocument;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 import java.io.PrintWriter;
46 import lib.StatusException;
47 import lib.TestCase;
48 import lib.TestEnvironment;
49 import lib.TestParameters;
50 import util.DesktopTools;
51 import util.SOfficeFactory;
52 import util.utils;
55 /**
56 * Test for object which is represented by service
57 * <code>com.sun.star.style.ParagraphStyle</code>. <p>
58 * @see com.sun.star.style.ParagraphStyle
60 public class ParagraphStyle extends TestCase {
62 XTextDocument xTextDoc;
63 SOfficeFactory SOF = null;
65 /**
66 * Creates text document.
68 protected void initialize( TestParameters tParam, PrintWriter log ) {
69 SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
70 try {
71 log.println( "creating a textdocument" );
72 xTextDoc = SOF.createTextDoc( null );
73 } catch ( com.sun.star.uno.Exception e ) {
74 e.printStackTrace( log );
75 throw new StatusException( "Couldn't create document", e );
79 /**
80 * Disposes text document.
82 protected void cleanup( TestParameters tParam, PrintWriter log ) {
83 log.println( " disposing xTextDoc " );
84 DesktopTools.closeDoc(xTextDoc);
87 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
88 TestEnvironment tEnv = null;
89 XNameAccess oSFNA = null;
90 XStyle oStyle = null;
91 XStyle oMyStyle = null;
93 log.println("creating a test environment");
95 try {
96 log.println("getting style");
97 XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
98 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
99 xTextDoc);
100 XNameAccess oSF = oSFS.getStyleFamilies();
101 oSFNA = (XNameAccess) UnoRuntime.queryInterface(
102 XNameAccess.class,oSF.getByName("ParagraphStyles"));
103 XIndexAccess oSFIA = (XIndexAccess)
104 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
105 String[] els = oSFNA.getElementNames();
106 oStyle = (XStyle) UnoRuntime.queryInterface(
107 XStyle.class,oSFIA.getByIndex(1));
108 } catch ( com.sun.star.lang.WrappedTargetException e ) {
109 log.println("Error: exception occured.");
110 e.printStackTrace(log);
111 throw new StatusException( "Couldn't create environment ", e );
112 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
113 log.println("Error: exception occured.");
114 e.printStackTrace(log);
115 throw new StatusException( "Couldn't create environment ", e );
116 } catch ( com.sun.star.container.NoSuchElementException e ) {
117 log.println("Error: exception occured.");
118 e.printStackTrace(log);
119 throw new StatusException( "Couldn't create environment ", e );
122 try {
123 log.print("Creating a user-defined style... ");
124 XMultiServiceFactory oMSF = (XMultiServiceFactory)
125 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
126 XInterface oInt = (XInterface)
127 oMSF.createInstance("com.sun.star.style.ParagraphStyle");
128 oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
129 } catch ( com.sun.star.uno.Exception e ) {
130 log.println("Error: exception occured.");
131 e.printStackTrace(log);
132 throw new StatusException( "Couldn't create environment ", e );
136 if (oMyStyle == null)
137 log.println("FAILED");
138 else
139 log.println("OK");
140 XNameContainer oSFNC = (XNameContainer)
141 UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
143 try {
144 if ( oSFNC.hasByName("My Style") )
145 oSFNC.removeByName("My Style");
146 oSFNC.insertByName("My Style", oMyStyle);
147 } catch ( com.sun.star.lang.WrappedTargetException e ) {
148 e.printStackTrace(log);
149 throw new StatusException( "Couldn't create environment ", e );
150 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
151 e.printStackTrace(log);
152 throw new StatusException( "Couldn't create environment ", e );
153 } catch ( com.sun.star.container.NoSuchElementException e ) {
154 e.printStackTrace(log);
155 throw new StatusException( "Couldn't create environment ", e );
156 } catch ( com.sun.star.container.ElementExistException e ) {
157 e.printStackTrace(log);
158 throw new StatusException( "Couldn't create environment ", e );
161 XText oText = xTextDoc.getText();
162 XTextCursor oCursor = oText.createTextCursor();
163 XPropertySet xProp = (XPropertySet)
164 UnoRuntime.queryInterface(XPropertySet.class, oCursor);
166 try {
167 xProp.setPropertyValue("ParaStyleName", oMyStyle.getName());
168 } catch ( com.sun.star.lang.WrappedTargetException e ) {
169 e.printStackTrace( log );
170 throw new StatusException( "Couldn't create environment ", e );
171 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
172 e.printStackTrace( log );
173 throw new StatusException( "Couldn't create environment ", e );
174 } catch ( com.sun.star.beans.PropertyVetoException e ) {
175 e.printStackTrace( log );
176 throw new StatusException( "Couldn't create environment ", e );
177 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
178 e.printStackTrace( log );
179 throw new StatusException( "Couldn't create environment ", e );
182 log.println("creating a new environment for object");
183 tEnv = new TestEnvironment(oMyStyle);
184 tEnv.addObjRelation("PoolStyle", oStyle);
186 XPropertySet xStyleProp = (XPropertySet)
187 UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
188 short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY;
189 String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude);
190 tEnv.addObjRelation("PropertyNames", names);
192 return tEnv;