1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package storagetesting; 23 24 import com.sun.star.uno.XInterface; 25 import com.sun.star.lang.XMultiServiceFactory; 26 import com.sun.star.lang.XSingleServiceFactory; 27 import com.sun.star.lang.DisposedException; 28 29 import com.sun.star.bridge.XUnoUrlResolver; 30 import com.sun.star.uno.UnoRuntime; 31 import com.sun.star.uno.XInterface; 32 33 import com.sun.star.container.XNameAccess; 34 35 import com.sun.star.embed.*; 36 37 import storagetesting.TestHelper; 38 import storagetesting.StorageTest; 39 40 public class Test04 implements StorageTest { 41 42 XMultiServiceFactory m_xMSF; 43 XSingleServiceFactory m_xStorageFactory; 44 TestHelper m_aTestHelper; 45 Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )46 public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory ) 47 { 48 m_xMSF = xMSF; 49 m_xStorageFactory = xStorageFactory; 50 m_aTestHelper = new TestHelper( "Test04: " ); 51 } 52 test()53 public boolean test() 54 { 55 try 56 { 57 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 58 if ( sTempFileURL == null || sTempFileURL == "" ) 59 { 60 m_aTestHelper.Error( "No valid temporary file was created!" ); 61 return false; 62 } 63 64 // create temporary storage based on arbitrary medium 65 // after such a storage is closed it is lost 66 Object oTempStorage = m_xStorageFactory.createInstance(); 67 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 68 if ( xTempStorage == null ) 69 { 70 m_aTestHelper.Error( "Can't create temporary storage representation!" ); 71 return false; 72 } 73 74 // open substorages and create streams there 75 76 // first substorage of the root storage 77 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage, 78 "SubStorage1", 79 ElementModes.ELEMENT_WRITE ); 80 if ( xTempSubStorage1 == null ) 81 { 82 m_aTestHelper.Error( "Can't create substorage!" ); 83 return false; 84 } 85 86 byte pBytes1[] = { 1, 1, 1, 1, 1 }; 87 88 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 89 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) ) 90 return false; 91 92 // second substorage of the root storage 93 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage, 94 "SubStorage2", 95 ElementModes.ELEMENT_WRITE ); 96 if ( xTempSubStorage2 == null ) 97 { 98 m_aTestHelper.Error( "Can't create substorage!" ); 99 return false; 100 } 101 102 byte pBytes2[] = { 2, 2, 2, 2, 2 }; 103 104 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 105 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) ) 106 return false; 107 108 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 109 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 110 "MediaType3", 111 true, 112 ElementModes.ELEMENT_READWRITE ) ) 113 return false; 114 115 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 116 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1, 117 "MediaType4", 118 false, 119 ElementModes.ELEMENT_WRITE ) ) 120 return false; 121 122 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 123 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2, 124 "MediaType5", 125 false, 126 ElementModes.ELEMENT_WRITE ) ) 127 return false; 128 129 // create temporary storage based on a previously created temporary file 130 Object pArgs[] = new Object[2]; 131 pArgs[0] = (Object) sTempFileURL; 132 pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE ); 133 134 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 135 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 136 if ( xTempFileStorage == null ) 137 { 138 m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 139 return false; 140 } 141 142 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) ) 143 return false; 144 145 // if storage is not commited before disposing all the changes will be lost 146 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) ) 147 return false; 148 149 // a storage must be disposed before moving/removing otherwise the access will be denied 150 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) ) 151 return false; 152 153 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) ) 154 return false; 155 156 // SubStorage2 must be removed and disposed now 157 try 158 { 159 xTempSubStorage2.isStreamElement( "SubStream2" ); 160 m_aTestHelper.Error( "SubStorage2 must be disposed already!" ); 161 return false; 162 } 163 catch( com.sun.star.lang.DisposedException de ) 164 { 165 } 166 catch( Exception e ) 167 { 168 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e ); 169 return false; 170 } 171 172 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) ) 173 return false; 174 175 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) ) 176 return false; 177 178 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) ) 179 return false; 180 181 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) ) 182 return false; 183 184 // dispose used storages to free resources 185 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 186 return false; 187 188 // ================================================ 189 // now check all the written and copied information 190 // ================================================ 191 192 // the temporary file must not be locked any more after storage disposing 193 pArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE ); 194 Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 195 XStorage xResStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResStorage ); 196 if ( xResStorage == null ) 197 { 198 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 199 return false; 200 } 201 202 // open and check SubStorage1 203 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage, 204 "SubStorage1", 205 ElementModes.ELEMENT_READ ); 206 if ( xResSubStorage1 == null ) 207 { 208 m_aTestHelper.Error( "Can't open existing substorage!" ); 209 return false; 210 } 211 212 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.ELEMENT_READ ) ) 213 return false; 214 215 216 // open and check SubStorage2 217 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage, 218 "SubStorage2", 219 ElementModes.ELEMENT_READ ); 220 if ( xResSubStorage2 == null ) 221 { 222 m_aTestHelper.Error( "Can't open existing substorage!" ); 223 return false; 224 } 225 226 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.ELEMENT_READ ) ) 227 return false; 228 229 230 // check all the result streams 231 232 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1 ) ) 233 return false; 234 235 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1 ) ) 236 return false; 237 238 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1 ) ) 239 return false; 240 241 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2 ) ) 242 return false; 243 244 // the storage must be disposed before removing 245 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) ) 246 return false; 247 248 // remove element and check that it was removed completelly 249 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) ) 250 return false; 251 252 try 253 { 254 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage ); 255 if ( xResAccess.hasByName( "SubStorage2" ) ) 256 m_aTestHelper.Error( "SubStorage2 must be removed already!" ); 257 } 258 catch( Exception e ) 259 { 260 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e ); 261 return false; 262 } 263 264 try 265 { 266 xResSubStorage2.isStreamElement( "SubStream2" ); 267 268 m_aTestHelper.Error( "SubStorage2 must be disposed already!" ); 269 return false; 270 } 271 catch( com.sun.star.lang.DisposedException de ) 272 { 273 } 274 catch( Exception e ) 275 { 276 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e ); 277 return false; 278 } 279 280 // dispose used storages to free resources 281 if ( !m_aTestHelper.disposeStorage( xResStorage ) ) 282 return false; 283 284 return true; 285 } 286 catch( Exception e ) 287 { 288 m_aTestHelper.Error( "Exception: " + e ); 289 return false; 290 } 291 } 292 293 } 294 295