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