xref: /aoo42x/main/package/qa/storages/Test09.java (revision cdf0e10c)
1*cdf0e10cSrcweir package complex.storages;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
4*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
5*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
8*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
9*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
10*cdf0e10cSrcweir 
11*cdf0e10cSrcweir import com.sun.star.embed.*;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import share.LogWriter;
14*cdf0e10cSrcweir import complex.storages.TestHelper;
15*cdf0e10cSrcweir import complex.storages.StorageTest;
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir public class Test09 implements StorageTest {
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
20*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
21*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
22*cdf0e10cSrcweir 
23*cdf0e10cSrcweir 	public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
24*cdf0e10cSrcweir 	{
25*cdf0e10cSrcweir 		m_xMSF = xMSF;
26*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
27*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test09: " );
28*cdf0e10cSrcweir 	}
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir     public boolean test()
31*cdf0e10cSrcweir 	{
32*cdf0e10cSrcweir 		try
33*cdf0e10cSrcweir 		{
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
36*cdf0e10cSrcweir 			// after such a storage is closed it is lost
37*cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
38*cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
39*cdf0e10cSrcweir 			if ( xTempStorage == null )
40*cdf0e10cSrcweir 			{
41*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
42*cdf0e10cSrcweir 				return false;
43*cdf0e10cSrcweir 			}
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 			String sPass1 = "123";
46*cdf0e10cSrcweir 			String sPass2 = "321";
47*cdf0e10cSrcweir 			byte pBytes[] = { 1, 1, 1, 1, 1 };
48*cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
49*cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
50*cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
53*cdf0e10cSrcweir 			// the stream will not be encrypted
54*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, sPass1 ) )
55*cdf0e10cSrcweir 				return false;
56*cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", false, pBigBytes, sPass1 ) )
57*cdf0e10cSrcweir 				return false;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 			// create temporary file
60*cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
61*cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
62*cdf0e10cSrcweir 			{
63*cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
64*cdf0e10cSrcweir 				return false;
65*cdf0e10cSrcweir 			}
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
68*cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
69*cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
70*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
73*cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
74*cdf0e10cSrcweir 			if ( xTempFileStorage == null )
75*cdf0e10cSrcweir 			{
76*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
77*cdf0e10cSrcweir 				return false;
78*cdf0e10cSrcweir 			}
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 			// copy xTempStorage to xTempFileStorage
81*cdf0e10cSrcweir 			// xTempFileStorage will be automatically commited
82*cdf0e10cSrcweir 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
83*cdf0e10cSrcweir 				return false;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 			// change password of the substream of new storage based on file
86*cdf0e10cSrcweir 			int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", sPass1, sPass2 );
87*cdf0e10cSrcweir 			if ( nResult == 0 )
88*cdf0e10cSrcweir 				return false; // test failed
89*cdf0e10cSrcweir 			else if ( nResult == -1 )
90*cdf0e10cSrcweir 				return true; // tested optional feature is not supported
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 			// change password of the substream of new storage based on file
93*cdf0e10cSrcweir 			nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "BigSubStream1", sPass1, sPass2 );
94*cdf0e10cSrcweir 			if ( nResult == 0 )
95*cdf0e10cSrcweir 				return false; // test failed
96*cdf0e10cSrcweir 			else if ( nResult == -1 )
97*cdf0e10cSrcweir 				return true; // tested optional feature is not supported
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
100*cdf0e10cSrcweir 				return false;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 			// dispose used storages to free resources
103*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
104*cdf0e10cSrcweir 				return false;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 			// ================================================
107*cdf0e10cSrcweir 			// now check all the written and copied information
108*cdf0e10cSrcweir 			// ================================================
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
111*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.READ );
112*cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
113*cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
114*cdf0e10cSrcweir 			if ( xResultStorage == null )
115*cdf0e10cSrcweir 			{
116*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
117*cdf0e10cSrcweir 				return false;
118*cdf0e10cSrcweir 			}
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, sPass2 ) )
121*cdf0e10cSrcweir 				return false;
122*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass2 ) )
123*cdf0e10cSrcweir 				return false;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 			// dispose used storages to free resources
126*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
127*cdf0e10cSrcweir 				return false;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 			return true;
130*cdf0e10cSrcweir 		}
131*cdf0e10cSrcweir 		catch( Exception e )
132*cdf0e10cSrcweir 		{
133*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
134*cdf0e10cSrcweir 			return false;
135*cdf0e10cSrcweir 		}
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139