Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXFieldEnumeration.java
blob5c891e716b64c7ef0041a6c5e169184d016e54c5
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sw;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.container.XEnumerationAccess;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.text.XDependentTextField;
33 import com.sun.star.text.XText;
34 import com.sun.star.text.XTextContent;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.text.XTextFieldsSupplier;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
42 /**
43 * Object implements the following interfaces :
44 * <ul>
45 * <li> <code>com::sun::star::container::XEnumeration</code></li>
46 * </ul> <p>
47 * This object test <b> is NOT </b> designed to be run in several
48 * threads concurently.
49 * @see com.sun.star.container.XEnumeration
50 * @see ifc.container._XEnumeration
52 public class SwXFieldEnumeration extends TestCase {
53 XTextDocument xTextDoc;
55 /**
56 * Creates text document.
58 @Override
59 protected void initialize( TestParameters tParam, PrintWriter log ) {
60 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
61 try {
62 log.println( "creating a textdocument" );
63 xTextDoc = SOF.createTextDoc( null );
64 } catch ( com.sun.star.uno.Exception e ) {
65 e.printStackTrace( log );
66 throw new StatusException( "Couldn't create document", e );
70 /**
71 * Disposes text document.
73 @Override
74 protected void cleanup( TestParameters tParam, PrintWriter log ) {
75 log.println( " disposing xTextDoc " );
76 util.DesktopTools.closeDoc(xTextDoc);
79 /**
80 * Creating a Testenvironment for the interfaces to be tested. FieldMaster
81 * object is created and attached to Bibliography database. After setting
82 * properties of created FieldMaster object, it is attached to the TextField
83 * object. Then TextField is inserted as TextContent to the text document.
84 * At the end, TextFields of text document are obtained and
85 * enumeration of these fields is created.<br>
86 * Creates instances of the services:
87 * <code>com.sun.star.text.FieldMaster.Database</code>,
88 * <code>com.sun.star.text.TextField.Database</code>.<br>
89 * Object relations created :
90 * <ul>
91 * <li> <code>'ENUM'</code> for
92 * {@link ifc.container._XEnumeration} : text fields</li>
93 * </ul>
95 @Override
96 public TestEnvironment createTestEnvironment(
97 TestParameters tParam, PrintWriter log ) throws StatusException {
98 XInterface oObj = null;
99 Object FieldMaster = null;
100 XPropertySet PFieldMaster = null;
101 XDependentTextField xTF = null;
102 XEnumerationAccess xFEA = null;
103 XText the_Text;
104 XTextCursor the_Cursor;
105 XTextContent the_Field;
107 log.println( "creating a test environment" );
108 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, xTextDoc );
110 try {
111 FieldMaster = oDocMSF.createInstance
112 ( "com.sun.star.text.FieldMaster.Database" );
113 PFieldMaster = UnoRuntime.queryInterface
114 (XPropertySet.class,FieldMaster);
115 oObj = (XInterface)
116 oDocMSF.createInstance("com.sun.star.text.TextField.Database");
117 xTF = UnoRuntime.queryInterface(XDependentTextField.class,oObj);
118 } catch ( com.sun.star.uno.Exception e ) {
119 e.printStackTrace(log);
122 try {
123 PFieldMaster.setPropertyValue("DataBaseName","Bibliography");
124 PFieldMaster.setPropertyValue("DataTableName","biblio");
125 PFieldMaster.setPropertyValue("DataColumnName","Address");
126 } catch ( com.sun.star.lang.WrappedTargetException e ) {
127 log.println("Error: can't set PropertyValue to a FieldMaster");
128 e.printStackTrace(log);
129 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
130 log.println("Error: can't set PropertyValue to a FieldMaster");
131 e.printStackTrace(log);
132 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
133 log.println("Error: can't set PropertyValue to a FieldMaster");
134 e.printStackTrace(log);
135 } catch ( com.sun.star.beans.PropertyVetoException e ) {
136 log.println("Error: can't set PropertyValue to a FieldMaster");
137 e.printStackTrace(log);
140 the_Text = xTextDoc.getText();
141 the_Cursor = the_Text.createTextCursor();
142 the_Field = UnoRuntime.queryInterface(XTextContent.class, oObj);
144 try {
145 xTF.attachTextFieldMaster(PFieldMaster);
146 the_Text.insertTextContent(the_Cursor,the_Field,false);
147 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
148 e.printStackTrace(log);
149 throw new StatusException( "Couldn't create TextField", e );
152 // create testobject here
153 XTextFieldsSupplier oTFS = UnoRuntime.queryInterface( XTextFieldsSupplier.class, xTextDoc );
154 xFEA = oTFS.getTextFields();
155 oObj = oTFS.getTextFields().createEnumeration();
157 log.println( "creating a new environment for FieldEnumeration object" );
158 TestEnvironment tEnv = new TestEnvironment( oObj );
160 tEnv.addObjRelation("ENUM", xFEA);
162 return tEnv;
163 } // finish method getTestEnvironment
165 } // finish class SwXFieldEnumeration