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.InstCreator;
33 import util.SOfficeFactory;
34 import util.TableDsc;
35 
36 import com.sun.star.container.XIndexAccess;
37 import com.sun.star.container.XNameAccess;
38 import com.sun.star.container.XNamed;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.text.XText;
42 import com.sun.star.text.XTextColumns;
43 import com.sun.star.text.XTextContent;
44 import com.sun.star.text.XTextCursor;
45 import com.sun.star.text.XTextDocument;
46 import com.sun.star.text.XTextSection;
47 import com.sun.star.text.XTextSectionsSupplier;
48 import com.sun.star.text.XWordCursor;
49 import com.sun.star.uno.AnyConverter;
50 import com.sun.star.uno.Type;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 
54 /**
55  *
56  * initial description
57  * @see com.sun.star.text.XText
58  *
59  */
60 
61 public class SwXTextSection extends TestCase {
62     XTextDocument xTextDoc;
63 
initialize( TestParameters tParam, PrintWriter log )64     protected void initialize( TestParameters tParam, PrintWriter log ) {
65     SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
66 
67     try {
68         log.println( "creating a textdocument" );
69         xTextDoc = SOF.createTextDoc( null );
70     } catch ( com.sun.star.uno.Exception e ) {
71         // Some exception occures.FAILED
72         e.printStackTrace( log );
73         throw new StatusException( "Couldn't create document", e );
74     }
75     }
76 
cleanup( TestParameters tParam, PrintWriter log )77     protected void cleanup( TestParameters tParam, PrintWriter log ) {
78     log.println( "    disposing xTextDoc " );
79     util.DesktopTools.closeDoc(xTextDoc);
80     }
81 
82     /**
83      *    creating a Testenvironment for the interfaces to be tested
84      */
createTestEnvironment(TestParameters Param, PrintWriter log)85     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
86 
87         XInterface oObj = null;
88         XInterface oTS = null;
89         XTextSection xTS = null;
90         XText oText = null;
91         XTextColumns TC = null;
92         Object instance = null;
93 
94         log.println( "creating a test environment" );
95 
96         oText = xTextDoc.getText();
97             XTextCursor oCursor = oText.createTextCursor();
98 
99         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
100             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
101 
102         try {
103             XTextSectionsSupplier oTSSupp = (XTextSectionsSupplier)
104             UnoRuntime.queryInterface
105                     ( XTextSectionsSupplier.class, xTextDoc );
106             XNameAccess oTSSuppName = oTSSupp.getTextSections();
107 
108             //cleanup if necessary
109             if (oTSSuppName.hasByName("SwXTextSection")) {
110                 XTextSection old = (XTextSection) AnyConverter.toObject(
111                     new Type(XTextSection.class),
112                         oTSSuppName.getByName("SwXTextSection"));
113                 XComponent oldC = (XComponent)
114                         UnoRuntime.queryInterface(XComponent.class,old);
115                 oldC.dispose();
116                 oText.setString("");
117             }
118 
119             //insert two sections parent and child
120             oTS = (XInterface) oDocMSF.createInstance
121                     ("com.sun.star.text.TextSection");
122             instance = oDocMSF.createInstance("com.sun.star.text.TextSection");
123             XTextContent oTSC = (XTextContent)
124                     UnoRuntime.queryInterface(XTextContent.class, oTS);
125             oText.insertTextContent(oCursor, oTSC, false);
126             XWordCursor oWordC = (XWordCursor)
127                 UnoRuntime.queryInterface(XWordCursor.class, oCursor);
128             oCursor.setString("End of TextSection");
129             oCursor.gotoStart(false);
130             oCursor.setString("Start of TextSection ");
131             oWordC.gotoEndOfWord(false);
132             XInterface oTS2 = (XInterface) oDocMSF.createInstance
133                 ("com.sun.star.text.TextSection");
134             oTSC = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, oTS2);
135             oText.insertTextContent(oCursor, oTSC, false);
136 
137             XIndexAccess oTSSuppIndex = (XIndexAccess)
138             UnoRuntime.queryInterface(XIndexAccess.class, oTSSuppName);
139 
140             log.println( "getting a TextSection with the XTextSectionSupplier()" );
141             xTS = (XTextSection) AnyConverter.toObject(
142                         new Type(XTextSection.class),oTSSuppIndex.getByIndex(0));
143             XNamed xTSName = (XNamed)
144                     UnoRuntime.queryInterface( XNamed.class, xTS);
145             xTSName.setName("SwXTextSection");
146         }
147         catch(Exception e){
148             System.out.println("Couldn't get Textsection " + e);
149         }
150 
151         oObj = xTS;
152 
153         log.println( "creating a new environment for TextSection object" );
154         TestEnvironment tEnv = new TestEnvironment( oObj );
155 
156         log.println( "adding InstDescriptor object" );
157         TableDsc tDsc = new TableDsc( 6, 4 );
158 
159         log.println( "adding InstCreator object" );
160         tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
161 
162         tEnv.addObjRelation("TRO",new Boolean(true));
163 
164         try {
165             TC = (XTextColumns) UnoRuntime.queryInterface(XTextColumns.class,
166                     oDocMSF.createInstance("com.sun.star.text.TextColumns"));
167         } catch ( com.sun.star.uno.Exception e ) {
168             e.printStackTrace(log);
169             throw new StatusException
170                     ("Couldn't create instance of service TextColumns", e );
171         }
172         tEnv.addObjRelation("TC",TC);
173 
174         tEnv.addObjRelation("CONTENT", (XTextContent)
175                         UnoRuntime.queryInterface(XTextContent.class,instance));
176         tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
177 
178         return tEnv;
179     } // finish method getTestEnvironment
180 
181 }// finish class SwXTextSection
182 
183 
184