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.InstCreator;
37 import util.ParagraphDsc;
38 import util.SOfficeFactory;
39 import util.TextSectionDsc;
40 
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.text.XFootnote;
43 import com.sun.star.text.XSimpleText;
44 import com.sun.star.text.XText;
45 import com.sun.star.text.XTextCursor;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51  * Object implements the following interfaces :
52  * <ul>
53  *  <li> <code>com::sun::star::text::XTextRangeMover</code></li>
54  *  <li> <code>com::sun::star::text::XSimpleText</code></li>
55  *  <li> <code>com::sun::star::text::XTextRange</code></li>
56  *  <li> <code>com::sun::star::text::XRelativeTextContentInsert</code></li>
57  *  <li> <code>com::sun::star::text::XTextRangeCompare</code></li>
58  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
59  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
60  *  <li> <code>com::sun::star::text::XText</code></li>
61  * </ul> <p>
62  * This object test <b> is NOT </b> designed to be run in several
63  * threads concurently.
64  * @see com.sun.star.text.XTextRangeMover
65  * @see com.sun.star.text.XSimpleText
66  * @see com.sun.star.text.XTextRange
67  * @see com.sun.star.text.XRelativeTextContentInsert
68  * @see com.sun.star.text.XTextRangeCompare
69  * @see com.sun.star.container.XElementAccess
70  * @see com.sun.star.container.XEnumerationAccess
71  * @see com.sun.star.text.XText
72  * @see ifc.text._XTextRangeMover
73  * @see ifc.text._XSimpleText
74  * @see ifc.text._XTextRange
75  * @see ifc.text._XRelativeTextContentInsert
76  * @see ifc.text._XTextRangeCompare
77  * @see ifc.container._XElementAccess
78  * @see ifc.container._XEnumerationAccess
79  * @see ifc.text._XText
80  */
81 public class SwXFootnoteText extends TestCase {
82     XTextDocument xTextDoc;
83     SOfficeFactory SOF;
84 
85     /**
86     * Creates text document.
87     */
88     protected void initialize( TestParameters tParam, PrintWriter log ) {
89         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
90         try {
91             log.println( "creating a textdocument" );
92             xTextDoc = SOF.createTextDoc( null );
93         } catch ( com.sun.star.uno.Exception e ) {
94             e.printStackTrace( log );
95             throw new StatusException( "Couldn't create document", e );
96         }
97     }
98 
99     /**
100     * Disposes text document.
101     */
102     protected void cleanup( TestParameters tParam, PrintWriter log ) {
103         log.println( "    disposing xTextDoc " );
104         util.DesktopTools.closeDoc(xTextDoc);
105     }
106 
107     /**
108     * Creating a Testenvironment for the interfaces to be tested.
109     * Creates an instance of the service
110     * <code>com.sun.star.text.Footnote</code>. Then inserts created Footnote
111     * to the text, and finally sets a string to the footnote. Then the text
112     * gotten from the footnote is returned as tested component.<br>
113     *     Object relations created :
114     * <ul>
115     *  <li> <code>'TEXT'</code> for
116     *    {@link ifc.text._XTextRangeCompare} : footnote text</li>
117     *  <li> <code>'XTEXTINFO'</code> for
118     *    {@link ifc.text._XRelativeTextContentInsert},
119     *    {@link ifc.text._XText} : text section creator</li>
120     *  <li> <code>'PARA'</code> for
121     *    {@link ifc.text._XRelativeTextContentInsert} : paragraph creator</li>
122     * </ul>
123     */
124     public synchronized TestEnvironment createTestEnvironment(
125             TestParameters tParam, PrintWriter log ) throws StatusException {
126         XInterface oObj = null;
127         XFootnote oFootnote;
128 
129         log.println( "creating a test environment" );
130         XMultiServiceFactory msf = (XMultiServiceFactory)
131             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
132         log.println("creating a footnote");
133 
134         try {
135             oFootnote = (XFootnote)UnoRuntime.queryInterface(XFootnote.class,
136                     msf.createInstance("com.sun.star.text.Footnote"));
137         } catch (com.sun.star.uno.Exception e) {
138             e.printStackTrace(log);
139             throw new StatusException("Couldn't create footnote", e);
140         }
141 
142         XText oText = xTextDoc.getText();
143         XTextCursor oCursor = oText.createTextCursor();
144 
145         log.println("inserting the footnote into text document");
146         try {
147             oText.insertTextContent(oCursor, oFootnote, false);
148         } catch (com.sun.star.lang.IllegalArgumentException e) {
149             e.printStackTrace(log);
150             throw new StatusException("Couldn't insert the footnote", e);
151         }
152 
153         XSimpleText oFootText = (XSimpleText)
154             UnoRuntime.queryInterface(XSimpleText.class, oFootnote);
155         oFootText.setString("SwXFootnoteText");
156 
157         oObj = oFootText.getText();
158 
159         TestEnvironment tEnv = new TestEnvironment(oObj);
160 
161         log.println( "adding TextDocument as mod relation to environment" );
162         tEnv.addObjRelation("TEXT", (XText) oObj);
163 
164         TextSectionDsc tDsc = new TextSectionDsc();
165         log.println( "    adding InstCreator object" );
166         tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
167 
168         log.println( "    adding Paragraph" );
169         ParagraphDsc pDsc = new ParagraphDsc();
170         tEnv.addObjRelation( "PARA", new InstCreator( xTextDoc, pDsc ) );
171 
172         return tEnv;
173     } // finish method getTestEnvironment
174 
175 }    // finish class SwXFootnoteText
176 
177