xref: /aoo42x/main/package/qa/storages/Test05.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 import com.sun.star.io.XStream;
11*cdf0e10cSrcweir 
12*cdf0e10cSrcweir import com.sun.star.embed.*;
13*cdf0e10cSrcweir 
14*cdf0e10cSrcweir import share.LogWriter;
15*cdf0e10cSrcweir import complex.storages.TestHelper;
16*cdf0e10cSrcweir import complex.storages.StorageTest;
17*cdf0e10cSrcweir 
18*cdf0e10cSrcweir public class Test05 implements StorageTest {
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
21*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
22*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
23*cdf0e10cSrcweir 
24*cdf0e10cSrcweir 	public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
25*cdf0e10cSrcweir 	{
26*cdf0e10cSrcweir 		m_xMSF = xMSF;
27*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
28*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test05: " );
29*cdf0e10cSrcweir 	}
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir     public boolean test()
32*cdf0e10cSrcweir 	{
33*cdf0e10cSrcweir 		try
34*cdf0e10cSrcweir 		{
35*cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
36*cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
37*cdf0e10cSrcweir 			{
38*cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
39*cdf0e10cSrcweir 				return false;
40*cdf0e10cSrcweir 			}
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
43*cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
44*cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
45*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
48*cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
49*cdf0e10cSrcweir 			if ( xTempFileStorage == null )
50*cdf0e10cSrcweir 			{
51*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
52*cdf0e10cSrcweir 				return false;
53*cdf0e10cSrcweir 			}
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 			// open a new substorage
56*cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
57*cdf0e10cSrcweir 																		"SubStorage1",
58*cdf0e10cSrcweir 																		ElementModes.WRITE );
59*cdf0e10cSrcweir 			if ( xTempSubStorage == null )
60*cdf0e10cSrcweir 			{
61*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
62*cdf0e10cSrcweir 				return false;
63*cdf0e10cSrcweir 			}
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 			// open a new substorage
66*cdf0e10cSrcweir 			XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
67*cdf0e10cSrcweir 																		"SubSubStorage1",
68*cdf0e10cSrcweir 																		ElementModes.WRITE );
69*cdf0e10cSrcweir 			if ( xSubSubStorage == null )
70*cdf0e10cSrcweir 			{
71*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
72*cdf0e10cSrcweir 				return false;
73*cdf0e10cSrcweir 			}
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
76*cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
77*cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
80*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
81*cdf0e10cSrcweir 				return false;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
84*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
85*cdf0e10cSrcweir 				return false;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
90*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
91*cdf0e10cSrcweir 				return false;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
96*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
97*cdf0e10cSrcweir 				return false;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
100*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
101*cdf0e10cSrcweir 															"MediaType3",
102*cdf0e10cSrcweir 															true,
103*cdf0e10cSrcweir 															ElementModes.WRITE ) )
104*cdf0e10cSrcweir 				return false;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
107*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
108*cdf0e10cSrcweir 															"MediaType4",
109*cdf0e10cSrcweir 															false,
110*cdf0e10cSrcweir 															ElementModes.WRITE ) )
111*cdf0e10cSrcweir 				return false;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
114*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
115*cdf0e10cSrcweir 															"MediaType5",
116*cdf0e10cSrcweir 															false,
117*cdf0e10cSrcweir 															ElementModes.WRITE ) )
118*cdf0e10cSrcweir 				return false;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 			// commit all the storages
122*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
123*cdf0e10cSrcweir 				return false;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
126*cdf0e10cSrcweir 				return false;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
129*cdf0e10cSrcweir 				return false;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 			// try to open an opened substorage, open call must fail
132*cdf0e10cSrcweir 			if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
133*cdf0e10cSrcweir 				return false;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 			// reopen created streams
137*cdf0e10cSrcweir 			XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
138*cdf0e10cSrcweir 															"SubStream1",
139*cdf0e10cSrcweir 															ElementModes.WRITE | ElementModes.NOCREATE );
140*cdf0e10cSrcweir 			XStream xBigSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
141*cdf0e10cSrcweir 															   "BigSubStream1",
142*cdf0e10cSrcweir 															   ElementModes.WRITE | ElementModes.NOCREATE );
143*cdf0e10cSrcweir 			XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
144*cdf0e10cSrcweir 															"SubStream2",
145*cdf0e10cSrcweir 															ElementModes.READ | ElementModes.NOCREATE );
146*cdf0e10cSrcweir 			XStream xBigSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
147*cdf0e10cSrcweir 															   "BigSubStream2",
148*cdf0e10cSrcweir 															   ElementModes.READ | ElementModes.NOCREATE );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 			if ( xSubStream1 == null || xBigSubStream1 == null || xSubStream2 == null || xBigSubStream2 == null )
151*cdf0e10cSrcweir 				return false;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			// it should be possible to have more then one copy of stream for reading
154*cdf0e10cSrcweir 			XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
155*cdf0e10cSrcweir 																"SubStream2",
156*cdf0e10cSrcweir 																ElementModes.READ | ElementModes.NOCREATE );
157*cdf0e10cSrcweir 			XStream xBigSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
158*cdf0e10cSrcweir 																    "BigSubStream2",
159*cdf0e10cSrcweir 																    ElementModes.READ | ElementModes.NOCREATE );
160*cdf0e10cSrcweir 			if ( xSubStream2clone == null || xBigSubStream2clone == null )
161*cdf0e10cSrcweir 				return false;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			// so now the first streams can not be open neither for reading nor for writing
165*cdf0e10cSrcweir 			if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.WRITE )
166*cdf0e10cSrcweir 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.READ )
167*cdf0e10cSrcweir 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream1", ElementModes.WRITE )
168*cdf0e10cSrcweir 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream1", ElementModes.READ ) )
169*cdf0e10cSrcweir 				return false;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 			// the second streams can not be open for writing
172*cdf0e10cSrcweir 			if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE )
173*cdf0e10cSrcweir 			  || !m_aTestHelper.cantOpenStream( xSubSubStorage, "BigSubStream2", ElementModes.WRITE ) )
174*cdf0e10cSrcweir 				return false;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 			// dispose xTestSubStorage, all the subtree must be disposed
178*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
179*cdf0e10cSrcweir 				return false;
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 			// check that subtree was disposed correctly
182*cdf0e10cSrcweir 			try
183*cdf0e10cSrcweir 			{
184*cdf0e10cSrcweir 				xSubSubStorage.isStreamElement( "SubStream1" );
185*cdf0e10cSrcweir 				m_aTestHelper.Error( "Substorage was not disposed!" );
186*cdf0e10cSrcweir 				return false;
187*cdf0e10cSrcweir 			}
188*cdf0e10cSrcweir 			catch ( com.sun.star.lang.DisposedException de )
189*cdf0e10cSrcweir 			{}
190*cdf0e10cSrcweir 			catch ( Exception e )
191*cdf0e10cSrcweir 			{
192*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
193*cdf0e10cSrcweir 				return false;
194*cdf0e10cSrcweir 			}
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 			try
197*cdf0e10cSrcweir 			{
198*cdf0e10cSrcweir 				xSubStream1.getInputStream();
199*cdf0e10cSrcweir 				m_aTestHelper.Error( "Writeable substream was not disposed!" );
200*cdf0e10cSrcweir 				return false;
201*cdf0e10cSrcweir 			}
202*cdf0e10cSrcweir 			catch ( com.sun.star.lang.DisposedException de )
203*cdf0e10cSrcweir 			{}
204*cdf0e10cSrcweir 			catch ( Exception e )
205*cdf0e10cSrcweir 			{
206*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
207*cdf0e10cSrcweir 				return false;
208*cdf0e10cSrcweir 			}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 			try
211*cdf0e10cSrcweir 			{
212*cdf0e10cSrcweir 				xSubStream2.getInputStream();
213*cdf0e10cSrcweir 				m_aTestHelper.Error( "Readonly substream was not disposed!" );
214*cdf0e10cSrcweir 				return false;
215*cdf0e10cSrcweir 			}
216*cdf0e10cSrcweir 			catch ( com.sun.star.lang.DisposedException de )
217*cdf0e10cSrcweir 			{}
218*cdf0e10cSrcweir 			catch ( Exception e )
219*cdf0e10cSrcweir 			{
220*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
221*cdf0e10cSrcweir 				return false;
222*cdf0e10cSrcweir 			}
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 			// dispose root storage
226*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
227*cdf0e10cSrcweir 				return false;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 			// ================================================
231*cdf0e10cSrcweir 			// now check all the written and copied information
232*cdf0e10cSrcweir 			// ================================================
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.READ );
235*cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
236*cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
237*cdf0e10cSrcweir 			if ( xResultStorage == null )
238*cdf0e10cSrcweir 			{
239*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
240*cdf0e10cSrcweir 				return false;
241*cdf0e10cSrcweir 			}
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
244*cdf0e10cSrcweir 				return false;
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 			// open existing substorage
247*cdf0e10cSrcweir 			XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
248*cdf0e10cSrcweir 																	"SubStorage1",
249*cdf0e10cSrcweir 																	ElementModes.READ );
250*cdf0e10cSrcweir 			if ( xResSubStorage == null )
251*cdf0e10cSrcweir 			{
252*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
253*cdf0e10cSrcweir 				return false;
254*cdf0e10cSrcweir 			}
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.READ ) )
257*cdf0e10cSrcweir 				return false;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 			// open existing substorage
260*cdf0e10cSrcweir 			XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
261*cdf0e10cSrcweir 																		"SubSubStorage1",
262*cdf0e10cSrcweir 																		ElementModes.READ );
263*cdf0e10cSrcweir 			if ( xResSubSubStorage == null )
264*cdf0e10cSrcweir 			{
265*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
266*cdf0e10cSrcweir 				return false;
267*cdf0e10cSrcweir 			}
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.READ ) )
270*cdf0e10cSrcweir 				return false;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 			// check substreams
273*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
274*cdf0e10cSrcweir 				return false;
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
277*cdf0e10cSrcweir 				return false;
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
280*cdf0e10cSrcweir 				return false;
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
283*cdf0e10cSrcweir 				return false;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 			// dispose used storages to free resources
286*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
287*cdf0e10cSrcweir 				return false;
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 			return true;
290*cdf0e10cSrcweir 		}
291*cdf0e10cSrcweir 		catch( Exception e )
292*cdf0e10cSrcweir 		{
293*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
294*cdf0e10cSrcweir 			return false;
295*cdf0e10cSrcweir 		}
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300