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
.lang
.XComponent
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
41 * Testing <code>com.sun.star.io.XPersistObject</code>
44 * <li><code> getServiceName()</code></li>
45 * <li><code> write()</code></li>
46 * <li><code> read()</code></li>
48 * This test need the following object relations :
50 * <li> <code>'OBJNAME'</code> : <code>String</code> value that
51 * contains service name which object represents.</li>
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;
66 boolean result
= true;
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() {
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!");
83 result
&= sname
.equals(objName
);
85 log
.println("Name of object must be '" + objName
+
86 "' but returned name is '" + sname
+"'");
89 tRes
.tested("getServiceName()", result
);
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 :
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>
105 public void _read() {
106 requiredMethod("getServiceName()");
107 requiredMethod("write()") ;
109 boolean bResult
= true;
112 Object noPS
= tEnv
.getObjRelation("noPS");
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)
151 log
.println("Property '" + copyprops
[i
].Name
153 dbg
.printPropertyInfo(objps
, objprops
[i
].Name
, log
);
154 dbg
.printPropertyInfo(copyps
, copyprops
[i
].Name
, log
);
159 XComponent comp
= UnoRuntime
.queryInterface(XComponent
.class, oCopy
);
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
);
177 } catch (com
.sun
.star
.uno
.Exception e
) {
178 log
.println("Exception occurred : ");
179 e
.printStackTrace(log
) ;
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;
196 } catch (com
.sun
.star
.io
.IOException e
) {
197 log
.println("Exception occurred while test. " + e
);
200 tRes
.tested("write()", bResult
);
205 * Creates the following stream scheme <code>
206 * ObjectOutputStream -> Pipe -> ObjectInputStream </code> for writing/reading
209 protected void initPipe() {
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);