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 .
21 import lib
.MultiMethodTest
;
22 import util
.ValueComparer
;
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
.uno
.UnoRuntime
;
40 * Testing <code>com.sun.star.io.XPersistObject</code>
43 * <li><code> getServiceName()</code></li>
44 * <li><code> write()</code></li>
45 * <li><code> read()</code></li>
47 * This test need the following object relations :
49 * <li> <code>'OBJNAME'</code> : <code>String</code> value that
50 * contains service name which object represents.</li>
52 * Test is <b> NOT </b> multithread compliant. <p>
53 * After test completion object environment has to be recreated.
54 * @see com.sun.star.io.XPersistObject
55 * @see com.sun.star.io.XObjectInputStream
56 * @see com.sun.star.io.XObjectOutputStream
58 public class _XPersistObject
extends MultiMethodTest
{
60 public XPersistObject oObj
= null;
61 XObjectInputStream iStream
= null;
62 XObjectOutputStream oStream
= null;
65 boolean result
= true;
69 * Test calls the method and checks return value. <p>
70 * Has <b> OK </b> status if the method returns proper service names
71 * which is equal to <code>'OBJNAME'</code> relation value. <p>
73 public void _getServiceName() {
75 sname
= oObj
.getServiceName();
76 log
.println("Method returned '" + sname
+ "'") ;
77 String objName
= (String
)tEnv
.getObjRelation("OBJNAME");
78 if (objName
== null) {
79 log
.println("No OBJNAME relation!");
82 result
&= sname
.equals(objName
);
84 log
.println("Name of object must be '" + objName
+
85 "' but returned name is '" + sname
+"'");
88 tRes
.tested("getServiceName()", result
);
92 * Creates service get by <code>getServiceName</code> method and tries
93 * to read object written to stream by <code>write</code> method test.
94 * Then properties of object written and object read are compared. <p>
95 * Has <b>OK</b> status if all properties of two objects are equal
96 * and no exceptions were thrown. <p>
97 * The following method tests are to be completed successfully before :
99 * <li> <code> getServiceName() </code> : to have service name
100 * which has to be created </li>
101 * <li> <code> write() </code> : to write object tested into stream</li>
104 public void _read() {
105 requiredMethod("getServiceName()");
106 requiredMethod("write()") ;
108 boolean bResult
= true;
111 Object noPS
= tEnv
.getObjRelation("noPS");
113 XPropertySet objps
= UnoRuntime
.queryInterface(
114 XPropertySet
.class, oObj
);
115 XPropertySetInfo objpsi
= objps
.getPropertySetInfo();
116 Property
[] objprops
= objpsi
.getProperties();
118 Object oCopy
= tParam
.getMSF().createInstance(sname
);
120 XPersistObject persCopy
= UnoRuntime
.queryInterface(XPersistObject
.class, oCopy
);
122 persCopy
.read(iStream
);
124 XPropertySet copyps
= UnoRuntime
.queryInterface(
125 XPropertySet
.class, oCopy
);
127 XPropertySetInfo copypsi
= copyps
.getPropertySetInfo();
128 Property
[] copyprops
= copypsi
.getProperties();
130 for (int i
= 0; i
< copyprops
.length
; i
++) {
131 Object cps
= copyps
.getPropertyValue(copyprops
[i
].Name
);
132 Object ops
= objps
.getPropertyValue(objprops
[i
].Name
);
133 boolean locRes
= ( (ValueComparer
.equalValue(cps
,ops
)) ||
134 (utils
.isVoid(cps
) && utils
.isVoid(ops
)) );
136 //transient properties aran't stored
137 if (isTransient(objprops
[i
])) locRes
= true;
139 Object pseudo
= tEnv
.getObjRelation("PSEUDOPERSISTENT");
140 if ( (pseudo
!= null) && !locRes
) {
141 String str
= copyprops
[i
].Name
;
142 locRes
= ( (str
.equals("Time")) || (str
.equals("Date"))
143 || (str
.equals("FormatsSupplier"))
144 || (str
.equals("Text"))
145 || (str
.equals("Value"))
146 || (str
.indexOf("UserDefined")>0)
150 log
.println("Property '" + copyprops
[i
].Name
152 dbg
.printPropertyInfo(objps
, objprops
[i
].Name
, log
);
153 dbg
.printPropertyInfo(copyps
, copyprops
[i
].Name
, log
);
158 Object oCopy
= tParam
.getMSF().createInstance(sname
);
159 XPersistObject persCopy
= UnoRuntime
.queryInterface(XPersistObject
.class, oCopy
);
161 persCopy
.read(iStream
);
163 bResult
= ( persCopy
.getServiceName().equals(sname
) );
167 } catch (com
.sun
.star
.uno
.Exception e
) {
168 log
.println("Exception occurred : ");
169 e
.printStackTrace(log
) ;
173 tRes
.tested("read()", bResult
);
177 * Test calls the method and checks that
178 * no exceptions were thrown. <p>
179 * Has <b> OK </b> status if no exceptions were thrown. <p>
181 public void _write() {
182 boolean bResult
= true;
186 } catch (com
.sun
.star
.io
.IOException e
) {
187 log
.println("Exception occurred while test. " + e
);
190 tRes
.tested("write()", bResult
);
195 * Creates the following stream scheme <code>
196 * ObjectOutputStream -> Pipe -> ObjectInputStream </code> for writing/reading
199 protected void initPipe() {
201 Object aPipe
= tParam
.getMSF().createInstance
202 ("com.sun.star.io.Pipe");
203 Object istream
= tParam
.getMSF().createInstance
204 ("com.sun.star.io.ObjectInputStream");
205 Object ostream
= tParam
.getMSF().createInstance
206 ("com.sun.star.io.ObjectOutputStream");
208 // Now the objects that aren't described anywhere
209 Object mistream
= tParam
.getMSF().createInstance
210 ("com.sun.star.io.MarkableInputStream");
211 Object mostream
= tParam
.getMSF().createInstance
212 ("com.sun.star.io.MarkableOutputStream");
214 XActiveDataSink xdSi
= UnoRuntime
.queryInterface(XActiveDataSink
.class, istream
);
215 XActiveDataSource xdSo
= UnoRuntime
.queryInterface(XActiveDataSource
.class, ostream
);
216 XActiveDataSink xdSmi
= UnoRuntime
.queryInterface(XActiveDataSink
.class, mistream
);
217 XActiveDataSource xdSmo
= UnoRuntime
.queryInterface(XActiveDataSource
.class, mostream
);
219 XInputStream miStream
= UnoRuntime
.queryInterface(XInputStream
.class, mistream
);
220 XOutputStream moStream
= UnoRuntime
.queryInterface(XOutputStream
.class, mostream
);
221 XInputStream PipeIn
= UnoRuntime
.queryInterface(XInputStream
.class, aPipe
);
222 XOutputStream PipeOut
= UnoRuntime
.queryInterface(XOutputStream
.class,aPipe
);
224 xdSi
.setInputStream(miStream
);
225 xdSo
.setOutputStream(moStream
);
226 xdSmi
.setInputStream(PipeIn
);
227 xdSmo
.setOutputStream(PipeOut
);
229 iStream
= UnoRuntime
.queryInterface(XObjectInputStream
.class, istream
);
230 oStream
= UnoRuntime
.queryInterface(XObjectOutputStream
.class, ostream
);
233 } catch (com
.sun
.star
.uno
.Exception e
) {
234 System
.out
.println("exc " + e
);
239 public static boolean isTransient(Property prop
) {
240 short attr
= prop
.Attributes
;
241 return ((attr
& com
.sun
.star
.beans
.PropertyAttribute
.TRANSIENT
) != 0);