Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Storage / Test08.java
blob43e9423781e20b641a818aa5f7e8ecfc76ea1708
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 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 )
33 m_xMSF = xMSF;
34 m_xStorageFactory = xStorageFactory;
35 m_aTestHelper = new TestHelper( "Test08: " );
38 public boolean test()
40 try
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!" );
50 return false;
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!" );
60 return true;
63 byte pPass1[] = { 1, 2, 3 };
64 byte pPass2[] = { 3, 2, 1 };
66 try {
67 xTempStorageEncryption.setEncryptionPassword( new String(pPass1) );
69 catch( Exception e )
71 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
72 return false;
75 // open a new substorage
76 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
77 "SubStorage1",
78 ElementModes.WRITE );
79 if ( xTempSubStorage == null )
81 m_aTestHelper.Error( "Can't create substorage!" );
82 return false;
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 ) )
89 return false;
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 ) )
95 return 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 ) )
104 return false;
106 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
107 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
108 "MediaType4",
109 true,
110 ElementModes.READWRITE ) )
111 return false;
113 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
114 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
115 "MediaType5",
116 false,
117 ElementModes.WRITE ) )
118 return false;
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!" );
125 return false;
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!" );
138 return false;
141 // copy xTempStorage to xTempFileStorage
142 // xTempFileStorage will be automatically committed
143 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
144 return false;
146 // dispose used storages to free resources
147 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
148 return false;
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!" );
161 return false;
164 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) )
165 return false;
167 // open existing substorage
168 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
169 "SubStorage1",
170 ElementModes.READ );
171 if ( xResultSubStorage == null )
173 m_aTestHelper.Error( "Can't open existing substorage!" );
174 return false;
177 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) )
178 return false;
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!" );
187 return false;
190 try {
191 xResultStorageEncryption.setEncryptionPassword( new String(pPass2) );
193 catch( Exception e )
195 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
196 return false;
199 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
200 return false;
202 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
203 return false;
205 // the common root storage password should allow to open this stream
206 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", pBytes3 ) )
207 return false;
209 // dispose used storages to free resources
210 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
211 return false;
213 return true;
215 catch( Exception e )
217 m_aTestHelper.Error( "Exception: " + e );
218 return false;