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
)
26 m_xStorageFactory
= xStorageFactory
;
27 m_aTestHelper
= new TestHelper( aLogWriter
, "Test08: " );
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!" );
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!" );
55 String sPass1
= "123";
56 String sPass2
= "321";
59 xTempStorageEncryption
.setEncryptionPassword( sPass1
);
63 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
67 // open a new substorage
68 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
71 if ( xTempSubStorage
== null )
73 m_aTestHelper
.Error( "Can't create substorage!" );
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 ) )
86 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
, true ) )
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 ) )
94 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
, 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
) )
105 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "BigSubStream3", "MediaType3", false, pBigBytes
, sPass2
) )
108 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
112 ElementModes
.WRITE
) )
115 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
116 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
119 ElementModes
.WRITE
) )
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!" );
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!" );
143 // copy xTempStorage to xTempFileStorage
144 // xTempFileStorage will be automatically commited
145 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
148 // dispose used storages to free resources
149 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
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!" );
166 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType4", true, ElementModes
.READ
) )
169 // open existing substorage
170 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
173 if ( xResultSubStorage
== null )
175 m_aTestHelper
.Error( "Can't open existing substorage!" );
179 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType5", false, ElementModes
.READ
) )
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!" );
193 xResultStorageEncryption
.setEncryptionPassword( sPass2
);
197 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
201 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
, sPass1
) )
203 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "BigSubStream1", "MediaType1", pBigBytes
, sPass1
) )
206 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
208 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
211 // the common root storage password should allow to open this stream
212 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream3", "MediaType3", true, pBytes3
) )
214 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "BigSubStream3", "MediaType3", true, pBigBytes
) )
217 // dispose used storages to free resources
218 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
225 m_aTestHelper
.Error( "Exception: " + e
);