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