1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.io;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import util.ValueComparer;
29 
30 import com.sun.star.beans.Property;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.io.XObjectInputStream;
33 import com.sun.star.io.XObjectOutputStream;
34 import com.sun.star.io.XPersistObject;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38 * Testing <code>com.sun.star.io.XObjectInputStream</code>
39 * interface methods:
40 * <ul>
41 *   <li><code>readObject()</code></li>
42 * </ul> <p>
43 * This test needs the following object relations :
44 * <ul>
45 *  <li> <code>'PersistObject'</code> (of type <code>Object</code>):
46 *   object that supports interface <code>XPersistObject</code> </li>
47 * <ul> <p>
48 * After test completion object environment has to be recreated.
49 * @see com.sun.star.io.XObjectInputStream
50 * @see com.sun.star.io.XPersistObject
51 */
52 public class _XObjectInputStream extends MultiMethodTest {
53 
54     public XObjectInputStream oObj = null;
55     private Object objRead = null ;
56     private Object objWrite = null ;
57 
58     /**
59     * Test reads perisist object from stream and compares properties
60     * of the object with properties of persist object obtained
61     * from relation <code>'PersistObject'</code> <p>
62     * Has <b> OK </b> status if returned value isn't null and values
63     * of objects properties are equal. <p>
64     */
_readObject()65     public void _readObject() {
66         objWrite = tEnv.getObjRelation("PersistObject") ;
67         if (objWrite == null) {
68             log.println("PersistObject not found in relations") ;
69             tRes.tested("readObject()", false) ;
70             return ;
71         }
72 
73         // write the object
74         try {
75             XObjectOutputStream oStream = (XObjectOutputStream)
76                                     tEnv.getObjRelation("StreamWriter");
77             oStream.writeObject((XPersistObject)objWrite);
78         } catch(com.sun.star.io.IOException e) {
79             log.println("Couldn't write object to stream");
80             e.printStackTrace(log);
81             tRes.tested("readObject()", Status.skipped(false));
82             return;
83         }
84 
85         try {
86             objRead = oObj.readObject() ;
87         } catch(com.sun.star.io.IOException e) {
88             log.println("Couldn't read object from stream");
89             e.printStackTrace(log);
90             tRes.tested("readObject()", false) ;
91             return ;
92         }
93 
94         if (objRead == null) {
95             log.println("No object was read.") ;
96             tRes.tested("readObject()", false) ;
97             return ;
98         }
99 
100         XPropertySet props1 = null ;
101         XPropertySet props2 = null ;
102 
103         props1 = (XPropertySet) UnoRuntime.queryInterface
104             (XPropertySet.class, objRead) ;
105 
106         props2 = (XPropertySet) UnoRuntime.queryInterface
107             (XPropertySet.class, objWrite) ;
108 
109         if (props1 == null) {
110             log.println("Object read doesn't implement XPropertySet") ;
111             tRes.tested("readObject()", false) ;
112             return ;
113         }
114         if (props2 == null) {
115             log.println("Object written doesn't implement XPropertySet") ;
116             tRes.tested("readObject()", false) ;
117             return ;
118         }
119 
120         tRes.tested("readObject()",
121             compareProperties(props1, props2)) ;
122     }
123 
compareProperties(XPropertySet props1, XPropertySet props2)124     protected boolean compareProperties(XPropertySet props1,
125         XPropertySet props2)  {
126 
127         Property[] p1 = props1.getPropertySetInfo().getProperties() ;
128         Property[] p2 = props2.getPropertySetInfo().getProperties() ;
129 
130         if (p1.length != p2.length) {
131             log.println("Number of properties differs") ;
132             return false ;
133         }
134 
135         boolean result = true ;
136 
137         for (int i = 0; i < p1.length; i++) {
138             String propName = p1[i].Name ;
139 
140             log.print("Comparing property '" + propName + "' ...") ;
141             boolean res = false ;
142             try {
143                 res = ValueComparer.equalValue
144                     (props1.getPropertyValue(propName),
145                      props2.getPropertyValue(propName)) ;
146             } catch (com.sun.star.beans.UnknownPropertyException e) {
147                 log.println("Not found !") ;
148             } catch (com.sun.star.lang.WrappedTargetException e) {
149                 log.println(e) ;
150             }
151 
152             if (res)
153                 log.println("OK.") ;
154             else
155                 log.println("Different !") ;
156 
157             result &= res ;
158         }
159 
160         return result ;
161     }
162 
163     /**
164     * Forces object environment recreation.
165     */
after()166     public void after() {
167         this.disposeEnvironment() ;
168     }
169 }
170 
171