1 package complex.ofopxmlstorages; 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 import com.sun.star.beans.StringPair; 13 14 import share.LogWriter; 15 import complex.ofopxmlstorages.TestHelper; 16 import complex.ofopxmlstorages.StorageTest; 17 18 public class Test01 implements StorageTest { 19 20 XMultiServiceFactory m_xMSF; 21 XSingleServiceFactory m_xStorageFactory; 22 TestHelper m_aTestHelper; 23 24 public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 25 { 26 m_xMSF = xMSF; 27 m_xStorageFactory = xStorageFactory; 28 m_aTestHelper = new TestHelper( aLogWriter, "Test01: " ); 29 } 30 31 public boolean test() 32 { 33 StringPair[][] aRelations1 = 34 { { new StringPair( "Id", "Num1" ) }, 35 { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) }, 36 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 37 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 38 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 39 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) }, 40 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) } 41 }; 42 43 StringPair[][] aRelations2 = 44 { { new StringPair( "Id", "Num1" ) }, 45 { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) }, 46 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 47 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) }, 48 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) }, 49 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }, 50 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) } 51 }; 52 53 try 54 { 55 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 56 if ( sTempFileURL == null || sTempFileURL == "" ) 57 { 58 m_aTestHelper.Error( "No valid temporary file was created!" ); 59 return false; 60 } 61 62 // create temporary storage based on arbitrary medium 63 // after such a storage is closed it is lost 64 XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory ); 65 if ( xTempStorage == null ) 66 { 67 m_aTestHelper.Error( "Can't create temporary storage representation!" ); 68 return false; 69 } 70 71 // open a new substorage 72 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 73 "SubStorage1", 74 ElementModes.WRITE ); 75 if ( xTempSubStorage == null ) 76 { 77 m_aTestHelper.Error( "Can't create substorage!" ); 78 return false; 79 } 80 81 byte pBytes1[] = { 1, 1, 1, 1, 1 }; 82 83 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 84 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 85 "SubStream1", 86 "MediaType1", 87 true, 88 pBytes1, 89 aRelations1 ) ) 90 return false; 91 92 byte pBytes2[] = { 2, 2, 2, 2, 2 }; 93 94 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 95 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 96 "SubStream2", 97 "MediaType2", 98 false, 99 pBytes2, 100 aRelations2 ) ) 101 return false; 102 103 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 104 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 105 true, 106 ElementModes.WRITE, 107 aRelations1 ) ) 108 return false; 109 110 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 111 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 112 false, 113 ElementModes.WRITE, 114 aRelations1 ) ) 115 return false; 116 117 // create temporary storage based on a previously created temporary file 118 XStorage xTempFileStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory, 119 sTempFileURL, 120 ElementModes.WRITE ); 121 if ( xTempFileStorage == null ) 122 { 123 m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 124 return false; 125 } 126 127 // copy xTempStorage to xTempFileStorage 128 // xTempFileStorage will be automatically commited 129 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) ) 130 return false; 131 132 // dispose used storages to free resources 133 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 134 return false; 135 136 // ================================================ 137 // now check all the written and copied information 138 // ================================================ 139 140 // the temporary file must not be locked any more after storage disposing 141 XStorage xResultStorage = m_aTestHelper.createStorageFromURL( m_xStorageFactory, 142 sTempFileURL, 143 ElementModes.WRITE ); 144 if ( xResultStorage == null ) 145 { 146 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 147 return false; 148 } 149 150 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, 151 true, 152 ElementModes.WRITE, 153 aRelations1 ) ) 154 return false; 155 156 // open existing substorage 157 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage, 158 "SubStorage1", 159 ElementModes.READ ); 160 if ( xResultSubStorage == null ) 161 { 162 m_aTestHelper.Error( "Can't open existing substorage!" ); 163 return false; 164 } 165 166 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, 167 false, 168 ElementModes.READ, 169 aRelations1 ) ) 170 return false; 171 172 if ( !m_aTestHelper.checkStream( xResultSubStorage, 173 "SubStream1", 174 "MediaType1", 175 pBytes1, 176 aRelations1 ) ) 177 return false; 178 179 if ( !m_aTestHelper.checkStream( xResultSubStorage, 180 "SubStream2", 181 "MediaType2", 182 pBytes2, 183 aRelations2 ) ) 184 return false; 185 186 // dispose used storages to free resources 187 if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 188 return false; 189 190 return true; 191 } 192 catch( Exception e ) 193 { 194 m_aTestHelper.Error( "Exception: " + e ); 195 return false; 196 } 197 } 198 199 } 200 201