bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test09.java
blob3790ecd8b9e66ac3410a8ba703c1b3a8771a66da
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex.storages;
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.lang.XSingleServiceFactory;
25 import com.sun.star.bridge.XUnoUrlResolver;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XInterface;
29 import com.sun.star.embed.*;
31 import share.LogWriter;
32 import complex.storages.TestHelper;
33 import complex.storages.StorageTest;
35 public class Test09 implements StorageTest {
37 XMultiServiceFactory m_xMSF;
38 XSingleServiceFactory m_xStorageFactory;
39 TestHelper m_aTestHelper;
41 public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
43 m_xMSF = xMSF;
44 m_xStorageFactory = xStorageFactory;
45 m_aTestHelper = new TestHelper( aLogWriter, "Test09: " );
48 public boolean test()
50 try
53 // create temporary storage based on arbitrary medium
54 // after such a storage is closed it is lost
55 Object oTempStorage = m_xStorageFactory.createInstance();
56 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
57 if ( xTempStorage == null )
59 m_aTestHelper.Error( "Can't create temporary storage representation!" );
60 return false;
63 String sPass1 = "123";
64 String sPass2 = "321";
65 byte pBytes[] = { 1, 1, 1, 1, 1 };
66 byte pBigBytes[] = new byte[33000];
67 for ( int nInd = 0; nInd < 33000; nInd++ )
68 pBigBytes[nInd] = (byte)( nInd % 128 );
70 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
71 // the stream will not be encrypted
72 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, sPass1 ) )
73 return false;
74 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", false, pBigBytes, sPass1 ) )
75 return false;
77 // create temporary file
78 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
79 if ( sTempFileURL == null || sTempFileURL == "" )
81 m_aTestHelper.Error( "No valid temporary file was created!" );
82 return false;
85 // create temporary storage based on a previously created temporary file
86 Object pArgs[] = new Object[2];
87 pArgs[0] = (Object) sTempFileURL;
88 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
90 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
91 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
92 if ( xTempFileStorage == null )
94 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
95 return false;
98 // copy xTempStorage to xTempFileStorage
99 // xTempFileStorage will be automatically committed
100 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
101 return false;
103 // change password of the substream of new storage based on file
104 int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", sPass1, sPass2 );
105 if ( nResult == 0 )
106 return false; // test failed
107 else if ( nResult == -1 )
108 return true; // tested optional feature is not supported
110 // change password of the substream of new storage based on file
111 nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "BigSubStream1", sPass1, sPass2 );
112 if ( nResult == 0 )
113 return false; // test failed
114 else if ( nResult == -1 )
115 return true; // tested optional feature is not supported
117 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
118 return false;
120 // dispose used storages to free resources
121 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
122 return false;
125 // now check all the written and copied information
128 // the temporary file must not be locked any more after storage disposing
129 pArgs[1] = Integer.valueOf( ElementModes.READ );
130 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
131 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
132 if ( xResultStorage == null )
134 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
135 return false;
138 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, sPass2 ) )
139 return false;
140 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass2 ) )
141 return false;
143 // dispose used storages to free resources
144 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
145 return false;
147 return true;
149 catch( Exception e )
151 m_aTestHelper.Error( "Exception: " + e );
152 return false;