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 import com
.sun
.star
.lang
.XMultiServiceFactory
;
20 import com
.sun
.star
.lang
.XSingleServiceFactory
;
22 import com
.sun
.star
.uno
.UnoRuntime
;
23 import com
.sun
.star
.embed
.*;
25 public class Test08
implements StorageTest
{
27 XMultiServiceFactory m_xMSF
;
28 XSingleServiceFactory m_xStorageFactory
;
29 TestHelper m_aTestHelper
;
31 public Test08( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
34 m_xStorageFactory
= xStorageFactory
;
35 m_aTestHelper
= new TestHelper( "Test08: " );
43 // create temporary storage based on arbitrary medium
44 // after such a storage is closed it is lost
45 Object oTempStorage
= m_xStorageFactory
.createInstance();
46 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
47 if ( xTempStorage
== null )
49 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
53 // set the global password for the root storage
54 XEncryptionProtectedSource xTempStorageEncryption
=
55 UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xTempStorage
);
57 if ( xTempStorageEncryption
== null )
59 m_aTestHelper
.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
63 byte pPass1
[] = { 1, 2, 3 };
64 byte pPass2
[] = { 3, 2, 1 };
67 xTempStorageEncryption
.setEncryptionPassword( new String(pPass1
) );
71 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
75 // open a new substorage
76 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
79 if ( xTempSubStorage
== null )
81 m_aTestHelper
.Error( "Can't create substorage!" );
85 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
86 // the stream will be encrypted with common password
87 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
88 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
, true ) )
91 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
92 // the stream will not be encrypted
93 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
94 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
, false ) )
97 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 // the stream will be compressed with own password
99 byte pBytes3
[] = { 3, 3, 3, 3, 3 };
101 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102 // the stream will not be encrypted
103 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "SubStream3", "MediaType3", false, pBytes3
, pPass2
) )
106 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
107 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
110 ElementModes
.READWRITE
) )
113 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
114 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
117 ElementModes
.WRITE
) )
120 // create temporary file
121 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
122 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
124 m_aTestHelper
.Error( "No valid temporary file was created!" );
128 // create temporary storage based on a previously created temporary file
129 Object pArgs
[] = new Object
[2];
130 pArgs
[0] = sTempFileURL
;
131 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
133 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
134 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
135 if ( xTempFileStorage
== null )
137 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
141 // copy xTempStorage to xTempFileStorage
142 // xTempFileStorage will be automatically committed
143 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
146 // dispose used storages to free resources
147 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
151 // now check all the written and copied information
154 // the temporary file must not be locked any more after storage disposing
155 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
156 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
157 XStorage xResultStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
158 if ( xResultStorage
== null )
160 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
164 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType4", true, ElementModes
.READ
) )
167 // open existing substorage
168 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
171 if ( xResultSubStorage
== null )
173 m_aTestHelper
.Error( "Can't open existing substorage!" );
177 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType5", false, ElementModes
.READ
) )
180 // set the global password for the root storage
181 XEncryptionProtectedSource xResultStorageEncryption
=
182 UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xResultStorage
);
184 if ( xResultStorageEncryption
== null )
186 m_aTestHelper
.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
191 xResultStorageEncryption
.setEncryptionPassword( new String(pPass2
) );
195 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
199 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
, pPass1
) )
202 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
205 // the common root storage password should allow to open this stream
206 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream3", "MediaType3", pBytes3
) )
209 // dispose used storages to free resources
210 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
217 m_aTestHelper
.Error( "Exception: " + e
);