1*cdf0e10cSrcweir package complex.ofopxmlstorages; 2*cdf0e10cSrcweir 3*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 4*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 5*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 6*cdf0e10cSrcweir 7*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 8*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 9*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 10*cdf0e10cSrcweir 11*cdf0e10cSrcweir import com.sun.star.embed.*; 12*cdf0e10cSrcweir import com.sun.star.beans.StringPair; 13*cdf0e10cSrcweir 14*cdf0e10cSrcweir import share.LogWriter; 15*cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper; 16*cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest; 17*cdf0e10cSrcweir 18*cdf0e10cSrcweir public class Test01 implements StorageTest { 19*cdf0e10cSrcweir 20*cdf0e10cSrcweir XMultiServiceFactory m_xMSF; 21*cdf0e10cSrcweir XSingleServiceFactory m_xStorageFactory; 22*cdf0e10cSrcweir TestHelper m_aTestHelper; 23*cdf0e10cSrcweir 24*cdf0e10cSrcweir public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 25*cdf0e10cSrcweir { 26*cdf0e10cSrcweir m_xMSF = xMSF; 27*cdf0e10cSrcweir m_xStorageFactory = xStorageFactory; 28*cdf0e10cSrcweir m_aTestHelper = new TestHelper( aLogWriter, "Test01: " ); 29*cdf0e10cSrcweir } 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir public boolean test() 32*cdf0e10cSrcweir { 33*cdf0e10cSrcweir StringPair[][] aRelations1 = 34*cdf0e10cSrcweir { { new StringPair( "Id", "Num1" ) }, 35*cdf0e10cSrcweir { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) }, 36*cdf0e10cSrcweir { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 37*cdf0e10cSrcweir { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 38*cdf0e10cSrcweir { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 39*cdf0e10cSrcweir { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 40*cdf0e10cSrcweir { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) } 41*cdf0e10cSrcweir }; 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir StringPair[][] aRelations2 = 44*cdf0e10cSrcweir { { new StringPair( "Id", "Num1" ) }, 45*cdf0e10cSrcweir { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) }, 46*cdf0e10cSrcweir { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 47*cdf0e10cSrcweir { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) }, 48*cdf0e10cSrcweir { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) }, 49*cdf0e10cSrcweir { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }, 50*cdf0e10cSrcweir { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) } 51*cdf0e10cSrcweir }; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir try 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 56*cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL == "" ) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir m_aTestHelper.Error( "No valid temporary file was created!" ); 59*cdf0e10cSrcweir return false; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // create temporary storage based on arbitrary medium 63*cdf0e10cSrcweir // after such a storage is closed it is lost 64*cdf0e10cSrcweir XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory ); 65*cdf0e10cSrcweir if ( xTempStorage == null ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create temporary storage representation!" ); 68*cdf0e10cSrcweir return false; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir // open a new substorage 72*cdf0e10cSrcweir XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 73*cdf0e10cSrcweir "SubStorage1", 74*cdf0e10cSrcweir ElementModes.WRITE ); 75*cdf0e10cSrcweir if ( xTempSubStorage == null ) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create substorage!" ); 78*cdf0e10cSrcweir return false; 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir byte pBytes1[] = { 1, 1, 1, 1, 1 }; 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 84*cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 85*cdf0e10cSrcweir "SubStream1", 86*cdf0e10cSrcweir "MediaType1", 87*cdf0e10cSrcweir true, 88*cdf0e10cSrcweir pBytes1, 89*cdf0e10cSrcweir aRelations1 ) ) 90*cdf0e10cSrcweir return false; 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir byte pBytes2[] = { 2, 2, 2, 2, 2 }; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 95*cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 96*cdf0e10cSrcweir "SubStream2", 97*cdf0e10cSrcweir "MediaType2", 98*cdf0e10cSrcweir false, 99*cdf0e10cSrcweir pBytes2, 100*cdf0e10cSrcweir aRelations2 ) ) 101*cdf0e10cSrcweir return false; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 104*cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 105*cdf0e10cSrcweir true, 106*cdf0e10cSrcweir ElementModes.WRITE, 107*cdf0e10cSrcweir aRelations1 ) ) 108*cdf0e10cSrcweir return false; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 111*cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 112*cdf0e10cSrcweir false, 113*cdf0e10cSrcweir ElementModes.WRITE, 114*cdf0e10cSrcweir aRelations1 ) ) 115*cdf0e10cSrcweir return false; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir // create temporary storage based on a previously created temporary file 118*cdf0e10cSrcweir XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory, 119*cdf0e10cSrcweir sTempFileURL, 120*cdf0e10cSrcweir ElementModes.WRITE ); 121*cdf0e10cSrcweir if ( xTempFileStorage == null ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 124*cdf0e10cSrcweir return false; 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir // copy xTempStorage to xTempFileStorage 128*cdf0e10cSrcweir // xTempFileStorage will be automatically commited 129*cdf0e10cSrcweir if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) ) 130*cdf0e10cSrcweir return false; 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir // dispose used storages to free resources 133*cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 134*cdf0e10cSrcweir return false; 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir // ================================================ 137*cdf0e10cSrcweir // now check all the written and copied information 138*cdf0e10cSrcweir // ================================================ 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir // the temporary file must not be locked any more after storage disposing 141*cdf0e10cSrcweir XStorage xResultStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory, 142*cdf0e10cSrcweir sTempFileURL, 143*cdf0e10cSrcweir ElementModes.WRITE ); 144*cdf0e10cSrcweir if ( xResultStorage == null ) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 147*cdf0e10cSrcweir return false; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultStorage, 151*cdf0e10cSrcweir true, 152*cdf0e10cSrcweir ElementModes.WRITE, 153*cdf0e10cSrcweir aRelations1 ) ) 154*cdf0e10cSrcweir return false; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir // open existing substorage 157*cdf0e10cSrcweir XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage, 158*cdf0e10cSrcweir "SubStorage1", 159*cdf0e10cSrcweir ElementModes.READ ); 160*cdf0e10cSrcweir if ( xResultSubStorage == null ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir m_aTestHelper.Error( "Can't open existing substorage!" ); 163*cdf0e10cSrcweir return false; 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, 167*cdf0e10cSrcweir false, 168*cdf0e10cSrcweir ElementModes.READ, 169*cdf0e10cSrcweir aRelations1 ) ) 170*cdf0e10cSrcweir return false; 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, 173*cdf0e10cSrcweir "SubStream1", 174*cdf0e10cSrcweir "MediaType1", 175*cdf0e10cSrcweir pBytes1, 176*cdf0e10cSrcweir aRelations1 ) ) 177*cdf0e10cSrcweir return false; 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, 180*cdf0e10cSrcweir "SubStream2", 181*cdf0e10cSrcweir "MediaType2", 182*cdf0e10cSrcweir pBytes2, 183*cdf0e10cSrcweir aRelations2 ) ) 184*cdf0e10cSrcweir return false; 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir // dispose used storages to free resources 187*cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 188*cdf0e10cSrcweir return false; 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir return true; 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir catch( Exception e ) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 195*cdf0e10cSrcweir return false; 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201