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 java
.io
.PrintWriter
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.DefaultDsc
;
28 import util
.InstCreator
;
29 import util
.SOfficeFactory
;
30 import com
.sun
.star
.container
.XNameAccess
;
31 import com
.sun
.star
.lang
.XMultiServiceFactory
;
32 import com
.sun
.star
.text
.XAutoTextContainer
;
33 import com
.sun
.star
.text
.XAutoTextEntry
;
34 import com
.sun
.star
.text
.XAutoTextGroup
;
35 import com
.sun
.star
.text
.XText
;
36 import com
.sun
.star
.text
.XTextDocument
;
37 import com
.sun
.star
.text
.XTextRange
;
38 import com
.sun
.star
.uno
.AnyConverter
;
39 import com
.sun
.star
.uno
.Type
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
45 * Test for object which is represented by service
46 * <code>com.sun.star.text.AutoTextEntry</code>.<p>
47 * Object implements the following interfaces :
49 * <li> <code>com::sun::star::text::XSimpleText</code></li>
50 * <li> <code>com::sun::star::text::XAutoTextEntry</code></li>
51 * <li> <code>com::sun::star::text::XTextRange</code></li>
52 * <li> <code>com::sun::star::text::XText</code></li>
54 * This object test <b> is NOT </b> designed to be run in several
55 * threads concurently.
56 * @see com.sun.star.text.AutoTextEntry
57 * @see com.sun.star.text.AutoTextContainer
58 * @see com.sun.star.text.XSimpleText
59 * @see com.sun.star.text.XAutoTextEntry
60 * @see com.sun.star.text.XTextRange
61 * @see com.sun.star.text.XText
62 * @see ifc.text._XSimpleText
63 * @see ifc.text._XAutoTextEntry
64 * @see ifc.text._XTextRange
65 * @see ifc.text._XText
67 public class SwXAutoTextEntry
extends TestCase
{
68 XTextDocument xTextDoc
;
69 XAutoTextGroup oGroup
;
72 * Creates text document.
75 protected void initialize( TestParameters tParam
, PrintWriter log
) {
76 XMultiServiceFactory msf
= tParam
.getMSF();
77 SOfficeFactory SOF
= SOfficeFactory
.getFactory( msf
);
79 log
.println( "creating a textdocument" );
80 xTextDoc
= SOF
.createTextDoc( null );
81 } catch ( com
.sun
.star
.uno
.Exception e
) {
82 e
.printStackTrace( log
);
83 throw new StatusException( "Couldn't create document", e
);
88 * Removes added element from AutoTextGroup
91 protected void cleanup( TestParameters Param
, PrintWriter log
) {
93 if ( oGroup
.hasByName("NewEntryName") ) {
94 log
.println("Removing 'NewEntryName' element");
95 oGroup
.removeByName("NewEntryName");
97 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
98 log
.println("Cannot remove TextEntry from group...");
99 e
.printStackTrace(log
);
101 log
.println( "disposing xTextDoc " );
102 util
.DesktopTools
.closeDoc(xTextDoc
);
107 * Creating a Testenvironment for the interfaces to be tested.
108 * Creates an instance of the service
109 * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts'
110 * group from the given container using <code>XNameAccess</code> interface,
111 * and inserts some text entry to this group. Then entry passed as test
113 * Object relations created :
115 * <li><code>'XTEXTINFO'</code> for
116 * {@link ifc.text._XText} : creates tables 6x4</li>
117 * <li><code>'TEXTDOC'</code> for
118 * {@link ifc.text._XAutoTextEntry} : text document</li>
122 protected synchronized TestEnvironment createTestEnvironment
123 (TestParameters Param
, PrintWriter log
) {
125 XAutoTextEntry oEntry
= null;
126 XAutoTextContainer oContainer
;
127 XInterface oObj
= null;
128 log
.println( "creating a test environment" );
130 XMultiServiceFactory myMSF
= Param
.getMSF();
131 Object oInst
= myMSF
.createInstance
132 ("com.sun.star.text.AutoTextContainer");
133 oContainer
= UnoRuntime
.queryInterface(XAutoTextContainer
.class,oInst
);
134 } catch (com
.sun
.star
.uno
.Exception e
) {
135 e
.printStackTrace(log
);
136 throw new StatusException("Couldn't create AutoTextContainer", e
);
139 XNameAccess oContNames
= UnoRuntime
.queryInterface(XNameAccess
.class, oContainer
);
141 String contNames
[] = oContNames
.getElementNames();
142 for (int i
=0; i
< contNames
.length
; i
++){
143 log
.println("ContainerNames[ "+ i
+ "]: " + contNames
[i
]);
147 oObj
= (XInterface
) AnyConverter
.toObject(new Type(XInterface
.class),oContNames
.getByName("mytexts"));
148 } catch (com
.sun
.star
.uno
.Exception e
) {
149 e
.printStackTrace(log
);
150 throw new StatusException("Couldn't get AutoTextGroup", e
);
153 oGroup
= UnoRuntime
.queryInterface
154 (XAutoTextGroup
.class, oObj
);
155 String
[] oENames
= oGroup
.getElementNames();
156 for (int i
=0; i
<oENames
.length
; i
++) {
157 log
.println("AutoTextEntryNames[" + i
+ "]: " + oENames
[i
]);
160 XText oText
= xTextDoc
.getText();
161 oText
.insertString(oText
.getStart(), "New AutoText", true);
162 XTextRange oTextRange
= oText
;
164 if ( oGroup
.hasByName("NewEntryName") ) {
165 oGroup
.removeByName("NewEntryName");
166 log
.println("Element 'NewEntryName' exists, removing...");
168 log
.println("Adding new element 'NewEntryName' to group...");
169 oGroup
.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange
);
170 oEntry
= (XAutoTextEntry
) AnyConverter
.toObject(
171 new Type(XAutoTextEntry
.class),oGroup
.getByName("NewEntryName"));
172 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
173 e
.printStackTrace(log
);
174 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
175 e
.printStackTrace(log
);
176 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
177 e
.printStackTrace(log
);
178 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
179 e
.printStackTrace(log
);
184 log
.println("Trying to use XText as TextRange in the method applyTo");
185 oEntry
.applyTo(oTextRange
);
187 oTextRange
= oText
.createTextCursor();
188 log
.println("Trying to use XTextCursor as TextRange in the method applyTo");
189 oEntry
.applyTo(oTextRange
);
191 log
.println( "creating a new environment for AutoTextEntry object" );
192 TestEnvironment tEnv
= new TestEnvironment( oObj
);
194 // adding relation for XText
195 DefaultDsc tDsc
= new DefaultDsc("com.sun.star.text.XTextContent",
196 "com.sun.star.text.TextField.DateTime");
197 log
.println( " adding InstCreator object" );
198 tEnv
.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc
, tDsc
) );
200 log
.println( "adding TextDocument as mod relation to environment" );
201 tEnv
.addObjRelation("TEXTDOC", xTextDoc
);
204 } // finish method getTestEnvironment
207 } // finish class SwXAutoTextEntry