1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sw;
29 
30 import com.sun.star.container.ElementExistException;
31 import com.sun.star.container.XNameAccess;
32 import java.io.PrintWriter;
33 
34 import lib.StatusException;
35 import lib.TestCase;
36 import lib.TestEnvironment;
37 import lib.TestParameters;
38 import util.SOfficeFactory;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XAutoTextContainer;
41 import com.sun.star.text.XText;
42 import com.sun.star.text.XTextDocument;
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 import util.utils;
48 
49 
50 /**
51  * Test for object which is represented by service
52  * <code>com.sun.star.text.AutoTextGroup</code>. <p>
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li> <code>com::sun::star::container::XNamed</code></li>
56  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
57  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
58  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
59  *  <li> <code>com::sun::star::text::XAutoTextGroup</code></li>
60  * </ul> <p>
61  * This object test <b> is NOT </b> designed to be run in several
62  * threads concurently.
63  * @see com.sun.star.container.XNamed
64  * @see com.sun.star.container.XNameAccess
65  * @see com.sun.star.container.XIndexAccess
66  * @see com.sun.star.container.XElementAccess
67  * @see com.sun.star.text.XAutoTextGroup
68  * @see com.sun.star.text.AutoTextContainer
69  * @see ifc.container._XNamed
70  * @see ifc.container._XNameAccess
71  * @see ifc.container._XIndexAccess
72  * @see ifc.container._XElementAccess
73  * @see ifc.text._XAutoTextGroup
74  */
75 public class SwXAutoTextGroup extends TestCase {
76     XTextDocument xTextDoc;
77 
78     /**
79      * Creates text document.
80      */
81     protected void initialize( TestParameters tParam, PrintWriter log ) {
82         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
83         try {
84             log.println( "creating a textdocument" );
85             xTextDoc = SOF.createTextDoc( null );
86         } catch ( com.sun.star.uno.Exception e ) {
87             e.printStackTrace( log );
88             throw new StatusException( "Couldn't create document", e );
89         }
90     }
91 
92     /**
93      * Disposes text document.
94      */
95     protected void cleanup( TestParameters tParam, PrintWriter log ) {
96         log.println( "    disposing xTextDoc " );
97         util.DesktopTools.closeDoc(xTextDoc);
98     }
99 
100     /**
101      * Creating a Testenvironment for the interfaces to be tested.
102      * Creates an instance of the service
103      * <code>com.sun.star.text.AutoTextContainer</code>, then creates a new
104      * group into the container.<p>
105      *     Object relations created :
106      * <ul>
107      *  <li> <code>'TextRange'</code> for
108      *      {@link ifc.text._XAutoTextGroup} range of text</li>
109      * </ul>
110      */
111     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
112 
113         XInterface oObj = null;
114         XAutoTextContainer oContainer;
115 
116         log.println( "creating a test environment" );
117         try {
118             XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF();
119             Object oInst = myMSF.createInstance("com.sun.star.text.AutoTextContainer");
120             oContainer = (XAutoTextContainer) UnoRuntime.queryInterface(XAutoTextContainer.class,oInst);
121         } catch (com.sun.star.uno.Exception e) {
122             e.printStackTrace(log);
123             throw new StatusException("Couldn't create AutoTextContainer", e);
124         }
125         String myGroupName="myNewGroup2*1";
126 
127         XAutoTextContainer xATC = (XAutoTextContainer) UnoRuntime.queryInterface(XAutoTextContainer.class, oContainer);
128 
129         try {
130             log.println("removing element with name '" + myGroupName + "'");
131             xATC.removeByName(myGroupName);
132         } catch (com.sun.star.container.NoSuchElementException e) {
133         }
134 
135         try {
136             log.println("adding element with name '" + myGroupName + "'");
137             xATC.insertNewByName(myGroupName);
138         } catch (ElementExistException ex) {
139             ex.printStackTrace(log);
140             throw new StatusException("could not insert '"+myGroupName+"' into container",ex);
141         } catch (com.sun.star.lang.IllegalArgumentException ex) {
142             ex.printStackTrace(log);
143             throw new StatusException("could not insert '"+myGroupName+"' into container",ex);
144         }
145 
146 
147         XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer);
148 
149         if (Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE)){
150             String contNames[] = oContNames.getElementNames();
151             for (int i =0; i < contNames.length; i++){
152                 log.println("ContainerNames[ "+ i + "]: " + contNames[i]);
153             }
154         }
155 
156         try{
157             oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName(myGroupName));
158         } catch (com.sun.star.uno.Exception e) {
159             e.printStackTrace(log);
160             throw new StatusException("Couldn't get AutoTextGroup '"+myGroupName + "'", e);
161         }
162 
163         log.println("ImplementationName " + utils.getImplName(oObj));
164 
165         log.println( "creating a new environment for AutoTextGroup object" );
166         TestEnvironment tEnv = new TestEnvironment( oObj );
167 
168         XText oText = xTextDoc.getText();
169         oText.insertString(oText.getStart(), "New AutoText", true);
170 
171         log.println( "adding TextRange as mod relation to environment" );
172         tEnv.addObjRelation("TextRange", oText);
173 
174         return tEnv;
175     } // finish method getTestEnvironment
176 
177 
178 }    // finish class SwXAutoTextGroup
179