Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / io / _XPersistObject.java
blob7df7903aef6bb08b7c9808cb9b6fb3e49e87e458
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package ifc.io;
30 import lib.MultiMethodTest;
31 import util.ValueComparer;
32 import util.dbg;
33 import util.utils;
35 import com.sun.star.beans.Property;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.beans.XPropertySetInfo;
38 import com.sun.star.io.XActiveDataSink;
39 import com.sun.star.io.XActiveDataSource;
40 import com.sun.star.io.XInputStream;
41 import com.sun.star.io.XObjectInputStream;
42 import com.sun.star.io.XObjectOutputStream;
43 import com.sun.star.io.XOutputStream;
44 import com.sun.star.io.XPersistObject;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.uno.UnoRuntime;
49 /**
50 * Testing <code>com.sun.star.io.XPersistObject</code>
51 * interface methods :
52 * <ul>
53 * <li><code> getServiceName()</code></li>
54 * <li><code> write()</code></li>
55 * <li><code> read()</code></li>
56 * </ul> <p>
57 * This test need the following object relations :
58 * <ul>
59 * <li> <code>'OBJNAME'</code> : <code>String</code> value that
60 * contains service name which object represents.</li>
61 * <ul> <p>
62 * Test is <b> NOT </b> multithread compilant. <p>
63 * After test completion object environment has to be recreated.
64 * @see com.sun.star.io.XPersistObject
65 * @see com.sun.star.io.XObjectInputStream
66 * @see com.sun.star.io.XObjectOutputStream
68 public class _XPersistObject extends MultiMethodTest {
70 public XPersistObject oObj = null;
71 XObjectInputStream iStream = null;
72 XObjectOutputStream oStream = null;
73 String sname = null;
75 boolean result = true;
78 /**
79 * Test calls the method and checks return value. <p>
80 * Has <b> OK </b> status if the method returns proper service names
81 * which is equal to <code>'OBJNAME'</code> relation value. <p>
83 public void _getServiceName() {
84 result = true;
85 sname = oObj.getServiceName();
86 log.println("Method returned '" + sname + "'") ;
87 String objName = (String)tEnv.getObjRelation("OBJNAME");
88 if (objName == null) {
89 log.println("No OBJNAME relation!");
90 result = false;
91 } else {
92 result &= sname.equals(objName);
93 if (!result)
94 log.println("Name of object must be '" + objName +
95 "' but returned name is '" + sname +"'");
98 tRes.tested("getServiceName()", result);
102 * Creates service get by <code>getServiceName</code> method and tries
103 * to read object written to stream by <code>write</code> method test.
104 * Then properties of object written and object read are compared. <p>
105 * Has <b>OK</b> status if all properties of two objects are equal
106 * and no exceptions were thrown. <p>
107 * The following method tests are to be completed successfully before :
108 * <ul>
109 * <li> <code> getServiceName() </code> : to have service name
110 * which has to be created </li>
111 * <li> <code> write() </code> : to write object tested into stream</li>
112 * </ul>
114 public void _read() {
115 requiredMethod("getServiceName()");
116 requiredMethod("write()") ;
118 boolean bResult = true;
120 try {
121 Object noPS = tEnv.getObjRelation("noPS");
122 if ( noPS == null) {
123 XPropertySet objps = (XPropertySet)UnoRuntime.queryInterface(
124 XPropertySet.class, oObj);
125 XPropertySetInfo objpsi = objps.getPropertySetInfo();
126 Property[] objprops = objpsi.getProperties();
128 Object oCopy = ((XMultiServiceFactory)tParam.getMSF()).createInstance(sname);
130 XPersistObject persCopy = (XPersistObject)
131 UnoRuntime.queryInterface(XPersistObject.class, oCopy);
133 persCopy.read(iStream);
135 XPropertySet copyps = (XPropertySet)UnoRuntime.queryInterface(
136 XPropertySet.class, oCopy);
138 XPropertySetInfo copypsi = copyps.getPropertySetInfo();
139 Property[] copyprops = copypsi.getProperties();
141 for (int i = 0; i < copyprops.length; i++) {
142 Object cps = copyps.getPropertyValue(copyprops[i].Name);
143 Object ops = objps.getPropertyValue(objprops[i].Name);
144 boolean locRes = ( (ValueComparer.equalValue(cps,ops)) ||
145 (utils.isVoid(cps) && utils.isVoid(ops)) );
147 //transient properties aran't stored
148 if (isTransient(objprops[i])) locRes = true;
150 Object pseudo = tEnv.getObjRelation("PSEUDOPERSISTENT");
151 if ( (pseudo != null) && !locRes) {
152 String str = copyprops[i].Name;
153 locRes = ( (str.equals("Time")) || (str.equals("Date"))
154 || (str.equals("FormatsSupplier"))
155 || (str.equals("Text"))
156 || (str.equals("Value"))
157 || (str.indexOf("UserDefined")>0)
160 if (!locRes) {
161 log.println("Property '" + copyprops[i].Name
162 + "' failed");
163 dbg.printPropertyInfo(objps, objprops[i].Name, log);
164 dbg.printPropertyInfo(copyps, copyprops[i].Name, log);
166 bResult &= locRes;
168 } else {
169 Object oCopy = ((XMultiServiceFactory)tParam.getMSF()).createInstance(sname);
170 XPersistObject persCopy = (XPersistObject)
171 UnoRuntime.queryInterface(XPersistObject.class, oCopy);
173 persCopy.read(iStream);
175 bResult = ( persCopy.getServiceName().equals(sname) );
179 } catch (com.sun.star.uno.Exception e) {
180 log.println("Exception occurred : ");
181 e.printStackTrace(log) ;
182 bResult = false;
185 tRes.tested("read()", bResult);
189 * Test calls the method and checks that
190 * no exceptions were thrown. <p>
191 * Has <b> OK </b> status if no exceptions were thrown. <p>
193 public void _write() {
194 boolean bResult = true;
195 try {
196 initPipe();
197 oObj.write(oStream);
198 } catch (com.sun.star.io.IOException e) {
199 log.println("Exception occurred while test. " + e);
200 bResult = false;
202 tRes.tested("write()", bResult);
207 * Creates the following stream scheme <code>
208 * ObjectOutputStream -> Pipe -> ObjectInputStream </code> for writing/reading
209 * object.
211 protected void initPipe() {
212 try {
213 Object aPipe = ((XMultiServiceFactory)tParam.getMSF()).createInstance
214 ("com.sun.star.io.Pipe");
215 Object istream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
216 ("com.sun.star.io.ObjectInputStream");
217 Object ostream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
218 ("com.sun.star.io.ObjectOutputStream");
220 // Now the objects that aren't described anywhere
221 Object mistream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
222 ("com.sun.star.io.MarkableInputStream");
223 Object mostream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
224 ("com.sun.star.io.MarkableOutputStream");
226 XActiveDataSink xdSi = (XActiveDataSink)
227 UnoRuntime.queryInterface(XActiveDataSink.class, istream);
228 XActiveDataSource xdSo = (XActiveDataSource)
229 UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
230 XActiveDataSink xdSmi = (XActiveDataSink)
231 UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
232 XActiveDataSource xdSmo = (XActiveDataSource)
233 UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
235 XInputStream miStream = (XInputStream)
236 UnoRuntime.queryInterface(XInputStream.class, mistream);
237 XOutputStream moStream = (XOutputStream)
238 UnoRuntime.queryInterface(XOutputStream.class, mostream);
239 XInputStream PipeIn = (XInputStream)
240 UnoRuntime.queryInterface(XInputStream.class, aPipe);
241 XOutputStream PipeOut = (XOutputStream)
242 UnoRuntime.queryInterface(XOutputStream.class,aPipe);
244 xdSi.setInputStream(miStream);
245 xdSo.setOutputStream(moStream);
246 xdSmi.setInputStream(PipeIn);
247 xdSmo.setOutputStream(PipeOut);
249 iStream = (XObjectInputStream)
250 UnoRuntime.queryInterface(XObjectInputStream.class, istream);
251 oStream = (XObjectOutputStream)
252 UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
255 } catch (com.sun.star.uno.Exception e) {
256 System.out.println("exc " + e);
261 public static boolean isTransient(Property prop) {
262 short attr = prop.Attributes;
263 return ((attr & com.sun.star.beans.PropertyAttribute.TRANSIENT) != 0);