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