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.SOfficeFactory;
33 
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XText;
37 import com.sun.star.text.XTextContent;
38 import com.sun.star.text.XTextCursor;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.text.XTextSectionsSupplier;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 /**
45  *
46  * initial description
47  * @see com.sun.star.text.XText
48  *
49  */
50 public class SwXTextSections extends TestCase {
51     XTextDocument xTextDoc;
52 
initialize( TestParameters tParam, PrintWriter log )53     protected void initialize( TestParameters tParam, PrintWriter log ) {
54         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
55 
56         try {
57             log.println( "creating a textdocument" );
58             xTextDoc = SOF.createTextDoc( null );
59         } catch ( com.sun.star.uno.Exception e ) {
60             // Some exception occures.FAILED
61             e.printStackTrace( log );
62             throw new StatusException( "Couldn't create document", e );
63         }
64     }
65 
cleanup( TestParameters tParam, PrintWriter log )66     protected void cleanup( TestParameters tParam, PrintWriter log ) {
67         log.println( "    disposing xTextDoc " );
68         util.DesktopTools.closeDoc(xTextDoc);
69     }
70 
71     /**
72      *    creating a Testenvironment for the interfaces to be tested
73      */
createTestEnvironment(TestParameters Param, PrintWriter log )74     public synchronized TestEnvironment createTestEnvironment
75         (TestParameters Param, PrintWriter log ) throws StatusException {
76 
77         XInterface oObj = null;
78         XInterface oTS = null;
79         XNameAccess oTSSuppName = null;
80 
81         XText oText = null;
82 
83         // creation of testobject here
84         // first we write what we are intend to do to log file
85         log.println( "creating a test environment" );
86 
87 
88         oText = xTextDoc.getText();
89         XTextCursor oCursor = oText.createTextCursor();
90 
91 
92         log.println( "inserting TextSections" );
93 
94         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
95             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
96 
97         // First TextSection
98         try {
99             oTS = (XInterface) oDocMSF.createInstance
100                 ("com.sun.star.text.TextSection");
101             XTextContent oTSC = (XTextContent)
102                 UnoRuntime.queryInterface(XTextContent.class, oTS);
103             oText.insertTextContent(oCursor, oTSC, false);
104         }
105         catch(Exception e){
106             e.printStackTrace( log );
107             throw new StatusException( "Couldn't create document", e );
108         }
109 
110         // Second TextSection
111         try {
112             oTS = (XInterface) oDocMSF.createInstance
113                 ("com.sun.star.text.TextSection");
114             XTextContent oTSC = (XTextContent)
115                 UnoRuntime.queryInterface(XTextContent.class, oTS);
116             oText.insertTextContent(oCursor, oTSC, false);
117         }
118         catch(Exception e){
119             e.printStackTrace( log );
120             throw new StatusException( "Couldn't create document", e );
121         }
122 
123 
124         log.println( "try to get a TextSection with the XTextSectionSupplier()" );
125 
126         try{
127             XTextSectionsSupplier oTSSupp = (XTextSectionsSupplier)
128                 UnoRuntime.queryInterface( XTextSectionsSupplier.class,
129                 xTextDoc );
130             oTSSuppName = oTSSupp.getTextSections();
131         }
132         catch(Exception e){
133             System.out.println("Couldn't get Textsection " + e);
134         }
135 
136 
137         oObj = oTSSuppName;
138 
139         log.println( "creating a new environment for TextSections object" );
140         TestEnvironment tEnv = new TestEnvironment( oObj );
141 
142         log.println( "adding TextDocument as mod relation to environment" );
143         tEnv.addObjRelation("TEXTDOC", xTextDoc);
144 
145         return tEnv;
146     } // finish method getTestEnvironment
147 
148 }    // finish class SwXTextSection
149