1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.io.XStream;
32 import com.sun.star.io.XInputStream;
33 
34 import com.sun.star.embed.*;
35 
36 import share.LogWriter;
37 import complex.storages.TestHelper;
38 import complex.storages.StorageTest;
39 
40 public class RegressionTest_i29169 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i29169( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i29169( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i29169: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58 			if ( xTempFileStream == null )
59 				return false;
60 
61 			// create storage based on the temporary stream
62 			Object pArgs[] = new Object[2];
63 			pArgs[0] = (Object) xTempFileStream;
64 			pArgs[1] = new Integer( ElementModes.WRITE );
65 
66 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68 			if ( xTempStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 				return false;
72 			}
73 
74 			// open a new substorage
75 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
76 																	"SubStorage1",
77 																	ElementModes.WRITE );
78 			if ( xTempSubStorage == null )
79 			{
80 				m_aTestHelper.Error( "Can't create substorage!" );
81 				return false;
82 			}
83 
84 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
85 
86 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
87 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
88 				return false;
89 
90 			// open a new substorage in the existing substorage
91 			XStorage xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
92 																		"SubSubStorage1",
93 																		ElementModes.WRITE );
94 			if ( xTempSubStorage == null )
95 			{
96 				m_aTestHelper.Error( "Can't create substorage!" );
97 				return false;
98 			}
99 
100 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
101 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubSubStorage, "SubSubStream1", "MediaType2", true, pBytes1 ) )
102 				return false;
103 
104 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubSubStorage,
106 															"MediaType3",
107 															false,
108 															ElementModes.WRITE ) )
109 				return false;
110 
111 
112 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
113 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
114 															"MediaType4",
115 															false,
116 															ElementModes.WRITE ) )
117 				return false;
118 
119 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
120 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
121 															"MediaType5",
122 															true,
123 															ElementModes.WRITE ) )
124 				return false;
125 
126 			// ================================================
127 			// commit the storages, and check the renaming in all stages
128 			// ================================================
129 
130 			// rename the storage before it is commited
131 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage1", "SubSubStorage2" ) )
132 				return false;
133 
134 			// rename the stream
135 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream1", "SubStream2" ) )
136 				return false;
137 
138 			// commit lowlevel substorage first
139 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
140 				return false;
141 
142 			// rename the storage after it is commited
143 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage2", "SubSubStorage3" ) )
144 				return false;
145 
146 			// rename the stream one more time
147 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream2", "SubStream3" ) )
148 				return false;
149 
150 			// commit substorage
151 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
152 				return false;
153 
154 			// rename the storage after it`s parent is commited
155 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage3", "SubSubStorage4" ) )
156 				return false;
157 
158 			// rename the stream after it`s parent is commited
159 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream3", "SubStream4" ) )
160 				return false;
161 
162 			// commit substorage to let the renaming take place
163 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
164 				return false;
165 
166 			// commit the root storage so the contents must be stored now
167 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
168 				return false;
169 
170 			// rename the storage after the package is commited
171 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage4", "SubSubStorage5" ) )
172 				return false;
173 
174 			// rename the stream after it`s parent is commited
175 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream4", "SubStream5" ) )
176 				return false;
177 
178 			// commit substorage to let the renaming take place
179 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
180 				return false;
181 
182 			// commit the root storage so the contents must be stored now
183 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
184 				return false;
185 
186 			// ================================================
187 			// dispose the storages
188 			// ================================================
189 
190 			// dispose lowerest substorage
191 			if ( !m_aTestHelper.disposeStorage( xTempSubSubStorage ) )
192 				return false;
193 
194 			// dispose substorage
195 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
196 				return false;
197 
198 			// dispose the temporary storage
199 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
200 				return false;
201 
202 			// ================================================
203 			// create a new storage based on the stream and check the substreams and substorages
204 			// ================================================
205 
206 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
207 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
208 			if ( xTempStorage == null )
209 			{
210 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
211 				return false;
212 			}
213 
214 			// open the substorage
215 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
216 															"SubStorage1",
217 															ElementModes.WRITE );
218 			if ( xTempSubStorage == null )
219 			{
220 				m_aTestHelper.Error( "Can't create substorage!" );
221 				return false;
222 			}
223 
224 			// open the lowlevel substorage
225 			xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
226 																		"SubSubStorage5",
227 																		ElementModes.WRITE );
228 			if ( xTempSubStorage == null )
229 			{
230 				m_aTestHelper.Error( "Can't create substorage!" );
231 				return false;
232 			}
233 
234 			// check the storages and streams
235 
236 			if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage, "MediaType3", false, ElementModes.WRITE ) )
237 				return false;
238 
239 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage, "MediaType4", false, ElementModes.WRITE ) )
240 				return false;
241 
242 			if ( !m_aTestHelper.checkStorageProperties( xTempStorage, "MediaType5", true, ElementModes.WRITE ) )
243 				return false;
244 
245 			if ( !m_aTestHelper.checkStream( xTempSubStorage, "SubStream5", "MediaType1", true, pBytes1 ) )
246 				return false;
247 
248 			if ( !m_aTestHelper.checkStream( xTempSubSubStorage, "SubSubStream1", "MediaType2", true, pBytes1 ) )
249 				return false;
250 
251 			// ================================================
252 			// rename the reopened storages and streams
253 			// ================================================
254 
255 			// rename the storage before it is commited
256 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage5", "SubSubStorage6" ) )
257 				return false;
258 
259 			// rename the stream
260 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream5", "SubStream6" ) )
261 				return false;
262 
263 			// commit lowlevel substorage first
264 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
265 				return false;
266 
267 			// rename the storage after it is commited
268 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage6", "SubSubStorage7" ) )
269 				return false;
270 
271 			// rename the stream one more time
272 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream6", "SubStream7" ) )
273 				return false;
274 
275 			// commit substorage
276 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
277 				return false;
278 
279 			// rename the storage after it`s parent is commited
280 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage7", "SubSubStorage8" ) )
281 				return false;
282 
283 			// rename the stream after it`s parent is commited
284 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream7", "SubStream8" ) )
285 				return false;
286 
287 			// commit substorage to let the renaming take place
288 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
289 				return false;
290 
291 			// commit the root storage so the contents must be stored now
292 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
293 				return false;
294 
295 			// rename the storage after the package is commited
296 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubSubStorage8", "SubSubStorage9" ) )
297 				return false;
298 
299 			// rename the stream after it`s parent is commited
300 			if ( !m_aTestHelper.renameElement( xTempSubStorage, "SubStream8", "SubStream9" ) )
301 				return false;
302 
303 			// commit substorage to let the renaming take place
304 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
305 				return false;
306 
307 			// commit the root storage so the contents must be stored now
308 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
309 				return false;
310 
311 			// ================================================
312 			// dispose the storages
313 			// ================================================
314 
315 			// dispose lowerest substorage
316 			if ( !m_aTestHelper.disposeStorage( xTempSubSubStorage ) )
317 				return false;
318 
319 			// dispose substorage
320 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
321 				return false;
322 
323 			// dispose the temporary storage
324 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
325 				return false;
326 
327 
328 			// ================================================
329 			// create a new readonly storage based on the stream and check the contents
330 			// ================================================
331 
332 			pArgs[1] = new Integer( ElementModes.READ );
333 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
334 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
335 			if ( xTempStorage == null )
336 			{
337 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
338 				return false;
339 			}
340 
341 			// open the substorage
342 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
343 															"SubStorage1",
344 															ElementModes.READ );
345 			if ( xTempSubStorage == null )
346 			{
347 				m_aTestHelper.Error( "Can't create substorage!" );
348 				return false;
349 			}
350 
351 			// open the lowlevel substorage
352 			xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
353 																		"SubSubStorage9",
354 																		ElementModes.READ );
355 			if ( xTempSubStorage == null )
356 			{
357 				m_aTestHelper.Error( "Can't create substorage!" );
358 				return false;
359 			}
360 
361 			// check the storages and streams
362 
363 			if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage, "MediaType3", false, ElementModes.READ ) )
364 				return false;
365 
366 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage, "MediaType4", false, ElementModes.READ ) )
367 				return false;
368 
369 			if ( !m_aTestHelper.checkStorageProperties( xTempStorage, "MediaType5", true, ElementModes.READ ) )
370 				return false;
371 
372 			if ( !m_aTestHelper.checkStream( xTempSubStorage, "SubStream9", "MediaType1", true, pBytes1 ) )
373 				return false;
374 
375 			if ( !m_aTestHelper.checkStream( xTempSubSubStorage, "SubSubStream1", "MediaType2", true, pBytes1 ) )
376 				return false;
377 
378 			// the storage is based on the temporary stream so it can be left undisposed, since it does not lock
379 			// any resource, later the garbage collector will release the object and it must die by refcount
380 
381 			return true;
382 		}
383 		catch( Exception e )
384 		{
385 			m_aTestHelper.Error( "Exception: " + e );
386 			return false;
387 		}
388     }
389 }
390 
391