Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test09.java
blobf4ae8ee879f3212e9a781f13304a1b81e5769c1b
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 Test09 implements StorageTest {
18 XMultiServiceFactory m_xMSF;
19 XSingleServiceFactory m_xStorageFactory;
20 TestHelper m_aTestHelper;
22 public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
24 m_xMSF = xMSF;
25 m_xStorageFactory = xStorageFactory;
26 m_aTestHelper = new TestHelper( "Test09: " );
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 byte pPass1[] = { 1, 2, 3 };
45 byte pPass2[] = { 3, 2, 1 };
46 byte pBytes[] = { 1, 1, 1, 1, 1 };
48 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
49 // the stream will not be encrypted
50 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, pPass1 ) )
51 return false;
53 // create temporary file
54 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
55 if ( sTempFileURL == null || sTempFileURL == "" )
57 m_aTestHelper.Error( "No valid temporary file was created!" );
58 return false;
61 // create temporary storage based on a previously created temporary file
62 Object pArgs[] = new Object[2];
63 pArgs[0] = (Object) sTempFileURL;
64 pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
66 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
68 if ( xTempFileStorage == null )
70 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
71 return false;
74 // copy xTempStorage to xTempFileStorage
75 // xTempFileStorage will be automatically commited
76 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
77 return false;
79 // change password of the substream of new storage based on file
80 int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", pPass1, pPass2 );
81 if ( nResult == 0 )
82 return false; // test failed
83 else if ( nResult == -1 )
84 return true; // tested optional feature is not supported
86 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
87 return false;
89 // dispose used storages to free resources
90 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
91 return false;
93 // ================================================
94 // now check all the written and copied information
95 // ================================================
97 // the temporary file must not be locked any more after storage disposing
98 pArgs[1] = new Integer( ElementModes.ELEMENT_READ );
99 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
100 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
101 if ( xResultStorage == null )
103 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
104 return false;
107 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, pPass2 ) )
108 return false;
110 // dispose used storages to free resources
111 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
112 return false;
114 return true;
116 catch( Exception e )
118 m_aTestHelper.Error( "Exception: " + e );
119 return false;