merge the formfield patch from ooo-build
[ooovba.git] / package / qa / storages / Test08.java
blobecf28c4a86b491834610cf4eda093a319fbbe261
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 Test08 implements StorageTest {
19 XMultiServiceFactory m_xMSF;
20 XSingleServiceFactory m_xStorageFactory;
21 TestHelper m_aTestHelper;
23 public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
25 m_xMSF = xMSF;
26 m_xStorageFactory = xStorageFactory;
27 m_aTestHelper = new TestHelper( aLogWriter, "Test08: " );
30 public boolean test()
32 try
35 // create temporary storage based on arbitrary medium
36 // after such a storage is closed it is lost
37 Object oTempStorage = m_xStorageFactory.createInstance();
38 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
39 if ( xTempStorage == null )
41 m_aTestHelper.Error( "Can't create temporary storage representation!" );
42 return false;
45 // set the global password for the root storage
46 XEncryptionProtectedSource xTempStorageEncryption =
47 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempStorage );
49 if ( xTempStorageEncryption == null )
51 m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
52 return true;
55 String sPass1 = "123";
56 String sPass2 = "321";
58 try {
59 xTempStorageEncryption.setEncryptionPassword( sPass1 );
61 catch( Exception e )
63 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
64 return false;
67 // open a new substorage
68 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
69 "SubStorage1",
70 ElementModes.WRITE );
71 if ( xTempSubStorage == null )
73 m_aTestHelper.Error( "Can't create substorage!" );
74 return false;
77 byte pBigBytes[] = new byte[33000];
78 for ( int nInd = 0; nInd < 33000; nInd++ )
79 pBigBytes[nInd] = (byte)( nInd % 128 );
81 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82 // the stream will be encrypted with common password
83 byte pBytes1[] = { 1, 1, 1, 1, 1 };
84 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1, true ) )
85 return false;
86 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes, true ) )
87 return false;
89 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
90 // the stream will not be encrypted
91 byte pBytes2[] = { 2, 2, 2, 2, 2 };
92 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2, false ) )
93 return false;
94 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes, false ) )
95 return false;
97 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 // the stream will be compressed with own password
99 byte pBytes3[] = { 3, 3, 3, 3, 3 };
101 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102 // the stream will not be encrypted
103 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream3", "MediaType3", false, pBytes3, sPass2 ) )
104 return false;
105 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "BigSubStream3", "MediaType3", false, pBigBytes, sPass2 ) )
106 return false;
108 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
110 "MediaType4",
111 true,
112 ElementModes.WRITE ) )
113 return false;
115 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
116 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
117 "MediaType5",
118 false,
119 ElementModes.WRITE ) )
120 return false;
122 // create temporary file
123 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
124 if ( sTempFileURL == null || sTempFileURL == "" )
126 m_aTestHelper.Error( "No valid temporary file was created!" );
127 return false;
130 // create temporary storage based on a previously created temporary file
131 Object pArgs[] = new Object[2];
132 pArgs[0] = (Object) sTempFileURL;
133 pArgs[1] = new Integer( ElementModes.WRITE );
135 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
136 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
137 if ( xTempFileStorage == null )
139 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
140 return false;
143 // copy xTempStorage to xTempFileStorage
144 // xTempFileStorage will be automatically commited
145 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
146 return false;
148 // dispose used storages to free resources
149 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
150 return false;
152 // ================================================
153 // now check all the written and copied information
154 // ================================================
156 // the temporary file must not be locked any more after storage disposing
157 pArgs[1] = new Integer( ElementModes.READ );
158 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
159 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
160 if ( xResultStorage == null )
162 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
163 return false;
166 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) )
167 return false;
169 // open existing substorage
170 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
171 "SubStorage1",
172 ElementModes.READ );
173 if ( xResultSubStorage == null )
175 m_aTestHelper.Error( "Can't open existing substorage!" );
176 return false;
179 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) )
180 return false;
182 // set the global password for the root storage
183 XEncryptionProtectedSource xResultStorageEncryption =
184 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
186 if ( xResultStorageEncryption == null )
188 m_aTestHelper.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
189 return false;
192 try {
193 xResultStorageEncryption.setEncryptionPassword( sPass2 );
195 catch( Exception e )
197 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
198 return false;
201 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
202 return false;
203 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
204 return false;
206 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
207 return false;
208 if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
209 return false;
211 // the common root storage password should allow to open this stream
212 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", true, pBytes3 ) )
213 return false;
214 if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream3", "MediaType3", true, pBigBytes ) )
215 return false;
217 // dispose used storages to free resources
218 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
219 return false;
221 return true;
223 catch( Exception e )
225 m_aTestHelper.Error( "Exception: " + e );
226 return false;