merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / qa / complex / docinfo / DocumentProperties.java
blob6a6281a574015ffbbdfe0e1362007dc7e14a03e8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DocumentProperties.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex.docinfo;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.beans.XPropertyContainer;
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.document.XDocumentInfo;
36 import com.sun.star.document.XDocumentInfoSupplier;
37 import com.sun.star.frame.XComponentLoader;
38 import com.sun.star.frame.XStorable;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 import com.sun.star.util.Date;
46 import complexlib.ComplexTestCase;
48 import util.DesktopTools;
49 import util.WriterTools;
52 public class DocumentProperties extends ComplexTestCase {
53 XMultiServiceFactory m_xMSF = null;
54 XTextDocument xTextDoc = null;
56 public String[] getTestMethodNames() {
57 return new String[] {"checkDocInfo", "cleanup"};
60 public void checkDocInfo() {
61 m_xMSF = (XMultiServiceFactory) param.getMSF();
63 log.println(
64 "check wether there is a valid MultiServiceFactory");
66 if (m_xMSF == null) {
67 assure("## Couldn't get MultiServiceFactory make sure your Office is started",
68 true);
71 log.println("... done");
73 log.println("Opening a Writer document");
74 xTextDoc = WriterTools.createTextDoc(m_xMSF);
75 log.println("... done");
77 XDocumentInfoSupplier xDocInfoSup =
78 (XDocumentInfoSupplier) UnoRuntime.queryInterface(XDocumentInfoSupplier.class,
79 xTextDoc);
80 XDocumentInfo xDocInfo = xDocInfoSup.getDocumentInfo();
81 XPropertyContainer xPropContainer =
82 (XPropertyContainer) UnoRuntime.queryInterface(XPropertyContainer.class,
83 xDocInfo);
85 log.println("Trying to add a existing property");
87 boolean worked =
88 addProperty(xPropContainer, "Author", (short) 0, "");
89 assure("Could set an existing property", !worked);
90 log.println("...done");
92 log.println("Trying to add a integer property");
93 worked =
94 addProperty(xPropContainer, "intValue", com.sun.star.beans.PropertyAttribute.READONLY,
95 new Integer(17));
96 assure("Couldn't set an integer property", worked);
97 log.println("...done");
99 log.println("Trying to add a double property");
100 worked =
101 addProperty(xPropContainer, "doubleValue", com.sun.star.beans.PropertyAttribute.REMOVEABLE ,
102 new Double(17.7));
103 assure("Couldn't set an double property", worked);
104 log.println("...done");
106 log.println("Trying to add a boolean property");
107 worked =
108 addProperty(xPropContainer, "booleanValue", com.sun.star.beans.PropertyAttribute.REMOVEABLE,
109 Boolean.TRUE);
110 assure("Couldn't set an boolean property", worked);
111 log.println("...done");
113 log.println("Trying to add a date property");
114 worked =
115 addProperty(xPropContainer, "dateValue", com.sun.star.beans.PropertyAttribute.REMOVEABLE,
116 new Date());
117 assure("Couldn't set an date property", worked);
118 log.println("...done");
120 log.println("trying to remove a read only Property");
121 try {
122 xPropContainer.removeProperty ("intValue");
123 assure("Could remove read only property", false);
124 } catch (Exception e) {
125 log.println("\tException was thrown "+e);
126 log.println("\t...OK");
128 log.println("...done");
131 String tempdir = System.getProperty("java.io.tmpdir");
132 String fs = System.getProperty("file.separator");
134 if (!tempdir.endsWith(fs)) {
135 tempdir += fs;
138 tempdir = util.utils.getFullURL(tempdir);
140 log.println("Storing the document");
142 try {
143 XStorable store =
144 (XStorable) UnoRuntime.queryInterface(XStorable.class,
145 xTextDoc);
146 store.storeToURL(tempdir + "DocInfo.oot",
147 new PropertyValue[] {});
148 DesktopTools.closeDoc(xTextDoc);
149 } catch (Exception e) {
150 assure("Couldn't store document", false);
153 log.println("...done");
155 log.println("loading the document");
157 try {
158 XComponentLoader xCL =
159 (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
160 m_xMSF.createInstance(
161 "com.sun.star.frame.Desktop"));
162 XComponent xComp =
163 xCL.loadComponentFromURL(tempdir + "DocInfo.oot",
164 "_blank", 0, new PropertyValue[] {});
165 xTextDoc =
166 (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,
167 xComp);
168 } catch (Exception e) {
169 assure("Couldn't load document", false);
172 log.println("...done");
174 xDocInfoSup =
175 (XDocumentInfoSupplier) UnoRuntime.queryInterface(XDocumentInfoSupplier.class,
176 xTextDoc);
177 xDocInfo = xDocInfoSup.getDocumentInfo();
179 XPropertySet xProps =
180 (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
181 xDocInfo);
183 assure("Integer doesn't work",
184 checkType(xProps, "intValue", "java.lang.Integer"));
185 assure("Double doesn't work",
186 checkType(xProps, "doubleValue", "java.lang.Double"));
187 assure("Boolean doesn't work",
188 checkType(xProps, "booleanValue", "java.lang.Boolean"));
189 assure("Date doesn't work",
190 checkType(xProps, "dateValue",
191 "com.sun.star.util.DateTime"));
193 xPropContainer =
194 (XPropertyContainer) UnoRuntime.queryInterface(XPropertyContainer.class,
195 xDocInfo);
197 log.println("trying to remove a not user defined Property");
198 try {
199 xPropContainer.removeProperty ("Author");
200 assure("Could remove non user defined property", false);
201 } catch (Exception e) {
202 log.println("\tException was thrown "+e);
203 log.println("\t...OK");
205 log.println("...done");
207 log.println("Trying to remove a user defined property");
208 try {
209 xPropContainer.removeProperty ("dateValue");
210 log.println("\t...OK");
211 } catch (Exception e) {
212 log.println("\tException was thrown "+e);
213 log.println("\t...FAILED");
214 assure("Could not remove user defined property", false);
216 log.println("...done");
220 public void cleanup() {
221 DesktopTools.closeDoc(xTextDoc);
224 private boolean checkType(XPropertySet xProps, String aName,
225 String expected) {
226 boolean ret = true;
227 log.println("Checking " + expected);
229 String getting =
230 getPropertyByName(xProps, aName).getClass().getName();
232 if (!getting.equals(expected)) {
233 log.println("\t Expected: " + expected);
234 log.println("\t Detting: " + getting);
235 ret = false;
238 if (ret) {
239 log.println("...OK");
242 return ret;
245 private Object getPropertyByName(XPropertySet xProps, String aName) {
246 Object ret = null;
248 try {
249 ret = xProps.getPropertyValue(aName);
250 } catch (Exception e) {
251 log.println("\tCouldn't get Property " + aName);
252 log.println("\tMessage " + e);
255 return ret;
258 private boolean addProperty(XPropertyContainer xPropContainer,
259 String aName, short attr, Object defaults) {
260 boolean ret = true;
262 try {
263 xPropContainer.addProperty(aName, attr, defaults);
264 } catch (Exception e) {
265 ret = false;
266 log.println("\tCouldn't get Property " + aName);
267 log.println("\tMessage " + e);
270 return ret;