Update ooo320-m1
[ooovba.git] / package / qa / storages / Test09.java
blob8d867a4fecaa851b78ce0c2772695c4e099ea7ad
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 Test09 implements StorageTest {
19 XMultiServiceFactory m_xMSF;
20 XSingleServiceFactory m_xStorageFactory;
21 TestHelper m_aTestHelper;
23 public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
25 m_xMSF = xMSF;
26 m_xStorageFactory = xStorageFactory;
27 m_aTestHelper = new TestHelper( aLogWriter, "Test09: " );
30 public boolean test()
32 try
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!" );
42 return false;
45 String sPass1 = "123";
46 String sPass2 = "321";
47 byte pBytes[] = { 1, 1, 1, 1, 1 };
48 byte pBigBytes[] = new byte[33000];
49 for ( int nInd = 0; nInd < 33000; nInd++ )
50 pBigBytes[nInd] = (byte)( nInd % 128 );
52 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
53 // the stream will not be encrypted
54 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, sPass1 ) )
55 return false;
56 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", false, pBigBytes, sPass1 ) )
57 return false;
59 // create temporary file
60 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
61 if ( sTempFileURL == null || sTempFileURL == "" )
63 m_aTestHelper.Error( "No valid temporary file was created!" );
64 return false;
67 // create temporary storage based on a previously created temporary file
68 Object pArgs[] = new Object[2];
69 pArgs[0] = (Object) sTempFileURL;
70 pArgs[1] = new Integer( ElementModes.WRITE );
72 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
73 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
74 if ( xTempFileStorage == null )
76 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
77 return false;
80 // copy xTempStorage to xTempFileStorage
81 // xTempFileStorage will be automatically commited
82 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
83 return false;
85 // change password of the substream of new storage based on file
86 int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", sPass1, sPass2 );
87 if ( nResult == 0 )
88 return false; // test failed
89 else if ( nResult == -1 )
90 return true; // tested optional feature is not supported
92 // change password of the substream of new storage based on file
93 nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "BigSubStream1", sPass1, sPass2 );
94 if ( nResult == 0 )
95 return false; // test failed
96 else if ( nResult == -1 )
97 return true; // tested optional feature is not supported
99 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
100 return false;
102 // dispose used storages to free resources
103 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
104 return false;
106 // ================================================
107 // now check all the written and copied information
108 // ================================================
110 // the temporary file must not be locked any more after storage disposing
111 pArgs[1] = new Integer( ElementModes.READ );
112 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
113 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
114 if ( xResultStorage == null )
116 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
117 return false;
120 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, sPass2 ) )
121 return false;
122 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass2 ) )
123 return false;
125 // dispose used storages to free resources
126 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
127 return false;
129 return true;
131 catch( Exception e )
133 m_aTestHelper.Error( "Exception: " + e );
134 return false;