Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXAutoTextGroup.java
blobd67f4fbe51b2e87ed5f3cb482fafd0c37d8076f9
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 .
19 package mod._sw;
21 import com.sun.star.container.ElementExistException;
22 import com.sun.star.container.XNameAccess;
23 import java.io.PrintWriter;
25 import lib.StatusException;
26 import lib.TestCase;
27 import lib.TestEnvironment;
28 import lib.TestParameters;
29 import util.SOfficeFactory;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.text.XAutoTextContainer;
32 import com.sun.star.text.XText;
33 import com.sun.star.text.XTextDocument;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 import util.utils;
41 /**
42 * Test for object which is represented by service
43 * <code>com.sun.star.text.AutoTextGroup</code>. <p>
44 * Object implements the following interfaces :
45 * <ul>
46 * <li> <code>com::sun::star::container::XNamed</code></li>
47 * <li> <code>com::sun::star::container::XNameAccess</code></li>
48 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
49 * <li> <code>com::sun::star::container::XElementAccess</code></li>
50 * <li> <code>com::sun::star::text::XAutoTextGroup</code></li>
51 * </ul> <p>
52 * This object test <b> is NOT </b> designed to be run in several
53 * threads concurently.
54 * @see com.sun.star.container.XNamed
55 * @see com.sun.star.container.XNameAccess
56 * @see com.sun.star.container.XIndexAccess
57 * @see com.sun.star.container.XElementAccess
58 * @see com.sun.star.text.XAutoTextGroup
59 * @see com.sun.star.text.AutoTextContainer
60 * @see ifc.container._XNamed
61 * @see ifc.container._XNameAccess
62 * @see ifc.container._XIndexAccess
63 * @see ifc.container._XElementAccess
64 * @see ifc.text._XAutoTextGroup
66 public class SwXAutoTextGroup extends TestCase {
67 XTextDocument xTextDoc;
69 /**
70 * Creates text document.
72 @Override
73 protected void initialize( TestParameters tParam, PrintWriter log ) {
74 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
75 try {
76 log.println( "creating a textdocument" );
77 xTextDoc = SOF.createTextDoc( null );
78 } catch ( com.sun.star.uno.Exception e ) {
79 e.printStackTrace( log );
80 throw new StatusException( "Couldn't create document", e );
84 /**
85 * Disposes text document.
87 @Override
88 protected void cleanup( TestParameters tParam, PrintWriter log ) {
89 log.println( " disposing xTextDoc " );
90 util.DesktopTools.closeDoc(xTextDoc);
93 /**
94 * Creating a Testenvironment for the interfaces to be tested.
95 * Creates an instance of the service
96 * <code>com.sun.star.text.AutoTextContainer</code>, then creates a new
97 * group into the container.<p>
98 * Object relations created :
99 * <ul>
100 * <li> <code>'TextRange'</code> for
101 * {@link ifc.text._XAutoTextGroup} range of text</li>
102 * </ul>
104 @Override
105 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
107 XInterface oObj = null;
108 XAutoTextContainer oContainer;
110 log.println( "creating a test environment" );
111 try {
112 XMultiServiceFactory myMSF = Param.getMSF();
113 Object oInst = myMSF.createInstance("com.sun.star.text.AutoTextContainer");
114 oContainer = UnoRuntime.queryInterface(XAutoTextContainer.class,oInst);
115 } catch (com.sun.star.uno.Exception e) {
116 e.printStackTrace(log);
117 throw new StatusException("Couldn't create AutoTextContainer", e);
119 String myGroupName="myNewGroup2*1";
121 XAutoTextContainer xATC = UnoRuntime.queryInterface(XAutoTextContainer.class, oContainer);
123 try {
124 log.println("removing element with name '" + myGroupName + "'");
125 xATC.removeByName(myGroupName);
126 } catch (com.sun.star.container.NoSuchElementException e) {
129 try {
130 log.println("adding element with name '" + myGroupName + "'");
131 xATC.insertNewByName(myGroupName);
132 } catch (ElementExistException ex) {
133 ex.printStackTrace(log);
134 throw new StatusException("could not insert '"+myGroupName+"' into container",ex);
135 } catch (com.sun.star.lang.IllegalArgumentException ex) {
136 ex.printStackTrace(log);
137 throw new StatusException("could not insert '"+myGroupName+"' into container",ex);
141 XNameAccess oContNames = UnoRuntime.queryInterface(XNameAccess.class, oContainer);
143 if (Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE)){
144 String contNames[] = oContNames.getElementNames();
145 for (int i =0; i < contNames.length; i++){
146 log.println("ContainerNames[ "+ i + "]: " + contNames[i]);
150 try{
151 oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName(myGroupName));
152 } catch (com.sun.star.uno.Exception e) {
153 e.printStackTrace(log);
154 throw new StatusException("Couldn't get AutoTextGroup '"+myGroupName + "'", e);
157 log.println("ImplementationName " + utils.getImplName(oObj));
159 log.println( "creating a new environment for AutoTextGroup object" );
160 TestEnvironment tEnv = new TestEnvironment( oObj );
162 XText oText = xTextDoc.getText();
163 oText.insertString(oText.getStart(), "New AutoText", true);
165 log.println( "adding TextRange as mod relation to environment" );
166 tEnv.addObjRelation("TextRange", oText);
168 return tEnv;
169 } // finish method getTestEnvironment
172 } // finish class SwXAutoTextGroup