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 .
21 import com
.sun
.star
.container
.ElementExistException
;
22 import com
.sun
.star
.container
.XNameAccess
;
23 import java
.io
.PrintWriter
;
25 import lib
.StatusException
;
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
;
42 * Test for object which is represented by service
43 * <code>com.sun.star.text.AutoTextGroup</code>. <p>
44 * Object implements the following interfaces :
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>
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
;
70 * Creates text document.
72 protected void initialize( TestParameters tParam
, PrintWriter log
) {
73 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
75 log
.println( "creating a textdocument" );
76 xTextDoc
= SOF
.createTextDoc( null );
77 } catch ( com
.sun
.star
.uno
.Exception e
) {
78 e
.printStackTrace( log
);
79 throw new StatusException( "Couldn't create document", e
);
84 * Disposes text document.
86 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
87 log
.println( " disposing xTextDoc " );
88 util
.DesktopTools
.closeDoc(xTextDoc
);
92 * Creating a Testenvironment for the interfaces to be tested.
93 * Creates an instance of the service
94 * <code>com.sun.star.text.AutoTextContainer</code>, then creates a new
95 * group into the container.<p>
96 * Object relations created :
98 * <li> <code>'TextRange'</code> for
99 * {@link ifc.text._XAutoTextGroup} range of text</li>
102 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
104 XInterface oObj
= null;
105 XAutoTextContainer oContainer
;
107 log
.println( "creating a test environment" );
109 XMultiServiceFactory myMSF
= (XMultiServiceFactory
)Param
.getMSF();
110 Object oInst
= myMSF
.createInstance("com.sun.star.text.AutoTextContainer");
111 oContainer
= UnoRuntime
.queryInterface(XAutoTextContainer
.class,oInst
);
112 } catch (com
.sun
.star
.uno
.Exception e
) {
113 e
.printStackTrace(log
);
114 throw new StatusException("Couldn't create AutoTextContainer", e
);
116 String myGroupName
="myNewGroup2*1";
118 XAutoTextContainer xATC
= UnoRuntime
.queryInterface(XAutoTextContainer
.class, oContainer
);
121 log
.println("removing element with name '" + myGroupName
+ "'");
122 xATC
.removeByName(myGroupName
);
123 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
127 log
.println("adding element with name '" + myGroupName
+ "'");
128 xATC
.insertNewByName(myGroupName
);
129 } catch (ElementExistException ex
) {
130 ex
.printStackTrace(log
);
131 throw new StatusException("could not insert '"+myGroupName
+"' into container",ex
);
132 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
133 ex
.printStackTrace(log
);
134 throw new StatusException("could not insert '"+myGroupName
+"' into container",ex
);
138 XNameAccess oContNames
= UnoRuntime
.queryInterface(XNameAccess
.class, oContainer
);
140 if (Param
.getBool(util
.PropertyName
.DEBUG_IS_ACTIVE
)){
141 String contNames
[] = oContNames
.getElementNames();
142 for (int i
=0; i
< contNames
.length
; i
++){
143 log
.println("ContainerNames[ "+ i
+ "]: " + contNames
[i
]);
148 oObj
= (XInterface
) AnyConverter
.toObject(new Type(XInterface
.class),oContNames
.getByName(myGroupName
));
149 } catch (com
.sun
.star
.uno
.Exception e
) {
150 e
.printStackTrace(log
);
151 throw new StatusException("Couldn't get AutoTextGroup '"+myGroupName
+ "'", e
);
154 log
.println("ImplementationName " + utils
.getImplName(oObj
));
156 log
.println( "creating a new environment for AutoTextGroup object" );
157 TestEnvironment tEnv
= new TestEnvironment( oObj
);
159 XText oText
= xTextDoc
.getText();
160 oText
.insertString(oText
.getStart(), "New AutoText", true);
162 log
.println( "adding TextRange as mod relation to environment" );
163 tEnv
.addObjRelation("TextRange", oText
);
166 } // finish method getTestEnvironment
169 } // finish class SwXAutoTextGroup