Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / mod / _sw / ConditionalParagraphStyle.java
blobc85f49739ef0f1cb4a00ec3a6ab32c539578d26c
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: ConditionalParagraphStyle.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.Status;
47 import lib.StatusException;
48 import lib.TestCase;
49 import lib.TestEnvironment;
50 import lib.TestParameters;
51 import util.DesktopTools;
52 import util.SOfficeFactory;
53 import util.utils;
55 /**
56 * Test for object which is represented by service
57 * <code>com.sun.star.style.ConditionalParagraphStyle</code>. <p>
58 * @see com.sun.star.style.ConditionalParagraphStyle
60 public class ConditionalParagraphStyle 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 /**
88 * Create a new instance of the conditional
89 * paragraph style, and insert it into the document.
90 * @see com.sun.star.style.ConditionalParagraphStyle
91 * @param tParam The test paramters
92 * @param log The log writer.
94 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
95 TestEnvironment tEnv = null;
96 XNameAccess oSFNA = null;
97 XStyle oStyle = null;
98 XStyle oMyStyle = null;
100 log.println("creating a test environment");
102 try {
103 log.println("getting style");
104 XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
105 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
106 xTextDoc);
107 XNameAccess oSF = oSFS.getStyleFamilies();
108 oSFNA = (XNameAccess) UnoRuntime.queryInterface(
109 XNameAccess.class,oSF.getByName("ParagraphStyles"));
110 XIndexAccess oSFIA = (XIndexAccess)
111 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
112 oStyle = (XStyle) UnoRuntime.queryInterface(
113 XStyle.class,oSFIA.getByIndex(1));
114 } catch ( com.sun.star.lang.WrappedTargetException e ) {
115 log.println("Error: exception occured.");
116 e.printStackTrace(log);
117 throw new StatusException( "Couldn't create environment ", e );
118 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
119 log.println("Error: exception occured.");
120 e.printStackTrace(log);
121 throw new StatusException( "Couldn't create environment ", e );
122 } catch ( com.sun.star.container.NoSuchElementException e ) {
123 log.println("Error: exception occured.");
124 e.printStackTrace(log);
125 throw new StatusException( "Couldn't create environment ", e );
128 try {
129 log.print("Creating a user-defined style... ");
130 XMultiServiceFactory oMSF = (XMultiServiceFactory)
131 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
132 XInterface oInt = (XInterface)
133 oMSF.createInstance("com.sun.star.style.ConditionalParagraphStyle");
134 oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
135 } catch ( com.sun.star.uno.Exception e ) {
136 log.println("Error: exception occured.");
137 e.printStackTrace(log);
138 throw new StatusException( "Couldn't create environment ", e );
142 if (oMyStyle == null) {
143 log.println("FAILED");
144 throw new StatusException(Status.failed("Couldn't create an own style "));
146 else {
147 log.println("OK");
150 XNameContainer oSFNC = (XNameContainer)
151 UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
153 try {
154 if ( oSFNC.hasByName("My Style") )
155 oSFNC.removeByName("My Style");
156 oSFNC.insertByName("My Style", oMyStyle);
157 } catch ( com.sun.star.lang.WrappedTargetException e ) {
158 e.printStackTrace(log);
159 throw new StatusException( "Couldn't create environment ", e );
160 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
161 e.printStackTrace(log);
162 throw new StatusException( "Couldn't create environment ", e );
163 } catch ( com.sun.star.container.NoSuchElementException e ) {
164 e.printStackTrace(log);
165 throw new StatusException( "Couldn't create environment ", e );
166 } catch ( com.sun.star.container.ElementExistException e ) {
167 e.printStackTrace(log);
168 throw new StatusException( "Couldn't create environment ", e );
171 XText oText = xTextDoc.getText();
172 XTextCursor oCursor = oText.createTextCursor();
173 XPropertySet xProp = (XPropertySet)
174 UnoRuntime.queryInterface(XPropertySet.class, oCursor);
175 try {
176 xProp.setPropertyValue("ParaStyleName", oMyStyle.getName());
177 } catch ( com.sun.star.lang.WrappedTargetException e ) {
178 e.printStackTrace( log );
179 throw new StatusException( "Couldn't create environment ", e );
180 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
181 e.printStackTrace( log );
182 throw new StatusException( "Couldn't create environment ", e );
183 } catch ( com.sun.star.beans.PropertyVetoException e ) {
184 e.printStackTrace( log );
185 throw new StatusException( "Couldn't create environment ", e );
186 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
187 e.printStackTrace( log );
188 throw new StatusException( "Couldn't create environment ", e );
191 log.println("creating a new environment for object");
192 tEnv = new TestEnvironment(oMyStyle);
193 tEnv.addObjRelation("PoolStyle", oStyle);
195 XPropertySet xStyleProp = (XPropertySet)
196 UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
197 short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY;
198 String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude);
199 tEnv.addObjRelation("PropertyNames", names);
201 return tEnv;