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.XFootnote;
36 import com.sun.star.text.XFootnotesSupplier;
37 import com.sun.star.text.XText;
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 object which is represented by service
46  * <code>com.sun.star.text.Footnotes</code>.<p>
47  * Object implements the following interfaces :
48  * <ul>
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.XIndexAccess
55  * @see com.sun.star.container.XElementAccess
56  * @see ifc.container._XIndexAccess
57  * @see ifc.container._XElementAccess
58  */
59 public class SwXFootnotes extends TestCase {
60     XTextDocument xTextDoc;
61 
62     /**
63     * Creates text document.
64     */
initialize( TestParameters tParam, PrintWriter log )65     protected void initialize( TestParameters tParam, PrintWriter log ) {
66         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
67         try {
68             log.println( "creating a textdocument" );
69             xTextDoc = SOF.createTextDoc( null );
70         } catch ( com.sun.star.uno.Exception e ) {
71             e.printStackTrace( log );
72             throw new StatusException( "Couldn't create document", e );
73         }
74     }
75 
76     /**
77     * Disposes text document.
78     */
cleanup( TestParameters tParam, PrintWriter log )79     protected void cleanup( TestParameters tParam, PrintWriter log ) {
80         log.println( "    disposing xTextDoc " );
81         util.DesktopTools.closeDoc(xTextDoc);
82     }
83 
84     /**
85     * Creating a Testenvironment for the interfaces to be tested.
86     * Creates an instance of the service
87     * <code>com.sun.star.text.Footnote</code>. Then inserts created Footnote
88     * to the text, and finally gets all footnotes of text document
89     * through <code>XFootnotesSupplier</code> interface.
90     */
createTestEnvironment( TestParameters Param, PrintWriter log )91     public synchronized TestEnvironment createTestEnvironment(
92             TestParameters Param, PrintWriter log ) throws StatusException {
93         XFootnotesSupplier oInterface = null;
94         XInterface oObj = null;
95 
96         log.println( "Creating a test environment" );
97         XMultiServiceFactory msf = (XMultiServiceFactory)
98             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
99         log.println("creating a footnote");
100         XFootnote oFootnote;
101 
102         try {
103             oFootnote = (XFootnote) UnoRuntime.queryInterface(XFootnote.class,
104                     msf.createInstance("com.sun.star.text.Footnote"));
105         } catch (com.sun.star.uno.Exception e) {
106             e.printStackTrace(log);
107             throw new StatusException("Couldn't create footnote", e);
108         }
109 
110         XText oText = xTextDoc.getText();
111         XTextCursor oCursor = oText.createTextCursor();
112 
113         log.println("inserting the footnote into text document");
114         try {
115             oText.insertTextContent(oCursor, oFootnote, false);
116         } catch (com.sun.star.lang.IllegalArgumentException e) {
117             e.printStackTrace(log);
118             throw new StatusException("Couldn't insert the footnote", e);
119         }
120         oInterface = (XFootnotesSupplier)
121             UnoRuntime.queryInterface(XFootnotesSupplier.class, xTextDoc);
122         oObj = oInterface.getFootnotes();
123 
124         log.println( "creating a new environment for Foontnotes object" );
125         TestEnvironment tEnv = new TestEnvironment(oObj);
126         return tEnv;
127     }
128 
129 }    // finish class SwXFootnote
130 
131