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.container.XNamed;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XReferenceMarksSupplier;
37 import com.sun.star.text.XText;
38 import com.sun.star.text.XTextContent;
39 import com.sun.star.text.XTextCursor;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 /**
45  * Test for object which is represented by service
46  * <code>com.sun.star.text.ReferenceMarks</code>. <p>
47  * Object implements the following interfaces :
48  * <ul>
49  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
50  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
51  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
52  * </ul> <p>
53  * This object test <b> is NOT </b> designed to be run in several
54  * threads concurently.
55  * @see com.sun.star.container.XNameAccess
56  * @see com.sun.star.container.XIndexAccess
57  * @see com.sun.star.container.XElementAccess
58  * @see com.sun.star.text.ReferenceMarks
59  * @see ifc.container._XNameAccess
60  * @see ifc.container._XIndexAccess
61  * @see ifc.container._XElementAccess
62  */
63 public class SwXReferenceMarks extends TestCase {
64     XTextDocument xTextDoc;
65 
66     /**
67     * Creates text document.
68     */
initialize( TestParameters tParam, PrintWriter log )69     protected void initialize( TestParameters tParam, PrintWriter log ) {
70         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
71         try {
72             log.println( "creating a textdocument" );
73             xTextDoc = SOF.createTextDoc( null );
74         } catch ( com.sun.star.uno.Exception e ) {
75             e.printStackTrace( log );
76             throw new StatusException( "Couldn't create document", e );
77         }
78     }
79 
80     /**
81     * Disposes text document.
82     */
cleanup( TestParameters tParam, PrintWriter log )83     protected void cleanup( TestParameters tParam, PrintWriter log ) {
84         log.println( "    disposing xTextDoc " );
85         util.DesktopTools.closeDoc(xTextDoc);
86     }
87 
88     /**
89     * Creating a Testenvironment for the interfaces to be tested.
90     * Creates an instances of the service
91     * <code>com.sun.star.text.ReferenceMark</code>, then sets new names to
92     * created ReferenceMark's using <code>XNamed</code> interface. Then renamed
93     * ReferenceMark's are inserted to a major text of text document. Finally,
94     * ReferenceMarks are gotten from text document using
95     * <code>XReferenceMarksSupplier</code> interface.
96     */
createTestEnvironment( TestParameters tParam, PrintWriter log )97     public synchronized TestEnvironment createTestEnvironment(
98             TestParameters tParam, PrintWriter log ) throws StatusException {
99         XInterface oObj = null;
100         XText oText = null;
101         String Name = "SwXReferenceMark01";
102         String Name2 = "SwXReferenceMark02";
103 
104         log.println( "creating a test environment" );
105         oText = xTextDoc.getText();
106 
107         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
108             UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
109 
110         // Creation and insertion of ReferenceMark01
111         try {
112             oObj = (XInterface)
113                 oDocMSF.createInstance( "com.sun.star.text.ReferenceMark" );
114         } catch ( com.sun.star.uno.Exception e ) {
115             e.printStackTrace( log );
116             throw new StatusException( "Couldn't get ReferenceMark", e);
117         }
118         XNamed oObjN = (XNamed) UnoRuntime.queryInterface(XNamed.class, oObj);
119         oObjN.setName(Name);
120         XTextContent oObjTC = (XTextContent)
121             UnoRuntime.queryInterface(XTextContent.class, oObj);
122         XTextCursor oCursor = oText.createTextCursor();
123         try {
124             oText.insertTextContent(oCursor, oObjTC, false);
125         } catch ( com.sun.star.lang.IllegalArgumentException e ){
126             e.printStackTrace( log );
127             throw new StatusException(" Couldn't insert ReferenceMark01", e);
128         }
129 
130         // Creation and insertion of ReferenceMark02
131         try {
132             oObj = (XInterface)
133                 oDocMSF.createInstance( "com.sun.star.text.ReferenceMark" );
134         } catch ( com.sun.star.uno.Exception e ) {
135             e.printStackTrace( log );
136             throw new StatusException( "Couldn't get ReferenceMark", e);
137         }
138         XNamed oObjN2 = (XNamed) UnoRuntime.queryInterface(XNamed.class, oObj);
139         oObjN2.setName(Name2);
140 
141         XTextContent oObjTC2 = (XTextContent)
142             UnoRuntime.queryInterface(XTextContent.class, oObj);
143         try {
144             oText.insertTextContent(oCursor, oObjTC2, false);
145         } catch ( com.sun.star.lang.IllegalArgumentException e ){
146             e.printStackTrace( log );
147             throw new StatusException(" Couldn't insert ReferenceMark02", e);
148         }
149 
150         // getting ReferenceMarks from text document
151         XReferenceMarksSupplier oRefSupp = (XReferenceMarksSupplier)
152             UnoRuntime.queryInterface(XReferenceMarksSupplier.class, xTextDoc);
153         oObj = oRefSupp.getReferenceMarks();
154 
155         TestEnvironment tEnv = new TestEnvironment( oObj );
156         return tEnv;
157     } // finish method getTestEnvironment
158 
159 }    // finish class SwXReferenceMarks
160 
161