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
)
26 m_xStorageFactory
= xStorageFactory
;
27 m_aTestHelper
= new TestHelper( aLogWriter
, "Test15: " );
32 String aStreamPrefix
= "";
33 for ( int nInd
= 0; nInd
< 4; ++nInd
, aStreamPrefix
+= "SubStorage" + nInd
)
34 if ( !testForPath( aStreamPrefix
) )
40 public boolean testForPath( String aStreamPrefix
)
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!" );
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!" );
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!" );
79 String sPass1
= "12345";
80 String sPass2
= "54321";
83 xTempStorageEncryption
.setEncryptionPassword( sPass1
);
87 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
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
97 if ( !m_aTestHelper
.WBToSubstrOfEncrH( xTempFileStorage
, aSubStream1Path
, "MediaType1", true, pBytes1
, true, true ) )
100 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
102 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", false, pBytes2
, sPass2
, true ) )
105 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
107 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType3", false, pBytes2
, sPass2
, true ) )
110 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
112 if ( !m_aTestHelper
.WBToSubstrOfEncrH( xTempFileStorage
, aSubStream4Path
, "MediaType2", true, pBytes1
, true, false ) )
116 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
117 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
120 ElementModes
.WRITE
) )
123 // commit the root storage so the contents must be stored now
124 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
127 // dispose used storages to free resources
128 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
131 // ================================================
132 // now reopen the storage,
133 // check all the written and copied information
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!" );
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!" );
157 xTempStorageEncryption
.setEncryptionPassword( sPass2
);
161 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
166 if ( !m_aTestHelper
.checkStorageProperties( xTempFileStorage
, "MediaType3", true, ElementModes
.WRITE
) )
169 if ( !m_aTestHelper
.checkEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType1", pBytes1
, sPass1
) )
172 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", true, pBytes2
) )
175 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType3", true, pBytes2
) )
178 if ( !m_aTestHelper
.cantOpenEncrStreamH( xTempFileStorage
, aSubStream4Path
, ElementModes
.READ
, sPass1
) )
181 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
183 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType4", true, pBytes2
, sPass1
, true ) )
186 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
188 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType5", true, pBytes1
, true ) )
191 // change the password of the existing stream
192 if ( m_aTestHelper
.ChangeStreamPassH( xTempFileStorage
, aSubStream2Path
, sPass2
, sPass1
, true ) != 1 )
195 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
197 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType5", true, pBytes1
, false ) )
200 // commit the root storage so the contents must be stored now
201 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
204 // dispose used storages to free resources
205 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
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!" );
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!" );
234 xTempStorageEncryption
.setEncryptionPassword( sPass1
);
238 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
242 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
245 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aSubStream1Path
, "MediaType4", true, pBytes2
) )
248 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aSubStream2Path
, "MediaType5", true, pBytes1
) )
251 if ( !m_aTestHelper
.checkEncrStreamH( xResultStorage
, aSubStream3Path
, "MediaType3", pBytes2
, sPass2
) )
254 // dispose used storages to free resources
255 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
262 m_aTestHelper
.Error( "Exception: " + e
);