Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / io / _XPersistObject.java
blob8ac80dcbe84393bf8269afffc325d4d5d5ae937f
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: _XPersistObject.java,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
31 package ifc.io;
33 import lib.MultiMethodTest;
34 import util.ValueComparer;
35 import util.dbg;
36 import util.utils;
38 import com.sun.star.beans.Property;
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.beans.XPropertySetInfo;
41 import com.sun.star.io.XActiveDataSink;
42 import com.sun.star.io.XActiveDataSource;
43 import com.sun.star.io.XInputStream;
44 import com.sun.star.io.XObjectInputStream;
45 import com.sun.star.io.XObjectOutputStream;
46 import com.sun.star.io.XOutputStream;
47 import com.sun.star.io.XPersistObject;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.uno.UnoRuntime;
52 /**
53 * Testing <code>com.sun.star.io.XPersistObject</code>
54 * interface methods :
55 * <ul>
56 * <li><code> getServiceName()</code></li>
57 * <li><code> write()</code></li>
58 * <li><code> read()</code></li>
59 * </ul> <p>
60 * This test need the following object relations :
61 * <ul>
62 * <li> <code>'OBJNAME'</code> : <code>String</code> value that
63 * contains service name which object represents.</li>
64 * <ul> <p>
65 * Test is <b> NOT </b> multithread compilant. <p>
66 * After test completion object environment has to be recreated.
67 * @see com.sun.star.io.XPersistObject
68 * @see com.sun.star.io.XObjectInputStream
69 * @see com.sun.star.io.XObjectOutputStream
71 public class _XPersistObject extends MultiMethodTest {
73 public XPersistObject oObj = null;
74 XObjectInputStream iStream = null;
75 XObjectOutputStream oStream = null;
76 String sname = null;
78 boolean result = true;
81 /**
82 * Test calls the method and checks return value. <p>
83 * Has <b> OK </b> status if the method returns proper service names
84 * which is equal to <code>'OBJNAME'</code> relation value. <p>
86 public void _getServiceName() {
87 result = true;
88 sname = oObj.getServiceName();
89 log.println("Method returned '" + sname + "'") ;
90 String objName = (String)tEnv.getObjRelation("OBJNAME");
91 if (objName == null) {
92 log.println("No OBJNAME relation!");
93 result = false;
94 } else {
95 result &= sname.equals(objName);
96 if (!result)
97 log.println("Name of object must be '" + objName +
98 "' but returned name is '" + sname +"'");
101 tRes.tested("getServiceName()", result);
105 * Creates service get by <code>getServiceName</code> method and tries
106 * to read object written to stream by <code>write</code> method test.
107 * Then properties of object written and object read are compared. <p>
108 * Has <b>OK</b> status if all properties of two objects are equal
109 * and no exceptions were thrown. <p>
110 * The following method tests are to be completed successfully before :
111 * <ul>
112 * <li> <code> getServiceName() </code> : to have service name
113 * which has to be created </li>
114 * <li> <code> write() </code> : to write object tested into stream</li>
115 * </ul>
117 public void _read() {
118 requiredMethod("getServiceName()");
119 requiredMethod("write()") ;
121 boolean bResult = true;
123 try {
124 Object noPS = tEnv.getObjRelation("noPS");
125 if ( noPS == null) {
126 XPropertySet objps = (XPropertySet)UnoRuntime.queryInterface(
127 XPropertySet.class, oObj);
128 XPropertySetInfo objpsi = objps.getPropertySetInfo();
129 Property[] objprops = objpsi.getProperties();
131 Object oCopy = ((XMultiServiceFactory)tParam.getMSF()).createInstance(sname);
133 XPersistObject persCopy = (XPersistObject)
134 UnoRuntime.queryInterface(XPersistObject.class, oCopy);
136 persCopy.read(iStream);
138 XPropertySet copyps = (XPropertySet)UnoRuntime.queryInterface(
139 XPropertySet.class, oCopy);
141 XPropertySetInfo copypsi = copyps.getPropertySetInfo();
142 Property[] copyprops = copypsi.getProperties();
144 for (int i = 0; i < copyprops.length; i++) {
145 Object cps = copyps.getPropertyValue(copyprops[i].Name);
146 Object ops = objps.getPropertyValue(objprops[i].Name);
147 boolean locRes = ( (ValueComparer.equalValue(cps,ops)) ||
148 (utils.isVoid(cps) && utils.isVoid(ops)) );
150 //transient properties aran't stored
151 if (isTransient(objprops[i])) locRes = true;
153 Object pseudo = tEnv.getObjRelation("PSEUDOPERSISTENT");
154 if ( (pseudo != null) && !locRes) {
155 String str = copyprops[i].Name;
156 locRes = ( (str.equals("Time")) || (str.equals("Date"))
157 || (str.equals("FormatsSupplier"))
158 || (str.equals("Text"))
159 || (str.equals("Value"))
160 || (str.indexOf("UserDefined")>0)
163 if (!locRes) {
164 log.println("Property '" + copyprops[i].Name
165 + "' failed");
166 dbg.printPropertyInfo(objps, objprops[i].Name, log);
167 dbg.printPropertyInfo(copyps, copyprops[i].Name, log);
169 bResult &= locRes;
171 } else {
172 Object oCopy = ((XMultiServiceFactory)tParam.getMSF()).createInstance(sname);
173 XPersistObject persCopy = (XPersistObject)
174 UnoRuntime.queryInterface(XPersistObject.class, oCopy);
176 persCopy.read(iStream);
178 bResult = ( persCopy.getServiceName().equals(sname) );
182 } catch (com.sun.star.uno.Exception e) {
183 log.println("Exception occured : ");
184 e.printStackTrace(log) ;
185 bResult = false;
188 tRes.tested("read()", bResult);
192 * Test calls the method and checks that
193 * no exceptions were thrown. <p>
194 * Has <b> OK </b> status if no exceptions were thrown. <p>
196 public void _write() {
197 boolean bResult = true;
198 try {
199 initPipe();
200 oObj.write(oStream);
201 } catch (com.sun.star.io.IOException e) {
202 log.println("Exception occured while test. " + e);
203 bResult = false;
205 tRes.tested("write()", bResult);
210 * Creates the following stream scheme <code>
211 * ObjectOutputStream -> Pipe -> ObjectInputStream </code> for writing/reading
212 * object.
214 protected void initPipe() {
215 try {
216 Object aPipe = ((XMultiServiceFactory)tParam.getMSF()).createInstance
217 ("com.sun.star.io.Pipe");
218 Object istream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
219 ("com.sun.star.io.ObjectInputStream");
220 Object ostream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
221 ("com.sun.star.io.ObjectOutputStream");
223 // Now the objects that aren't described anywhere
224 Object mistream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
225 ("com.sun.star.io.MarkableInputStream");
226 Object mostream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
227 ("com.sun.star.io.MarkableOutputStream");
229 XActiveDataSink xdSi = (XActiveDataSink)
230 UnoRuntime.queryInterface(XActiveDataSink.class, istream);
231 XActiveDataSource xdSo = (XActiveDataSource)
232 UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
233 XActiveDataSink xdSmi = (XActiveDataSink)
234 UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
235 XActiveDataSource xdSmo = (XActiveDataSource)
236 UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
238 XInputStream miStream = (XInputStream)
239 UnoRuntime.queryInterface(XInputStream.class, mistream);
240 XOutputStream moStream = (XOutputStream)
241 UnoRuntime.queryInterface(XOutputStream.class, mostream);
242 XInputStream PipeIn = (XInputStream)
243 UnoRuntime.queryInterface(XInputStream.class, aPipe);
244 XOutputStream PipeOut = (XOutputStream)
245 UnoRuntime.queryInterface(XOutputStream.class,aPipe);
247 xdSi.setInputStream(miStream);
248 xdSo.setOutputStream(moStream);
249 xdSmi.setInputStream(PipeIn);
250 xdSmo.setOutputStream(PipeOut);
252 iStream = (XObjectInputStream)
253 UnoRuntime.queryInterface(XObjectInputStream.class, istream);
254 oStream = (XObjectOutputStream)
255 UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
258 } catch (com.sun.star.uno.Exception e) {
259 System.out.println("exc " + e);
264 public static boolean isTransient(Property prop) {
265 short attr = prop.Attributes;
266 return ((attr & com.sun.star.beans.PropertyAttribute.TRANSIENT) != 0);