1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.lang
.XMultiServiceFactory
;
21 import com
.sun
.star
.lang
.XSingleServiceFactory
;
23 import com
.sun
.star
.uno
.UnoRuntime
;
24 import com
.sun
.star
.embed
.*;
26 public class Test08
implements StorageTest
{
28 XMultiServiceFactory m_xMSF
;
29 XSingleServiceFactory m_xStorageFactory
;
30 TestHelper m_aTestHelper
;
32 public Test08( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
35 m_xStorageFactory
= xStorageFactory
;
36 m_aTestHelper
= new TestHelper( "Test08: " );
44 // create temporary storage based on arbitrary medium
45 // after such a storage is closed it is lost
46 Object oTempStorage
= m_xStorageFactory
.createInstance();
47 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
48 if ( xTempStorage
== null )
50 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
54 // set the global password for the root storage
55 XEncryptionProtectedSource xTempStorageEncryption
=
56 UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xTempStorage
);
58 if ( xTempStorageEncryption
== null )
60 m_aTestHelper
.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
64 byte pPass1
[] = { 1, 2, 3 };
65 byte pPass2
[] = { 3, 2, 1 };
68 xTempStorageEncryption
.setEncryptionPassword( new String(pPass1
) );
72 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
76 // open a new substorage
77 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
80 if ( xTempSubStorage
== null )
82 m_aTestHelper
.Error( "Can't create substorage!" );
86 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
87 // the stream will be encrypted with common password
88 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
89 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
, true ) )
92 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
93 // the stream will not be encrypted
94 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
95 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
, false ) )
98 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
99 // the stream will be compressed with own password
100 byte pBytes3
[] = { 3, 3, 3, 3, 3 };
102 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
103 // the stream will not be encrypted
104 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "SubStream3", "MediaType3", false, pBytes3
, pPass2
) )
107 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
108 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
111 ElementModes
.READWRITE
) )
114 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
115 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
118 ElementModes
.WRITE
) )
121 // create temporary file
122 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
123 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
125 m_aTestHelper
.Error( "No valid temporary file was created!" );
129 // create temporary storage based on a previously created temporary file
130 Object pArgs
[] = new Object
[2];
131 pArgs
[0] = sTempFileURL
;
132 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
134 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
135 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
136 if ( xTempFileStorage
== null )
138 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
142 // copy xTempStorage to xTempFileStorage
143 // xTempFileStorage will be automatically committed
144 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
147 // dispose used storages to free resources
148 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
152 // now check all the written and copied information
155 // the temporary file must not be locked any more after storage disposing
156 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
157 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
158 XStorage xResultStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
159 if ( xResultStorage
== null )
161 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
165 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType4", true, ElementModes
.READ
) )
168 // open existing substorage
169 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
172 if ( xResultSubStorage
== null )
174 m_aTestHelper
.Error( "Can't open existing substorage!" );
178 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType5", false, ElementModes
.READ
) )
181 // set the global password for the root storage
182 XEncryptionProtectedSource xResultStorageEncryption
=
183 UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xResultStorage
);
185 if ( xResultStorageEncryption
== null )
187 m_aTestHelper
.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
192 xResultStorageEncryption
.setEncryptionPassword( new String(pPass2
) );
196 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
200 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
, pPass1
) )
203 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
206 // the common root storage password should allow to open this stream
207 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream3", "MediaType3", pBytes3
) )
210 // dispose used storages to free resources
211 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
218 m_aTestHelper
.Error( "Exception: " + e
);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */