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 import com.sun.star.lang.DisposedException; 7 8 import com.sun.star.bridge.XUnoUrlResolver; 9 import com.sun.star.uno.UnoRuntime; 10 import com.sun.star.uno.XInterface; 11 12 import com.sun.star.container.XNameAccess; 13 14 import com.sun.star.embed.*; 15 16 import storagetesting.TestHelper; 17 import storagetesting.StorageTest; 18 19 public class Test04 implements StorageTest { 20 21 XMultiServiceFactory m_xMSF; 22 XSingleServiceFactory m_xStorageFactory; 23 TestHelper m_aTestHelper; 24 25 public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory ) 26 { 27 m_xMSF = xMSF; 28 m_xStorageFactory = xStorageFactory; 29 m_aTestHelper = new TestHelper( "Test04: " ); 30 } 31 32 public boolean test() 33 { 34 try 35 { 36 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 37 if ( sTempFileURL == null || sTempFileURL == "" ) 38 { 39 m_aTestHelper.Error( "No valid temporary file was created!" ); 40 return false; 41 } 42 43 // create temporary storage based on arbitrary medium 44 // after such a storage is closed it is lost 45 Object oTempStorage = m_xStorageFactory.createInstance(); 46 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 47 if ( xTempStorage == null ) 48 { 49 m_aTestHelper.Error( "Can't create temporary storage representation!" ); 50 return false; 51 } 52 53 // open substorages and create streams there 54 55 // first substorage of the root storage 56 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage, 57 "SubStorage1", 58 ElementModes.ELEMENT_WRITE ); 59 if ( xTempSubStorage1 == null ) 60 { 61 m_aTestHelper.Error( "Can't create substorage!" ); 62 return false; 63 } 64 65 byte pBytes1[] = { 1, 1, 1, 1, 1 }; 66 67 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 68 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) ) 69 return false; 70 71 // second substorage of the root storage 72 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage, 73 "SubStorage2", 74 ElementModes.ELEMENT_WRITE ); 75 if ( xTempSubStorage2 == null ) 76 { 77 m_aTestHelper.Error( "Can't create substorage!" ); 78 return false; 79 } 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( xTempSubStorage2, "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( xTempStorage, 89 "MediaType3", 90 true, 91 ElementModes.ELEMENT_READWRITE ) ) 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( xTempSubStorage1, 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( xTempSubStorage2, 103 "MediaType5", 104 false, 105 ElementModes.ELEMENT_WRITE ) ) 106 return false; 107 108 // create temporary storage based on a previously created temporary file 109 Object pArgs[] = new Object[2]; 110 pArgs[0] = (Object) sTempFileURL; 111 pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE ); 112 113 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 114 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 115 if ( xTempFileStorage == null ) 116 { 117 m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 118 return false; 119 } 120 121 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) ) 122 return false; 123 124 // if storage is not commited before disposing all the changes will be lost 125 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) ) 126 return false; 127 128 // a storage must be disposed before moving/removing otherwise the access will be denied 129 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) ) 130 return false; 131 132 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) ) 133 return false; 134 135 // SubStorage2 must be removed and disposed now 136 try 137 { 138 xTempSubStorage2.isStreamElement( "SubStream2" ); 139 m_aTestHelper.Error( "SubStorage2 must be disposed already!" ); 140 return false; 141 } 142 catch( com.sun.star.lang.DisposedException de ) 143 { 144 } 145 catch( Exception e ) 146 { 147 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e ); 148 return false; 149 } 150 151 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) ) 152 return false; 153 154 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) ) 155 return false; 156 157 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) ) 158 return false; 159 160 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) ) 161 return false; 162 163 // dispose used storages to free resources 164 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 165 return false; 166 167 // ================================================ 168 // now check all the written and copied information 169 // ================================================ 170 171 // the temporary file must not be locked any more after storage disposing 172 pArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE ); 173 Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 174 XStorage xResStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResStorage ); 175 if ( xResStorage == null ) 176 { 177 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 178 return false; 179 } 180 181 // open and check SubStorage1 182 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage, 183 "SubStorage1", 184 ElementModes.ELEMENT_READ ); 185 if ( xResSubStorage1 == null ) 186 { 187 m_aTestHelper.Error( "Can't open existing substorage!" ); 188 return false; 189 } 190 191 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.ELEMENT_READ ) ) 192 return false; 193 194 195 // open and check SubStorage2 196 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage, 197 "SubStorage2", 198 ElementModes.ELEMENT_READ ); 199 if ( xResSubStorage2 == null ) 200 { 201 m_aTestHelper.Error( "Can't open existing substorage!" ); 202 return false; 203 } 204 205 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.ELEMENT_READ ) ) 206 return false; 207 208 209 // check all the result streams 210 211 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1 ) ) 212 return false; 213 214 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1 ) ) 215 return false; 216 217 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1 ) ) 218 return false; 219 220 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2 ) ) 221 return false; 222 223 // the storage must be disposed before removing 224 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) ) 225 return false; 226 227 // remove element and check that it was removed completelly 228 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) ) 229 return false; 230 231 try 232 { 233 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage ); 234 if ( xResAccess.hasByName( "SubStorage2" ) ) 235 m_aTestHelper.Error( "SubStorage2 must be removed already!" ); 236 } 237 catch( Exception e ) 238 { 239 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e ); 240 return false; 241 } 242 243 try 244 { 245 xResSubStorage2.isStreamElement( "SubStream2" ); 246 247 m_aTestHelper.Error( "SubStorage2 must be disposed already!" ); 248 return false; 249 } 250 catch( com.sun.star.lang.DisposedException de ) 251 { 252 } 253 catch( Exception e ) 254 { 255 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e ); 256 return false; 257 } 258 259 // dispose used storages to free resources 260 if ( !m_aTestHelper.disposeStorage( xResStorage ) ) 261 return false; 262 263 return true; 264 } 265 catch( Exception e ) 266 { 267 m_aTestHelper.Error( "Exception: " + e ); 268 return false; 269 } 270 } 271 272 } 273 274