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_i30400 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i30400( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i30400( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i30400: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			// ================================================
58 			// create a temporary stream and a storage based on it
59 			// fill the storage with the data that will be used for testing
60 			// ================================================
61 
62 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
63 			if ( xTempFileStream == null )
64 				return false;
65 
66 			// create storage based on the temporary stream
67 			Object pArgs[] = new Object[2];
68 			pArgs[0] = (Object) xTempFileStream;
69 			pArgs[1] = new Integer( ElementModes.WRITE );
70 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
71 			String pPass1 = "1, 2, 3, 4, 5";
72 
73 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
74 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
75 			if ( xTempStorage == null )
76 			{
77 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
78 				return false;
79 			}
80 
81 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "Stream1", "MediaType1", true, pBytes1 ) )
83 				return false;
84 
85 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
86 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "EncrStream1", "MediaType2", true, pBytes1, pPass1 ) )
87 				return false;
88 
89 
90 			// open a new substorage
91 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
92 																	"SubStorage1",
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( xTempSubStorage, "SubStream1", "MediaType3", true, pBytes1 ) )
102 				return false;
103 
104 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
105 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubEncrStream1", "MediaType4", true, pBytes1, pPass1 ) )
106 				return false;
107 
108 			// open a new substorage in the existing substorage
109 			XStorage xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
110 																		"SubSubStorage1",
111 																		ElementModes.WRITE );
112 			if ( xTempSubStorage == null )
113 			{
114 				m_aTestHelper.Error( "Can't create substorage!" );
115 				return false;
116 			}
117 
118 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
119 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubSubStorage, "SubSubStream1", "MediaType5", true, pBytes1 ) )
120 				return false;
121 
122 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
123 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubSubStorage,
124 															"MediaType6",
125 															false,
126 															ElementModes.WRITE ) )
127 				return false;
128 
129 
130 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
131 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
132 															"MediaType7",
133 															false,
134 															ElementModes.WRITE ) )
135 				return false;
136 
137 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
138 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
139 															"MediaType8",
140 															true,
141 															ElementModes.WRITE ) )
142 				return false;
143 
144 			// ================================================
145 			// check the copying with renaming
146 			// ================================================
147 
148 			if ( !TestCopyWithRenaming( xTempStorage, xTempSubStorage, xTempSubSubStorage ) )
149 				return false;
150 
151 			// ================================================
152 			// commit the storages
153 			// ================================================
154 
155 			// commit lowlevel substorage
156 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
157 				return false;
158 
159 			// commit substorage
160 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
161 				return false;
162 
163 			// commit the root storage so the contents must be stored now
164 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
165 				return false;
166 
167 			// ================================================
168 			// dispose the storages
169 			// ================================================
170 
171 			// dispose lowerest substorage
172 			if ( !m_aTestHelper.disposeStorage( xTempSubSubStorage ) )
173 				return false;
174 
175 			// dispose substorage
176 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
177 				return false;
178 
179 			// dispose the temporary storage
180 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
181 				return false;
182 
183 			// ================================================
184 			// reopen the target storage readonly, and check the copying with renaming
185 			// ================================================
186 
187 			pArgs[1] = new Integer( ElementModes.READ );
188 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
189 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
190 			if ( xTempStorage == null )
191 			{
192 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
193 				return false;
194 			}
195 
196 			// open the substorages
197 
198 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
199 															"SubStorage1",
200 															ElementModes.READ );
201 			if ( xTempSubStorage == null )
202 			{
203 				m_aTestHelper.Error( "Can't create substorage!" );
204 				return false;
205 			}
206 
207 			// open the lowlevel substorages
208 
209 			xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
210 																"SubSubStorage1",
211 																ElementModes.READ );
212 			if ( xTempSubSubStorage == null )
213 			{
214 				m_aTestHelper.Error( "Can't create substorage!" );
215 				return false;
216 			}
217 
218 			// test the copying with renaming
219 			if ( !TestCopyWithRenaming( xTempStorage, xTempSubStorage, xTempSubSubStorage ) )
220 				return false;
221 
222 
223 			// the storage is based on the temporary stream so it can be left undisposed, since it does not lock
224 			// any resource, later the garbage collector will release the object and it must die by refcount
225 
226 			return true;
227 		}
228 		catch( Exception e )
229 		{
230 			m_aTestHelper.Error( "Exception: " + e );
231 			return false;
232 		}
233     }
234 
235 
TestCopyWithRenaming( XStorage xTempStorage, XStorage xTempSubStorage, XStorage xTempSubSubStorage )236 	public boolean TestCopyWithRenaming( XStorage xTempStorage, XStorage xTempSubStorage, XStorage xTempSubSubStorage )
237 		throws com.sun.star.uno.Exception
238 	{
239 		// ================================================
240 		// create a second temporary stream and copy all the staff there
241 		// with renaming, check the success
242 		// ================================================
243 
244 		XStream xTempFileStream2 = m_aTestHelper.CreateTempFileStream( m_xMSF );
245 		if ( xTempFileStream2 == null )
246 			return false;
247 
248 		Object pArgs[] = new Object[2];
249 		pArgs[0] = (Object) xTempFileStream2;
250 		pArgs[1] = new Integer( ElementModes.WRITE );
251 		byte pBytes1[] = { 1, 1, 1, 1, 1 };
252 		String pPass1 = "1, 2, 3, 4, 5";
253 
254 		Object oTempStorage2 = m_xStorageFactory.createInstanceWithArguments( pArgs );
255 		XStorage xTempStorage2 = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage2 );
256 		if ( xTempStorage2 == null )
257 		{
258 			m_aTestHelper.Error( "Can't create temporary storage representation!" );
259 			return false;
260 		}
261 
262 		// open a new substorage
263 		XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage2,
264 																"SubStorage1_target",
265 																ElementModes.WRITE );
266 		if ( xTempSubStorage2 == null )
267 		{
268 			m_aTestHelper.Error( "Can't create substorage!" );
269 			return false;
270 		}
271 
272 		// open a new substorage in the existing substorage
273 		XStorage xTempSubSubStorage2 = m_aTestHelper.openSubStorage( xTempSubStorage2,
274 																	"SubSubStorage1_target",
275 																	ElementModes.WRITE );
276 		if ( xTempSubSubStorage2 == null )
277 		{
278 			m_aTestHelper.Error( "Can't create substorage!" );
279 			return false;
280 		}
281 
282 		// make a copy with renaming on lowerest level
283 		if ( !m_aTestHelper.copyElementTo( xTempSubSubStorage, "SubSubStream1", xTempSubSubStorage2, "SubSubStream1_renamed" ) )
284 			return false;
285 
286 		// make a copy with renaming on the next level
287 
288 		if ( !m_aTestHelper.copyElementTo( xTempSubStorage, "SubStream1", xTempSubStorage2, "SubStream1_renamed" ) )
289 			return false;
290 
291 		if ( !m_aTestHelper.copyElementTo( xTempSubStorage, "SubEncrStream1", xTempSubStorage2, "SubEncrStream1_renamed" ) )
292 			return false;
293 
294 		if ( !m_aTestHelper.copyElementTo( xTempSubStorage, "SubSubStorage1", xTempSubStorage2, "SubSubStorage1_renamed" ) )
295 			return false;
296 
297 		// make a copy with renaming of subelements of the root storage
298 
299 		if ( !m_aTestHelper.copyElementTo( xTempStorage, "Stream1", xTempStorage2, "Stream1_renamed" ) )
300 			return false;
301 
302 		if ( !m_aTestHelper.copyElementTo( xTempStorage, "EncrStream1", xTempStorage2, "EncrStream1_renamed" ) )
303 			return false;
304 
305 		if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempStorage2, "SubStorage1_renamed" ) )
306 			return false;
307 
308 		// ================================================
309 		// commit the storages, and check the renaming in all stages
310 		// ================================================
311 
312 		// commit substorage to let the renaming take place
313 		if ( !m_aTestHelper.commitStorage( xTempSubSubStorage2 ) )
314 			return false;
315 
316 		// commit substorage to let the renaming take place
317 		if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
318 			return false;
319 
320 		// commit the root storage so the contents must be stored now
321 		if ( !m_aTestHelper.commitStorage( xTempStorage2 ) )
322 			return false;
323 
324 		// ================================================
325 		// dispose the storages
326 		// ================================================
327 
328 		// dispose lowerest substorage
329 		if ( !m_aTestHelper.disposeStorage( xTempSubSubStorage2 ) )
330 			return false;
331 
332 		// dispose substorage
333 		if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
334 			return false;
335 
336 		// dispose the temporary storage
337 		if ( !m_aTestHelper.disposeStorage( xTempStorage2 ) )
338 			return false;
339 
340 		// ================================================
341 		// reopen the target storage readonly, and check the contents
342 		// ================================================
343 
344 		pArgs[1] = new Integer( ElementModes.READ );
345 		oTempStorage2 = m_xStorageFactory.createInstanceWithArguments( pArgs );
346 		xTempStorage2 = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage2 );
347 		if ( xTempStorage2 == null )
348 		{
349 			m_aTestHelper.Error( "Can't create temporary storage representation!" );
350 			return false;
351 		}
352 
353 		// open the substorages
354 
355 		XStorage xTempSubStorage2_target = m_aTestHelper.openSubStorage( xTempStorage2,
356 																"SubStorage1_target",
357 																ElementModes.READ );
358 		if ( xTempSubStorage2_target == null )
359 		{
360 			m_aTestHelper.Error( "Can't create substorage!" );
361 			return false;
362 		}
363 
364 		XStorage xTempSubStorage2_renamed = m_aTestHelper.openSubStorage( xTempStorage2,
365 																"SubStorage1_renamed",
366 																ElementModes.READ );
367 		if ( xTempSubStorage2_renamed == null )
368 		{
369 			m_aTestHelper.Error( "Can't create substorage!" );
370 			return false;
371 		}
372 
373 		// open the lowlevel substorages
374 
375 		XStorage xTempSubSubStorage2_inRenamed = m_aTestHelper.openSubStorage( xTempSubStorage2_renamed,
376 																				"SubSubStorage1",
377 																				ElementModes.READ );
378 		if ( xTempSubSubStorage2_inRenamed == null )
379 		{
380 			m_aTestHelper.Error( "Can't create substorage!" );
381 			return false;
382 		}
383 
384 		XStorage xTempSubSubStorage2_renamed = m_aTestHelper.openSubStorage( xTempSubStorage2_target,
385 																			"SubSubStorage1_renamed",
386 																			ElementModes.READ );
387 		if ( xTempSubSubStorage2_renamed == null )
388 		{
389 			m_aTestHelper.Error( "Can't create substorage!" );
390 			return false;
391 		}
392 
393 		XStorage xTempSubSubStorage2_target = m_aTestHelper.openSubStorage( xTempSubStorage2_target,
394 																			"SubSubStorage1_target",
395 																			ElementModes.READ );
396 		if ( xTempSubSubStorage2_target == null )
397 		{
398 			m_aTestHelper.Error( "Can't create substorage!" );
399 			return false;
400 		}
401 
402 		// check the storages
403 
404 		if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage2_inRenamed, "MediaType6", false, ElementModes.READ ) )
405 			return false;
406 
407 		if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage2_renamed, "MediaType6", false, ElementModes.READ ) )
408 			return false;
409 
410 		if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage2_renamed, "MediaType7", false, ElementModes.READ ) )
411 			return false;
412 
413 
414 		// check the streams
415 
416 
417 		// sub sub level
418 
419 		if ( !m_aTestHelper.checkStream( xTempSubSubStorage2_inRenamed, "SubSubStream1", "MediaType5", true, pBytes1 ) )
420 			return false;
421 
422 		if ( !m_aTestHelper.checkStream( xTempSubSubStorage2_renamed, "SubSubStream1", "MediaType5", true, pBytes1 ) )
423 			return false;
424 
425 		if ( !m_aTestHelper.checkStream( xTempSubSubStorage2_target, "SubSubStream1_renamed", "MediaType5", true, pBytes1 ) )
426 			return false;
427 
428 		// sub level
429 
430 		if ( !m_aTestHelper.checkStream( xTempSubStorage2_renamed, "SubStream1", "MediaType3", true, pBytes1 ) )
431 			return false;
432 
433 		if ( !m_aTestHelper.checkEncrStream( xTempSubStorage2_renamed, "SubEncrStream1", "MediaType4", pBytes1, pPass1 ) )
434 			return false;
435 
436 		if ( !m_aTestHelper.checkStream( xTempSubStorage2_target, "SubStream1_renamed", "MediaType3", true, pBytes1 ) )
437 			return false;
438 
439 		if ( !m_aTestHelper.checkEncrStream( xTempSubStorage2_target, "SubEncrStream1_renamed", "MediaType4", pBytes1, pPass1 ) )
440 			return false;
441 
442 		// root storage level
443 
444 		if ( !m_aTestHelper.checkStream( xTempStorage2, "Stream1_renamed", "MediaType1", true, pBytes1 ) )
445 			return false;
446 
447 		if ( !m_aTestHelper.checkEncrStream( xTempStorage2, "EncrStream1_renamed", "MediaType2", pBytes1, pPass1 ) )
448 			return false;
449 
450 		// the storage is based on the temporary stream so it can be left undisposed, since it does not lock
451 		// any resource, later the garbage collector will release the object and it must die by refcount
452 
453 		return true;
454 	}
455 }
456 
457