1*cdf0e10cSrcweir package storagetesting; 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 13*cdf0e10cSrcweir import storagetesting.TestHelper; 14*cdf0e10cSrcweir import storagetesting.StorageTest; 15*cdf0e10cSrcweir 16*cdf0e10cSrcweir public class Test01 implements StorageTest { 17*cdf0e10cSrcweir 18*cdf0e10cSrcweir XMultiServiceFactory m_xMSF; 19*cdf0e10cSrcweir XSingleServiceFactory m_xStorageFactory; 20*cdf0e10cSrcweir TestHelper m_aTestHelper; 21*cdf0e10cSrcweir 22*cdf0e10cSrcweir public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory ) 23*cdf0e10cSrcweir { 24*cdf0e10cSrcweir m_xMSF = xMSF; 25*cdf0e10cSrcweir m_xStorageFactory = xStorageFactory; 26*cdf0e10cSrcweir m_aTestHelper = new TestHelper( "Test01: " ); 27*cdf0e10cSrcweir } 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir public boolean test() 30*cdf0e10cSrcweir { 31*cdf0e10cSrcweir try 32*cdf0e10cSrcweir { 33*cdf0e10cSrcweir String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 34*cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL == "" ) 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir m_aTestHelper.Error( "No valid temporary file was created!" ); 37*cdf0e10cSrcweir return false; 38*cdf0e10cSrcweir } 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir // create temporary storage based on arbitrary medium 41*cdf0e10cSrcweir // after such a storage is closed it is lost 42*cdf0e10cSrcweir Object oTempStorage = m_xStorageFactory.createInstance(); 43*cdf0e10cSrcweir XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 44*cdf0e10cSrcweir if ( xTempStorage == null ) 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create temporary storage representation!" ); 47*cdf0e10cSrcweir return false; 48*cdf0e10cSrcweir } 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir // open a new substorage 51*cdf0e10cSrcweir XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 52*cdf0e10cSrcweir "SubStorage1", 53*cdf0e10cSrcweir ElementModes.ELEMENT_WRITE ); 54*cdf0e10cSrcweir if ( xTempSubStorage == null ) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create substorage!" ); 57*cdf0e10cSrcweir return false; 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir byte pBytes1[] = { 1, 1, 1, 1, 1 }; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 63*cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) ) 64*cdf0e10cSrcweir return false; 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir byte pBytes2[] = { 2, 2, 2, 2, 2 }; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 69*cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) ) 70*cdf0e10cSrcweir return false; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 73*cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 74*cdf0e10cSrcweir "MediaType3", 75*cdf0e10cSrcweir true, 76*cdf0e10cSrcweir ElementModes.ELEMENT_READWRITE ) ) 77*cdf0e10cSrcweir return false; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 80*cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 81*cdf0e10cSrcweir "MediaType4", 82*cdf0e10cSrcweir false, 83*cdf0e10cSrcweir ElementModes.ELEMENT_WRITE ) ) 84*cdf0e10cSrcweir return false; 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // create temporary storage based on a previously created temporary file 87*cdf0e10cSrcweir Object pArgs[] = new Object[2]; 88*cdf0e10cSrcweir pArgs[0] = (Object) sTempFileURL; 89*cdf0e10cSrcweir pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE ); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 92*cdf0e10cSrcweir XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 93*cdf0e10cSrcweir if ( xTempFileStorage == null ) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 96*cdf0e10cSrcweir return false; 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir // copy xTempStorage to xTempFileStorage 100*cdf0e10cSrcweir // xTempFileStorage will be automatically commited 101*cdf0e10cSrcweir if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) ) 102*cdf0e10cSrcweir return false; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir // dispose used storages to free resources 105*cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 106*cdf0e10cSrcweir return false; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir // ================================================ 109*cdf0e10cSrcweir // now check all the written and copied information 110*cdf0e10cSrcweir // ================================================ 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir // the temporary file must not be locked any more after storage disposing 113*cdf0e10cSrcweir pArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE ); 114*cdf0e10cSrcweir Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 115*cdf0e10cSrcweir XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage ); 116*cdf0e10cSrcweir if ( xResultStorage == null ) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 119*cdf0e10cSrcweir return false; 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.ELEMENT_READWRITE ) ) 123*cdf0e10cSrcweir return false; 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir // open existing substorage 126*cdf0e10cSrcweir XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage, 127*cdf0e10cSrcweir "SubStorage1", 128*cdf0e10cSrcweir ElementModes.ELEMENT_READ ); 129*cdf0e10cSrcweir if ( xResultSubStorage == null ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir m_aTestHelper.Error( "Can't open existing substorage!" ); 132*cdf0e10cSrcweir return false; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.ELEMENT_READ ) ) 136*cdf0e10cSrcweir return false; 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) ) 139*cdf0e10cSrcweir return false; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) ) 142*cdf0e10cSrcweir return false; 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // dispose used storages to free resources 145*cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 146*cdf0e10cSrcweir return false; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir return true; 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir catch( Exception e ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 153*cdf0e10cSrcweir return false; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159