merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sw / SwXAutoTextGroup.java
blob89794faa6d7d76ef3ba6fe067295dd4410434d0c
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: SwXAutoTextGroup.java,v $
10 * $Revision: 1.9 $
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 ************************************************************************/
31 package mod._sw;
33 import com.sun.star.container.ElementExistException;
34 import com.sun.star.container.XNameAccess;
35 import java.io.PrintWriter;
37 import lib.StatusException;
38 import lib.TestCase;
39 import lib.TestEnvironment;
40 import lib.TestParameters;
41 import util.SOfficeFactory;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.text.XAutoTextContainer;
44 import com.sun.star.text.XText;
45 import com.sun.star.text.XTextDocument;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import util.utils;
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.text.AutoTextGroup</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 * <li> <code>com::sun::star::container::XNamed</code></li>
59 * <li> <code>com::sun::star::container::XNameAccess</code></li>
60 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
61 * <li> <code>com::sun::star::container::XElementAccess</code></li>
62 * <li> <code>com::sun::star::text::XAutoTextGroup</code></li>
63 * </ul> <p>
64 * This object test <b> is NOT </b> designed to be run in several
65 * threads concurently.
66 * @see com.sun.star.container.XNamed
67 * @see com.sun.star.container.XNameAccess
68 * @see com.sun.star.container.XIndexAccess
69 * @see com.sun.star.container.XElementAccess
70 * @see com.sun.star.text.XAutoTextGroup
71 * @see com.sun.star.text.AutoTextContainer
72 * @see ifc.container._XNamed
73 * @see ifc.container._XNameAccess
74 * @see ifc.container._XIndexAccess
75 * @see ifc.container._XElementAccess
76 * @see ifc.text._XAutoTextGroup
78 public class SwXAutoTextGroup extends TestCase {
79 XTextDocument xTextDoc;
81 /**
82 * Creates text document.
84 protected void initialize( TestParameters tParam, PrintWriter log ) {
85 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
86 try {
87 log.println( "creating a textdocument" );
88 xTextDoc = SOF.createTextDoc( null );
89 } catch ( com.sun.star.uno.Exception e ) {
90 e.printStackTrace( log );
91 throw new StatusException( "Couldn't create document", e );
95 /**
96 * Disposes text document.
98 protected void cleanup( TestParameters tParam, PrintWriter log ) {
99 log.println( " disposing xTextDoc " );
100 util.DesktopTools.closeDoc(xTextDoc);
104 * Creating a Testenvironment for the interfaces to be tested.
105 * Creates an instance of the service
106 * <code>com.sun.star.text.AutoTextContainer</code>, then creates a new
107 * group into the container.<p>
108 * Object relations created :
109 * <ul>
110 * <li> <code>'TextRange'</code> for
111 * {@link ifc.text._XAutoTextGroup} range of text</li>
112 * </ul>
114 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
116 XInterface oObj = null;
117 XAutoTextContainer oContainer;
119 log.println( "creating a test environment" );
120 try {
121 XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF();
122 Object oInst = myMSF.createInstance("com.sun.star.text.AutoTextContainer");
123 oContainer = (XAutoTextContainer) UnoRuntime.queryInterface(XAutoTextContainer.class,oInst);
124 } catch (com.sun.star.uno.Exception e) {
125 e.printStackTrace(log);
126 throw new StatusException("Couldn't create AutoTextContainer", e);
128 String myGroupName="myNewGroup2*1";
130 XAutoTextContainer xATC = (XAutoTextContainer) UnoRuntime.queryInterface(XAutoTextContainer.class, oContainer);
132 try {
133 log.println("removing element with name '" + myGroupName + "'");
134 xATC.removeByName(myGroupName);
135 } catch (com.sun.star.container.NoSuchElementException e) {
138 try {
139 log.println("adding element with name '" + myGroupName + "'");
140 xATC.insertNewByName(myGroupName);
141 } catch (ElementExistException ex) {
142 ex.printStackTrace(log);
143 throw new StatusException("could not insert '"+myGroupName+"' into container",ex);
144 } catch (com.sun.star.lang.IllegalArgumentException ex) {
145 ex.printStackTrace(log);
146 throw new StatusException("could not insert '"+myGroupName+"' into container",ex);
150 XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer);
152 if (Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE)){
153 String contNames[] = oContNames.getElementNames();
154 for (int i =0; i < contNames.length; i++){
155 log.println("ContainerNames[ "+ i + "]: " + contNames[i]);
159 try{
160 oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName(myGroupName));
161 } catch (com.sun.star.uno.Exception e) {
162 e.printStackTrace(log);
163 throw new StatusException("Couldn't get AutoTextGroup '"+myGroupName + "'", e);
166 log.println("ImplementationName " + utils.getImplName(oObj));
168 log.println( "creating a new environment for AutoTextGroup object" );
169 TestEnvironment tEnv = new TestEnvironment( oObj );
171 XText oText = xTextDoc.getText();
172 oText.insertString(oText.getStart(), "New AutoText", true);
174 log.println( "adding TextRange as mod relation to environment" );
175 tEnv.addObjRelation("TextRange", oText);
177 return tEnv;
178 } // finish method getTestEnvironment
181 } // finish class SwXAutoTextGroup