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