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