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.beans.XPropertySet;
35 import com.sun.star.container.XEnumerationAccess;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.text.XDependentTextField;
38 import com.sun.star.text.XText;
39 import com.sun.star.text.XTextContent;
40 import com.sun.star.text.XTextCursor;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.text.XTextFieldsSupplier;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 
46 
47 /**
48  * Object implements the following interfaces :
49  * <ul>
50  *  <li> <code>com::sun::star::container::XEnumeration</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.XEnumeration
55  * @see ifc.container._XEnumeration
56  */
57 public class SwXFieldEnumeration extends TestCase {
58     XTextDocument xTextDoc;
59 
60     /**
61     * Creates text document.
62     */
initialize( TestParameters tParam, PrintWriter log )63     protected void initialize( TestParameters tParam, PrintWriter log ) {
64         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
65         try {
66             log.println( "creating a textdocument" );
67             xTextDoc = SOF.createTextDoc( null );
68         } catch ( com.sun.star.uno.Exception e ) {
69             e.printStackTrace( log );
70             throw new StatusException( "Couldn't create document", e );
71         }
72     }
73 
74     /**
75     * Disposes text document.
76     */
cleanup( TestParameters tParam, PrintWriter log )77     protected void cleanup( TestParameters tParam, PrintWriter log ) {
78         log.println( "    disposing xTextDoc " );
79         util.DesktopTools.closeDoc(xTextDoc);
80     }
81 
82     /**
83     * Creating a Testenvironment for the interfaces to be tested. FieldMaster
84     * object is created and attached to Bibliography database. After setting
85     * properties of created FieldMaster object, it is attached to the TextField
86     * object. Then TextField is inserted as TextContent to the text document.
87     * At the end, TextFields of text document are obtained and
88     * enumeration of these fields is created.<br>
89     * Creates instances of the services:
90     * <code>com.sun.star.text.FieldMaster.Database</code>,
91     * <code>com.sun.star.text.TextField.Database</code>.<br>
92     *     Object relations created :
93     * <ul>
94     *  <li> <code>'ENUM'</code> for
95     *     {@link ifc.container._XEnumeration} : text fields</li>
96     * </ul>
97     */
createTestEnvironment( TestParameters tParam, PrintWriter log )98     public TestEnvironment createTestEnvironment(
99             TestParameters tParam, PrintWriter log ) throws StatusException {
100         XInterface oObj = null;
101         Object FieldMaster = null;
102         XPropertySet PFieldMaster = null;
103         XDependentTextField xTF = null;
104         XEnumerationAccess xFEA = null;
105         XText the_Text;
106         XTextCursor the_Cursor;
107         XTextContent the_Field;
108 
109         log.println( "creating a test environment" );
110         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
111             UnoRuntime.queryInterface( XMultiServiceFactory.class, xTextDoc );
112 
113         try {
114             FieldMaster = oDocMSF.createInstance
115                 ( "com.sun.star.text.FieldMaster.Database" );
116             PFieldMaster = (XPropertySet) UnoRuntime.queryInterface
117                 (XPropertySet.class,(XInterface) FieldMaster);
118             oObj = (XInterface)
119                 oDocMSF.createInstance("com.sun.star.text.TextField.Database");
120             xTF = (XDependentTextField)
121                 UnoRuntime.queryInterface(XDependentTextField.class,oObj);
122         } catch ( com.sun.star.uno.Exception e ) {
123             e.printStackTrace(log);
124         }
125 
126         try {
127             PFieldMaster.setPropertyValue("DataBaseName","Bibliography");
128             PFieldMaster.setPropertyValue("DataTableName","biblio");
129             PFieldMaster.setPropertyValue("DataColumnName","Address");
130         } catch ( com.sun.star.lang.WrappedTargetException e ) {
131             log.println("Error: can't set PropertyValue to a FieldMaster");
132             e.printStackTrace(log);
133         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
134             log.println("Error: can't set PropertyValue to a FieldMaster");
135             e.printStackTrace(log);
136         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
137             log.println("Error: can't set PropertyValue to a FieldMaster");
138             e.printStackTrace(log);
139         } catch ( com.sun.star.beans.PropertyVetoException e ) {
140             log.println("Error: can't set PropertyValue to a FieldMaster");
141             e.printStackTrace(log);
142         }
143 
144         the_Text = xTextDoc.getText();
145         the_Cursor = the_Text.createTextCursor();
146         the_Field = (XTextContent)
147             UnoRuntime.queryInterface(XTextContent.class, oObj);
148 
149         try {
150             xTF.attachTextFieldMaster(PFieldMaster);
151             the_Text.insertTextContent(the_Cursor,the_Field,false);
152         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
153             e.printStackTrace(log);
154             throw new StatusException( "Couldn't create TextField", e );
155         }
156 
157         // create testobject here
158         XTextFieldsSupplier oTFS = (XTextFieldsSupplier)
159             UnoRuntime.queryInterface( XTextFieldsSupplier.class, xTextDoc );
160         xFEA = oTFS.getTextFields();
161         oObj = oTFS.getTextFields().createEnumeration();
162 
163         log.println( "creating a new environment for FieldEnumeration object" );
164         TestEnvironment tEnv = new TestEnvironment( oObj );
165 
166         tEnv.addObjRelation("ENUM", xFEA);
167 
168         return tEnv;
169     } // finish method getTestEnvironment
170 
171 }    // finish class SwXFieldEnumeration
172 
173