Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Storage / Test09.java
blob5e59562c6eb09c03184396d3e0474d1e44fd8864
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 Test09 implements StorageTest {
27 XMultiServiceFactory m_xMSF;
28 XSingleServiceFactory m_xStorageFactory;
29 TestHelper m_aTestHelper;
31 public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
33 m_xMSF = xMSF;
34 m_xStorageFactory = xStorageFactory;
35 m_aTestHelper = new TestHelper( "Test09: " );
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 byte pPass1[] = { 1, 2, 3 };
54 byte pPass2[] = { 3, 2, 1 };
55 byte pBytes[] = { 1, 1, 1, 1, 1 };
57 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
58 // the stream will not be encrypted
59 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, pPass1 ) )
60 return false;
62 // create temporary file
63 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
64 if ( sTempFileURL == null || sTempFileURL.equals( "" ) )
66 m_aTestHelper.Error( "No valid temporary file was created!" );
67 return false;
70 // create temporary storage based on a previously created temporary file
71 Object pArgs[] = new Object[2];
72 pArgs[0] = sTempFileURL;
73 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
75 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
76 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
77 if ( xTempFileStorage == null )
79 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
80 return false;
83 // copy xTempStorage to xTempFileStorage
84 // xTempFileStorage will be automatically committed
85 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
86 return false;
88 // change password of the substream of new storage based on file
89 int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", pPass1, pPass2 );
90 if ( nResult == 0 )
91 return false; // test failed
92 else if ( nResult == -1 )
93 return true; // tested optional feature is not supported
95 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
96 return false;
98 // dispose used storages to free resources
99 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
100 return false;
103 // now check all the written and copied information
106 // the temporary file must not be locked any more after storage disposing
107 pArgs[1] = Integer.valueOf( ElementModes.READ );
108 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
109 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
110 if ( xResultStorage == null )
112 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
113 return false;
116 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, pPass2 ) )
117 return false;
119 // dispose used storages to free resources
120 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
121 return false;
123 return true;
125 catch( Exception e )
127 m_aTestHelper.Error( "Exception: " + e );
128 return false;