Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test07.java
blob25e3fedf4df359914a2875198fd69b0e7527f9d6
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 Test07 implements StorageTest {
18 XMultiServiceFactory m_xMSF;
19 XSingleServiceFactory m_xStorageFactory;
20 TestHelper m_aTestHelper;
22 public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
24 m_xMSF = xMSF;
25 m_xStorageFactory = xStorageFactory;
26 m_aTestHelper = new TestHelper( "Test07: " );
29 public boolean test()
31 try
33 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
34 if ( sTempFileURL == null || sTempFileURL == "" )
36 m_aTestHelper.Error( "No valid temporary file was created!" );
37 return false;
40 // create temporary storage based on arbitrary medium
41 // after such a storage is closed it is lost
42 Object oTempStorage = m_xStorageFactory.createInstance();
43 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
44 if ( xTempStorage == null )
46 m_aTestHelper.Error( "Can't create temporary storage representation!" );
47 return false;
50 byte pBytes1[] = { 1, 1, 1, 1, 1 };
51 byte pPass1[] = { 1, 2, 3, 4, 5 };
53 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
54 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, pPass1 ) )
55 return false;
57 byte pBytes2[] = { 2, 2, 2, 2, 2 };
58 byte pPass2[] = { 5, 4, 3, 2, 1 };
60 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
61 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes2, pPass2 ) )
62 return false;
64 // create temporary storage based on a previously created temporary file
65 Object pArgs[] = new Object[2];
66 pArgs[0] = (Object) sTempFileURL;
67 pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
69 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
70 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
71 if ( xTempFileStorage == null )
73 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
74 return false;
77 // copy xTempStorage to xTempFileStorage
78 // xTempFileStorage will be automatically commited
79 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
80 return false;
82 // dispose used storages to free resources
83 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
84 return false;
86 // ================================================
87 // now check all the written and copied information
88 // ================================================
90 // the temporary file must not be locked any more after storage disposing
91 pArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
92 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
93 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
94 if ( xResultStorage == null )
96 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
97 return false;
100 Object o2CopyStorage = m_xStorageFactory.createInstance();
101 XStorage x2CopyStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, o2CopyStorage );
102 if ( x2CopyStorage == null )
104 m_aTestHelper.Error( "Can't create temporary storage representation!" );
105 return false;
108 if ( !m_aTestHelper.copyStorage( xResultStorage, x2CopyStorage ) )
109 return false;
111 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
112 return false;
114 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
115 return false;
117 if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
118 return false;
120 if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
121 return false;
123 // dispose used storages to free resources
124 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
125 return false;
127 return true;
129 catch( Exception e )
131 m_aTestHelper.Error( "Exception: " + e );
132 return false;