Update git submodules
[LibreOffice.git] / odk / examples / java / Storage / Test09.java
blob131ff06d81013c7fd9944e4700c8fbc7a39a8334
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 Test09 implements StorageTest {
28 XMultiServiceFactory m_xMSF;
29 XSingleServiceFactory m_xStorageFactory;
30 TestHelper m_aTestHelper;
32 public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
34 m_xMSF = xMSF;
35 m_xStorageFactory = xStorageFactory;
36 m_aTestHelper = new TestHelper( "Test09: " );
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 byte pPass1[] = { 1, 2, 3 };
55 byte pPass2[] = { 3, 2, 1 };
56 byte pBytes[] = { 1, 1, 1, 1, 1 };
58 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
59 // the stream will not be encrypted
60 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, pPass1 ) )
61 return false;
63 // create temporary file
64 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
65 if ( sTempFileURL == null || sTempFileURL.equals( "" ) )
67 m_aTestHelper.Error( "No valid temporary file was created!" );
68 return false;
71 // create temporary storage based on a previously created temporary file
72 Object pArgs[] = new Object[2];
73 pArgs[0] = sTempFileURL;
74 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
76 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
77 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
78 if ( xTempFileStorage == null )
80 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
81 return false;
84 // copy xTempStorage to xTempFileStorage
85 // xTempFileStorage will be automatically committed
86 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
87 return false;
89 // change password of the substream of new storage based on file
90 int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", pPass1, pPass2 );
91 if ( nResult == 0 )
92 return false; // test failed
93 else if ( nResult == -1 )
94 return true; // tested optional feature is not supported
96 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
97 return false;
99 // dispose used storages to free resources
100 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
101 return false;
104 // now check all the written and copied information
107 // the temporary file must not be locked any more after storage disposing
108 pArgs[1] = Integer.valueOf( ElementModes.READ );
109 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
110 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
111 if ( xResultStorage == null )
113 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
114 return false;
117 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, pPass2 ) )
118 return false;
120 // dispose used storages to free resources
121 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
122 return false;
124 return true;
126 catch( Exception e )
128 m_aTestHelper.Error( "Exception: " + e );
129 return false;
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */