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.XDocumentIndexesSupplier;
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.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 
43 
44 /**
45  * Test for the object, which is represented by collection of document indexes.
46  * Object implements the following interfaces:
47  * <ul>
48  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
49  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
50  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
51  * </ul> <p>
52  * This object test <b> is NOT </b> designed to be run in several
53  * threads concurently.
54  * @see com.sun.star.container.XNameAccess
55  * @see com.sun.star.container.XIndexAccess
56  * @see com.sun.star.container.XElementAccess
57  * @see ifc.container._XNameAccess
58  * @see ifc.container._XIndexAccess
59  * @see ifc.container._XElementAccess
60  */
61 public class SwXDocumentIndexes extends TestCase {
62     XTextDocument xTextDoc;
63 
64     /**
65     * Creates text document.
66     */
initialize( TestParameters tParam, PrintWriter log )67     protected void initialize( TestParameters tParam, PrintWriter log ) {
68         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
69         try {
70             log.println( "creating a textdocument" );
71             xTextDoc = SOF.createTextDoc( null );
72         } catch ( com.sun.star.uno.Exception e ) {
73             e.printStackTrace( log );
74             throw new StatusException( "Couldn't create document", e );
75         }
76     }
77 
78     /**
79     * Disposes text document.
80     */
cleanup( TestParameters tParam, PrintWriter log )81     protected void cleanup( TestParameters tParam, PrintWriter log ) {
82         log.println( "    disposing xTextDoc " );
83         util.DesktopTools.closeDoc(xTextDoc);
84     }
85 
86 
87     /**
88     * Creating a Testenvironment for the interfaces to be tested.
89     * Creates an instance of the service
90     * <code>com.sun.star.text.ContentIndex</code>, then created content index is
91     * inserted to the text document, and finally all document indexes are gotten
92     * from a text document using <code>XDocumentIndexesSupplier</code> interface.
93     */
createTestEnvironment( TestParameters tParam, PrintWriter log )94     public TestEnvironment createTestEnvironment(
95             TestParameters tParam, PrintWriter log ) throws StatusException {
96         XInterface oObj = null;
97         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
98 
99         log.println( "creating a test environment" );
100         XTextContent xTC = null;
101 
102         try {
103             xTC = SOF.createIndex(xTextDoc,"com.sun.star.text.ContentIndex");
104         }
105         catch ( com.sun.star.uno.Exception e) {
106             e.printStackTrace(log);
107             throw new StatusException("Couldn't create the Index", e);
108         }
109 
110         XText oText = xTextDoc.getText();
111         XTextCursor oCursor = oText.createTextCursor();
112 
113         log.println("inserting the Index into text document");
114         try {
115             oText.insertTextContent(oCursor, xTC, false);
116         } catch (com.sun.star.lang.IllegalArgumentException e) {
117             e.printStackTrace(log);
118             throw new StatusException("Couldn't insert the Index", e);
119         }
120 
121         XDocumentIndexesSupplier xDocInd = (XDocumentIndexesSupplier)
122                 UnoRuntime.queryInterface(XDocumentIndexesSupplier.class,xTextDoc);
123 
124         oObj = xDocInd.getDocumentIndexes();
125         TestEnvironment tEnv = new TestEnvironment(oObj);
126         return tEnv;
127 
128     } // finish method getTestEnvironment
129 
130 }    // finish class SwXDocumentIndexes
131 
132