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 Test15
implements StorageTest
{
37 XMultiServiceFactory m_xMSF
;
38 XSingleServiceFactory m_xStorageFactory
;
39 TestHelper m_aTestHelper
;
41 public Test15( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
44 m_xStorageFactory
= xStorageFactory
;
45 m_aTestHelper
= new TestHelper( aLogWriter
, "Test15: " );
50 String aStreamPrefix
= "";
51 for ( int nInd
= 0; nInd
< 4; ++nInd
, aStreamPrefix
+= "SubStorage" + nInd
)
52 if ( !testForPath( aStreamPrefix
) )
58 public boolean testForPath( String aStreamPrefix
)
62 String aSubStream1Path
= aStreamPrefix
+ "SubStream1";
63 String aSubStream2Path
= aStreamPrefix
+ "SubStream2";
64 String aSubStream3Path
= aStreamPrefix
+ "SubStream3";
65 String aSubStream4Path
= aStreamPrefix
+ "SubStream4";
67 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
68 if ( sTempFileURL
== null || sTempFileURL
== "" )
70 m_aTestHelper
.Error( "No valid temporary file was created!" );
74 // create temporary storage based on a previously created temporary file
75 Object pArgs
[] = new Object
[2];
76 pArgs
[0] = (Object
) sTempFileURL
;
77 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
79 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
80 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
81 if ( xTempFileStorage
== null )
83 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
87 // set the global password for the root storage
88 XEncryptionProtectedSource xTempStorageEncryption
=
89 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xTempFileStorage
);
91 if ( xTempStorageEncryption
== null )
93 m_aTestHelper
.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
97 String sPass1
= "12345";
98 String sPass2
= "54321";
101 xTempStorageEncryption
.setEncryptionPassword( sPass1
);
105 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
110 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
111 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
113 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
115 if ( !m_aTestHelper
.WBToSubstrOfEncrH( xTempFileStorage
, aSubStream1Path
, "MediaType1", true, pBytes1
, true, true ) )
118 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
120 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", false, pBytes2
, sPass2
, true ) )
123 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
125 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType3", false, pBytes2
, sPass2
, true ) )
128 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
130 if ( !m_aTestHelper
.WBToSubstrOfEncrH( xTempFileStorage
, aSubStream4Path
, "MediaType2", true, pBytes1
, true, false ) )
134 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
135 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
138 ElementModes
.WRITE
) )
141 // commit the root storage so the contents must be stored now
142 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
145 // dispose used storages to free resources
146 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
150 // now reopen the storage,
151 // check all the written and copied information
155 // the temporary file must not be locked any more after storage disposing
156 oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
157 xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
158 if ( xTempFileStorage
== null )
160 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
164 // set the global password for the root storage
165 xTempStorageEncryption
=
166 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xTempFileStorage
);
168 if ( xTempStorageEncryption
== null )
170 m_aTestHelper
.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
175 xTempStorageEncryption
.setEncryptionPassword( sPass2
);
179 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
184 if ( !m_aTestHelper
.checkStorageProperties( xTempFileStorage
, "MediaType3", true, ElementModes
.WRITE
) )
187 if ( !m_aTestHelper
.checkEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType1", pBytes1
, sPass1
) )
190 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", true, pBytes2
) )
193 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType3", true, pBytes2
) )
196 if ( !m_aTestHelper
.cantOpenEncrStreamH( xTempFileStorage
, aSubStream4Path
, ElementModes
.READ
, sPass1
) )
199 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
201 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType4", true, pBytes2
, sPass1
, true ) )
204 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
206 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType5", true, pBytes1
, true ) )
209 // change the password of the existing stream
210 if ( m_aTestHelper
.ChangeStreamPassH( xTempFileStorage
, aSubStream2Path
, sPass2
, sPass1
, true ) != 1 )
213 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
215 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType5", true, pBytes1
, false ) )
218 // commit the root storage so the contents must be stored now
219 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
222 // dispose used storages to free resources
223 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
227 // now reopen the storage,
228 // check all the written information
231 // the temporary file must not be locked any more after storage disposing
232 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
233 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
234 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
235 if ( xResultStorage
== null )
237 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
241 // set the global password for the root storage
242 xTempStorageEncryption
=
243 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xResultStorage
);
245 if ( xTempStorageEncryption
== null )
247 m_aTestHelper
.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
252 xTempStorageEncryption
.setEncryptionPassword( sPass1
);
256 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
260 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
263 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aSubStream1Path
, "MediaType4", true, pBytes2
) )
266 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aSubStream2Path
, "MediaType5", true, pBytes1
) )
269 if ( !m_aTestHelper
.checkEncrStreamH( xResultStorage
, aSubStream3Path
, "MediaType3", pBytes2
, sPass2
) )
272 // dispose used storages to free resources
273 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
280 m_aTestHelper
.Error( "Exception: " + e
);