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 complex.storages; 23 24 import com.sun.star.uno.XInterface; 25 import com.sun.star.lang.XMultiServiceFactory; 26 import com.sun.star.lang.XSingleServiceFactory; 27 28 import com.sun.star.bridge.XUnoUrlResolver; 29 import com.sun.star.uno.UnoRuntime; 30 import com.sun.star.uno.XInterface; 31 32 import com.sun.star.container.XNameAccess; 33 import com.sun.star.io.XStream; 34 35 import com.sun.star.embed.*; 36 37 import share.LogWriter; 38 import complex.storages.TestHelper; 39 import complex.storages.StorageTest; 40 41 public class Test11 implements StorageTest { 42 43 XMultiServiceFactory m_xMSF; 44 XSingleServiceFactory m_xStorageFactory; 45 TestHelper m_aTestHelper; 46 Test11( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47 public Test11( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 48 { 49 m_xMSF = xMSF; 50 m_xStorageFactory = xStorageFactory; 51 m_aTestHelper = new TestHelper( aLogWriter, "Test11: " ); 52 } 53 test()54 public boolean test() 55 { 56 try 57 { 58 // create temporary storage based on arbitrary medium 59 // after such a storage is closed it is lost 60 Object oTempStorage = m_xStorageFactory.createInstance(); 61 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 62 if ( xTempStorage == null ) 63 { 64 m_aTestHelper.Error( "Can't create temporary storage representation!" ); 65 return false; 66 } 67 68 byte pBigBytes[] = new byte[33000]; 69 for ( int nInd = 0; nInd < 33000; nInd++ ) 70 pBigBytes[nInd] = (byte)( nInd % 128 ); 71 72 String sPass1 = "111111111"; 73 byte pBytes1[] = { 1, 1, 1, 1, 1 }; 74 75 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 76 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, sPass1 ) ) 77 return false; 78 79 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 80 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes, sPass1 ) ) 81 return false; 82 83 // open a new substorage 84 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 85 "SubStorage1", 86 ElementModes.WRITE ); 87 if ( xTempSubStorage == null ) 88 { 89 m_aTestHelper.Error( "Can't create substorage!" ); 90 return false; 91 } 92 93 String sPass2 = "2222222222"; 94 byte pBytes2[] = { 2, 2, 2, 2, 2 }; 95 96 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 97 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream2", "MediaType2", true, pBytes2, sPass2 ) ) 98 return false; 99 100 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 101 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", true, pBigBytes, sPass2 ) ) 102 return false; 103 104 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 105 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 106 "MediaType3", 107 true, 108 ElementModes.WRITE ) ) 109 return false; 110 111 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 112 "MediaType4", 113 false, 114 ElementModes.WRITE ) ) 115 return false; 116 117 // ============================== 118 // check cloning at current state 119 // ============================== 120 121 // the new storage still was not commited so the clone must be empty 122 XStorage xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" ); 123 124 if ( xClonedSubStorage == null ) 125 { 126 m_aTestHelper.Error( "The result of clone is empty!" ); 127 return false; 128 } 129 130 XNameAccess xClonedNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xClonedSubStorage ); 131 if ( xClonedNameAccess == null ) 132 { 133 m_aTestHelper.Error( "XNameAccess is not implemented by the clone!" ); 134 return false; 135 } 136 137 if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "", true, ElementModes.WRITE ) ) 138 return false; 139 140 if ( xClonedNameAccess.hasElements() ) 141 { 142 m_aTestHelper.Error( "The new substorage still was not commited so it must be empty!" ); 143 return false; 144 } 145 146 if ( !m_aTestHelper.disposeStorage( xClonedSubStorage ) ) 147 return false; 148 149 xClonedSubStorage = null; 150 xClonedNameAccess = null; 151 152 // the new stream was opened, written and closed, that means flashed 153 // so the clone must contain all the information 154 XStream xClonedSubStream = m_aTestHelper.cloneEncrSubStream( xTempStorage, "SubStream1", sPass1 ); 155 if ( !m_aTestHelper.InternalCheckStream( xClonedSubStream, "SubStream1", "MediaType1", true, pBytes1, true ) ) 156 return false; 157 158 XStream xClonedBigSubStream = m_aTestHelper.cloneEncrSubStream( xTempStorage, "BigSubStream1", sPass1 ); 159 if ( !m_aTestHelper.InternalCheckStream( xClonedBigSubStream, "BigSubStream1", "MediaType1", true, pBigBytes, true ) ) 160 return false; 161 162 if ( !m_aTestHelper.disposeStream( xClonedSubStream, "SubStream1" ) ) 163 return false; 164 165 if ( !m_aTestHelper.disposeStream( xClonedBigSubStream, "BigSubStream1" ) ) 166 return false; 167 168 // ============================== 169 // commit substorage and check cloning 170 // ============================== 171 172 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) ) 173 return false; 174 175 xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" ); 176 if ( xClonedSubStorage == null ) 177 { 178 m_aTestHelper.Error( "The result of clone is empty!" ); 179 return false; 180 } 181 182 if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "MediaType4", true, ElementModes.WRITE ) ) 183 return false; 184 185 if ( !m_aTestHelper.checkEncrStream( xClonedSubStorage, "SubStream2", "MediaType2", pBytes2, sPass2 ) ) 186 return false; 187 188 if ( !m_aTestHelper.checkEncrStream( xClonedSubStorage, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) ) 189 return false; 190 191 // ============================== 192 // commit the root storage and check cloning 193 // ============================== 194 195 if ( !m_aTestHelper.commitStorage( xTempStorage ) ) 196 return false; 197 198 XStorage xCloneOfRoot = m_aTestHelper.cloneStorage( m_xStorageFactory, xTempStorage ); 199 if ( xCloneOfRoot == null ) 200 { 201 m_aTestHelper.Error( "The result of root clone is empty!" ); 202 return false; 203 } 204 205 if ( !m_aTestHelper.checkStorageProperties( xCloneOfRoot, "MediaType3", true, ElementModes.WRITE ) ) 206 return false; 207 208 if ( !m_aTestHelper.checkEncrStream( xCloneOfRoot, "SubStream1", "MediaType1", pBytes1, sPass1 ) ) 209 return false; 210 211 if ( !m_aTestHelper.checkEncrStream( xCloneOfRoot, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) ) 212 return false; 213 214 XStorage xSubStorageOfClone = xCloneOfRoot.openStorageElement( "SubStorage1", ElementModes.READ ); 215 if ( xSubStorageOfClone == null ) 216 { 217 m_aTestHelper.Error( "The result of root clone is wrong!" ); 218 return false; 219 } 220 221 if ( !m_aTestHelper.checkStorageProperties( xSubStorageOfClone, "MediaType4", false, ElementModes.READ ) ) 222 return false; 223 224 if ( !m_aTestHelper.checkEncrStream( xSubStorageOfClone, "SubStream2", "MediaType2", pBytes2, sPass2 ) ) 225 return false; 226 227 if ( !m_aTestHelper.checkEncrStream( xSubStorageOfClone, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) ) 228 return false; 229 230 return true; 231 } 232 catch( Exception e ) 233 { 234 m_aTestHelper.Error( "Exception: " + e ); 235 return false; 236 } 237 } 238 } 239 240