merge the formfield patch from ooo-build
[ooovba.git] / package / qa / storages / Test15.java
blobca89cfa19e95bb862872d15549a8c638ad12f363
1 package complex.storages;
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
11 import com.sun.star.embed.*;
13 import share.LogWriter;
14 import complex.storages.TestHelper;
15 import complex.storages.StorageTest;
17 public class Test15 implements StorageTest {
19 XMultiServiceFactory m_xMSF;
20 XSingleServiceFactory m_xStorageFactory;
21 TestHelper m_aTestHelper;
23 public Test15( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
25 m_xMSF = xMSF;
26 m_xStorageFactory = xStorageFactory;
27 m_aTestHelper = new TestHelper( aLogWriter, "Test15: " );
30 public boolean test()
32 String aStreamPrefix = "";
33 for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
34 if ( !testForPath( aStreamPrefix ) )
35 return false;
37 return true;
40 public boolean testForPath( String aStreamPrefix )
42 try
44 String aSubStream1Path = aStreamPrefix + "SubStream1";
45 String aSubStream2Path = aStreamPrefix + "SubStream2";
46 String aSubStream3Path = aStreamPrefix + "SubStream3";
47 String aSubStream4Path = aStreamPrefix + "SubStream4";
49 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
50 if ( sTempFileURL == null || sTempFileURL == "" )
52 m_aTestHelper.Error( "No valid temporary file was created!" );
53 return false;
56 // create temporary storage based on a previously created temporary file
57 Object pArgs[] = new Object[2];
58 pArgs[0] = (Object) sTempFileURL;
59 pArgs[1] = new Integer( ElementModes.WRITE );
61 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
62 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
63 if ( xTempFileStorage == null )
65 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
66 return false;
69 // set the global password for the root storage
70 XEncryptionProtectedSource xTempStorageEncryption =
71 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage );
73 if ( xTempStorageEncryption == null )
75 m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
76 return true;
79 String sPass1 = "12345";
80 String sPass2 = "54321";
82 try {
83 xTempStorageEncryption.setEncryptionPassword( sPass1 );
85 catch( Exception e )
87 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
88 return false;
92 byte pBytes1[] = { 1, 1, 1, 1, 1 };
93 byte pBytes2[] = { 2, 2, 2, 2, 2 };
95 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
96 // and commit
97 if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, true, true ) )
98 return false;
100 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
101 // and commit
102 if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) )
103 return false;
105 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
106 // and commit
107 if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", false, pBytes2, sPass2, true ) )
108 return false;
110 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
111 // and dont commit
112 if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream4Path, "MediaType2", true, pBytes1, true, false ) )
113 return false;
116 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
117 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
118 "MediaType3",
119 true,
120 ElementModes.WRITE ) )
121 return false;
123 // commit the root storage so the contents must be stored now
124 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
125 return false;
127 // dispose used storages to free resources
128 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
129 return false;
131 // ================================================
132 // now reopen the storage,
133 // check all the written and copied information
134 // and change it
135 // ================================================
137 // the temporary file must not be locked any more after storage disposing
138 oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
139 xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
140 if ( xTempFileStorage == null )
142 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
143 return false;
146 // set the global password for the root storage
147 xTempStorageEncryption =
148 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage );
150 if ( xTempStorageEncryption == null )
152 m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
153 return false;
156 try {
157 xTempStorageEncryption.setEncryptionPassword( sPass2 );
159 catch( Exception e )
161 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
162 return false;
166 if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
167 return false;
169 if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) )
170 return false;
172 if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", true, pBytes2 ) )
173 return false;
175 if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", true, pBytes2 ) )
176 return false;
178 if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream4Path, ElementModes.READ, sPass1 ) )
179 return false;
181 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
182 // and commit
183 if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType4", true, pBytes2, sPass1, true ) )
184 return false;
186 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
187 // and don't commit
188 if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType5", true, pBytes1, true ) )
189 return false;
191 // change the password of the existing stream
192 if ( m_aTestHelper.ChangeStreamPassH( xTempFileStorage, aSubStream2Path, sPass2, sPass1, true ) != 1 )
193 return false;
195 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
196 // and don't commit
197 if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream3Path, "MediaType5", true, pBytes1, false ) )
198 return false;
200 // commit the root storage so the contents must be stored now
201 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
202 return false;
204 // dispose used storages to free resources
205 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
206 return false;
208 // ================================================
209 // now reopen the storage,
210 // check all the written information
211 // ================================================
213 // the temporary file must not be locked any more after storage disposing
214 pArgs[1] = new Integer( ElementModes.READ );
215 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
216 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
217 if ( xResultStorage == null )
219 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
220 return false;
223 // set the global password for the root storage
224 xTempStorageEncryption =
225 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
227 if ( xTempStorageEncryption == null )
229 m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
230 return false;
233 try {
234 xTempStorageEncryption.setEncryptionPassword( sPass1 );
236 catch( Exception e )
238 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
239 return false;
242 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
243 return false;
245 if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream1Path, "MediaType4", true, pBytes2 ) )
246 return false;
248 if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream2Path, "MediaType5", true, pBytes1 ) )
249 return false;
251 if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream3Path, "MediaType3", pBytes2, sPass2 ) )
252 return false;
254 // dispose used storages to free resources
255 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
256 return false;
258 return true;
260 catch( Exception e )
262 m_aTestHelper.Error( "Exception: " + e );
263 return false;