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 util.ValueComparer;
32 import util.dbg;
33 import util.utils;
34 
35 import com.sun.star.beans.Property;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.beans.XPropertySetInfo;
38 import com.sun.star.io.XActiveDataSink;
39 import com.sun.star.io.XActiveDataSource;
40 import com.sun.star.io.XInputStream;
41 import com.sun.star.io.XObjectInputStream;
42 import com.sun.star.io.XObjectOutputStream;
43 import com.sun.star.io.XOutputStream;
44 import com.sun.star.io.XPersistObject;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.uno.UnoRuntime;
47 
48 
49 /**
50 * Testing <code>com.sun.star.io.XPersistObject</code>
51 * interface methods :
52 * <ul>
53 *  <li><code> getServiceName()</code></li>
54 *  <li><code> write()</code></li>
55 *  <li><code> read()</code></li>
56 * </ul> <p>
57 * This test need the following object relations :
58 * <ul>
59 *  <li> <code>'OBJNAME'</code> : <code>String</code> value that
60 *   contains service name which object represents.</li>
61 * <ul> <p>
62 * Test is <b> NOT </b> multithread compilant. <p>
63 * After test completion object environment has to be recreated.
64 * @see com.sun.star.io.XPersistObject
65 * @see com.sun.star.io.XObjectInputStream
66 * @see com.sun.star.io.XObjectOutputStream
67 */
68 public class _XPersistObject extends MultiMethodTest {
69 
70     public XPersistObject    oObj = null;
71     XObjectInputStream iStream = null;
72     XObjectOutputStream oStream = null;
73     String sname = null;
74 
75     boolean    result = true;
76 
77 
78     /**
79     * Test calls the method and checks return value. <p>
80     * Has <b> OK </b> status if the method returns proper service names
81     * which is equal to <code>'OBJNAME'</code> relation value. <p>
82     */
83     public void _getServiceName() {
84         result = true;
85         sname = oObj.getServiceName();
86         log.println("Method returned '" + sname + "'") ;
87         String objName = (String)tEnv.getObjRelation("OBJNAME");
88         if (objName == null) {
89             log.println("No OBJNAME relation!");
90             result = false;
91         } else {
92             result &= sname.equals(objName);
93             if (!result)
94                 log.println("Name of object must be '" + objName +
95                             "' but returned name is '" + sname +"'");
96         }
97 
98         tRes.tested("getServiceName()", result);
99     }
100 
101     /**
102     * Creates service get by <code>getServiceName</code> method and tries
103     * to read object written to stream by <code>write</code> method test.
104     * Then properties of object written and object read are compared. <p>
105     * Has <b>OK</b> status if all properties of two objects are equal
106     * and no exceptions were thrown. <p>
107     * The following method tests are to be completed successfully before :
108     * <ul>
109     *  <li> <code> getServiceName() </code> : to have service name
110     *   which has to be created </li>
111     *  <li> <code> write() </code> : to write object tested into stream</li>
112     * </ul>
113     */
114     public void _read() {
115         requiredMethod("getServiceName()");
116         requiredMethod("write()") ;
117 
118         boolean bResult = true;
119 
120         try {
121             Object noPS = tEnv.getObjRelation("noPS");
122             if ( noPS == null) {
123                 XPropertySet objps = (XPropertySet)UnoRuntime.queryInterface(
124                     XPropertySet.class, oObj);
125                 XPropertySetInfo objpsi = objps.getPropertySetInfo();
126                 Property[] objprops = objpsi.getProperties();
127 
128                 Object oCopy = ((XMultiServiceFactory)tParam.getMSF()).createInstance(sname);
129 
130                 XPersistObject persCopy = (XPersistObject)
131                         UnoRuntime.queryInterface(XPersistObject.class, oCopy);
132 
133                 persCopy.read(iStream);
134 
135                 XPropertySet copyps = (XPropertySet)UnoRuntime.queryInterface(
136                                                     XPropertySet.class, oCopy);
137 
138                 XPropertySetInfo copypsi = copyps.getPropertySetInfo();
139                 Property[] copyprops = copypsi.getProperties();
140 
141                 for (int i = 0; i < copyprops.length; i++) {
142                     Object cps = copyps.getPropertyValue(copyprops[i].Name);
143                     Object ops = objps.getPropertyValue(objprops[i].Name);
144                     boolean locRes = ( (ValueComparer.equalValue(cps,ops)) ||
145                                     (utils.isVoid(cps) && utils.isVoid(ops)) );
146 
147                     //transient properties aran't stored
148                     if (isTransient(objprops[i])) locRes = true;
149 
150                     Object pseudo = tEnv.getObjRelation("PSEUDOPERSISTENT");
151                     if ( (pseudo != null) && !locRes) {
152                         String str = copyprops[i].Name;
153                         locRes = ( (str.equals("Time")) || (str.equals("Date"))
154                                             || (str.equals("FormatsSupplier"))
155                                             || (str.equals("Text"))
156                                             || (str.equals("Value"))
157                                             || (str.indexOf("UserDefined")>0)
158                                             );
159                     }
160                     if (!locRes) {
161                         log.println("Property '" + copyprops[i].Name
162                             + "' failed");
163                         dbg.printPropertyInfo(objps, objprops[i].Name, log);
164                         dbg.printPropertyInfo(copyps, copyprops[i].Name, log);
165                     }
166                     bResult &= locRes;
167                 }
168             } else {
169                 Object oCopy = ((XMultiServiceFactory)tParam.getMSF()).createInstance(sname);
170                 XPersistObject persCopy = (XPersistObject)
171                         UnoRuntime.queryInterface(XPersistObject.class, oCopy);
172 
173                 persCopy.read(iStream);
174 
175                 bResult = ( persCopy.getServiceName().equals(sname) );
176 
177             }
178 
179         } catch (com.sun.star.uno.Exception e) {
180             log.println("Exception occured : ");
181             e.printStackTrace(log) ;
182             bResult = false;
183         }
184 
185         tRes.tested("read()", bResult);
186     }
187 
188     /**
189     * Test calls the method and checks that
190     * no exceptions were thrown. <p>
191     * Has <b> OK </b> status if no exceptions were thrown. <p>
192     */
193     public void _write() {
194         boolean bResult = true;
195         try {
196             initPipe();
197             oObj.write(oStream);
198         } catch (com.sun.star.io.IOException e) {
199             log.println("Exception occured while test. " + e);
200             bResult = false;
201         }
202         tRes.tested("write()", bResult);
203     }
204 
205 
206     /**
207     * Creates the following stream scheme <code>
208     * ObjectOutputStream -> Pipe -> ObjectInputStream </code> for writing/reading
209     * object.
210     */
211     protected void initPipe() {
212         try {
213             Object aPipe = ((XMultiServiceFactory)tParam.getMSF()).createInstance
214                 ("com.sun.star.io.Pipe");
215             Object istream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
216                 ("com.sun.star.io.ObjectInputStream");
217             Object ostream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
218                 ("com.sun.star.io.ObjectOutputStream");
219 
220             // Now the objects that aren't described anywhere
221             Object mistream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
222                 ("com.sun.star.io.MarkableInputStream");
223             Object mostream = ((XMultiServiceFactory)tParam.getMSF()).createInstance
224                 ("com.sun.star.io.MarkableOutputStream");
225 
226             XActiveDataSink xdSi = (XActiveDataSink)
227                 UnoRuntime.queryInterface(XActiveDataSink.class, istream);
228             XActiveDataSource xdSo = (XActiveDataSource)
229                 UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
230             XActiveDataSink xdSmi = (XActiveDataSink)
231                 UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
232             XActiveDataSource xdSmo = (XActiveDataSource)
233                 UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
234 
235             XInputStream miStream = (XInputStream)
236                 UnoRuntime.queryInterface(XInputStream.class, mistream);
237             XOutputStream moStream = (XOutputStream)
238                 UnoRuntime.queryInterface(XOutputStream.class, mostream);
239             XInputStream PipeIn = (XInputStream)
240                 UnoRuntime.queryInterface(XInputStream.class, aPipe);
241             XOutputStream PipeOut = (XOutputStream)
242                 UnoRuntime.queryInterface(XOutputStream.class,aPipe);
243 
244             xdSi.setInputStream(miStream);
245             xdSo.setOutputStream(moStream);
246             xdSmi.setInputStream(PipeIn);
247             xdSmo.setOutputStream(PipeOut);
248 
249             iStream = (XObjectInputStream)
250                 UnoRuntime.queryInterface(XObjectInputStream.class, istream);
251             oStream = (XObjectOutputStream)
252                 UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
253 
254 
255         } catch (com.sun.star.uno.Exception e) {
256             System.out.println("exc " + e);
257         }
258 
259     }
260 
261     public static boolean isTransient(Property prop) {
262         short attr = prop.Attributes;
263         return ((attr & com.sun.star.beans.PropertyAttribute.TRANSIENT) != 0);
264     }
265 
266 
267 }
268 
269 
270