1 package storagetesting; 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 import com.sun.star.io.XStream; 11 12 import com.sun.star.embed.*; 13 14 import storagetesting.TestHelper; 15 import storagetesting.StorageTest; 16 17 public class Test05 implements StorageTest { 18 19 XMultiServiceFactory m_xMSF; 20 XSingleServiceFactory m_xStorageFactory; 21 TestHelper m_aTestHelper; 22 23 public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory ) 24 { 25 m_xMSF = xMSF; 26 m_xStorageFactory = xStorageFactory; 27 m_aTestHelper = new TestHelper( "Test05: " ); 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 a previously created temporary file 42 Object pArgs[] = new Object[2]; 43 pArgs[0] = (Object) sTempFileURL; 44 pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE ); 45 46 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 47 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 48 if ( xTempFileStorage == null ) 49 { 50 m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 51 return false; 52 } 53 54 // open a new substorage 55 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage, 56 "SubStorage1", 57 ElementModes.ELEMENT_WRITE ); 58 if ( xTempSubStorage == null ) 59 { 60 m_aTestHelper.Error( "Can't create substorage!" ); 61 return false; 62 } 63 64 // open a new substorage 65 XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage, 66 "SubSubStorage1", 67 ElementModes.ELEMENT_WRITE ); 68 if ( xSubSubStorage == null ) 69 { 70 m_aTestHelper.Error( "Can't create substorage!" ); 71 return false; 72 } 73 74 75 byte pBytes1[] = { 1, 1, 1, 1, 1 }; 76 77 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 78 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) ) 79 return false; 80 81 byte pBytes2[] = { 2, 2, 2, 2, 2 }; 82 83 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 84 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) ) 85 return false; 86 87 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 88 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage, 89 "MediaType3", 90 true, 91 ElementModes.ELEMENT_WRITE ) ) 92 return false; 93 94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 95 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 96 "MediaType4", 97 false, 98 ElementModes.ELEMENT_WRITE ) ) 99 return false; 100 101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 102 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage, 103 "MediaType5", 104 false, 105 ElementModes.ELEMENT_WRITE ) ) 106 return false; 107 108 109 // commit all the storages 110 if ( !m_aTestHelper.commitStorage( xSubSubStorage ) ) 111 return false; 112 113 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) ) 114 return false; 115 116 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) ) 117 return false; 118 119 // try to open an opened substorage, open call must fail 120 if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) ) 121 return false; 122 123 124 // reopen created streams 125 XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage, 126 "SubStream1", 127 ElementModes.ELEMENT_READWRITE | ElementModes.ELEMENT_NOCREATE ); 128 XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage, 129 "SubStream2", 130 ElementModes.ELEMENT_READ | ElementModes.ELEMENT_NOCREATE ); 131 if ( xSubStream1 == null || xSubStream2 == null ) 132 return false; 133 134 // it should be possible to have more then one copy of stream for reading 135 XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage, 136 "SubStream2", 137 ElementModes.ELEMENT_READ | ElementModes.ELEMENT_NOCREATE ); 138 if ( xSubStream2 == null ) 139 return false; 140 141 142 // so now the first stream can not be open neither for reading nor for writing 143 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.ELEMENT_WRITE ) 144 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.ELEMENT_READ ) ) 145 return false; 146 147 // the second stream can not be open for writing 148 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.ELEMENT_WRITE ) ) 149 return false; 150 151 152 // dispose xTestSubStorage, all the subtree must be disposed 153 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) ) 154 return false; 155 156 // check that subtree was disposed correctly 157 try 158 { 159 xSubSubStorage.isStreamElement( "SubStream1" ); 160 m_aTestHelper.Error( "Substorage was not disposed!" ); 161 return false; 162 } 163 catch ( com.sun.star.lang.DisposedException de ) 164 {} 165 catch ( Exception e ) 166 { 167 m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e ); 168 return false; 169 } 170 171 try 172 { 173 xSubStream1.getInputStream(); 174 m_aTestHelper.Error( "Writeable substream was not disposed!" ); 175 return false; 176 } 177 catch ( com.sun.star.lang.DisposedException de ) 178 {} 179 catch ( Exception e ) 180 { 181 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e ); 182 return false; 183 } 184 185 try 186 { 187 xSubStream2.getInputStream(); 188 m_aTestHelper.Error( "Readonly substream was not disposed!" ); 189 return false; 190 } 191 catch ( com.sun.star.lang.DisposedException de ) 192 {} 193 catch ( Exception e ) 194 { 195 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e ); 196 return false; 197 } 198 199 200 // dispose root storage 201 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 202 return false; 203 204 205 // ================================================ 206 // now check all the written and copied information 207 // ================================================ 208 209 pArgs[1] = new Integer( ElementModes.ELEMENT_READ ); 210 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 211 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage ); 212 if ( xResultStorage == null ) 213 { 214 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 215 return false; 216 } 217 218 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.ELEMENT_READ ) ) 219 return false; 220 221 // open existing substorage 222 XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage, 223 "SubStorage1", 224 ElementModes.ELEMENT_READ ); 225 if ( xResSubStorage == null ) 226 { 227 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" ); 228 return false; 229 } 230 231 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.ELEMENT_READ ) ) 232 return false; 233 234 // open existing substorage 235 XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage, 236 "SubSubStorage1", 237 ElementModes.ELEMENT_READ ); 238 if ( xResSubSubStorage == null ) 239 { 240 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" ); 241 return false; 242 } 243 244 if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.ELEMENT_READ ) ) 245 return false; 246 247 // check substreams 248 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", pBytes1 ) ) 249 return false; 250 251 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", pBytes2 ) ) 252 return false; 253 254 // dispose used storages to free resources 255 if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 256 return false; 257 258 return true; 259 } 260 catch( Exception e ) 261 { 262 m_aTestHelper.Error( "Exception: " + e ); 263 return false; 264 } 265 } 266 267 } 268 269