xref: /aoo42x/main/package/qa/storages/Test04.java (revision a893be29)
1a740f2aaSAndrew Rist /**************************************************************
2a740f2aaSAndrew Rist  *
3a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a740f2aaSAndrew Rist  * distributed with this work for additional information
6a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a740f2aaSAndrew Rist  *
11a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a740f2aaSAndrew Rist  *
13a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18a740f2aaSAndrew Rist  * under the License.
19a740f2aaSAndrew Rist  *
20a740f2aaSAndrew Rist  *************************************************************/
21a740f2aaSAndrew Rist 
22cdf0e10cSrcweir package complex.storages;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir import com.sun.star.lang.DisposedException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import com.sun.star.uno.XInterface;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.embed.*;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import share.LogWriter;
38cdf0e10cSrcweir import complex.storages.TestHelper;
39cdf0e10cSrcweir import complex.storages.StorageTest;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir public class Test04 implements StorageTest {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
44cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
45cdf0e10cSrcweir 	TestHelper m_aTestHelper;
46cdf0e10cSrcweir 
Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47cdf0e10cSrcweir 	public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 		m_xMSF = xMSF;
50cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
51cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
52cdf0e10cSrcweir 	}
53cdf0e10cSrcweir 
test()54cdf0e10cSrcweir     public boolean test()
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		try
57cdf0e10cSrcweir 		{
58cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
59cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
60cdf0e10cSrcweir 			{
61cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
62cdf0e10cSrcweir 				return false;
63cdf0e10cSrcweir 			}
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
66cdf0e10cSrcweir 			// after such a storage is closed it is lost
67cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
68cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
69cdf0e10cSrcweir 			if ( xTempStorage == null )
70cdf0e10cSrcweir 			{
71cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
72cdf0e10cSrcweir 				return false;
73cdf0e10cSrcweir 			}
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 			// open substorages and create streams there
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 			// first substorage of the root storage
78cdf0e10cSrcweir 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
79cdf0e10cSrcweir 																		"SubStorage1",
80cdf0e10cSrcweir 																		ElementModes.WRITE );
81cdf0e10cSrcweir 			if ( xTempSubStorage1 == null )
82cdf0e10cSrcweir 			{
83cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
84cdf0e10cSrcweir 				return false;
85cdf0e10cSrcweir 			}
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
88cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
89cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
92cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
93cdf0e10cSrcweir 				return false;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
99cdf0e10cSrcweir 				return false;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 			// second substorage of the root storage
102cdf0e10cSrcweir 			XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
103cdf0e10cSrcweir 																		"SubStorage2",
104cdf0e10cSrcweir 																		ElementModes.WRITE );
105cdf0e10cSrcweir 			if ( xTempSubStorage2 == null )
106cdf0e10cSrcweir 			{
107cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
108cdf0e10cSrcweir 				return false;
109cdf0e10cSrcweir 			}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
112cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
113cdf0e10cSrcweir 				return false;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
118cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
119cdf0e10cSrcweir 				return false;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
122cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
123cdf0e10cSrcweir 															"MediaType3",
124cdf0e10cSrcweir 															true,
125cdf0e10cSrcweir 															ElementModes.WRITE ) )
126cdf0e10cSrcweir 				return false;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
129cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
130cdf0e10cSrcweir 															"MediaType4",
131cdf0e10cSrcweir 															false,
132cdf0e10cSrcweir 															ElementModes.WRITE ) )
133cdf0e10cSrcweir 				return false;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
136cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
137cdf0e10cSrcweir 															"MediaType5",
138cdf0e10cSrcweir 															false,
139cdf0e10cSrcweir 															ElementModes.WRITE ) )
140cdf0e10cSrcweir 				return false;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
143cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
144cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
145cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
148cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
149cdf0e10cSrcweir 			if ( xTempFileStorage == null )
150cdf0e10cSrcweir 			{
151cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
152cdf0e10cSrcweir 				return false;
153cdf0e10cSrcweir 			}
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
156cdf0e10cSrcweir 				return false;
157cdf0e10cSrcweir 
158*a893be29SPedro Giffuni 			// if storage is not committed before disposing all the changes will be lost
159cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
160cdf0e10cSrcweir 				return false;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 			// a storage must be disposed before moving/removing otherwise the access will be denied
163cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
164cdf0e10cSrcweir 				return false;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 			if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
167cdf0e10cSrcweir 				return false;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 			// SubStorage2 must be removed and disposed now
170cdf0e10cSrcweir 			try
171cdf0e10cSrcweir 			{
172cdf0e10cSrcweir 				xTempSubStorage2.isStreamElement( "SubStream2" );
173cdf0e10cSrcweir 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
174cdf0e10cSrcweir 				return false;
175cdf0e10cSrcweir 			}
176cdf0e10cSrcweir 			catch( com.sun.star.lang.DisposedException de )
177cdf0e10cSrcweir 			{
178cdf0e10cSrcweir 			}
179cdf0e10cSrcweir 			catch( Exception e )
180cdf0e10cSrcweir 			{
181cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
182cdf0e10cSrcweir 				return false;
183cdf0e10cSrcweir 			}
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
186cdf0e10cSrcweir 				return false;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
189cdf0e10cSrcweir 				return false;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
192cdf0e10cSrcweir 				return false;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
195cdf0e10cSrcweir 				return false;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 			if ( !m_aTestHelper.renameElement( xTempFileStorage, "BigSubStream1", "BigSubStream1_copy" ) )
198cdf0e10cSrcweir 				return false;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 			if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
201cdf0e10cSrcweir 				return false;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
204cdf0e10cSrcweir 				return false;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 			// dispose used storages to free resources
207cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
208cdf0e10cSrcweir 				return false;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 			// ================================================
211cdf0e10cSrcweir 			// now check all the written and copied information
212cdf0e10cSrcweir 			// ================================================
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
215cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
216cdf0e10cSrcweir 			Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
217cdf0e10cSrcweir 			XStorage xResStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResStorage );
218cdf0e10cSrcweir 			if ( xResStorage == null )
219cdf0e10cSrcweir 			{
220cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
221cdf0e10cSrcweir 				return false;
222cdf0e10cSrcweir 			}
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 			// open and check SubStorage1
225cdf0e10cSrcweir 			XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
226cdf0e10cSrcweir 																		"SubStorage1",
227cdf0e10cSrcweir 																		ElementModes.READ );
228cdf0e10cSrcweir 			if ( xResSubStorage1 == null )
229cdf0e10cSrcweir 			{
230cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
231cdf0e10cSrcweir 				return false;
232cdf0e10cSrcweir 			}
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
235cdf0e10cSrcweir 				return false;
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 			// open and check SubStorage2
239cdf0e10cSrcweir 			XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
240cdf0e10cSrcweir 																		"SubStorage2",
241cdf0e10cSrcweir 																		ElementModes.READ );
242cdf0e10cSrcweir 			if ( xResSubStorage2 == null )
243cdf0e10cSrcweir 			{
244cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
245cdf0e10cSrcweir 				return false;
246cdf0e10cSrcweir 			}
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
249cdf0e10cSrcweir 				return false;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 			// check all the result streams
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
255cdf0e10cSrcweir 				return false;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
258cdf0e10cSrcweir 				return false;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", true, pBytes1 ) )
261cdf0e10cSrcweir 				return false;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1_copy", "MediaType1", true, pBigBytes ) )
264cdf0e10cSrcweir 				return false;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
267cdf0e10cSrcweir 				return false;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
270cdf0e10cSrcweir 				return false;
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
273cdf0e10cSrcweir 				return false;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
276cdf0e10cSrcweir 				return false;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 			// the storage must be disposed before removing
279cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
280cdf0e10cSrcweir 				return false;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 			// remove element and check that it was removed completelly
283cdf0e10cSrcweir 			if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
284cdf0e10cSrcweir 				return false;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 			try
287cdf0e10cSrcweir 			{
288cdf0e10cSrcweir 				XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
289cdf0e10cSrcweir 				if ( xResAccess.hasByName( "SubStorage2" ) )
290cdf0e10cSrcweir 					m_aTestHelper.Error( "SubStorage2 must be removed already!" );
291cdf0e10cSrcweir 			}
292cdf0e10cSrcweir 			catch( Exception e )
293cdf0e10cSrcweir 			{
294cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
295cdf0e10cSrcweir 				return false;
296cdf0e10cSrcweir 			}
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 			try
299cdf0e10cSrcweir 			{
300cdf0e10cSrcweir 				xResSubStorage2.isStreamElement( "SubStream2" );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 				m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
303cdf0e10cSrcweir 				return false;
304cdf0e10cSrcweir 			}
305cdf0e10cSrcweir 			catch( com.sun.star.lang.DisposedException de )
306cdf0e10cSrcweir 			{
307cdf0e10cSrcweir 			}
308cdf0e10cSrcweir 			catch( Exception e )
309cdf0e10cSrcweir 			{
310cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
311cdf0e10cSrcweir 				return false;
312cdf0e10cSrcweir 			}
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 			// dispose used storages to free resources
315cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResStorage ) )
316cdf0e10cSrcweir 				return false;
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 			return true;
319cdf0e10cSrcweir 		}
320cdf0e10cSrcweir 		catch( Exception e )
321cdf0e10cSrcweir 		{
322cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
323cdf0e10cSrcweir 			return false;
324cdf0e10cSrcweir 		}
325cdf0e10cSrcweir     }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir }
328cdf0e10cSrcweir 
329