1*a740f2aaSAndrew Rist /************************************************************** 2*a740f2aaSAndrew Rist * 3*a740f2aaSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a740f2aaSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a740f2aaSAndrew Rist * distributed with this work for additional information 6*a740f2aaSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a740f2aaSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a740f2aaSAndrew Rist * "License"); you may not use this file except in compliance 9*a740f2aaSAndrew Rist * with the License. You may obtain a copy of the License at 10*a740f2aaSAndrew Rist * 11*a740f2aaSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*a740f2aaSAndrew Rist * 13*a740f2aaSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a740f2aaSAndrew Rist * software distributed under the License is distributed on an 15*a740f2aaSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a740f2aaSAndrew Rist * KIND, either express or implied. See the License for the 17*a740f2aaSAndrew Rist * specific language governing permissions and limitations 18*a740f2aaSAndrew Rist * under the License. 19*a740f2aaSAndrew Rist * 20*a740f2aaSAndrew Rist *************************************************************/ 21*a740f2aaSAndrew Rist 22cdf0e10cSrcweir package complex.storages; 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.uno.XInterface; 25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 30cdf0e10cSrcweir import com.sun.star.uno.XInterface; 31cdf0e10cSrcweir 32cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 33cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 34cdf0e10cSrcweir import com.sun.star.container.ElementExistException; 35cdf0e10cSrcweir 36cdf0e10cSrcweir import com.sun.star.embed.*; 37cdf0e10cSrcweir 38cdf0e10cSrcweir import share.LogWriter; 39cdf0e10cSrcweir import complex.storages.TestHelper; 40cdf0e10cSrcweir import complex.storages.StorageTest; 41cdf0e10cSrcweir 42cdf0e10cSrcweir public class Test06 implements StorageTest { 43cdf0e10cSrcweir 44cdf0e10cSrcweir XMultiServiceFactory m_xMSF; 45cdf0e10cSrcweir XSingleServiceFactory m_xStorageFactory; 46cdf0e10cSrcweir TestHelper m_aTestHelper; 47cdf0e10cSrcweir Test06( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )48cdf0e10cSrcweir public Test06( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 49cdf0e10cSrcweir { 50cdf0e10cSrcweir m_xMSF = xMSF; 51cdf0e10cSrcweir m_xStorageFactory = xStorageFactory; 52cdf0e10cSrcweir m_aTestHelper = new TestHelper( aLogWriter, "Test06: " ); 53cdf0e10cSrcweir } 54cdf0e10cSrcweir test()55cdf0e10cSrcweir public boolean test() 56cdf0e10cSrcweir { 57cdf0e10cSrcweir try 58cdf0e10cSrcweir { 59cdf0e10cSrcweir // create temporary storage based on arbitrary medium 60cdf0e10cSrcweir // after such a storage is closed it is lost 61cdf0e10cSrcweir Object oTempStorage = m_xStorageFactory.createInstance(); 62cdf0e10cSrcweir XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 63cdf0e10cSrcweir if ( xTempStorage == null ) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir m_aTestHelper.Error( "Can't create temporary storage representation!" ); 66cdf0e10cSrcweir return false; 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir try 70cdf0e10cSrcweir { 71cdf0e10cSrcweir xTempStorage.copyToStorage( null ); 72cdf0e10cSrcweir m_aTestHelper.Error( "The method must throw an exception because of illegal parameter!" ); 73cdf0e10cSrcweir return false; 74cdf0e10cSrcweir } 75cdf0e10cSrcweir catch( com.sun.star.lang.IllegalArgumentException iae ) 76cdf0e10cSrcweir {} 77cdf0e10cSrcweir catch( com.sun.star.uno.Exception ue ) 78cdf0e10cSrcweir {} 79cdf0e10cSrcweir catch( Exception e ) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion because of illegal parameter : " + e ); 82cdf0e10cSrcweir return false; 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir // open new substorages 86cdf0e10cSrcweir XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage, 87cdf0e10cSrcweir "SubStorage1", 88cdf0e10cSrcweir ElementModes.WRITE ); 89cdf0e10cSrcweir XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage, 90cdf0e10cSrcweir "SubStorage2", 91cdf0e10cSrcweir ElementModes.WRITE ); 92cdf0e10cSrcweir if ( xTempSubStorage1 == null || xTempSubStorage2 == null ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir m_aTestHelper.Error( "Can't create substorage!" ); 95cdf0e10cSrcweir return false; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir // in case stream is open for reading it must exist 99cdf0e10cSrcweir try 100cdf0e10cSrcweir { 101cdf0e10cSrcweir xTempSubStorage1.openStreamElement( "NonExistingStream", ElementModes.READ ); 102cdf0e10cSrcweir m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent stream for reading!" ); 103cdf0e10cSrcweir return false; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir catch( com.sun.star.uno.Exception ue ) 106cdf0e10cSrcweir {} 107cdf0e10cSrcweir catch( Exception e ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of try to open nonexistent stream for reading : " + e ); 110cdf0e10cSrcweir return false; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir // in case a storage is open for reading it must exist 114cdf0e10cSrcweir try 115cdf0e10cSrcweir { 116cdf0e10cSrcweir xTempSubStorage1.openStreamElement( "NonExistingStorage", ElementModes.READ ); 117cdf0e10cSrcweir m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent storage for reading!" ); 118cdf0e10cSrcweir return false; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir catch( com.sun.star.uno.Exception ue ) 121cdf0e10cSrcweir {} 122cdf0e10cSrcweir catch( Exception e ) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of try to open nonexistent storage for reading : " + e ); 125cdf0e10cSrcweir return false; 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir // in case of removing nonexistent element an exception must be thrown 129cdf0e10cSrcweir try 130cdf0e10cSrcweir { 131cdf0e10cSrcweir xTempSubStorage1.removeElement( "NonExistingElement" ); 132cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case of removing nonexistent element!" ); 133cdf0e10cSrcweir return false; 134cdf0e10cSrcweir } 135cdf0e10cSrcweir catch( com.sun.star.container.NoSuchElementException ne ) 136cdf0e10cSrcweir {} 137cdf0e10cSrcweir catch( Exception e ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of try to remove nonexistent element : " + e ); 140cdf0e10cSrcweir return false; 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir // in case of renaming of nonexistent element an exception must be thrown 144cdf0e10cSrcweir try 145cdf0e10cSrcweir { 146cdf0e10cSrcweir xTempSubStorage1.renameElement( "NonExistingElement", "NewName" ); 147cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case of renaming nonexistent element!" ); 148cdf0e10cSrcweir return false; 149cdf0e10cSrcweir } 150cdf0e10cSrcweir catch( com.sun.star.container.NoSuchElementException ne ) 151cdf0e10cSrcweir {} 152cdf0e10cSrcweir catch( Exception e ) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of try to rename nonexistent element : " + e ); 155cdf0e10cSrcweir return false; 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir // in case of renaming to a name of existent element an exception must be thrown 159cdf0e10cSrcweir try 160cdf0e10cSrcweir { 161cdf0e10cSrcweir xTempStorage.renameElement( "SubStorage1", "SubStorage2" ); 162cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case of renaming to the name of existent element!" ); 163cdf0e10cSrcweir return false; 164cdf0e10cSrcweir } 165cdf0e10cSrcweir catch( com.sun.star.container.ElementExistException ee ) 166cdf0e10cSrcweir {} 167cdf0e10cSrcweir catch( Exception e ) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of try to rename to the name of existent element : " + e ); 170cdf0e10cSrcweir return false; 171cdf0e10cSrcweir } 172cdf0e10cSrcweir 173cdf0e10cSrcweir // in case of copying target storage must be provided 174cdf0e10cSrcweir try 175cdf0e10cSrcweir { 176cdf0e10cSrcweir xTempStorage.copyElementTo( "SubStorage1", null, "SubStorage1" ); 177cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for copying!" ); 178cdf0e10cSrcweir return false; 179cdf0e10cSrcweir } 180cdf0e10cSrcweir catch( com.sun.star.lang.IllegalArgumentException iae ) 181cdf0e10cSrcweir {} 182cdf0e10cSrcweir catch( com.sun.star.uno.Exception ue ) 183cdf0e10cSrcweir {} 184cdf0e10cSrcweir catch( Exception e ) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case empty reference is provieded as target for copying : " + e ); 187cdf0e10cSrcweir return false; 188cdf0e10cSrcweir } 189cdf0e10cSrcweir 190cdf0e10cSrcweir // in case of moving target storage must be provided 191cdf0e10cSrcweir try 192cdf0e10cSrcweir { 193cdf0e10cSrcweir xTempStorage.moveElementTo( "SubStorage1", null, "SubStorage1" ); 194cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for moving!" ); 195cdf0e10cSrcweir return false; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir catch( com.sun.star.lang.IllegalArgumentException iae ) 198cdf0e10cSrcweir {} 199cdf0e10cSrcweir catch( com.sun.star.uno.Exception ue ) 200cdf0e10cSrcweir {} 201cdf0e10cSrcweir catch( Exception e ) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case empty reference is provieded as target for moving : " + e ); 204cdf0e10cSrcweir return false; 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir 208cdf0e10cSrcweir // prepare target for further testings 209cdf0e10cSrcweir 210cdf0e10cSrcweir // create new temporary storage based on arbitrary medium 211cdf0e10cSrcweir Object oTargetStorage = m_xStorageFactory.createInstance(); 212cdf0e10cSrcweir XStorage xTargetStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTargetStorage ); 213cdf0e10cSrcweir if ( xTargetStorage == null ) 214cdf0e10cSrcweir { 215cdf0e10cSrcweir m_aTestHelper.Error( "Can't create temporary storage representation!" ); 216cdf0e10cSrcweir return false; 217cdf0e10cSrcweir } 218cdf0e10cSrcweir 219cdf0e10cSrcweir // open a new substorage 220cdf0e10cSrcweir XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage, 221cdf0e10cSrcweir "SubStorage1", 222cdf0e10cSrcweir ElementModes.WRITE ); 223cdf0e10cSrcweir if ( xTargetSubStorage == null ) 224cdf0e10cSrcweir { 225cdf0e10cSrcweir m_aTestHelper.Error( "Can't create substorage!" ); 226cdf0e10cSrcweir return false; 227cdf0e10cSrcweir } 228cdf0e10cSrcweir 229cdf0e10cSrcweir // in case of copying of nonexistent element an exception must be thrown 230cdf0e10cSrcweir try 231cdf0e10cSrcweir { 232cdf0e10cSrcweir xTempStorage.copyElementTo( "Nonexistent element", xTargetStorage, "Target" ); 233cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case of copying of nonexisting element!" ); 234cdf0e10cSrcweir return false; 235cdf0e10cSrcweir } 236cdf0e10cSrcweir catch( com.sun.star.container.NoSuchElementException ne ) 237cdf0e10cSrcweir {} 238cdf0e10cSrcweir catch( Exception e ) 239cdf0e10cSrcweir { 240cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of copying of nonexistent element: " + e ); 241cdf0e10cSrcweir return false; 242cdf0e10cSrcweir } 243cdf0e10cSrcweir 244cdf0e10cSrcweir // in case of moving of nonexistent element an exception must be thrown 245cdf0e10cSrcweir try 246cdf0e10cSrcweir { 247cdf0e10cSrcweir xTempStorage.moveElementTo( "Nonexistent element", xTargetStorage, "Target" ); 248cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case of moving of nonexisting element!" ); 249cdf0e10cSrcweir return false; 250cdf0e10cSrcweir } 251cdf0e10cSrcweir catch( com.sun.star.container.NoSuchElementException ne ) 252cdf0e10cSrcweir {} 253cdf0e10cSrcweir catch( Exception e ) 254cdf0e10cSrcweir { 255cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case of moving of nonexistent element: " + e ); 256cdf0e10cSrcweir return false; 257cdf0e10cSrcweir } 258cdf0e10cSrcweir 259cdf0e10cSrcweir // in case target for copying already exists an exception must be thrown 260cdf0e10cSrcweir try 261cdf0e10cSrcweir { 262cdf0e10cSrcweir xTempStorage.copyElementTo( "SubStorage1", xTargetStorage, "SubStorage1" ); 263cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case target for copying already exists!" ); 264cdf0e10cSrcweir return false; 265cdf0e10cSrcweir } 266cdf0e10cSrcweir catch( com.sun.star.container.ElementExistException ee ) 267cdf0e10cSrcweir {} 268cdf0e10cSrcweir catch( Exception e ) 269cdf0e10cSrcweir { 270cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case target for copying already exists: " + e ); 271cdf0e10cSrcweir return false; 272cdf0e10cSrcweir } 273cdf0e10cSrcweir 274cdf0e10cSrcweir // in case target for moving already exists an exception must be thrown 275cdf0e10cSrcweir try 276cdf0e10cSrcweir { 277cdf0e10cSrcweir xTempStorage.moveElementTo( "SubStorage1", xTargetStorage, "SubStorage1" ); 278cdf0e10cSrcweir m_aTestHelper.Error( "An exception must be thrown in case target for moving already exists!" ); 279cdf0e10cSrcweir return false; 280cdf0e10cSrcweir } 281cdf0e10cSrcweir catch( com.sun.star.container.ElementExistException ee ) 282cdf0e10cSrcweir {} 283cdf0e10cSrcweir catch( Exception e ) 284cdf0e10cSrcweir { 285cdf0e10cSrcweir m_aTestHelper.Error( "Unexpected excepion in case target for moving already exists: " + e ); 286cdf0e10cSrcweir return false; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir 289cdf0e10cSrcweir 290cdf0e10cSrcweir return true; 291cdf0e10cSrcweir } 292cdf0e10cSrcweir catch( Exception e ) 293cdf0e10cSrcweir { 294cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 295cdf0e10cSrcweir return false; 296cdf0e10cSrcweir } 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 299cdf0e10cSrcweir } 300cdf0e10cSrcweir 301