1*a740f2aaSAndrew Rist /**************************************************************
2*a740f2aaSAndrew Rist  *
3*a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a740f2aaSAndrew Rist  * distributed with this work for additional information
6*a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9*a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a740f2aaSAndrew Rist  *
11*a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a740f2aaSAndrew Rist  *
13*a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15*a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18*a740f2aaSAndrew Rist  * under the License.
19*a740f2aaSAndrew Rist  *
20*a740f2aaSAndrew Rist  *************************************************************/
21*a740f2aaSAndrew 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 
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir import com.sun.star.io.XStream;
32cdf0e10cSrcweir import com.sun.star.io.XInputStream;
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import com.sun.star.embed.*;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import share.LogWriter;
39cdf0e10cSrcweir import complex.storages.TestHelper;
40cdf0e10cSrcweir import complex.storages.StorageTest;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir ///////////////////////////////////
43cdf0e10cSrcweir // Tests also fix for i51352
44cdf0e10cSrcweir ///////////////////////////////////
45cdf0e10cSrcweir 
46cdf0e10cSrcweir public class RegressionTest_i27773 implements StorageTest {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
49cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
50cdf0e10cSrcweir 	TestHelper m_aTestHelper;
51cdf0e10cSrcweir 
RegressionTest_i27773( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )52cdf0e10cSrcweir 	public RegressionTest_i27773( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		m_xMSF = xMSF;
55cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
56cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i27773: " );
57cdf0e10cSrcweir 	}
58cdf0e10cSrcweir 
test()59cdf0e10cSrcweir     public boolean test()
60cdf0e10cSrcweir 	{
61cdf0e10cSrcweir 		try
62cdf0e10cSrcweir 		{
63cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
64cdf0e10cSrcweir 			if ( xTempFileStream == null )
65cdf0e10cSrcweir 				return false;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 			if ( true )
68cdf0e10cSrcweir 			{
69cdf0e10cSrcweir 				// for debugging proposes
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 				XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xTempFileStream );
72cdf0e10cSrcweir 				if ( xPropSet != null )
73cdf0e10cSrcweir 				{
74cdf0e10cSrcweir 					try
75cdf0e10cSrcweir 					{
76cdf0e10cSrcweir 						String sTempURL = AnyConverter.toString( xPropSet.getPropertyValue( "Uri" ) );
77cdf0e10cSrcweir 						// m_aTestHelper.Message( "URL: " + sTempURL );
78cdf0e10cSrcweir 						xPropSet.setPropertyValue( "RemoveFile", new Boolean( false ) );
79cdf0e10cSrcweir 					}
80cdf0e10cSrcweir 					catch ( Exception e )
81cdf0e10cSrcweir 					{
82cdf0e10cSrcweir 					}
83cdf0e10cSrcweir 				}
84cdf0e10cSrcweir 			}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 			// create storage based on the temporary stream
87cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
88cdf0e10cSrcweir 			pArgs[0] = (Object) xTempFileStream;
89cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
92cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
93cdf0e10cSrcweir 			if ( xTempStorage == null )
94cdf0e10cSrcweir 			{
95cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
96cdf0e10cSrcweir 				return false;
97cdf0e10cSrcweir 			}
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 			// open a new substorage
100cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
101cdf0e10cSrcweir 																	"SubStorage1",
102cdf0e10cSrcweir 																	ElementModes.WRITE );
103cdf0e10cSrcweir 			if ( xTempSubStorage == null )
104cdf0e10cSrcweir 			{
105cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
106cdf0e10cSrcweir 				return false;
107cdf0e10cSrcweir 			}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 			// open an empty substorage
110cdf0e10cSrcweir 			XStorage xEmptySubStorage = m_aTestHelper.openSubStorage( xTempStorage,
111cdf0e10cSrcweir 																	"EmptySubStorage1",
112cdf0e10cSrcweir 																	ElementModes.WRITE );
113cdf0e10cSrcweir 			if ( xEmptySubStorage == null )
114cdf0e10cSrcweir 			{
115cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
116cdf0e10cSrcweir 				return false;
117cdf0e10cSrcweir 			}
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 			// open an empty substorage
120cdf0e10cSrcweir 			XStorage xEmptySubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
121cdf0e10cSrcweir 																		"EmptySubSubStorage1",
122cdf0e10cSrcweir 																		ElementModes.WRITE );
123cdf0e10cSrcweir 			if ( xEmptySubSubStorage == null )
124cdf0e10cSrcweir 			{
125cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
126cdf0e10cSrcweir 				return false;
127cdf0e10cSrcweir 			}
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
133cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
134cdf0e10cSrcweir 				return false;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
137cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
138cdf0e10cSrcweir 															"MediaType2",
139cdf0e10cSrcweir 															true,
140cdf0e10cSrcweir 															ElementModes.WRITE ) )
141cdf0e10cSrcweir 				return false;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
144cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xEmptySubStorage,
145cdf0e10cSrcweir 															"MediaType3",
146cdf0e10cSrcweir 															false,
147cdf0e10cSrcweir 															ElementModes.WRITE ) )
148cdf0e10cSrcweir 				return false;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
151cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
152cdf0e10cSrcweir 															"MediaType4",
153cdf0e10cSrcweir 															false,
154cdf0e10cSrcweir 															ElementModes.WRITE ) )
155cdf0e10cSrcweir 				return false;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
158cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xEmptySubSubStorage,
159cdf0e10cSrcweir 															"MediaType5",
160cdf0e10cSrcweir 															false,
161cdf0e10cSrcweir 															ElementModes.WRITE ) )
162cdf0e10cSrcweir 				return false;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 			// make a copy of substorage
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempStorage, "SubStorage1_copy" ) )
168cdf0e10cSrcweir 				return false;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			if ( !m_aTestHelper.copyElementTo( xTempStorage, "EmptySubStorage1", xTempStorage, "EmptySubStorage1_copy" ) )
171cdf0e10cSrcweir 				return false;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 			// ================================================
174cdf0e10cSrcweir 			// copy all the changed and noncommited substorages
175cdf0e10cSrcweir 			// and dispose them
176cdf0e10cSrcweir 			// ================================================
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xEmptySubSubStorage ) )
179cdf0e10cSrcweir 				return false;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
182cdf0e10cSrcweir 				return false;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xEmptySubStorage ) )
185cdf0e10cSrcweir 				return false;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
188cdf0e10cSrcweir 				return false;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 			// dispose substorages
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xEmptySubSubStorage ) )
193cdf0e10cSrcweir 				return false;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
196cdf0e10cSrcweir 				return false;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xEmptySubStorage ) )
199cdf0e10cSrcweir 				return false;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
202cdf0e10cSrcweir 				return false;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 			// ================================================
205cdf0e10cSrcweir 			// reopen the storage in readonly mode an check contents
206cdf0e10cSrcweir 			// ================================================
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.READ );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
211cdf0e10cSrcweir 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
212cdf0e10cSrcweir 			if ( xTempStorage == null )
213cdf0e10cSrcweir 			{
214cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
215cdf0e10cSrcweir 				return false;
216cdf0e10cSrcweir 			}
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 			// open original substorage
219cdf0e10cSrcweir 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
220cdf0e10cSrcweir 																	"SubStorage1",
221cdf0e10cSrcweir 																	ElementModes.READ );
222cdf0e10cSrcweir 			if ( xTempSubStorage == null )
223cdf0e10cSrcweir 			{
224cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
225cdf0e10cSrcweir 				return false;
226cdf0e10cSrcweir 			}
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 			// open copy of the original substorage
229cdf0e10cSrcweir 			XStorage xTempSubStorage_copy = m_aTestHelper.openSubStorage( xTempStorage,
230cdf0e10cSrcweir 																	"SubStorage1_copy",
231cdf0e10cSrcweir 																	ElementModes.READ );
232cdf0e10cSrcweir 			if ( xTempSubStorage_copy == null )
233cdf0e10cSrcweir 			{
234cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
235cdf0e10cSrcweir 				return false;
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 			// open empty substorage
239cdf0e10cSrcweir 			xEmptySubStorage = m_aTestHelper.openSubStorage( xTempStorage,
240cdf0e10cSrcweir 															"EmptySubStorage1",
241cdf0e10cSrcweir 															ElementModes.READ );
242cdf0e10cSrcweir 			if ( xEmptySubStorage == null )
243cdf0e10cSrcweir 			{
244cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
245cdf0e10cSrcweir 				return false;
246cdf0e10cSrcweir 			}
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 			// open copy of empty substorage
249cdf0e10cSrcweir 			XStorage xEmptySubStorage_copy = m_aTestHelper.openSubStorage( xTempStorage,
250cdf0e10cSrcweir 																		"EmptySubStorage1_copy",
251cdf0e10cSrcweir 																		ElementModes.READ );
252cdf0e10cSrcweir 			if ( xEmptySubStorage_copy == null )
253cdf0e10cSrcweir 			{
254cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
255cdf0e10cSrcweir 				return false;
256cdf0e10cSrcweir 			}
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 			// open an empty substorage of the substorage
259cdf0e10cSrcweir 			xEmptySubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
260cdf0e10cSrcweir 																"EmptySubSubStorage1",
261cdf0e10cSrcweir 																ElementModes.READ );
262cdf0e10cSrcweir 			if ( xEmptySubSubStorage == null )
263cdf0e10cSrcweir 			{
264cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
265cdf0e10cSrcweir 				return false;
266cdf0e10cSrcweir 			}
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 			// open an empty substorage of the substorage copy
269cdf0e10cSrcweir 			XStorage xEmptySubSubStorage_inCopy = m_aTestHelper.openSubStorage( xTempSubStorage_copy,
270cdf0e10cSrcweir 																				"EmptySubSubStorage1",
271cdf0e10cSrcweir 																				ElementModes.READ );
272cdf0e10cSrcweir 			if ( xEmptySubSubStorage_inCopy == null )
273cdf0e10cSrcweir 			{
274cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
275cdf0e10cSrcweir 				return false;
276cdf0e10cSrcweir 			}
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 			// check contents
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubSubStorage, "MediaType5", false, ElementModes.READ ) )
282cdf0e10cSrcweir 				return false;
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubSubStorage_inCopy, "MediaType5", false, ElementModes.READ ) )
285cdf0e10cSrcweir 				return false;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage, "MediaType4", false, ElementModes.READ ) )
288cdf0e10cSrcweir 				return false;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage_copy, "MediaType4", false, ElementModes.READ ) )
291cdf0e10cSrcweir 				return false;
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubStorage, "MediaType3", false, ElementModes.READ ) )
294cdf0e10cSrcweir 				return false;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xEmptySubStorage_copy, "MediaType3", false, ElementModes.READ ) )
297cdf0e10cSrcweir 				return false;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xTempStorage, "MediaType2", true, ElementModes.READ ) )
300cdf0e10cSrcweir 				return false;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
303cdf0e10cSrcweir 				return false;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xTempSubStorage_copy, "SubStream1", "MediaType1", true, pBytes1 ) )
306cdf0e10cSrcweir 				return false;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 			// the root storage is based on the temporary stream so it can be left undisposed, since it does not lock
309cdf0e10cSrcweir 			// any resource, later the garbage collector will release the object and it must die by refcount
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 			return true;
312cdf0e10cSrcweir 		}
313cdf0e10cSrcweir 		catch( Exception e )
314cdf0e10cSrcweir 		{
315cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
316cdf0e10cSrcweir 			return false;
317cdf0e10cSrcweir 		}
318cdf0e10cSrcweir     }
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321