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