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.lang.XMultiServiceFactory;
35 import com.sun.star.text.XText;
36 import com.sun.star.text.XTextContent;
37 import com.sun.star.text.XTextCursor;
38 import com.sun.star.text.XTextRange;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.uno.UnoRuntime;
41 
42 
43 /**
44  * Test for object which is represented by service
45  * <code>com.sun.star.text.DocumentIndex</code>. <p>
46  * Object implements the following interfaces :
47  * <ul>
48  *  <li> <code>com::sun::star::lang::XComponent</code></li>
49  *  <li> <code>com::sun::star::text::XDocumentIndex</code></li>
50  *  <li> <code>com::sun::star::text::BaseIndex</code></li>
51  *  <li> <code>com::sun::star::text::XTextContent</code></li>
52  *  <li> <code>com::sun::star::text::DocumentIndex</code></li>
53  * </ul> <p>
54  * This object test <b> is NOT </b> designed to be run in several
55  * threads concurently.
56  * @see com.sun.star.lang.XComponent
57  * @see com.sun.star.text.XDocumentIndex
58  * @see com.sun.star.text.BaseIndex
59  * @see com.sun.star.text.XTextContent
60  * @see com.sun.star.text.DocumentIndex
61  * @see ifc.lang._XComponent
62  * @see ifc.text._XDocumentIndex
63  * @see ifc.text._BaseIndex
64  * @see ifc.text._XTextContent
65  * @see ifc.text._DocumentIndex
66  */
67 public class SwXDocumentIndex extends TestCase {
68     XTextDocument xTextDoc;
69 
70     /**
71     * Creates text document.
72     */
initialize( TestParameters tParam, PrintWriter log )73     protected void initialize( TestParameters tParam, PrintWriter log ) {
74         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
75         try {
76             log.println( "creating a textdocument" );
77             xTextDoc = SOF.createTextDoc( null );
78         } catch ( com.sun.star.uno.Exception e ) {
79             e.printStackTrace( log );
80             throw new StatusException( "Couldn't create document", e );
81         }
82     }
83 
84     /**
85     * Disposes text document.
86     */
cleanup( TestParameters tParam, PrintWriter log )87     protected void cleanup( TestParameters tParam, PrintWriter log ) {
88         log.println( "    disposing xTextDoc " );
89         util.DesktopTools.closeDoc(xTextDoc);
90     }
91 
92     /**
93     * Creating a Testenvironment for the interfaces to be tested.
94     * Creates an instance of the service
95     * <code>com.sun.star.text.DocumentIndex</code>, then created document index
96     * is inserted to the text of the document as content.
97     *
98     */
createTestEnvironment(TestParameters tParam, PrintWriter log)99     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
100         XTextContent xTC = null;
101         Object instance = null;
102 
103         SOfficeFactory SOF = SOfficeFactory.getFactory((XMultiServiceFactory)tParam.getMSF());
104         log.println( "creating a test environment" );
105         try {
106             xTC = SOF.createIndex(xTextDoc, "com.sun.star.text.DocumentIndex");
107             instance = SOF.createIndex(xTextDoc, "com.sun.star.text.DocumentIndex");
108         }
109         catch ( com.sun.star.uno.Exception e) {
110             e.printStackTrace(log);
111             throw new StatusException("Couldn't create the Index", e);
112         }
113 
114         XText oText = xTextDoc.getText();
115         XTextCursor oCursor = oText.createTextCursor();
116 
117         log.println("inserting the Index into text document");
118         try {
119             oText.insertTextContent(oCursor, xTC, false);
120         } catch (com.sun.star.uno.Exception e) {
121             e.printStackTrace(log);
122             throw new StatusException("Couldn't insert the Index", e);
123         }
124 
125         TestEnvironment tEnv = new TestEnvironment(xTC);
126 
127         tEnv.addObjRelation("CONTENT", (XTextContent)
128                         UnoRuntime.queryInterface(XTextContent.class,instance));
129         oCursor.gotoEnd(false);
130         tEnv.addObjRelation("RANGE", (XTextRange)
131                         UnoRuntime.queryInterface(XTextRange.class, oCursor));
132 
133         // relation for XDocumentIndex
134         tEnv.addObjRelation("TextDoc", xTextDoc);
135 
136         return tEnv;
137     } // finish method getTestEnvironment
138 
139 }    // finish class SwXDocumentIndex
140 
141