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 import com.sun.star.io.XStream;
11*cdf0e10cSrcweir import com.sun.star.io.XInputStream;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import com.sun.star.embed.*;
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir import share.LogWriter;
16*cdf0e10cSrcweir import complex.storages.TestHelper;
17*cdf0e10cSrcweir import complex.storages.StorageTest;
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir public class RegressionTest_i59886 implements StorageTest {
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
22*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
23*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
24*cdf0e10cSrcweir 
25*cdf0e10cSrcweir 	public RegressionTest_i59886( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
26*cdf0e10cSrcweir 	{
27*cdf0e10cSrcweir 		m_xMSF = xMSF;
28*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
29*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i59886: " );
30*cdf0e10cSrcweir 	}
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir     public boolean test()
33*cdf0e10cSrcweir 	{
34*cdf0e10cSrcweir 		try
35*cdf0e10cSrcweir 		{
36*cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
37*cdf0e10cSrcweir 			if ( xTempFileStream == null )
38*cdf0e10cSrcweir 				return false;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 			// create storage based on the temporary stream
41*cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
42*cdf0e10cSrcweir 			pArgs[0] = (Object) xTempFileStream;
43*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
46*cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
47*cdf0e10cSrcweir 			if ( xTempStorage == null )
48*cdf0e10cSrcweir 			{
49*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
50*cdf0e10cSrcweir 				return false;
51*cdf0e10cSrcweir 			}
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 			byte pBytes[] = new byte[36000];
54*cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 36000; nInd++ )
55*cdf0e10cSrcweir 				pBytes[nInd] = (byte)( nInd % 128 );
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 			String sPass = "12345";
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
60*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes, sPass ) )
61*cdf0e10cSrcweir 				return false;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 			// open a new substorage
64*cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
65*cdf0e10cSrcweir 																		"SubStorage1",
66*cdf0e10cSrcweir 																		ElementModes.WRITE );
67*cdf0e10cSrcweir 			if ( xTempSubStorage == null )
68*cdf0e10cSrcweir 			{
69*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
70*cdf0e10cSrcweir 				return false;
71*cdf0e10cSrcweir 			}
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
74*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes, sPass ) )
75*cdf0e10cSrcweir 				return false;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
78*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
79*cdf0e10cSrcweir 															"MediaType3",
80*cdf0e10cSrcweir 															true,
81*cdf0e10cSrcweir 															ElementModes.WRITE ) )
82*cdf0e10cSrcweir 				return false;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
85*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
86*cdf0e10cSrcweir 															"MediaType4",
87*cdf0e10cSrcweir 															false,
88*cdf0e10cSrcweir 															ElementModes.WRITE ) )
89*cdf0e10cSrcweir 				return false;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 			// commit substorage first
92*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
93*cdf0e10cSrcweir 				return false;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
96*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
97*cdf0e10cSrcweir 				return false;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			// dispose used storage to free resources
100*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
101*cdf0e10cSrcweir 				return false;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 			// ================================================
104*cdf0e10cSrcweir 			// now reopen the storage, set the common storage key
105*cdf0e10cSrcweir 			// and copy the storage
106*cdf0e10cSrcweir 			// ================================================
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 			Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
109*cdf0e10cSrcweir 			XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
110*cdf0e10cSrcweir 			if ( xStep2TempStorage == null )
111*cdf0e10cSrcweir 			{
112*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
113*cdf0e10cSrcweir 				return false;
114*cdf0e10cSrcweir 			}
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 			XStorage xStep2TempSubStorage = m_aTestHelper.openSubStorage( xStep2TempStorage,
118*cdf0e10cSrcweir 																		"SubStorage1",
119*cdf0e10cSrcweir 																		ElementModes.WRITE );
120*cdf0e10cSrcweir 			if ( xStep2TempSubStorage == null )
121*cdf0e10cSrcweir 			{
122*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
123*cdf0e10cSrcweir 				return false;
124*cdf0e10cSrcweir 			}
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 			// set the common storage password
127*cdf0e10cSrcweir 			XEncryptionProtectedSource xEncr = (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xStep2TempStorage );
128*cdf0e10cSrcweir 			if ( xEncr == null )
129*cdf0e10cSrcweir 			{
130*cdf0e10cSrcweir 				m_aTestHelper.Error( "The storage does not support encryption access!" );
131*cdf0e10cSrcweir 				return false;
132*cdf0e10cSrcweir 			}
133*cdf0e10cSrcweir 			try
134*cdf0e10cSrcweir 			{
135*cdf0e10cSrcweir 				xEncr.setEncryptionPassword( sPass );
136*cdf0e10cSrcweir 			}
137*cdf0e10cSrcweir 			catch( Exception e )
138*cdf0e10cSrcweir 			{
139*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can not set the common storage password!" );
140*cdf0e10cSrcweir 				return false;
141*cdf0e10cSrcweir 			}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 			// open the stream for writing and read them so that the cache is created, but do not change
144*cdf0e10cSrcweir 			byte pDummyData[][] = new byte[1][3];
145*cdf0e10cSrcweir 			XStream xTempStream1 = m_aTestHelper.OpenStream( xStep2TempStorage, "SubStream1", ElementModes.WRITE );
146*cdf0e10cSrcweir 			XStream xTempStream2 = m_aTestHelper.OpenStream( xStep2TempSubStorage, "SubStream2", ElementModes.WRITE );
147*cdf0e10cSrcweir 			if ( xTempStream1 == null || xTempStream2 == null )
148*cdf0e10cSrcweir 				return false;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 			XInputStream xTempInStream1 = xTempStream1.getInputStream();
151*cdf0e10cSrcweir 			XInputStream xTempInStream2 = xTempStream2.getInputStream();
152*cdf0e10cSrcweir 			if ( xTempInStream1 == null || xTempInStream2 == null )
153*cdf0e10cSrcweir 			{
154*cdf0e10cSrcweir 				m_aTestHelper.Error( "No input stream is available!" );
155*cdf0e10cSrcweir 				return false;
156*cdf0e10cSrcweir 			}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 			xTempInStream1.readBytes( pDummyData, 3 );
159*cdf0e10cSrcweir 			xTempInStream2.readBytes( pDummyData, 3 );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 			// create temporary storage, it will be checked later
163*cdf0e10cSrcweir 			Object oTargetStorage = m_xStorageFactory.createInstance();
164*cdf0e10cSrcweir 			XStorage xTargetStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTargetStorage );
165*cdf0e10cSrcweir 			if ( xTargetStorage == null )
166*cdf0e10cSrcweir 			{
167*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
168*cdf0e10cSrcweir 				return false;
169*cdf0e10cSrcweir 			}
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 			// copy the current storage to the target
172*cdf0e10cSrcweir 			try
173*cdf0e10cSrcweir 			{
174*cdf0e10cSrcweir 				xStep2TempStorage.copyToStorage( xTargetStorage );
175*cdf0e10cSrcweir 			}
176*cdf0e10cSrcweir 			catch( Exception e )
177*cdf0e10cSrcweir 			{
178*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can not copy the storage with common storage password!" );
179*cdf0e10cSrcweir 				return false;
180*cdf0e10cSrcweir 			}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 			// dispose used storage to free resources
183*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
184*cdf0e10cSrcweir 				return false;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 			// ================================================
187*cdf0e10cSrcweir 			// now check all the information in the copy
188*cdf0e10cSrcweir 			// ================================================
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xTargetStorage, "MediaType3", true, ElementModes.WRITE ) )
191*cdf0e10cSrcweir 				return false;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			// open existing substorage
194*cdf0e10cSrcweir 			XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
195*cdf0e10cSrcweir 																		"SubStorage1",
196*cdf0e10cSrcweir 																		ElementModes.WRITE );
197*cdf0e10cSrcweir 			if ( xTargetSubStorage == null )
198*cdf0e10cSrcweir 			{
199*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
200*cdf0e10cSrcweir 				return false;
201*cdf0e10cSrcweir 			}
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xTargetSubStorage, "MediaType4", false, ElementModes.WRITE ) )
204*cdf0e10cSrcweir 				return false;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 			// set the common storage password
207*cdf0e10cSrcweir 			XEncryptionProtectedSource xTargetEncr = (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTargetStorage );
208*cdf0e10cSrcweir 			if ( xTargetEncr == null )
209*cdf0e10cSrcweir 			{
210*cdf0e10cSrcweir 				m_aTestHelper.Error( "The storage does not support encryption access!" );
211*cdf0e10cSrcweir 				return false;
212*cdf0e10cSrcweir 			}
213*cdf0e10cSrcweir 			try
214*cdf0e10cSrcweir 			{
215*cdf0e10cSrcweir 				xTargetEncr.setEncryptionPassword( sPass );
216*cdf0e10cSrcweir 			}
217*cdf0e10cSrcweir 			catch( Exception e )
218*cdf0e10cSrcweir 			{
219*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can not set the common storage password!" );
220*cdf0e10cSrcweir 				return false;
221*cdf0e10cSrcweir 			}
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 			// check the streams
224*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xTargetStorage, "SubStream1", "MediaType1", true, pBytes ) )
225*cdf0e10cSrcweir 				return false;
226*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xTargetSubStorage, "SubStream2", "MediaType2", true, pBytes ) )
227*cdf0e10cSrcweir 				return false;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 			// dispose used storages to free resources
231*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTargetStorage ) )
232*cdf0e10cSrcweir 				return false;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 			return true;
235*cdf0e10cSrcweir 		}
236*cdf0e10cSrcweir 		catch( Exception e )
237*cdf0e10cSrcweir 		{
238*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
239*cdf0e10cSrcweir 			return false;
240*cdf0e10cSrcweir 		}
241*cdf0e10cSrcweir     }
242*cdf0e10cSrcweir }
243*cdf0e10cSrcweir 
244