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.
74 protected void initialize( TestParameters tParam
, PrintWriter log
) {
75 XMultiServiceFactory msf
= (XMultiServiceFactory
) tParam
.getMSF();
76 SOfficeFactory SOF
= SOfficeFactory
.getFactory( msf
);
78 log
.println( "creating a textdocument" );
79 xTextDoc
= SOF
.createTextDoc( null );
80 } catch ( com
.sun
.star
.uno
.Exception e
) {
81 e
.printStackTrace( log
);
82 throw new StatusException( "Couldn't create document", e
);
87 * Removes added element from AutoTextGroup
89 protected void cleanup( TestParameters Param
, PrintWriter log
) {
91 if ( oGroup
.hasByName("NewEntryName") ) {
92 log
.println("Removing 'NewEntryName' element");
93 oGroup
.removeByName("NewEntryName");
95 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
96 log
.println("Cannot remove TextEntry from group...");
97 e
.printStackTrace(log
);
99 log
.println( "disposing xTextDoc " );
100 util
.DesktopTools
.closeDoc(xTextDoc
);
105 * Creating a Testenvironment for the interfaces to be tested.
106 * Creates an instance of the service
107 * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts'
108 * group from the given container using <code>XNameAccess</code> interface,
109 * and inserts some text entry to this group. Then entry passed as test
111 * Object relations created :
113 * <li><code>'XTEXTINFO'</code> for
114 * {@link ifc.text._XText} : creates tables 6x4</li>
115 * <li><code>'TEXTDOC'</code> for
116 * {@link ifc.text._XAutoTextEntry} : text document</li>
119 protected synchronized TestEnvironment createTestEnvironment
120 (TestParameters Param
, PrintWriter log
) {
122 XAutoTextEntry oEntry
= null;
123 XAutoTextContainer oContainer
;
124 XInterface oObj
= null;
125 log
.println( "creating a test environment" );
127 XMultiServiceFactory myMSF
= (XMultiServiceFactory
)Param
.getMSF();
128 Object oInst
= myMSF
.createInstance
129 ("com.sun.star.text.AutoTextContainer");
130 oContainer
= UnoRuntime
.queryInterface(XAutoTextContainer
.class,oInst
);
131 } catch (com
.sun
.star
.uno
.Exception e
) {
132 e
.printStackTrace(log
);
133 throw new StatusException("Couldn't create AutoTextContainer", e
);
136 XNameAccess oContNames
= UnoRuntime
.queryInterface(XNameAccess
.class, oContainer
);
138 String contNames
[] = oContNames
.getElementNames();
139 for (int i
=0; i
< contNames
.length
; i
++){
140 log
.println("ContainerNames[ "+ i
+ "]: " + contNames
[i
]);
144 oObj
= (XInterface
) AnyConverter
.toObject(new Type(XInterface
.class),oContNames
.getByName("mytexts"));
145 } catch (com
.sun
.star
.uno
.Exception e
) {
146 e
.printStackTrace(log
);
147 throw new StatusException("Couldn't get AutoTextGroup", e
);
150 oGroup
= UnoRuntime
.queryInterface
151 (XAutoTextGroup
.class, oObj
);
152 String
[] oENames
= oGroup
.getElementNames();
153 for (int i
=0; i
<oENames
.length
; i
++) {
154 log
.println("AutoTextEntryNames[" + i
+ "]: " + oENames
[i
]);
157 XText oText
= xTextDoc
.getText();
158 oText
.insertString(oText
.getStart(), "New AutoText", true);
159 XTextRange oTextRange
= oText
;
161 if ( oGroup
.hasByName("NewEntryName") ) {
162 oGroup
.removeByName("NewEntryName");
163 log
.println("Element 'NewEntryName' exists, removing...");
165 log
.println("Adding new element 'NewEntryName' to group...");
166 oGroup
.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange
);
167 oEntry
= (XAutoTextEntry
) AnyConverter
.toObject(
168 new Type(XAutoTextEntry
.class),oGroup
.getByName("NewEntryName"));
169 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
170 e
.printStackTrace(log
);
171 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
172 e
.printStackTrace(log
);
173 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
174 e
.printStackTrace(log
);
175 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
176 e
.printStackTrace(log
);
181 log
.println("Trying to use XText as TextRange in the method applyTo");
182 oEntry
.applyTo(oTextRange
);
184 oTextRange
= oText
.createTextCursor();
185 log
.println("Trying to use XTextCursor as TextRange in the method applyTo");
186 oEntry
.applyTo(oTextRange
);
188 log
.println( "creating a new environment for AutoTextEntry object" );
189 TestEnvironment tEnv
= new TestEnvironment( oObj
);
191 // adding relation for XText
192 DefaultDsc tDsc
= new DefaultDsc("com.sun.star.text.XTextContent",
193 "com.sun.star.text.TextField.DateTime");
194 log
.println( " adding InstCreator object" );
195 tEnv
.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc
, tDsc
) );
197 log
.println( "adding TextDocument as mod relation to environment" );
198 tEnv
.addObjRelation("TEXTDOC", xTextDoc
);
201 } // finish method getTestEnvironment
204 } // finish class SwXAutoTextEntry