PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/qadevOOo/tests/java/mod/_stm/ObjectInputStream.java

https://bitbucket.org/markjenkins/libreoffice_ubuntu-debian-fixes
Java | 279 lines | 140 code | 28 blank | 111 comment | 2 complexity | ac8f057b8ed1f67ffe92152c90377fc3 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause-No-Nuclear-License-2014
  1. /*
  2. * This file is part of the LibreOffice project.
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. *
  8. * This file incorporates work covered by the following license notice:
  9. *
  10. * Licensed to the Apache Software Foundation (ASF) under one or more
  11. * contributor license agreements. See the NOTICE file distributed
  12. * with this work for additional information regarding copyright
  13. * ownership. The ASF licenses this file to you under the Apache
  14. * License, Version 2.0 (the "License"); you may not use this file
  15. * except in compliance with the License. You may obtain a copy of
  16. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. */
  18. package mod._stm;
  19. import java.io.PrintWriter;
  20. import java.util.ArrayList;
  21. import lib.StatusException;
  22. import lib.TestCase;
  23. import lib.TestEnvironment;
  24. import lib.TestParameters;
  25. import com.sun.star.io.XActiveDataSink;
  26. import com.sun.star.io.XActiveDataSource;
  27. import com.sun.star.io.XInputStream;
  28. import com.sun.star.io.XObjectInputStream;
  29. import com.sun.star.io.XObjectOutputStream;
  30. import com.sun.star.io.XOutputStream;
  31. import com.sun.star.io.XPersistObject;
  32. import com.sun.star.lang.XMultiServiceFactory;
  33. import com.sun.star.registry.CannotRegisterImplementationException;
  34. import com.sun.star.registry.XImplementationRegistration;
  35. import com.sun.star.registry.XSimpleRegistry;
  36. import com.sun.star.uno.UnoRuntime;
  37. import com.sun.star.uno.XInterface;
  38. /**
  39. * Test for object which is represented by service
  40. * <code>com.sun.star.io.ObjectInputStream</code>. <p>
  41. * Object implements the following interfaces :
  42. * <ul>
  43. * <li> <code>com::sun::star::io::XInputStream</code></li>
  44. * <li> <code>com::sun::star::io::XMarkableStream</code></li>
  45. * <li> <code>com::sun::star::io::XDataInputStream</code></li>
  46. * <li> <code>com::sun::star::io::XConnectable</code></li>
  47. * <li> <code>com::sun::star::io::XActiveDataSink</code></li>
  48. * <li> <code>com::sun::star::io::XObjectInputStream</code></li>
  49. * </ul>
  50. * The following files used by this test :
  51. * <ul>
  52. * <li><b> MyPersistObjectImpl.jar </b> : the implementation of the persist
  53. * object</li>
  54. * </ul> <p>
  55. * @see com.sun.star.io.ObjectInputStream
  56. * @see com.sun.star.io.XInputStream
  57. * @see com.sun.star.io.XMarkableStream
  58. * @see com.sun.star.io.XDataInputStream
  59. * @see com.sun.star.io.XConnectable
  60. * @see com.sun.star.io.XActiveDataSink
  61. * @see com.sun.star.io.XObjectInputStream
  62. * @see ifc.io._XInputStream
  63. * @see ifc.io._XMarkableStream
  64. * @see ifc.io._XDataInputStream
  65. * @see ifc.io._XConnectable
  66. * @see ifc.io._XActiveDataSink
  67. * @see ifc.io._XObjectInputStream
  68. */
  69. public class ObjectInputStream extends TestCase {
  70. /**
  71. * Register the implementation of service
  72. * <code>com.sun.star.cmp.PersistObject</code> if not yet registered.
  73. * @see com.sun.star.cmp.PersistObject
  74. */
  75. public void initialize(TestParameters tParam, PrintWriter log) {
  76. XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
  77. Object oPersObj = null;
  78. // test first if object is already registered
  79. try {
  80. oPersObj = xMSF.createInstance("com.sun.star.cmp.PersistObject");
  81. }
  82. catch( com.sun.star.uno.Exception e ) {
  83. log.println("Could not create instance of PersistObject");
  84. e.printStackTrace(log);
  85. log.println("Going on with test...");
  86. }
  87. if ( oPersObj == null ) {
  88. // object is not available: it has to be registered
  89. String url = util.utils.getFullTestURL
  90. ("qadevlibs/MyPersistObjectImpl.jar");
  91. XImplementationRegistration xir;
  92. try {
  93. Object o = xMSF.createInstance(
  94. "com.sun.star.registry.ImplementationRegistration");
  95. xir = UnoRuntime.queryInterface(
  96. XImplementationRegistration.class, o);
  97. }
  98. catch (com.sun.star.uno.Exception e) {
  99. System.err.println(
  100. "Couldn't create implementation registration");
  101. e.printStackTrace();
  102. throw new StatusException("Couldn't create ImplReg", e);
  103. }
  104. XSimpleRegistry xReg = null;
  105. try {
  106. System.out.println("Register library: " + url);
  107. xir.registerImplementation(
  108. "com.sun.star.loader.Java2", url, xReg);
  109. System.out.println("...done");
  110. } catch (CannotRegisterImplementationException e) {
  111. System.err.println("Name: " + url + " msg: " +
  112. e.getMessage());
  113. e.printStackTrace();
  114. throw new StatusException(
  115. "Couldn't register MyPersistObject", e);
  116. }
  117. }
  118. }
  119. /**
  120. * Creating a Testenvironment for the interfaces to be tested.
  121. * Creates an instances of services
  122. * <code>com.sun.star.io.ObjectInputStream</code>,
  123. * <code>com.sun.star.io.ObjectOutputStream</code>,
  124. * <code>com.sun.star.io.Pipe</code>,
  125. * <code>com.sun.star.io.MarkableInputStream</code> and
  126. * <code>com.sun.star.io.MarkableOutputStream</code>. Plugs the created
  127. * markable output stream as output stream for the created
  128. * <code>ObjectOutputStream</code>. Plugs the created pipe as output stream
  129. * for the created <code>MarkableOutputStream</code>. Plugs the created
  130. * markable input stream as input stream for the created
  131. * <code>ObjectInputStream</code>. Plugs the created pipe as input stream
  132. * for the created <code>MarkableInputStream</code>. Creates an instance
  133. * of the service <code>com.sun.star.cmp.PersistObject</code> and writes
  134. * the created object to the object output stream.
  135. * Object relations created :
  136. * <ul>
  137. * <li> <code>'PersistObject'</code> for
  138. * {@link ifc.io._XObjectInputStream}(the created instance of the
  139. * persist object ) </li>
  140. * <li> <code>'StreamData'</code> for
  141. * {@link ifc.io._XDataInputStream}(the data that should be written into
  142. * the stream) </li>
  143. * <li> <code>'ByteData'</code> for
  144. * {@link ifc.io._XInputStream}(the data that should be written into
  145. * the stream) </li>
  146. * <li> <code>'StreamWriter'</code> for
  147. * {@link ifc.io._XDataInputStream}
  148. * {@link ifc.io._XObjectInputStream}
  149. * {@link ifc.io._XInputStream}(a stream to write data to) </li>
  150. * <li> <code>'Connectable'</code> for
  151. * {@link ifc.io._XConnectable}
  152. * (another object that can be connected) </li>
  153. * <li> <code>'InputStream'</code> for
  154. * {@link ifc.io._XActiveDataSink}(an input stream to set and get) </li>
  155. * </ul>
  156. * @see com.sun.star.io.ObjectInputStream
  157. * @see com.sun.star.io.ObjectOutputStream
  158. * @see com.sun.star.io.Pipe
  159. * @see com.sun.star.io.MarkableInputStream
  160. * @see com.sun.star.io.MarkableOutputStream
  161. * @see com.sun.star.cmp.PersistObject
  162. */
  163. public TestEnvironment createTestEnvironment(
  164. TestParameters Param, PrintWriter log) throws StatusException {
  165. System.out.println("create TestEnvironment started.");
  166. XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
  167. Object ostream = null;
  168. Object aPipe = null;
  169. Object mostream = null;
  170. Object mistream = null;
  171. Object istream = null;
  172. Object xConnect = null;
  173. try {
  174. istream = xMSF.createInstance
  175. ("com.sun.star.io.ObjectInputStream");
  176. ostream = xMSF.createInstance
  177. ("com.sun.star.io.ObjectOutputStream");
  178. aPipe = xMSF.createInstance
  179. ("com.sun.star.io.Pipe");
  180. mistream = xMSF.createInstance
  181. ("com.sun.star.io.MarkableInputStream");
  182. mostream = xMSF.createInstance
  183. ("com.sun.star.io.MarkableOutputStream");
  184. xConnect = xMSF.createInstance
  185. ("com.sun.star.io.DataInputStream") ;
  186. } catch( com.sun.star.uno.Exception e ) {
  187. e.printStackTrace(log);
  188. throw new StatusException("Couldn't create instance", e);
  189. }
  190. // Creating construction :
  191. // ObjectOutputStream -> MarkableOutputStream -> Pipe ->
  192. // -> MarkableInputStream -> ObjectInputStream
  193. XActiveDataSource xdSo = UnoRuntime.queryInterface(XActiveDataSource.class, ostream);
  194. XActiveDataSource xdSmo = UnoRuntime.queryInterface(XActiveDataSource.class, mostream);
  195. XOutputStream moStream = UnoRuntime.queryInterface(XOutputStream.class, mostream);
  196. XOutputStream PipeOut = UnoRuntime.queryInterface(XOutputStream.class, aPipe);
  197. XInputStream PipeIn = UnoRuntime.queryInterface(XInputStream.class, aPipe);
  198. xdSo.setOutputStream(moStream);
  199. xdSmo.setOutputStream(PipeOut);
  200. XObjectInputStream iStream = UnoRuntime.queryInterface(XObjectInputStream.class, istream);
  201. XObjectOutputStream oStream = null;
  202. oStream = UnoRuntime.queryInterface(XObjectOutputStream.class, ostream);
  203. XActiveDataSink xmSi = UnoRuntime.queryInterface(XActiveDataSink.class, mistream);
  204. XInputStream xmIstream = UnoRuntime.queryInterface(XInputStream.class, mistream);
  205. XActiveDataSink xdSi = UnoRuntime.queryInterface
  206. (XActiveDataSink.class, istream);
  207. xdSi.setInputStream(xmIstream);
  208. xmSi.setInputStream(PipeIn);
  209. // creating Persist object which has to be written
  210. XPersistObject xPersObj = null;
  211. try {
  212. Object oPersObj = xMSF.createInstance
  213. ("com.sun.star.cmp.PersistObject");
  214. xPersObj = UnoRuntime.queryInterface(XPersistObject.class, oPersObj);
  215. } catch (com.sun.star.uno.Exception e) {
  216. e.printStackTrace(log);
  217. throw new StatusException("Can't write persist object.", e);
  218. }
  219. // all data types for writing to an XDataInputStream
  220. ArrayList<Object> data = new ArrayList<Object>() ;
  221. data.add(new Boolean(true)) ;
  222. data.add(new Byte((byte)123)) ;
  223. data.add(new Character((char)1234)) ;
  224. data.add(new Short((short)1234)) ;
  225. data.add(new Integer(123456)) ;
  226. data.add(new Float(1.234)) ;
  227. data.add(new Double(1.23456)) ;
  228. data.add("DataInputStream") ;
  229. // information for writing to the pipe
  230. byte[] byteData = new byte[] {
  231. 1, 2, 3, 4, 5, 6, 7, 8 } ;
  232. System.out.println("create environment");
  233. XInterface oObj = iStream;
  234. log.println( "creating a new environment for object" );
  235. TestEnvironment tEnv = new TestEnvironment( oObj );
  236. // adding persistent object
  237. tEnv.addObjRelation("PersistObject", xPersObj);
  238. // add a connectable
  239. tEnv.addObjRelation("Connectable", xConnect);
  240. tEnv.addObjRelation("StreamWriter", oStream);
  241. // for XActiveDataSink
  242. tEnv.addObjRelation("InputStream", aPipe);
  243. // adding sequence of data that must be read
  244. // by XDataInputStream interface methods
  245. tEnv.addObjRelation("StreamData", data) ;
  246. // and by XInputStream interface methods
  247. tEnv.addObjRelation("ByteData", byteData) ;
  248. System.out.println("create TestEnvironment finished.");
  249. return tEnv;
  250. } // finish method getTestEnvironment
  251. }