tdf#130857 qt weld: Introduce QtInstanceScrolledWindow
[LibreOffice.git] / odk / examples / java / Storage / Test08.java
blob323e2d78cb9d361293c614b818aafb8a401a9745
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 )
34 m_xMSF = xMSF;
35 m_xStorageFactory = xStorageFactory;
36 m_aTestHelper = new TestHelper( "Test08: " );
39 public boolean test()
41 try
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!" );
51 return false;
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!" );
61 return true;
64 byte pPass1[] = { 1, 2, 3 };
65 byte pPass2[] = { 3, 2, 1 };
67 try {
68 xTempStorageEncryption.setEncryptionPassword( new String(pPass1) );
70 catch( Exception e )
72 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
73 return false;
76 // open a new substorage
77 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
78 "SubStorage1",
79 ElementModes.WRITE );
80 if ( xTempSubStorage == null )
82 m_aTestHelper.Error( "Can't create substorage!" );
83 return false;
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 ) )
90 return false;
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 ) )
96 return 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 ) )
105 return false;
107 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
108 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
109 "MediaType4",
110 true,
111 ElementModes.READWRITE ) )
112 return false;
114 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
115 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
116 "MediaType5",
117 false,
118 ElementModes.WRITE ) )
119 return false;
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!" );
126 return false;
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!" );
139 return false;
142 // copy xTempStorage to xTempFileStorage
143 // xTempFileStorage will be automatically committed
144 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
145 return false;
147 // dispose used storages to free resources
148 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
149 return false;
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!" );
162 return false;
165 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) )
166 return false;
168 // open existing substorage
169 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
170 "SubStorage1",
171 ElementModes.READ );
172 if ( xResultSubStorage == null )
174 m_aTestHelper.Error( "Can't open existing substorage!" );
175 return false;
178 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) )
179 return false;
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!" );
188 return false;
191 try {
192 xResultStorageEncryption.setEncryptionPassword( new String(pPass2) );
194 catch( Exception e )
196 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
197 return false;
200 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
201 return false;
203 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
204 return false;
206 // the common root storage password should allow to open this stream
207 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", pBytes3 ) )
208 return false;
210 // dispose used storages to free resources
211 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
212 return false;
214 return true;
216 catch( Exception e )
218 m_aTestHelper.Error( "Exception: " + e );
219 return false;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */