1 package storagetesting
;
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 storagetesting
.TestHelper
;
14 import storagetesting
.StorageTest
;
16 public class Test08
implements StorageTest
{
18 XMultiServiceFactory m_xMSF
;
19 XSingleServiceFactory m_xStorageFactory
;
20 TestHelper m_aTestHelper
;
22 public Test08( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
25 m_xStorageFactory
= xStorageFactory
;
26 m_aTestHelper
= new TestHelper( "Test08: " );
34 // create temporary storage based on arbitrary medium
35 // after such a storage is closed it is lost
36 Object oTempStorage
= m_xStorageFactory
.createInstance();
37 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
38 if ( xTempStorage
== null )
40 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
44 // set the global password for the root storage
45 XEncryptionProtectedSource xTempStorageEncryption
=
46 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xTempStorage
);
48 if ( xTempStorageEncryption
== null )
50 m_aTestHelper
.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
54 byte pPass1
[] = { 1, 2, 3 };
55 byte pPass2
[] = { 3, 2, 1 };
58 xTempStorageEncryption
.setEncryptionKey( pPass1
);
62 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
66 // open a new substorage
67 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
69 ElementModes
.ELEMENT_WRITE
);
70 if ( xTempSubStorage
== null )
72 m_aTestHelper
.Error( "Can't create substorage!" );
76 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
77 // the stream will be encrypted with common password
78 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
79 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
, true ) )
82 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
83 // the stream will not be encrypted
84 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
85 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
, false ) )
88 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89 // the stream will be compressed with own password
90 byte pBytes3
[] = { 3, 3, 3, 3, 3 };
92 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
93 // the stream will not be encrypted
94 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "SubStream3", "MediaType3", false, pBytes3
, pPass2
) )
97 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
101 ElementModes
.ELEMENT_READWRITE
) )
104 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
108 ElementModes
.ELEMENT_WRITE
) )
111 // create temporary file
112 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
113 if ( sTempFileURL
== null || sTempFileURL
== "" )
115 m_aTestHelper
.Error( "No valid temporary file was created!" );
119 // create temporary storage based on a previously created temporary file
120 Object pArgs
[] = new Object
[2];
121 pArgs
[0] = (Object
) sTempFileURL
;
122 pArgs
[1] = new Integer( ElementModes
.ELEMENT_WRITE
);
124 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
125 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
126 if ( xTempFileStorage
== null )
128 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
132 // copy xTempStorage to xTempFileStorage
133 // xTempFileStorage will be automatically commited
134 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
137 // dispose used storages to free resources
138 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
141 // ================================================
142 // now check all the written and copied information
143 // ================================================
145 // the temporary file must not be locked any more after storage disposing
146 pArgs
[1] = new Integer( ElementModes
.ELEMENT_READ
);
147 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
148 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
149 if ( xResultStorage
== null )
151 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
155 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType4", true, ElementModes
.ELEMENT_READ
) )
158 // open existing substorage
159 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
161 ElementModes
.ELEMENT_READ
);
162 if ( xResultSubStorage
== null )
164 m_aTestHelper
.Error( "Can't open existing substorage!" );
168 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType5", false, ElementModes
.ELEMENT_READ
) )
171 // set the global password for the root storage
172 XEncryptionProtectedSource xResultStorageEncryption
=
173 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xResultStorage
);
175 if ( xResultStorageEncryption
== null )
177 m_aTestHelper
.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
182 xResultStorageEncryption
.setEncryptionKey( pPass2
);
186 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
190 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
, pPass1
) )
193 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
196 // the common root storage password should allow to open this stream
197 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream3", "MediaType3", pBytes3
) )
200 // dispose used storages to free resources
201 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
208 m_aTestHelper
.Error( "Exception: " + e
);