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 
34cdf0e10cSrcweir import com.sun.star.embed.*;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir import share.LogWriter;
37cdf0e10cSrcweir import complex.storages.TestHelper;
38cdf0e10cSrcweir import complex.storages.StorageTest;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir public class RegressionTest_i49755 implements StorageTest {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
43cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
44cdf0e10cSrcweir 	TestHelper m_aTestHelper;
45cdf0e10cSrcweir 
RegressionTest_i49755( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46cdf0e10cSrcweir 	public RegressionTest_i49755( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47cdf0e10cSrcweir 	{
48cdf0e10cSrcweir 		m_xMSF = xMSF;
49cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
50cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i49755: " );
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir 
test()53cdf0e10cSrcweir     public boolean test()
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		try
56cdf0e10cSrcweir 		{
57cdf0e10cSrcweir 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58cdf0e10cSrcweir 			if ( xTempFileStream == null )
59cdf0e10cSrcweir 				return false;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 			// create storage based on the temporary stream
62cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
63cdf0e10cSrcweir 			pArgs[0] = (Object) xTempFileStream;
64cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68cdf0e10cSrcweir 			if ( xTempStorage == null )
69cdf0e10cSrcweir 			{
70cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71cdf0e10cSrcweir 				return false;
72cdf0e10cSrcweir 			}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
75cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
76cdf0e10cSrcweir 															"MediaType1",
77cdf0e10cSrcweir 															true,
78cdf0e10cSrcweir 															ElementModes.WRITE ) )
79cdf0e10cSrcweir 				return false;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 			byte pBytes[] = new byte[36000];
83cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 36000; nInd++ )
84cdf0e10cSrcweir 				pBytes[nInd] = (byte)( nInd % 128 );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 			// open a new substorage
87cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
88cdf0e10cSrcweir 																		"SubStorage1",
89cdf0e10cSrcweir 																		ElementModes.WRITE );
90cdf0e10cSrcweir 			if ( xTempSubStorage == null )
91cdf0e10cSrcweir 			{
92cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
93cdf0e10cSrcweir 				return false;
94cdf0e10cSrcweir 			}
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
97cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
98cdf0e10cSrcweir 															"MediaType2",
99cdf0e10cSrcweir 															false,
100cdf0e10cSrcweir 															ElementModes.WRITE ) )
101cdf0e10cSrcweir 				return false;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 			// open a new substorage
104cdf0e10cSrcweir 			XStorage xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
105cdf0e10cSrcweir 																		"SubStorage2",
106cdf0e10cSrcweir 																		ElementModes.WRITE );
107cdf0e10cSrcweir 			if ( xTempSubStorage == null )
108cdf0e10cSrcweir 			{
109cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
110cdf0e10cSrcweir 				return false;
111cdf0e10cSrcweir 			}
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
114cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubSubStorage,
115cdf0e10cSrcweir 															"MediaType3",
116cdf0e10cSrcweir 															false,
117cdf0e10cSrcweir 															ElementModes.WRITE ) )
118cdf0e10cSrcweir 				return false;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
121cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubSubStorage, "SubStream1", "MediaType4", true, pBytes ) )
122cdf0e10cSrcweir 				return false;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 			// open a new substorage
125cdf0e10cSrcweir 			XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
126cdf0e10cSrcweir 																		"SubStorage3",
127cdf0e10cSrcweir 																		ElementModes.WRITE );
128cdf0e10cSrcweir 			if ( xTempSubStorage1 == null )
129cdf0e10cSrcweir 			{
130cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
131cdf0e10cSrcweir 				return false;
132cdf0e10cSrcweir 			}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
135cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
136cdf0e10cSrcweir 															"MediaType5",
137cdf0e10cSrcweir 															false,
138cdf0e10cSrcweir 															ElementModes.WRITE ) )
139cdf0e10cSrcweir 				return false;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
142cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream2", "MediaType4", false, pBytes ) )
143cdf0e10cSrcweir 				return false;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 			// commit substorages first
147cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
148cdf0e10cSrcweir 				return false;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
151cdf0e10cSrcweir 				return false;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage1 ) )
154cdf0e10cSrcweir 				return false;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
157cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
158cdf0e10cSrcweir 				return false;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 			// dispose used storage to free resources
161cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
162cdf0e10cSrcweir 				return false;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 			// ================================================
165cdf0e10cSrcweir 			// now change the contents of the second substorage
166cdf0e10cSrcweir 			// without changing of the contents of the first substorage
167cdf0e10cSrcweir 			// ================================================
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 			Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
170cdf0e10cSrcweir 			XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
171cdf0e10cSrcweir 			if ( xStep2TempStorage == null )
172cdf0e10cSrcweir 			{
173cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
174cdf0e10cSrcweir 				return false;
175cdf0e10cSrcweir 			}
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 			XStorage xStep2TempSubStorage1 = m_aTestHelper.openSubStorage( xStep2TempStorage,
178cdf0e10cSrcweir 																			"SubStorage3",
179cdf0e10cSrcweir 																			ElementModes.WRITE );
180cdf0e10cSrcweir 			if ( xStep2TempSubStorage1 == null )
181cdf0e10cSrcweir 			{
182cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
183cdf0e10cSrcweir 				return false;
184cdf0e10cSrcweir 			}
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
187cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xStep2TempSubStorage1, "SubStream2", "MediaType4", false, pBytes ) )
188cdf0e10cSrcweir 				return false;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xStep2TempSubStorage1 ) )
191cdf0e10cSrcweir 				return false;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
194cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xStep2TempStorage ) )
195cdf0e10cSrcweir 				return false;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 			// dispose used storage to free resources
198cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
199cdf0e10cSrcweir 				return false;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 			// ================================================
203cdf0e10cSrcweir 			// now check all the written information
204cdf0e10cSrcweir 			// and the raw stream contents
205cdf0e10cSrcweir 			// ================================================
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 			// close the output part of the temporary stream
208cdf0e10cSrcweir 			// the output part must present since we already wrote to the stream
209cdf0e10cSrcweir 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
210cdf0e10cSrcweir 				return false;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
213cdf0e10cSrcweir 			if ( xTempInStream == null )
214cdf0e10cSrcweir 				return false;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 			// open input stream
217cdf0e10cSrcweir 			// since no mode is provided the result storage must be opened readonly
218cdf0e10cSrcweir 			Object pOneArg[] = new Object[1];
219cdf0e10cSrcweir 			pOneArg[0] = (Object) xTempInStream;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
222cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
223cdf0e10cSrcweir 			if ( xResultStorage == null )
224cdf0e10cSrcweir 			{
225cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
226cdf0e10cSrcweir 				return false;
227cdf0e10cSrcweir 			}
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType1", true, ElementModes.READ ) )
230cdf0e10cSrcweir 				return false;
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 			// open existing substorage
233cdf0e10cSrcweir 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
234cdf0e10cSrcweir 																		"SubStorage1",
235cdf0e10cSrcweir 																		ElementModes.READ );
236cdf0e10cSrcweir 			if ( xResultSubStorage == null )
237cdf0e10cSrcweir 			{
238cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
239cdf0e10cSrcweir 				return false;
240cdf0e10cSrcweir 			}
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType2", false, ElementModes.READ ) )
243cdf0e10cSrcweir 				return false;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 			// open existing substorage
246cdf0e10cSrcweir 			XStorage xResultSubSubStorage = m_aTestHelper.openSubStorage( xResultSubStorage,
247cdf0e10cSrcweir 																			"SubStorage2",
248cdf0e10cSrcweir 																			ElementModes.READ );
249cdf0e10cSrcweir 			if ( xResultSubSubStorage == null )
250cdf0e10cSrcweir 			{
251cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
252cdf0e10cSrcweir 				return false;
253cdf0e10cSrcweir 			}
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubSubStorage, "MediaType3", false, ElementModes.READ ) )
256cdf0e10cSrcweir 				return false;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubSubStorage, "SubStream1", "MediaType4", true, pBytes ) )
259cdf0e10cSrcweir 				return false;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 			XStorage xResultSubStorage1 = m_aTestHelper.openSubStorage( xResultStorage,
264cdf0e10cSrcweir 																		"SubStorage3",
265cdf0e10cSrcweir 																		ElementModes.READ );
266cdf0e10cSrcweir 			if ( xResultSubStorage1 == null )
267cdf0e10cSrcweir 			{
268cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
269cdf0e10cSrcweir 				return false;
270cdf0e10cSrcweir 			}
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage1, "MediaType5", false, ElementModes.READ ) )
273cdf0e10cSrcweir 				return false;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubStorage1, "SubStream2", "MediaType4", false, pBytes ) )
276cdf0e10cSrcweir 				return false;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 			// dispose used storages to free resources
280cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
281cdf0e10cSrcweir 				return false;
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 			return true;
284cdf0e10cSrcweir 		}
285cdf0e10cSrcweir 		catch( Exception e )
286cdf0e10cSrcweir 		{
287cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
288cdf0e10cSrcweir 			return false;
289cdf0e10cSrcweir 		}
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294