summaryrefslogtreecommitdiffstats
path: root/package/qa/storages/RegressionTest_i30400.java
blob: 39866503b6159936ed40e7a15f3fdd60a0e33368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package complex.storages;

import com.sun.star.uno.XInterface;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;

import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;
import com.sun.star.io.XStream;
import com.sun.star.io.XInputStream;

import com.sun.star.embed.*;

import share.LogWriter;
import complex.storages.TestHelper;
import complex.storages.StorageTest;

public class RegressionTest_i30400 implements StorageTest {

    XMultiServiceFactory m_xMSF;
    XSingleServiceFactory m_xStorageFactory;
    TestHelper m_aTestHelper;

    public RegressionTest_i30400( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
    {
        m_xMSF = xMSF;
        m_xStorageFactory = xStorageFactory;
        m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i30400: " );
    }

    public boolean test()
    {
        try
        {

            // create a temporary stream and a storage based on it
            // fill the storage with the data that will be used for testing


            XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
            if ( xTempFileStream == null )
                return false;

            // create storage based on the temporary stream
            Object pArgs[] = new Object[2];
            pArgs[0] = (Object) xTempFileStream;
            pArgs[1] = Integer.valueOf( ElementModes.WRITE );
            byte pBytes1[] = { 1, 1, 1, 1, 1 };
            String pPass1 = "1, 2, 3, 4, 5";

            Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
            XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
            if ( xTempStorage == null )
            {
                m_aTestHelper.Error( "Can't create temporary storage representation!" );
                return false;
            }

            // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
            if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "Stream1", "MediaType1", true, pBytes1 ) )
                return false;

            // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
            if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "EncrStream1", "MediaType2", true, pBytes1, pPass1 ) )
                return false;


            // open a new substorage
            XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
                                                                    "SubStorage1",
                                                                    ElementModes.WRITE );
            if ( xTempSubStorage == null )
            {
                m_aTestHelper.Error( "Can't create substorage!" );
                return false;
            }

            // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
            if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType3", true, pBytes1 ) )
                return false;

            // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
            if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubEncrStream1", "MediaType4", true, pBytes1, pPass1 ) )
                return false;

            // open a new substorage in the existing substorage
            XStorage xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
                                                                        "SubSubStorage1",
                                                                        ElementModes.WRITE );
            if ( xTempSubStorage == null )
            {
                m_aTestHelper.Error( "Can't create substorage!" );
                return false;
            }

            // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
            if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubSubStorage, "SubSubStream1", "MediaType5", true, pBytes1 ) )
                return false;

            // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
            if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubSubStorage,
                                                            "MediaType6",
                                                            false,
                                                            ElementModes.WRITE ) )
                return false;


            // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
            if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
                                                            "MediaType7",
                                                            false,
                                                            ElementModes.WRITE ) )
                return false;

            // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
            if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
                                                            "MediaType8",
                                                            true,
                                                            ElementModes.WRITE ) )
                return false;


            // check the copying with renaming


            if ( !TestCopyWithRenaming( xTempStorage, xTempSubStorage, xTempSubSubStorage ) )
                return false;


            // commit the storages


            // commit lowlevel substorage
            if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
                return false;

            // commit substorage
            if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
                return false;

            // commit the root storage so the contents must be stored now
            if ( !m_aTestHelper.commitStorage( xTempStorage ) )
                return false;


            // dispose the storages


            // dispose lowerest substorage
            if ( !m_aTestHelper.disposeStorage( xTempSubSubStorage ) )
                return false;

            // dispose substorage
            if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
                return false;

            // dispose the temporary storage
            if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
                return false;


            // reopen the target storage readonly, and check the copying with renaming


            pArgs[1] = Integer.valueOf( ElementModes.READ );
            oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
            xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
            if ( xTempStorage == null )
            {
                m_aTestHelper.Error( "Can't create temporary storage representation!" );
                return false;
            }

            // open the substorages

            xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
                                                            "SubStorage1",
                                                            ElementModes.READ );
            if ( xTempSubStorage == null )
            {
                m_aTestHelper.Error( "Can't create substorage!" );
                return false;
            }

            // open the lowlevel substorages

            xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
                                                                "SubSubStorage1",
                                                                ElementModes.READ );
            if ( xTempSubSubStorage == null )
            {
                m_aTestHelper.Error( "Can't create substorage!" );
                return false;
            }

            // test the copying with renaming
            if ( !TestCopyWithRenaming( xTempStorage, xTempSubStorage, xTempSubSubStorage ) )
                return false;


            // the storage is based on the temporary stream so it can be left undisposed, since it does not lock
            // any resource, later the garbage collector will release the object and it must die by refcount

            return true;
        }
        catch( Exception e )
        {
            m_aTestHelper.Error( "Exception: " + e );
            return false;
        }
    }


    public boolean TestCopyWithRenaming( XStorage xTempStorage, XStorage xTempSubStorage, XStorage xTempSubSubStorage )
        throws com.sun.star.uno.Exception
    {

        // create a second temporary stream and copy all the staff there
        // with renaming, check the success


        XStream xTempFileStream2 = m_aTestHelper.CreateTempFileStream( m_xMSF );
        if ( xTempFileStream2 == null )
            return false;

        Object pArgs[] = new Object[2];
        pArgs[0] = (Object) xTempFileStream2;
        pArgs[1] = Integer.valueOf( ElementModes.WRITE );
        byte pBytes1[] = { 1, 1, 1, 1, 1 };
        String pPass1 = "1, 2, 3, 4, 5";

        Object oTempStorage2 = m_xStorageFactory.createInstanceWithArguments( pArgs );
        XStorage xTempStorage2 = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage2 );
        if ( xTempStorage2 == null )
        {
            m_aTestHelper.Error( "Can't create temporary storage representation!" );
            return false;
        }

        // open a new substorage
        XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage2,
                                                                "SubStorage1_target",
                                                                ElementModes.WRITE );
        if ( xTempSubStorage2 == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        // open a new substorage in the existing substorage
        XStorage xTempSubSubStorage2 = m_aTestHelper.openSubStorage( xTempSubStorage2,
                                                                    "SubSubStorage1_target",
                                                                    ElementModes.WRITE );
        if ( xTempSubSubStorage2 == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        // make a copy with renaming on lowerest level
        if ( !m_aTestHelper.copyElementTo( xTempSubSubStorage, "SubSubStream1", xTempSubSubStorage2, "SubSubStream1_renamed" ) )
            return false;

        // make a copy with renaming on the next level

        if ( !m_aTestHelper.copyElementTo( xTempSubStorage, "SubStream1", xTempSubStorage2, "SubStream1_renamed" ) )
            return false;

        if ( !m_aTestHelper.copyElementTo( xTempSubStorage, "SubEncrStream1", xTempSubStorage2, "SubEncrStream1_renamed" ) )
            return false;

        if ( !m_aTestHelper.copyElementTo( xTempSubStorage, "SubSubStorage1", xTempSubStorage2, "SubSubStorage1_renamed" ) )
            return false;

        // make a copy with renaming of subelements of the root storage

        if ( !m_aTestHelper.copyElementTo( xTempStorage, "Stream1", xTempStorage2, "Stream1_renamed" ) )
            return false;

        if ( !m_aTestHelper.copyElementTo( xTempStorage, "EncrStream1", xTempStorage2, "EncrStream1_renamed" ) )
            return false;

        if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempStorage2, "SubStorage1_renamed" ) )
            return false;


        // commit the storages, and check the renaming in all stages


        // commit substorage to let the renaming take place
        if ( !m_aTestHelper.commitStorage( xTempSubSubStorage2 ) )
            return false;

        // commit substorage to let the renaming take place
        if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
            return false;

        // commit the root storage so the contents must be stored now
        if ( !m_aTestHelper.commitStorage( xTempStorage2 ) )
            return false;


        // dispose the storages


        // dispose lowerest substorage
        if ( !m_aTestHelper.disposeStorage( xTempSubSubStorage2 ) )
            return false;

        // dispose substorage
        if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
            return false;

        // dispose the temporary storage
        if ( !m_aTestHelper.disposeStorage( xTempStorage2 ) )
            return false;


        // reopen the target storage readonly, and check the contents


        pArgs[1] = Integer.valueOf( ElementModes.READ );
        oTempStorage2 = m_xStorageFactory.createInstanceWithArguments( pArgs );
        xTempStorage2 = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage2 );
        if ( xTempStorage2 == null )
        {
            m_aTestHelper.Error( "Can't create temporary storage representation!" );
            return false;
        }

        // open the substorages

        XStorage xTempSubStorage2_target = m_aTestHelper.openSubStorage( xTempStorage2,
                                                                "SubStorage1_target",
                                                                ElementModes.READ );
        if ( xTempSubStorage2_target == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        XStorage xTempSubStorage2_renamed = m_aTestHelper.openSubStorage( xTempStorage2,
                                                                "SubStorage1_renamed",
                                                                ElementModes.READ );
        if ( xTempSubStorage2_renamed == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        // open the lowlevel substorages

        XStorage xTempSubSubStorage2_inRenamed = m_aTestHelper.openSubStorage( xTempSubStorage2_renamed,
                                                                                "SubSubStorage1",
                                                                                ElementModes.READ );
        if ( xTempSubSubStorage2_inRenamed == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        XStorage xTempSubSubStorage2_renamed = m_aTestHelper.openSubStorage( xTempSubStorage2_target,
                                                                            "SubSubStorage1_renamed",
                                                                            ElementModes.READ );
        if ( xTempSubSubStorage2_renamed == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        XStorage xTempSubSubStorage2_target = m_aTestHelper.openSubStorage( xTempSubStorage2_target,
                                                                            "SubSubStorage1_target",
                                                                            ElementModes.READ );
        if ( xTempSubSubStorage2_target == null )
        {
            m_aTestHelper.Error( "Can't create substorage!" );
            return false;
        }

        // check the storages

        if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage2_inRenamed, "MediaType6", false, ElementModes.READ ) )
            return false;

        if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage2_renamed, "MediaType6", false, ElementModes.READ ) )
            return false;

        if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage2_renamed, "MediaType7", false, ElementModes.READ ) )
            return false;


        // check the streams


        // sub sub level

        if ( !m_aTestHelper.checkStream( xTempSubSubStorage2_inRenamed, "SubSubStream1", "MediaType5", true, pBytes1 ) )
            return false;

        if ( !m_aTestHelper.checkStream( xTempSubSubStorage2_renamed, "SubSubStream1", "MediaType5", true, pBytes1 ) )
            return false;

        if ( !m_aTestHelper.checkStream( xTempSubSubStorage2_target, "SubSubStream1_renamed", "MediaType5", true, pBytes1 ) )
            return false;

        // sub level

        if ( !m_aTestHelper.checkStream( xTempSubStorage2_renamed, "SubStream1", "MediaType3", true, pBytes1 ) )
            return false;

        if ( !m_aTestHelper.checkEncrStream( xTempSubStorage2_renamed, "SubEncrStream1", "MediaType4", pBytes1, pPass1 ) )
            return false;

        if ( !m_aTestHelper.checkStream( xTempSubStorage2_target, "SubStream1_renamed", "MediaType3", true, pBytes1 ) )
            return false;

        if ( !m_aTestHelper.checkEncrStream( xTempSubStorage2_target, "SubEncrStream1_renamed", "MediaType4", pBytes1, pPass1 ) )
            return false;

        // root storage level

        if ( !m_aTestHelper.checkStream( xTempStorage2, "Stream1_renamed", "MediaType1", true, pBytes1 ) )
            return false;

        if ( !m_aTestHelper.checkEncrStream( xTempStorage2, "EncrStream1_renamed", "MediaType2", pBytes1, pPass1 ) )
            return false;

        // the storage is based on the temporary stream so it can be left undisposed, since it does not lock
        // any resource, later the garbage collector will release the object and it must die by refcount

        return true;
    }
}