Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Storage / Test07.java
blobc89268817a2100f4d600c8e9f5924b2098a17bf9
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 Test07 implements StorageTest {
27 XMultiServiceFactory m_xMSF;
28 XSingleServiceFactory m_xStorageFactory;
29 TestHelper m_aTestHelper;
31 public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
33 m_xMSF = xMSF;
34 m_xStorageFactory = xStorageFactory;
35 m_aTestHelper = new TestHelper( "Test07: " );
38 public boolean test()
40 try
42 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
43 if ( sTempFileURL == null || sTempFileURL.equals("") )
45 m_aTestHelper.Error( "No valid temporary file was created!" );
46 return false;
49 // create temporary storage based on arbitrary medium
50 // after such a storage is closed it is lost
51 Object oTempStorage = m_xStorageFactory.createInstance();
52 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
53 if ( xTempStorage == null )
55 m_aTestHelper.Error( "Can't create temporary storage representation!" );
56 return false;
59 byte pBytes1[] = { 1, 1, 1, 1, 1 };
60 byte pPass1[] = { 1, 2, 3, 4, 5 };
62 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
63 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, pPass1 ) )
64 return false;
66 byte pBytes2[] = { 2, 2, 2, 2, 2 };
67 byte pPass2[] = { 5, 4, 3, 2, 1 };
69 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
70 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes2, pPass2 ) )
71 return false;
73 // create temporary storage based on a previously created temporary file
74 Object pArgs[] = new Object[2];
75 pArgs[0] = sTempFileURL;
76 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
78 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
79 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
80 if ( xTempFileStorage == null )
82 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
83 return false;
86 // copy xTempStorage to xTempFileStorage
87 // xTempFileStorage will be automatically committed
88 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
89 return false;
91 // dispose used storages to free resources
92 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
93 return false;
96 // now check all the written and copied information
99 // the temporary file must not be locked any more after storage disposing
100 pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
101 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
102 XStorage xResultStorage = UnoRuntime.queryInterface( XStorage.class, oResultStorage );
103 if ( xResultStorage == null )
105 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
106 return false;
109 Object o2CopyStorage = m_xStorageFactory.createInstance();
110 XStorage x2CopyStorage = UnoRuntime.queryInterface( XStorage.class, o2CopyStorage );
111 if ( x2CopyStorage == null )
113 m_aTestHelper.Error( "Can't create temporary storage representation!" );
114 return false;
117 if ( !m_aTestHelper.copyStorage( xResultStorage, x2CopyStorage ) )
118 return false;
120 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
121 return false;
123 if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
124 return false;
126 if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
127 return false;
129 if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream2", "MediaType2", pBytes2, pPass2 ) )
130 return false;
132 // dispose used storages to free resources
133 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
134 return false;
136 return true;
138 catch( Exception e )
140 m_aTestHelper.Error( "Exception: " + e );
141 return false;