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