Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / io / _XObjectInputStream.java
blob5f55f0b778d249d27075a9ec7bf1c617065348e0
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 lib.Status;
23 import util.ValueComparer;
25 import com.sun.star.beans.Property;
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.io.XObjectInputStream;
28 import com.sun.star.io.XObjectOutputStream;
29 import com.sun.star.io.XPersistObject;
30 import com.sun.star.uno.UnoRuntime;
32 /**
33 * Testing <code>com.sun.star.io.XObjectInputStream</code>
34 * interface methods:
35 * <ul>
36 * <li><code>readObject()</code></li>
37 * </ul> <p>
38 * This test needs the following object relations :
39 * <ul>
40 * <li> <code>'PersistObject'</code> (of type <code>Object</code>):
41 * object that supports interface <code>XPersistObject</code> </li>
42 * <ul> <p>
43 * After test completion object environment has to be recreated.
44 * @see com.sun.star.io.XObjectInputStream
45 * @see com.sun.star.io.XPersistObject
47 public class _XObjectInputStream extends MultiMethodTest {
49 public XObjectInputStream oObj = null;
51 /**
52 * Test reads perisist object from stream and compares properties
53 * of the object with properties of persist object obtained
54 * from relation <code>'PersistObject'</code> <p>
55 * Has <b> OK </b> status if returned value isn't null and values
56 * of objects properties are equal. <p>
58 public void _readObject() {
59 Object objWrite = tEnv.getObjRelation("PersistObject") ;
60 if (objWrite == null) {
61 log.println("PersistObject not found in relations") ;
62 tRes.tested("readObject()", false) ;
63 return ;
66 // write the object
67 try {
68 XObjectOutputStream oStream = (XObjectOutputStream)
69 tEnv.getObjRelation("StreamWriter");
70 oStream.writeObject((XPersistObject)objWrite);
71 } catch(com.sun.star.io.IOException e) {
72 log.println("Couldn't write object to stream");
73 e.printStackTrace(log);
74 tRes.tested("readObject()", Status.skipped(false));
75 return;
78 Object objRead = null ;
79 try {
80 objRead = oObj.readObject() ;
81 } catch(com.sun.star.io.IOException e) {
82 log.println("Couldn't read object from stream");
83 e.printStackTrace(log);
84 tRes.tested("readObject()", false) ;
85 return ;
88 if (objRead == null) {
89 log.println("No object was read.") ;
90 tRes.tested("readObject()", false) ;
91 return ;
94 XPropertySet props1 = null ;
95 XPropertySet props2 = null ;
97 props1 = UnoRuntime.queryInterface
98 (XPropertySet.class, objRead) ;
100 props2 = UnoRuntime.queryInterface
101 (XPropertySet.class, objWrite) ;
103 if (props1 == null) {
104 log.println("Object read doesn't implement XPropertySet") ;
105 tRes.tested("readObject()", false) ;
106 return ;
108 if (props2 == null) {
109 log.println("Object written doesn't implement XPropertySet") ;
110 tRes.tested("readObject()", false) ;
111 return ;
114 tRes.tested("readObject()",
115 compareProperties(props1, props2)) ;
118 protected boolean compareProperties(XPropertySet props1,
119 XPropertySet props2) {
121 Property[] p1 = props1.getPropertySetInfo().getProperties() ;
122 Property[] p2 = props2.getPropertySetInfo().getProperties() ;
124 if (p1.length != p2.length) {
125 log.println("Number of properties differs") ;
126 return false ;
129 boolean result = true ;
131 for (int i = 0; i < p1.length; i++) {
132 String propName = p1[i].Name ;
134 log.print("Comparing property '" + propName + "' ...") ;
135 boolean res = false ;
136 try {
137 res = ValueComparer.equalValue
138 (props1.getPropertyValue(propName),
139 props2.getPropertyValue(propName)) ;
140 } catch (com.sun.star.beans.UnknownPropertyException e) {
141 log.println("Not found !") ;
142 } catch (com.sun.star.lang.WrappedTargetException e) {
143 log.println(e) ;
146 if (res)
147 log.println("OK.") ;
148 else
149 log.println("Different !") ;
151 result &= res ;
154 return result ;
158 * Forces object environment recreation.
160 @Override
161 public void after() {
162 this.disposeEnvironment() ;