Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / io / _XPersistObject.java
blob717a611eb8e25ec83b5d551936274baf13b96359
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 ifc.io;
21 import lib.MultiMethodTest;
22 import util.ValueComparer;
23 import util.dbg;
24 import util.utils;
26 import com.sun.star.beans.Property;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.beans.XPropertySetInfo;
29 import com.sun.star.io.XActiveDataSink;
30 import com.sun.star.io.XActiveDataSource;
31 import com.sun.star.io.XInputStream;
32 import com.sun.star.io.XObjectInputStream;
33 import com.sun.star.io.XObjectOutputStream;
34 import com.sun.star.io.XOutputStream;
35 import com.sun.star.io.XPersistObject;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.uno.UnoRuntime;
40 /**
41 * Testing <code>com.sun.star.io.XPersistObject</code>
42 * interface methods :
43 * <ul>
44 * <li><code> getServiceName()</code></li>
45 * <li><code> write()</code></li>
46 * <li><code> read()</code></li>
47 * </ul> <p>
48 * This test need the following object relations :
49 * <ul>
50 * <li> <code>'OBJNAME'</code> : <code>String</code> value that
51 * contains service name which object represents.</li>
52 * <ul> <p>
53 * Test is <b> NOT </b> multithread compliant. <p>
54 * After test completion object environment has to be recreated.
55 * @see com.sun.star.io.XPersistObject
56 * @see com.sun.star.io.XObjectInputStream
57 * @see com.sun.star.io.XObjectOutputStream
59 public class _XPersistObject extends MultiMethodTest {
61 public XPersistObject oObj = null;
62 XObjectInputStream iStream = null;
63 XObjectOutputStream oStream = null;
64 String sname = null;
66 boolean result = true;
69 /**
70 * Test calls the method and checks return value. <p>
71 * Has <b> OK </b> status if the method returns proper service names
72 * which is equal to <code>'OBJNAME'</code> relation value. <p>
74 public void _getServiceName() {
75 result = true;
76 sname = oObj.getServiceName();
77 log.println("Method returned '" + sname + "'") ;
78 String objName = (String)tEnv.getObjRelation("OBJNAME");
79 if (objName == null) {
80 log.println("No OBJNAME relation!");
81 result = false;
82 } else {
83 result &= sname.equals(objName);
84 if (!result)
85 log.println("Name of object must be '" + objName +
86 "' but returned name is '" + sname +"'");
89 tRes.tested("getServiceName()", result);
92 /**
93 * Creates service get by <code>getServiceName</code> method and tries
94 * to read object written to stream by <code>write</code> method test.
95 * Then properties of object written and object read are compared. <p>
96 * Has <b>OK</b> status if all properties of two objects are equal
97 * and no exceptions were thrown. <p>
98 * The following method tests are to be completed successfully before :
99 * <ul>
100 * <li> <code> getServiceName() </code> : to have service name
101 * which has to be created </li>
102 * <li> <code> write() </code> : to write object tested into stream</li>
103 * </ul>
105 public void _read() {
106 requiredMethod("getServiceName()");
107 requiredMethod("write()") ;
109 boolean bResult = true;
111 try {
112 Object noPS = tEnv.getObjRelation("noPS");
113 if ( noPS == null) {
114 XPropertySet objps = UnoRuntime.queryInterface(
115 XPropertySet.class, oObj);
116 XPropertySetInfo objpsi = objps.getPropertySetInfo();
117 Property[] objprops = objpsi.getProperties();
119 Object oCopy = tParam.getMSF().createInstance(sname);
121 XPersistObject persCopy = UnoRuntime.queryInterface(XPersistObject.class, oCopy);
123 persCopy.read(iStream);
125 XPropertySet copyps = UnoRuntime.queryInterface(
126 XPropertySet.class, oCopy);
128 XPropertySetInfo copypsi = copyps.getPropertySetInfo();
129 Property[] copyprops = copypsi.getProperties();
131 for (int i = 0; i < copyprops.length; i++) {
132 Object cps = copyps.getPropertyValue(copyprops[i].Name);
133 Object ops = objps.getPropertyValue(objprops[i].Name);
134 boolean locRes = ( (ValueComparer.equalValue(cps,ops)) ||
135 (utils.isVoid(cps) && utils.isVoid(ops)) );
137 //transient properties aren't stored
138 if (isTransient(objprops[i])) locRes = true;
140 Object pseudo = tEnv.getObjRelation("PSEUDOPERSISTENT");
141 if ( (pseudo != null) && !locRes) {
142 String str = copyprops[i].Name;
143 locRes = ( (str.equals("Time")) || (str.equals("Date"))
144 || (str.equals("FormatsSupplier"))
145 || (str.equals("Text"))
146 || (str.equals("Value"))
147 || (str.indexOf("UserDefined")>0)
150 if (!locRes) {
151 log.println("Property '" + copyprops[i].Name
152 + "' failed");
153 dbg.printPropertyInfo(objps, objprops[i].Name, log);
154 dbg.printPropertyInfo(copyps, copyprops[i].Name, log);
156 bResult &= locRes;
159 XComponent comp = UnoRuntime.queryInterface(XComponent.class, oCopy);
160 if (comp != null) {
161 comp.dispose();
163 } else {
164 Object oCopy = tParam.getMSF().createInstance(sname);
165 XPersistObject persCopy = UnoRuntime.queryInterface(XPersistObject.class, oCopy);
167 persCopy.read(iStream);
169 bResult = persCopy.getServiceName().equals(sname);
171 XComponent comp = UnoRuntime.queryInterface(XComponent.class, oCopy);
172 if (comp != null) {
173 comp.dispose();
177 } catch (com.sun.star.uno.Exception e) {
178 log.println("Exception occurred : ");
179 e.printStackTrace(log) ;
180 bResult = false;
183 tRes.tested("read()", bResult);
187 * Test calls the method and checks that
188 * no exceptions were thrown. <p>
189 * Has <b> OK </b> status if no exceptions were thrown. <p>
191 public void _write() {
192 boolean bResult = true;
193 try {
194 initPipe();
195 oObj.write(oStream);
196 } catch (com.sun.star.io.IOException e) {
197 log.println("Exception occurred while test. " + e);
198 bResult = false;
200 tRes.tested("write()", bResult);
205 * Creates the following stream scheme <code>
206 * ObjectOutputStream -> Pipe -> ObjectInputStream </code> for writing/reading
207 * object.
209 protected void initPipe() {
210 try {
211 Object aPipe = tParam.getMSF().createInstance
212 ("com.sun.star.io.Pipe");
213 Object istream = tParam.getMSF().createInstance
214 ("com.sun.star.io.ObjectInputStream");
215 Object ostream = tParam.getMSF().createInstance
216 ("com.sun.star.io.ObjectOutputStream");
218 // Now the objects that aren't described anywhere
219 Object mistream = tParam.getMSF().createInstance
220 ("com.sun.star.io.MarkableInputStream");
221 Object mostream = tParam.getMSF().createInstance
222 ("com.sun.star.io.MarkableOutputStream");
224 XActiveDataSink xdSi = UnoRuntime.queryInterface(XActiveDataSink.class, istream);
225 XActiveDataSource xdSo = UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
226 XActiveDataSink xdSmi = UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
227 XActiveDataSource xdSmo = UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
229 XInputStream miStream = UnoRuntime.queryInterface(XInputStream.class, mistream);
230 XOutputStream moStream = UnoRuntime.queryInterface(XOutputStream.class, mostream);
231 XInputStream PipeIn = UnoRuntime.queryInterface(XInputStream.class, aPipe);
232 XOutputStream PipeOut = UnoRuntime.queryInterface(XOutputStream.class,aPipe);
234 xdSi.setInputStream(miStream);
235 xdSo.setOutputStream(moStream);
236 xdSmi.setInputStream(PipeIn);
237 xdSmo.setOutputStream(PipeOut);
239 iStream = UnoRuntime.queryInterface(XObjectInputStream.class, istream);
240 oStream = UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
243 } catch (com.sun.star.uno.Exception e) {
244 System.out.println("exc " + e);
249 public static boolean isTransient(Property prop) {
250 short attr = prop.Attributes;
251 return ((attr & com.sun.star.beans.PropertyAttribute.TRANSIENT) != 0);