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