Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test08.java
blob147f57262992f015514dd752a0de2bf3042e2b9a
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 )
24 m_xMSF = xMSF;
25 m_xStorageFactory = xStorageFactory;
26 m_aTestHelper = new TestHelper( "Test08: " );
29 public boolean test()
31 try
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!" );
41 return false;
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!" );
51 return true;
54 byte pPass1[] = { 1, 2, 3 };
55 byte pPass2[] = { 3, 2, 1 };
57 try {
58 xTempStorageEncryption.setEncryptionKey( pPass1 );
60 catch( Exception e )
62 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
63 return false;
66 // open a new substorage
67 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
68 "SubStorage1",
69 ElementModes.ELEMENT_WRITE );
70 if ( xTempSubStorage == null )
72 m_aTestHelper.Error( "Can't create substorage!" );
73 return false;
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 ) )
80 return false;
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 ) )
86 return 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 ) )
95 return false;
97 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
99 "MediaType4",
100 true,
101 ElementModes.ELEMENT_READWRITE ) )
102 return false;
104 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
106 "MediaType5",
107 false,
108 ElementModes.ELEMENT_WRITE ) )
109 return false;
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!" );
116 return false;
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!" );
129 return false;
132 // copy xTempStorage to xTempFileStorage
133 // xTempFileStorage will be automatically commited
134 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
135 return false;
137 // dispose used storages to free resources
138 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
139 return false;
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!" );
152 return false;
155 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.ELEMENT_READ ) )
156 return false;
158 // open existing substorage
159 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
160 "SubStorage1",
161 ElementModes.ELEMENT_READ );
162 if ( xResultSubStorage == null )
164 m_aTestHelper.Error( "Can't open existing substorage!" );
165 return false;
168 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.ELEMENT_READ ) )
169 return false;
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!" );
178 return false;
181 try {
182 xResultStorageEncryption.setEncryptionKey( pPass2 );
184 catch( Exception e )
186 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
187 return false;
190 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
191 return false;
193 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
194 return false;
196 // the common root storage password should allow to open this stream
197 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", pBytes3 ) )
198 return false;
200 // dispose used storages to free resources
201 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
202 return false;
204 return true;
206 catch( Exception e )
208 m_aTestHelper.Error( "Exception: " + e );
209 return false;