1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package ifc.io; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import lib.MultiMethodTest; 31*cdf0e10cSrcweir import lib.Status; 32*cdf0e10cSrcweir import util.ValueComparer; 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir import com.sun.star.beans.Property; 35*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 36*cdf0e10cSrcweir import com.sun.star.io.XObjectInputStream; 37*cdf0e10cSrcweir import com.sun.star.io.XObjectOutputStream; 38*cdf0e10cSrcweir import com.sun.star.io.XPersistObject; 39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir /** 42*cdf0e10cSrcweir * Testing <code>com.sun.star.io.XObjectInputStream</code> 43*cdf0e10cSrcweir * interface methods: 44*cdf0e10cSrcweir * <ul> 45*cdf0e10cSrcweir * <li><code>readObject()</code></li> 46*cdf0e10cSrcweir * </ul> <p> 47*cdf0e10cSrcweir * This test needs the following object relations : 48*cdf0e10cSrcweir * <ul> 49*cdf0e10cSrcweir * <li> <code>'PersistObject'</code> (of type <code>Object</code>): 50*cdf0e10cSrcweir * object that supports interface <code>XPersistObject</code> </li> 51*cdf0e10cSrcweir * <ul> <p> 52*cdf0e10cSrcweir * After test completion object environment has to be recreated. 53*cdf0e10cSrcweir * @see com.sun.star.io.XObjectInputStream 54*cdf0e10cSrcweir * @see com.sun.star.io.XPersistObject 55*cdf0e10cSrcweir */ 56*cdf0e10cSrcweir public class _XObjectInputStream extends MultiMethodTest { 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir public XObjectInputStream oObj = null; 59*cdf0e10cSrcweir private Object objRead = null ; 60*cdf0e10cSrcweir private Object objWrite = null ; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /** 63*cdf0e10cSrcweir * Test reads perisist object from stream and compares properties 64*cdf0e10cSrcweir * of the object with properties of persist object obtained 65*cdf0e10cSrcweir * from relation <code>'PersistObject'</code> <p> 66*cdf0e10cSrcweir * Has <b> OK </b> status if returned value isn't null and values 67*cdf0e10cSrcweir * of objects properties are equal. <p> 68*cdf0e10cSrcweir */ 69*cdf0e10cSrcweir public void _readObject() { 70*cdf0e10cSrcweir objWrite = tEnv.getObjRelation("PersistObject") ; 71*cdf0e10cSrcweir if (objWrite == null) { 72*cdf0e10cSrcweir log.println("PersistObject not found in relations") ; 73*cdf0e10cSrcweir tRes.tested("readObject()", false) ; 74*cdf0e10cSrcweir return ; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir // write the object 78*cdf0e10cSrcweir try { 79*cdf0e10cSrcweir XObjectOutputStream oStream = (XObjectOutputStream) 80*cdf0e10cSrcweir tEnv.getObjRelation("StreamWriter"); 81*cdf0e10cSrcweir oStream.writeObject((XPersistObject)objWrite); 82*cdf0e10cSrcweir } catch(com.sun.star.io.IOException e) { 83*cdf0e10cSrcweir log.println("Couldn't write object to stream"); 84*cdf0e10cSrcweir e.printStackTrace(log); 85*cdf0e10cSrcweir tRes.tested("readObject()", Status.skipped(false)); 86*cdf0e10cSrcweir return; 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir try { 90*cdf0e10cSrcweir objRead = oObj.readObject() ; 91*cdf0e10cSrcweir } catch(com.sun.star.io.IOException e) { 92*cdf0e10cSrcweir log.println("Couldn't read object from stream"); 93*cdf0e10cSrcweir e.printStackTrace(log); 94*cdf0e10cSrcweir tRes.tested("readObject()", false) ; 95*cdf0e10cSrcweir return ; 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir if (objRead == null) { 99*cdf0e10cSrcweir log.println("No object was read.") ; 100*cdf0e10cSrcweir tRes.tested("readObject()", false) ; 101*cdf0e10cSrcweir return ; 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir XPropertySet props1 = null ; 105*cdf0e10cSrcweir XPropertySet props2 = null ; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir props1 = (XPropertySet) UnoRuntime.queryInterface 108*cdf0e10cSrcweir (XPropertySet.class, objRead) ; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir props2 = (XPropertySet) UnoRuntime.queryInterface 111*cdf0e10cSrcweir (XPropertySet.class, objWrite) ; 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir if (props1 == null) { 114*cdf0e10cSrcweir log.println("Object read doesn't implement XPropertySet") ; 115*cdf0e10cSrcweir tRes.tested("readObject()", false) ; 116*cdf0e10cSrcweir return ; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir if (props2 == null) { 119*cdf0e10cSrcweir log.println("Object written doesn't implement XPropertySet") ; 120*cdf0e10cSrcweir tRes.tested("readObject()", false) ; 121*cdf0e10cSrcweir return ; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir tRes.tested("readObject()", 125*cdf0e10cSrcweir compareProperties(props1, props2)) ; 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir protected boolean compareProperties(XPropertySet props1, 129*cdf0e10cSrcweir XPropertySet props2) { 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir Property[] p1 = props1.getPropertySetInfo().getProperties() ; 132*cdf0e10cSrcweir Property[] p2 = props2.getPropertySetInfo().getProperties() ; 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir if (p1.length != p2.length) { 135*cdf0e10cSrcweir log.println("Number of properties differs") ; 136*cdf0e10cSrcweir return false ; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir boolean result = true ; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir for (int i = 0; i < p1.length; i++) { 142*cdf0e10cSrcweir String propName = p1[i].Name ; 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir log.print("Comparing property '" + propName + "' ...") ; 145*cdf0e10cSrcweir boolean res = false ; 146*cdf0e10cSrcweir try { 147*cdf0e10cSrcweir res = ValueComparer.equalValue 148*cdf0e10cSrcweir (props1.getPropertyValue(propName), 149*cdf0e10cSrcweir props2.getPropertyValue(propName)) ; 150*cdf0e10cSrcweir } catch (com.sun.star.beans.UnknownPropertyException e) { 151*cdf0e10cSrcweir log.println("Not found !") ; 152*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 153*cdf0e10cSrcweir log.println(e) ; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir if (res) 157*cdf0e10cSrcweir log.println("OK.") ; 158*cdf0e10cSrcweir else 159*cdf0e10cSrcweir log.println("Different !") ; 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir result &= res ; 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir return result ; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir /** 168*cdf0e10cSrcweir * Forces object environment recreation. 169*cdf0e10cSrcweir */ 170*cdf0e10cSrcweir public void after() { 171*cdf0e10cSrcweir this.disposeEnvironment() ; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir 175