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