xref: /AOO41X/main/package/qa/storages/Test04.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 import com.sun.star.lang.DisposedException;
7*cdf0e10cSrcweir 
8*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
9*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
10*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
11*cdf0e10cSrcweir 
12*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
13*cdf0e10cSrcweir 
14*cdf0e10cSrcweir import com.sun.star.embed.*;
15*cdf0e10cSrcweir 
16*cdf0e10cSrcweir import share.LogWriter;
17*cdf0e10cSrcweir import complex.storages.TestHelper;
18*cdf0e10cSrcweir import complex.storages.StorageTest;
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir public class Test04 implements StorageTest {
21*cdf0e10cSrcweir 
22*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
23*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
24*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir 	public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
27*cdf0e10cSrcweir 	{
28*cdf0e10cSrcweir 		m_xMSF = xMSF;
29*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
30*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
31*cdf0e10cSrcweir 	}
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir     public boolean test()
34*cdf0e10cSrcweir 	{
35*cdf0e10cSrcweir 		try
36*cdf0e10cSrcweir 		{
37*cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
38*cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
39*cdf0e10cSrcweir 			{
40*cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
41*cdf0e10cSrcweir 				return false;
42*cdf0e10cSrcweir 			}
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
45*cdf0e10cSrcweir 			// after such a storage is closed it is lost
46*cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
47*cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
48*cdf0e10cSrcweir 			if ( xTempStorage == null )
49*cdf0e10cSrcweir 			{
50*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
51*cdf0e10cSrcweir 				return false;
52*cdf0e10cSrcweir 			}
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 			// open substorages and create streams there
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 			// first substorage of the root storage
57*cdf0e10cSrcweir 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
58*cdf0e10cSrcweir 																		"SubStorage1",
59*cdf0e10cSrcweir 																		ElementModes.WRITE );
60*cdf0e10cSrcweir 			if ( xTempSubStorage1 == null )
61*cdf0e10cSrcweir 			{
62*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
63*cdf0e10cSrcweir 				return false;
64*cdf0e10cSrcweir 			}
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
67*cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
68*cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
71*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
72*cdf0e10cSrcweir 				return false;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
77*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
78*cdf0e10cSrcweir 				return false;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 			// second substorage of the root storage
81*cdf0e10cSrcweir 			XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
82*cdf0e10cSrcweir 																		"SubStorage2",
83*cdf0e10cSrcweir 																		ElementModes.WRITE );
84*cdf0e10cSrcweir 			if ( xTempSubStorage2 == null )
85*cdf0e10cSrcweir 			{
86*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
87*cdf0e10cSrcweir 				return false;
88*cdf0e10cSrcweir 			}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
92*cdf0e10cSrcweir 				return false;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
97*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
98*cdf0e10cSrcweir 				return false;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
101*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
102*cdf0e10cSrcweir 															"MediaType3",
103*cdf0e10cSrcweir 															true,
104*cdf0e10cSrcweir 															ElementModes.WRITE ) )
105*cdf0e10cSrcweir 				return false;
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
108*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
109*cdf0e10cSrcweir 															"MediaType4",
110*cdf0e10cSrcweir 															false,
111*cdf0e10cSrcweir 															ElementModes.WRITE ) )
112*cdf0e10cSrcweir 				return false;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
115*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
116*cdf0e10cSrcweir 															"MediaType5",
117*cdf0e10cSrcweir 															false,
118*cdf0e10cSrcweir 															ElementModes.WRITE ) )
119*cdf0e10cSrcweir 				return false;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
122*cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
123*cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
124*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
127*cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
128*cdf0e10cSrcweir 			if ( xTempFileStorage == null )
129*cdf0e10cSrcweir 			{
130*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
131*cdf0e10cSrcweir 				return false;
132*cdf0e10cSrcweir 			}
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
135*cdf0e10cSrcweir 				return false;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 			// if storage is not commited before disposing all the changes will be lost
138*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
139*cdf0e10cSrcweir 				return false;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 			// a storage must be disposed before moving/removing otherwise the access will be denied
142*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
143*cdf0e10cSrcweir 				return false;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
146*cdf0e10cSrcweir 				return false;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 			// SubStorage2 must be removed and disposed now
149*cdf0e10cSrcweir 			try
150*cdf0e10cSrcweir 			{
151*cdf0e10cSrcweir 				xTempSubStorage2.isStreamElement( "SubStream2" );
152*cdf0e10cSrcweir 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
153*cdf0e10cSrcweir 				return false;
154*cdf0e10cSrcweir 			}
155*cdf0e10cSrcweir 			catch( com.sun.star.lang.DisposedException de )
156*cdf0e10cSrcweir 			{
157*cdf0e10cSrcweir 			}
158*cdf0e10cSrcweir 			catch( Exception e )
159*cdf0e10cSrcweir 			{
160*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
161*cdf0e10cSrcweir 				return false;
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
165*cdf0e10cSrcweir 				return false;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
168*cdf0e10cSrcweir 				return false;
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
171*cdf0e10cSrcweir 				return false;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
174*cdf0e10cSrcweir 				return false;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "BigSubStream1", "BigSubStream1_copy" ) )
177*cdf0e10cSrcweir 				return false;
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
180*cdf0e10cSrcweir 				return false;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
183*cdf0e10cSrcweir 				return false;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 			// dispose used storages to free resources
186*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
187*cdf0e10cSrcweir 				return false;
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 			// ================================================
190*cdf0e10cSrcweir 			// now check all the written and copied information
191*cdf0e10cSrcweir 			// ================================================
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
194*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
195*cdf0e10cSrcweir 			Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
196*cdf0e10cSrcweir 			XStorage xResStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResStorage );
197*cdf0e10cSrcweir 			if ( xResStorage == null )
198*cdf0e10cSrcweir 			{
199*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
200*cdf0e10cSrcweir 				return false;
201*cdf0e10cSrcweir 			}
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 			// open and check SubStorage1
204*cdf0e10cSrcweir 			XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
205*cdf0e10cSrcweir 																		"SubStorage1",
206*cdf0e10cSrcweir 																		ElementModes.READ );
207*cdf0e10cSrcweir 			if ( xResSubStorage1 == null )
208*cdf0e10cSrcweir 			{
209*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
210*cdf0e10cSrcweir 				return false;
211*cdf0e10cSrcweir 			}
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
214*cdf0e10cSrcweir 				return false;
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 			// open and check SubStorage2
218*cdf0e10cSrcweir 			XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
219*cdf0e10cSrcweir 																		"SubStorage2",
220*cdf0e10cSrcweir 																		ElementModes.READ );
221*cdf0e10cSrcweir 			if ( xResSubStorage2 == null )
222*cdf0e10cSrcweir 			{
223*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
224*cdf0e10cSrcweir 				return false;
225*cdf0e10cSrcweir 			}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
228*cdf0e10cSrcweir 				return false;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 			// check all the result streams
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
234*cdf0e10cSrcweir 				return false;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
237*cdf0e10cSrcweir 				return false;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", true, pBytes1 ) )
240*cdf0e10cSrcweir 				return false;
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1_copy", "MediaType1", true, pBigBytes ) )
243*cdf0e10cSrcweir 				return false;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
246*cdf0e10cSrcweir 				return false;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
249*cdf0e10cSrcweir 				return false;
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
252*cdf0e10cSrcweir 				return false;
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
255*cdf0e10cSrcweir 				return false;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 			// the storage must be disposed before removing
258*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
259*cdf0e10cSrcweir 				return false;
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 			// remove element and check that it was removed completelly
262*cdf0e10cSrcweir 			if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
263*cdf0e10cSrcweir 				return false;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 			try
266*cdf0e10cSrcweir 			{
267*cdf0e10cSrcweir 				XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
268*cdf0e10cSrcweir 				if ( xResAccess.hasByName( "SubStorage2" ) )
269*cdf0e10cSrcweir 					m_aTestHelper.Error( "SubStorage2 must be removed already!" );
270*cdf0e10cSrcweir 			}
271*cdf0e10cSrcweir 			catch( Exception e )
272*cdf0e10cSrcweir 			{
273*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
274*cdf0e10cSrcweir 				return false;
275*cdf0e10cSrcweir 			}
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 			try
278*cdf0e10cSrcweir 			{
279*cdf0e10cSrcweir 				xResSubStorage2.isStreamElement( "SubStream2" );
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
282*cdf0e10cSrcweir 				return false;
283*cdf0e10cSrcweir 			}
284*cdf0e10cSrcweir 			catch( com.sun.star.lang.DisposedException de )
285*cdf0e10cSrcweir 			{
286*cdf0e10cSrcweir 			}
287*cdf0e10cSrcweir 			catch( Exception e )
288*cdf0e10cSrcweir 			{
289*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
290*cdf0e10cSrcweir 				return false;
291*cdf0e10cSrcweir 			}
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 			// dispose used storages to free resources
294*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResStorage ) )
295*cdf0e10cSrcweir 				return false;
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 			return true;
298*cdf0e10cSrcweir 		}
299*cdf0e10cSrcweir 		catch( Exception e )
300*cdf0e10cSrcweir 		{
301*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
302*cdf0e10cSrcweir 			return false;
303*cdf0e10cSrcweir 		}
304*cdf0e10cSrcweir     }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir }
307*cdf0e10cSrcweir 
308