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