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 import com.sun.star.io.XInputStream;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import com.sun.star.embed.*;
14*cdf0e10cSrcweir import com.sun.star.beans.StringPair;
15*cdf0e10cSrcweir 
16*cdf0e10cSrcweir import share.LogWriter;
17*cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper;
18*cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest;
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir public class Test08 implements StorageTest {
21*cdf0e10cSrcweir 
22*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
23*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
24*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir 	public Test08( 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, "Test08: " );
31*cdf0e10cSrcweir 	}
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir     public boolean test()
34*cdf0e10cSrcweir 	{
35*cdf0e10cSrcweir 		StringPair[][] aRelations1 =
36*cdf0e10cSrcweir 			{ { new StringPair( "Id", "Num1" ) },
37*cdf0e10cSrcweir 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
38*cdf0e10cSrcweir 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
39*cdf0e10cSrcweir 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
40*cdf0e10cSrcweir 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
41*cdf0e10cSrcweir 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
42*cdf0e10cSrcweir 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
43*cdf0e10cSrcweir 			};
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 		try
46*cdf0e10cSrcweir 		{
47*cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
48*cdf0e10cSrcweir 			if ( xTempFileStream == null )
49*cdf0e10cSrcweir 				return false;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 			// create storage based on the temporary stream
52*cdf0e10cSrcweir 			XStorage xTempStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
53*cdf0e10cSrcweir 																			xTempFileStream,
54*cdf0e10cSrcweir 																			ElementModes.WRITE );
55*cdf0e10cSrcweir 			if ( xTempStorage == null )
56*cdf0e10cSrcweir 			{
57*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
58*cdf0e10cSrcweir 				return false;
59*cdf0e10cSrcweir 			}
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 			// open a new substorage
62*cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
63*cdf0e10cSrcweir 																	"SubStorage1",
64*cdf0e10cSrcweir 																	ElementModes.WRITE );
65*cdf0e10cSrcweir 			if ( xTempSubStorage == null )
66*cdf0e10cSrcweir 			{
67*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
68*cdf0e10cSrcweir 				return false;
69*cdf0e10cSrcweir 			}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
74*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
75*cdf0e10cSrcweir 														"SubStream1",
76*cdf0e10cSrcweir 														"MediaType1",
77*cdf0e10cSrcweir 														true,
78*cdf0e10cSrcweir 														pBytes1,
79*cdf0e10cSrcweir 														aRelations1 ) )
80*cdf0e10cSrcweir 				return false;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
83*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
84*cdf0e10cSrcweir 															true,
85*cdf0e10cSrcweir 															ElementModes.WRITE,
86*cdf0e10cSrcweir 															aRelations1 ) )
87*cdf0e10cSrcweir 				return false;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
91*cdf0e10cSrcweir 															false,
92*cdf0e10cSrcweir 															ElementModes.WRITE,
93*cdf0e10cSrcweir 															aRelations1 ) )
94*cdf0e10cSrcweir 				return false;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 			// commit substorage first
97*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
98*cdf0e10cSrcweir 				return false;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
101*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
102*cdf0e10cSrcweir 				return false;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 			// dispose substorage
105*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
106*cdf0e10cSrcweir 				return false;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 			// ================================================
109*cdf0e10cSrcweir 			// check substorage
110*cdf0e10cSrcweir 			// ================================================
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 			if ( !checkSubStorages( xTempStorage, pBytes1, aRelations1 ) )
113*cdf0e10cSrcweir 				return false;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 			// dispose used storage to free resources
116*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
117*cdf0e10cSrcweir 				return false;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 			// ================================================
120*cdf0e10cSrcweir 			// now check all the written information with readwrite access
121*cdf0e10cSrcweir 			// ================================================
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 			XStorage xResWriteStorage = m_aTestHelper.createStorageFromStream( m_xStorageFactory,
124*cdf0e10cSrcweir 																			xTempFileStream,
125*cdf0e10cSrcweir 																			ElementModes.WRITE );
126*cdf0e10cSrcweir 			if ( xResWriteStorage == null )
127*cdf0e10cSrcweir 			{
128*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
129*cdf0e10cSrcweir 				return false;
130*cdf0e10cSrcweir 			}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResWriteStorage,
133*cdf0e10cSrcweir 														true,
134*cdf0e10cSrcweir 														ElementModes.WRITE,
135*cdf0e10cSrcweir 														aRelations1 ) )
136*cdf0e10cSrcweir 				return false;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 			if( !checkSubStorages( xResWriteStorage, pBytes1, aRelations1 ) )
139*cdf0e10cSrcweir 				return false;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 			// try to open for writing after opening for reading
142*cdf0e10cSrcweir 			XStorage xResWSubStorage = m_aTestHelper.openSubStorage( xResWriteStorage,
143*cdf0e10cSrcweir 																	"SubStorage1",
144*cdf0e10cSrcweir 																	ElementModes.WRITE );
145*cdf0e10cSrcweir 			if ( xResWSubStorage == null )
146*cdf0e10cSrcweir 			{
147*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open substorage for writing after it was opened for reading!" );
148*cdf0e10cSrcweir 				return false;
149*cdf0e10cSrcweir 			}
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResWSubStorage,
152*cdf0e10cSrcweir 														false,
153*cdf0e10cSrcweir 														ElementModes.WRITE,
154*cdf0e10cSrcweir 														aRelations1 ) )
155*cdf0e10cSrcweir 				return false;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResWSubStorage,
158*cdf0e10cSrcweir 											"SubStream1",
159*cdf0e10cSrcweir 											"MediaType1",
160*cdf0e10cSrcweir 											pBytes1,
161*cdf0e10cSrcweir 											aRelations1 ) )
162*cdf0e10cSrcweir 				return false;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			// dispose used storage to free resources
165*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResWriteStorage ) )
166*cdf0e10cSrcweir 				return false;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 			// ================================================
170*cdf0e10cSrcweir 			// now check all the written information with readonly access
171*cdf0e10cSrcweir 			// ================================================
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 			// close the output part of the temporary stream
174*cdf0e10cSrcweir 			// the output part must present since we already wrote to the stream
175*cdf0e10cSrcweir 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
176*cdf0e10cSrcweir 				return false;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
179*cdf0e10cSrcweir 			if ( xTempInStream == null )
180*cdf0e10cSrcweir 				return false;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 			// open input stream
183*cdf0e10cSrcweir 			// since no mode is provided the result storage must be opened readonly
184*cdf0e10cSrcweir 			XStorage xResultStorage = m_aTestHelper.createStorageFromInputStream( m_xStorageFactory,
185*cdf0e10cSrcweir 																					xTempInStream );
186*cdf0e10cSrcweir 			if ( xResultStorage == null )
187*cdf0e10cSrcweir 			{
188*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
189*cdf0e10cSrcweir 				return false;
190*cdf0e10cSrcweir 			}
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage,
193*cdf0e10cSrcweir 														true,
194*cdf0e10cSrcweir 														ElementModes.READ,
195*cdf0e10cSrcweir 														aRelations1 ) )
196*cdf0e10cSrcweir 				return false;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 			if( !checkSubStorages( xResultStorage, pBytes1, aRelations1 ) )
199*cdf0e10cSrcweir 				return false;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 			return true;
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 		catch( Exception e )
204*cdf0e10cSrcweir 		{
205*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
206*cdf0e10cSrcweir 			return false;
207*cdf0e10cSrcweir 		}
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	private boolean checkSubStorages( XStorage xStorage, byte[] pBytes1, StringPair[][] aRelations )
211*cdf0e10cSrcweir 	{
212*cdf0e10cSrcweir 		XStorage xReadSubStorage1 = m_aTestHelper.openSubStorage( xStorage,
213*cdf0e10cSrcweir 																"SubStorage1",
214*cdf0e10cSrcweir 																ElementModes.READ );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 		XStorage xReadSubStorage2 = m_aTestHelper.openSubStorage( xStorage,
217*cdf0e10cSrcweir 																"SubStorage1",
218*cdf0e10cSrcweir 																ElementModes.READ );
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 		if ( xReadSubStorage1 == null || xReadSubStorage2 == null )
221*cdf0e10cSrcweir 		{
222*cdf0e10cSrcweir 			m_aTestHelper.Error( "Can't open substorage for reading!" );
223*cdf0e10cSrcweir 			return false;
224*cdf0e10cSrcweir 		}
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage1,
227*cdf0e10cSrcweir 													false,
228*cdf0e10cSrcweir 													ElementModes.READ,
229*cdf0e10cSrcweir 													aRelations ) )
230*cdf0e10cSrcweir 			return false;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage2,
233*cdf0e10cSrcweir 													false,
234*cdf0e10cSrcweir 													ElementModes.READ,
235*cdf0e10cSrcweir 													aRelations ) )
236*cdf0e10cSrcweir 			return false;
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStream( xReadSubStorage1,
239*cdf0e10cSrcweir 											"SubStream1",
240*cdf0e10cSrcweir 											"MediaType1",
241*cdf0e10cSrcweir 											pBytes1,
242*cdf0e10cSrcweir 											aRelations ) )
243*cdf0e10cSrcweir 			return false;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 		if ( !m_aTestHelper.checkStream( xReadSubStorage2,
246*cdf0e10cSrcweir 										"SubStream1",
247*cdf0e10cSrcweir 										"MediaType1",
248*cdf0e10cSrcweir 										pBytes1,
249*cdf0e10cSrcweir 										aRelations ) )
250*cdf0e10cSrcweir 			return false;
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage1 ) )
253*cdf0e10cSrcweir 			return false;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage2 ) )
256*cdf0e10cSrcweir 			return false;
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 		return true;
259*cdf0e10cSrcweir 	}
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir 
262