1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._sw;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.DefaultDsc;
33 import util.InstCreator;
34 import util.SOfficeFactory;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.text.XAutoTextContainer;
38 import com.sun.star.text.XAutoTextEntry;
39 import com.sun.star.text.XAutoTextGroup;
40 import com.sun.star.text.XText;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.text.XTextRange;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 
49 /**
50  * Test for object which is represented by service
51  * <code>com.sun.star.text.AutoTextEntry</code>.<p>
52  * Object implements the following interfaces :
53  * <ul>
54  *  <li> <code>com::sun::star::text::XSimpleText</code></li>
55  *  <li> <code>com::sun::star::text::XAutoTextEntry</code></li>
56  *  <li> <code>com::sun::star::text::XTextRange</code></li>
57  *  <li> <code>com::sun::star::text::XText</code></li>
58  * </ul> <p>
59  * This object test <b> is NOT </b> designed to be run in several
60  * threads concurently.
61  * @see com.sun.star.text.AutoTextEntry
62  * @see com.sun.star.text.AutoTextContainer
63  * @see com.sun.star.text.XSimpleText
64  * @see com.sun.star.text.XAutoTextEntry
65  * @see com.sun.star.text.XTextRange
66  * @see com.sun.star.text.XText
67  * @see ifc.text._XSimpleText
68  * @see ifc.text._XAutoTextEntry
69  * @see ifc.text._XTextRange
70  * @see ifc.text._XText
71  */
72 public class SwXAutoTextEntry extends TestCase {
73     XTextDocument xTextDoc;
74     XAutoTextGroup oGroup;
75 
76     /**
77      * Creates text document.
78      */
initialize( TestParameters tParam, PrintWriter log )79     protected void initialize( TestParameters tParam, PrintWriter log ) {
80         XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF();
81         SOfficeFactory SOF = SOfficeFactory.getFactory( msf );
82         try {
83             log.println( "creating a textdocument" );
84             xTextDoc = SOF.createTextDoc( null );
85         } catch ( com.sun.star.uno.Exception e ) {
86             e.printStackTrace( log );
87             throw new StatusException( "Couldn't create document", e );
88         }
89     }
90 
91     /**
92      *  Removes added element from AutoTextGroup
93      */
cleanup( TestParameters Param, PrintWriter log)94     protected void cleanup( TestParameters Param, PrintWriter log) {
95         try {
96             if ( oGroup.hasByName("NewEntryName") ) {
97                 log.println("Removing 'NewEntryName' element");
98                 oGroup.removeByName("NewEntryName");
99             }
100         } catch ( com.sun.star.container.NoSuchElementException e ) {
101             log.println("Cannot remove TextEntry from group...");
102             e.printStackTrace(log);
103         }
104         log.println( "disposing xTextDoc " );
105         util.DesktopTools.closeDoc(xTextDoc);
106     }
107 
108 
109     /**
110      * Creating a Testenvironment for the interfaces to be tested.
111      * Creates an instance of the service
112      * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts'
113      * group from the given container using <code>XNameAccess</code> interface,
114      * and inserts some text entry to this group. Then entry passed as test
115      * component.<p>
116      *     Object relations created :
117      * <ul>
118      *  <li><code>'XTEXTINFO'</code> for
119      *    {@link ifc.text._XText} : creates tables 6x4</li>
120      *  <li><code>'TEXTDOC'</code> for
121      *    {@link ifc.text._XAutoTextEntry} : text document</li>
122      * </ul>
123      */
createTestEnvironment(TestParameters Param, PrintWriter log)124     protected synchronized TestEnvironment createTestEnvironment
125             (TestParameters Param, PrintWriter log) {
126 
127         XAutoTextEntry oEntry = null;
128         XAutoTextContainer oContainer;
129         XInterface oObj = null;
130         int n = 0;
131         int nCount = 0;
132 
133         log.println( "creating a test environment" );
134         try {
135             XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF();
136             Object oInst = myMSF.createInstance
137                     ("com.sun.star.text.AutoTextContainer");
138             oContainer = (XAutoTextContainer)
139             UnoRuntime.queryInterface(XAutoTextContainer.class,oInst);
140         } catch (com.sun.star.uno.Exception e) {
141             e.printStackTrace(log);
142             throw new StatusException("Couldn't create AutoTextContainer", e);
143         }
144 
145         XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer);
146 
147         String contNames[] = oContNames.getElementNames();
148         for (int i =0; i < contNames.length; i++){
149             log.println("ContainerNames[ "+ i + "]: " + contNames[i]);
150         }
151 
152         try{
153             oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName("mytexts"));
154         } catch (com.sun.star.uno.Exception e) {
155             e.printStackTrace(log);
156             throw new StatusException("Couldn't get AutoTextGroup", e);
157         }
158 
159         oGroup = (XAutoTextGroup) UnoRuntime.queryInterface
160                 (XAutoTextGroup.class, oObj);
161         String[] oENames = oGroup.getElementNames();
162         for (int i=0; i<oENames.length; i++) {
163             log.println("AutoTextEntryNames[" + i + "]: " + oENames[i]);
164         }
165 
166         XText oText = xTextDoc.getText();
167         oText.insertString(oText.getStart(), "New AutoText", true);
168         XTextRange oTextRange = (XTextRange) oText;
169         try {
170             if ( oGroup.hasByName("NewEntryName") ) {
171                 oGroup.removeByName("NewEntryName");
172                 log.println("Element 'NewEntryName' exists, removing...");
173             }
174             log.println("Adding new element 'NewEntryName' to group...");
175             oGroup.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange);
176             oEntry = (XAutoTextEntry) AnyConverter.toObject(
177                     new Type(XAutoTextEntry.class),oGroup.getByName("NewEntryName"));
178         } catch ( com.sun.star.container.ElementExistException e ) {
179             e.printStackTrace(log);
180         } catch ( com.sun.star.container.NoSuchElementException e ) {
181             e.printStackTrace(log);
182         } catch ( com.sun.star.lang.WrappedTargetException e ) {
183             e.printStackTrace(log);
184         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
185             e.printStackTrace(log);
186         }
187 
188         oObj = oEntry;
189 
190         log.println("Trying to use XText as TextRange in the method applyTo");
191         oEntry.applyTo(oTextRange);
192 
193         oTextRange = oText.createTextCursor();
194         log.println("Trying to use XTextCursor as TextRange in the method applyTo");
195         oEntry.applyTo(oTextRange);
196 
197         log.println( "creating a new environment for AutoTextEntry object" );
198         TestEnvironment tEnv = new TestEnvironment( oObj );
199 
200         // adding relation for XText
201         DefaultDsc tDsc = new DefaultDsc("com.sun.star.text.XTextContent",
202                 "com.sun.star.text.TextField.DateTime");
203         log.println( "    adding InstCreator object" );
204         tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
205 
206         log.println( "adding TextDocument as mod relation to environment" );
207         tEnv.addObjRelation("TEXTDOC", xTextDoc);
208 
209         return tEnv;
210     } // finish method getTestEnvironment
211 
212 
213 }    // finish class SwXAutoTextEntry
214