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 19 package complex.olesimplestorage; 20 21 import com.sun.star.lang.XMultiServiceFactory; 22 import com.sun.star.io.XTempFile; 23 import com.sun.star.embed.XOLESimpleStorage; 24 import com.sun.star.uno.UnoRuntime; 25 26 import java.util.Random; 27 28 29 public class Test01 implements OLESimpleStorageTest 30 { 31 private XMultiServiceFactory m_xMSF = null; 32 private TestHelper m_aTestHelper = null; 33 private static final int pStreamCnt = 5; 34 private static final int pBytesCnt = 10; 35 Test01( XMultiServiceFactory xMSF )36 public Test01 ( XMultiServiceFactory xMSF ) 37 { 38 m_xMSF = xMSF; 39 m_aTestHelper = new TestHelper ("Test01: "); 40 } 41 test()42 public boolean test () 43 { 44 try 45 { 46 //create a new temporary stream 47 Object oTempFile = m_xMSF.createInstance ( "com.sun.star.io.TempFile" ); 48 XTempFile xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); 49 m_aTestHelper.Message ( "A new temporary stream created." ); 50 51 //create OLESimpleStorage based on it 52 Object pArgs[] = new Object[2]; 53 pArgs[0] = xTempFile; 54 pArgs[1] = Boolean.TRUE; 55 Object oOLESimpleStorage = m_xMSF.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs ); 56 XOLESimpleStorage xOLESimpleStorage = UnoRuntime.queryInterface(XOLESimpleStorage.class, oOLESimpleStorage); 57 m_aTestHelper.Message ( "OLESimpleStorage based on XStream created." ); 58 59 //fill it with some streams 60 Object oStream[] = new Object[pStreamCnt]; 61 byte pBytesIn[][][] = new byte [pStreamCnt][1][pBytesCnt]; 62 byte pBytesOut[][] = new byte [pStreamCnt][pBytesCnt]; 63 XTempFile xTempStream[] = new XTempFile[pStreamCnt]; 64 Random oRandom = new Random (); 65 final String sSubStreamPrefix = "SubStream"; 66 for ( int i = 0; i < pStreamCnt; i++ ) 67 { 68 oRandom.nextBytes (pBytesOut[i]); 69 oStream[i] = m_xMSF.createInstance ( "com.sun.star.io.TempFile" ); 70 xTempStream[i] = UnoRuntime.queryInterface(XTempFile.class, oStream[i]); 71 xTempStream[i].getOutputStream ().writeBytes (pBytesOut[i]); 72 xTempStream[i].seek (0); 73 m_aTestHelper.Message ( "Substream " + i + " initialized." ); 74 if (xOLESimpleStorage.hasByName (sSubStreamPrefix + i)) 75 { 76 xOLESimpleStorage.replaceByName ( sSubStreamPrefix + i, xTempStream[i] ); 77 } 78 else 79 { 80 xOLESimpleStorage.insertByName ( sSubStreamPrefix + i, xTempStream[i] ); 81 m_aTestHelper.Message ( "Substream " + i + " inserted." ); 82 } 83 } 84 85 //commit the storage and close it 86 xOLESimpleStorage.commit (); 87 m_aTestHelper.Message ( "Storage committed." ); 88 xOLESimpleStorage.dispose (); 89 for ( int i = 0; i < pStreamCnt; ++i ) 90 { 91 xTempStream[i].setRemoveFile ( true ); 92 xTempStream[i].getInputStream ().closeInput (); 93 xTempStream[i].getOutputStream ().closeOutput (); 94 } 95 m_aTestHelper.Message ( "Storage closed." ); 96 97 //open the same stream with the constructor for inputstream 98 pArgs[0] = xTempFile.getInputStream (); 99 oOLESimpleStorage = m_xMSF.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs ); 100 xOLESimpleStorage = UnoRuntime.queryInterface(XOLESimpleStorage.class, oOLESimpleStorage); 101 m_aTestHelper.Message ( "Storage reopened, based on XInputStream." ); 102 103 //check that all the streams contain correct information 104 m_aTestHelper.Message ( "Checking data contained in all the substreams..." ); 105 for ( int i = 0; i < pStreamCnt; ++i ) 106 { 107 if ( xOLESimpleStorage.hasByName (sSubStreamPrefix + i) ) 108 { 109 xTempStream[i] = UnoRuntime.queryInterface(XTempFile.class, xOLESimpleStorage.getByName(sSubStreamPrefix + i)); 110 xTempStream[i].seek (0); 111 xTempStream[i].getInputStream ().readBytes (pBytesIn[i], pBytesIn[i][0].length + 1 ); 112 for ( int j = 0; j < pBytesCnt; ++j ) 113 { 114 if ( pBytesIn[i][0][j] != pBytesOut[i][j] ) 115 { 116 m_aTestHelper.Error ( "Stream " + i + " byte " + j + ": INCORRECT DATA!"); 117 return false; 118 } 119 else 120 { 121 m_aTestHelper.Message ( "Stream " + i + " byte " + j + ": CORRECT." ); 122 } 123 } 124 } 125 else 126 { 127 m_aTestHelper.Error( "Stream " + i + " is lost!"); 128 return false; 129 } 130 } 131 m_aTestHelper.Message ( "All substreams contain correct data. SUCCESS." ); 132 } 133 catch ( Exception e ) 134 { 135 m_aTestHelper.Error ( "Exception: " + e ); 136 return false; 137 } 138 return true; 139 } 140 } 141
