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.container.XNameAccess; 13 import com.sun.star.beans.StringPair; 14 15 import share.LogWriter; 16 import complex.ofopxmlstorages.TestHelper; 17 import complex.ofopxmlstorages.StorageTest; 18 19 public class Test03 implements StorageTest { 20 21 XMultiServiceFactory m_xMSF; 22 XSingleServiceFactory m_xStorageFactory; 23 TestHelper m_aTestHelper; 24 25 public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 26 { 27 m_xMSF = xMSF; 28 m_xStorageFactory = xStorageFactory; 29 m_aTestHelper = new TestHelper( aLogWriter, "Test03: " ); 30 } 31 32 public boolean test() 33 { 34 try 35 { 36 StringPair[][] aRelations = 37 { { new StringPair( "Id", "Num1" ) }, 38 { new StringPair( "Target", "TargetURLValue" ), new StringPair( "Id", "Num6" ) }, 39 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) }, 40 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }, 41 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }, 42 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }, 43 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) } 44 }; 45 46 // create temporary storage based on arbitrary medium 47 // after such a storage is closed it is lost 48 XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory ); 49 if ( xTempStorage == null ) 50 { 51 m_aTestHelper.Error( "Can't create temporary storage representation!" ); 52 return false; 53 } 54 55 // open a new substorage 56 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 57 "SubStorage1", 58 ElementModes.WRITE ); 59 if ( xTempSubStorage == 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( xTempStorage, 69 "SubStream1", 70 "MediaType1", 71 true, 72 pBytes1, 73 aRelations ) ) 74 return false; 75 76 byte pBytes2[] = { 2, 2, 2, 2, 2 }; 77 78 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 79 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, 80 "SubStream2", 81 "MediaType2", 82 false, 83 pBytes2, 84 aRelations ) ) 85 return false; 86 87 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly 88 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 89 false, 90 ElementModes.WRITE, 91 aRelations ) ) 92 return false; 93 94 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) ) 95 return false; 96 97 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) ) 98 return false; 99 100 // ================================================ 101 // check storage hyerarchy tree 102 // ================================================ 103 104 // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly 105 try { 106 xTempStorage.isStorageElement( "does not exist" ); 107 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" ); 108 return false; 109 } 110 catch( com.sun.star.container.NoSuchElementException ne ) 111 { 112 } 113 catch( Exception e ) 114 { 115 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e ); 116 return false; 117 } 118 119 try { 120 xTempStorage.isStreamElement( "does not exist" ); 121 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" ); 122 return false; 123 } 124 catch( com.sun.star.container.NoSuchElementException ne ) 125 { 126 } 127 catch( Exception e ) 128 { 129 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e ); 130 return false; 131 } 132 133 XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage ); 134 if ( xRootNameAccess == null ) 135 { 136 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" ); 137 return false; 138 } 139 140 try { 141 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) ) 142 { 143 m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" ); 144 return false; 145 } 146 147 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) ) 148 { 149 m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" ); 150 return false; 151 } 152 } 153 catch( Exception e ) 154 { 155 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e ); 156 return false; 157 } 158 159 160 // check that root storage contents are represented correctly 161 String sRootCont[] = xRootNameAccess.getElementNames(); 162 163 if ( sRootCont.length != 2 ) 164 { 165 m_aTestHelper.Error( "Root storage contains wrong amount of children!" ); 166 return false; 167 } 168 169 if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" ) 170 || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) ) 171 || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) ) 172 { 173 m_aTestHelper.Error( "Root storage contains wrong list of children!" ); 174 return false; 175 } 176 177 // get storage through XNameAccess 178 XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" ); 179 if ( xResultSubStorage == null ) 180 return false; 181 182 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, 183 false, 184 ElementModes.READ, 185 aRelations ) ) 186 return false; 187 188 XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage ); 189 if ( xChildAccess == null ) 190 { 191 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" ); 192 return false; 193 } 194 195 if ( !xChildAccess.hasByName( "SubStream2" ) 196 || !xResultSubStorage.isStreamElement( "SubStream2" ) 197 || xResultSubStorage.isStorageElement( "SubStream2" ) ) 198 { 199 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" ); 200 return false; 201 } 202 203 return true; 204 } 205 catch( Exception e ) 206 { 207 m_aTestHelper.Error( "Exception: " + e ); 208 return false; 209 } 210 } 211 212 public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName ) 213 { 214 try 215 { 216 Object oStorage = xAccess.getByName( sName ); 217 XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage ); 218 219 if ( xResult != null ) 220 return xResult; 221 else 222 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" ); 223 } 224 catch( Exception e ) 225 { 226 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e ); 227 } 228 229 return null; 230 } 231 232 } 233 234