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.SOfficeFactory;
37 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XDependentTextField;
41 import com.sun.star.text.XText;
42 import com.sun.star.text.XTextContent;
43 import com.sun.star.text.XTextCursor;
44 import com.sun.star.text.XTextDocument;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49  *
50  * initial description
51  * @see com.sun.star.lang.XComponent
52  * @see com.sun.star.text.TextContent
53  * @see com.sun.star.text.XTextContent
54  * @see com.sun.star.text.XTextField
55  *
56  */
57 public class SwXTextField extends TestCase {
58 
59     XTextDocument xTextDoc;
60 
61     /**
62      * in general this method creates a testdocument
63      *
64      *  @param tParam    class which contains additional test parameters
65      *  @param log        class to log the test state and result
66      *
67      *
68      *  @see TestParameters
69      *    @see PrintWriter
70      *
71      */
72     protected void initialize( TestParameters tParam, PrintWriter log ) {
73         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
74 
75         try {
76             log.println( "creating a textdocument" );
77             xTextDoc = SOF.createTextDoc( null );
78         } catch ( com.sun.star.uno.Exception e ) {
79             // Some exception occures.FAILED
80             e.printStackTrace( log );
81             throw new StatusException( "Couldn't create document", e );
82         }
83     }
84 
85     /**
86      * in general this method disposes the testenvironment and document
87      *
88      *  @param tParam    class which contains additional test parameters
89      *  @param log        class to log the test state and result
90      *
91      *
92      *  @see TestParameters
93      *    @see PrintWriter
94      *
95      */
96     protected void cleanup( TestParameters tParam, PrintWriter log ) {
97         log.println( "    disposing xTextDoc " );
98         util.DesktopTools.closeDoc(xTextDoc);
99     }
100 
101 
102     /**
103      *    creating a Testenvironment for the interfaces to be tested
104      *
105      *  @param tParam    class which contains additional test parameters
106      *  @param log        class to log the test state and result
107      *
108      *  @return    Status class
109      *
110      *  @see TestParameters
111      *    @see PrintWriter
112      */
113     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
114 
115         XInterface oObj = null;
116 
117         // creation of testobject here
118         // first we write what we are intend to do to log file
119         log.println( "creating a test environment" );
120 
121         Object instance = null;
122 
123           // create testobject here
124         try {
125             XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
126                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
127 
128             Object FieldMaster = oDocMSF.createInstance
129                 ( "com.sun.star.text.FieldMaster.Database" );
130             XPropertySet PFieldMaster = (XPropertySet) UnoRuntime.queryInterface
131                 (XPropertySet.class,(XInterface) FieldMaster);
132             oObj = (XInterface) oDocMSF.createInstance
133                 ( "com.sun.star.text.TextField.Database" );
134 
135             instance = (XInterface) oDocMSF.createInstance
136                 ( "com.sun.star.text.TextField.DateTime" );
137 
138             XDependentTextField xTF = (XDependentTextField)
139                 UnoRuntime.queryInterface(XDependentTextField.class,oObj);
140 
141             PFieldMaster.setPropertyValue("DataBaseName","Address Book File");
142             PFieldMaster.setPropertyValue("DataTableName","address");
143             PFieldMaster.setPropertyValue("DataColumnName","FIRSTNAME");
144             XText the_Text = xTextDoc.getText();
145             XTextCursor the_Cursor = the_Text.createTextCursor();
146             XTextContent the_Field = (XTextContent)
147                              UnoRuntime.queryInterface(XTextContent.class,oObj);
148 
149             xTF.attachTextFieldMaster(PFieldMaster);
150             the_Text.insertTextContent(the_Cursor,the_Field,false);
151         }
152         catch (Exception ex) {
153             log.println("Couldn't create instance");
154             ex.printStackTrace(log);
155         }
156 
157         log.println( "creating a new environment for FieldMaster object" );
158         TestEnvironment tEnv = new TestEnvironment( oObj );
159 
160 
161         tEnv.addObjRelation("CONTENT", (XTextContent)
162                         UnoRuntime.queryInterface(XTextContent.class,instance));
163         tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
164 
165         return tEnv;
166     } // finish method getTestEnvironment
167 }    // finish class SwXTextField
168 
169