bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test12.java
blob13850daf47cfc0695f339e9405a05bedace88500
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 package complex.storages;
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.lang.XSingleServiceFactory;
25 import com.sun.star.bridge.XUnoUrlResolver;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.io.XStream;
29 import com.sun.star.io.XInputStream;
31 import com.sun.star.embed.*;
33 import share.LogWriter;
34 import complex.storages.TestHelper;
35 import complex.storages.StorageTest;
37 public class Test12 implements StorageTest {
39 XMultiServiceFactory m_xMSF;
40 XSingleServiceFactory m_xStorageFactory;
41 TestHelper m_aTestHelper;
43 public Test12( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 m_xMSF = xMSF;
46 m_xStorageFactory = xStorageFactory;
47 m_aTestHelper = new TestHelper( aLogWriter, "Test12: " );
50 public boolean test()
52 try
54 XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
55 if ( xTempFileStream == null )
56 return false;
58 // create storage based on the temporary stream
59 Object pArgs[] = new Object[2];
60 pArgs[0] = (Object) xTempFileStream;
61 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
63 Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
64 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
65 if ( xTempStorage == null )
67 m_aTestHelper.Error( "Can't create temporary storage representation!" );
68 return false;
71 // open a new substorage
72 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
73 "SubStorage1",
74 ElementModes.WRITE );
75 if ( xTempSubStorage == null )
77 m_aTestHelper.Error( "Can't create substorage!" );
78 return false;
81 byte pBigBytes[] = new byte[33000];
82 for ( int nInd = 0; nInd < 33000; nInd++ )
83 pBigBytes[nInd] = (byte)( nInd % 128 );
85 byte pBytes1[] = { 1, 1, 1, 1, 1 };
87 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
89 return false;
91 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
92 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
93 return false;
95 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
96 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
97 "MediaType2",
98 true,
99 ElementModes.WRITE ) )
100 return false;
102 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
103 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
104 "MediaType3",
105 false,
106 ElementModes.WRITE ) )
107 return false;
109 // commit substorage first
110 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
111 return false;
113 // commit the root storage so the contents must be stored now
114 if ( !m_aTestHelper.commitStorage( xTempStorage ) )
115 return false;
117 // dispose substorage
118 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
119 return false;
122 // check substorage
125 if ( !checkSubStorages( xTempStorage, pBytes1, pBigBytes ) )
126 return false;
128 // dispose used storage to free resources
129 if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
130 return false;
133 // now check all the written information with readwrite access
136 Object oResWriteStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
137 XStorage xResWriteStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResWriteStorage );
138 if ( xResWriteStorage == null )
140 m_aTestHelper.Error( "Can't open storage based on input stream!" );
141 return false;
144 if ( !m_aTestHelper.checkStorageProperties( xResWriteStorage, "MediaType2", true, ElementModes.WRITE ) )
145 return false;
147 if( !checkSubStorages( xResWriteStorage, pBytes1, pBigBytes ) )
148 return false;
150 // try to open for writing after opening for reading
151 XStorage xResWSubStorage = m_aTestHelper.openSubStorage( xResWriteStorage,
152 "SubStorage1",
153 ElementModes.WRITE );
154 if ( xResWSubStorage == null )
156 m_aTestHelper.Error( "Can't open substorage for writing after it was opened for reading!" );
157 return false;
160 if ( !m_aTestHelper.checkStorageProperties( xResWSubStorage, "MediaType3", false, ElementModes.WRITE ) )
161 return false;
163 if ( !m_aTestHelper.checkStream( xResWSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
164 return false;
166 if ( !m_aTestHelper.checkStream( xResWSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
167 return false;
169 // dispose used storage to free resources
170 if ( !m_aTestHelper.disposeStorage( xResWriteStorage ) )
171 return false;
175 // now check all the written information with readonly access
178 // close the output part of the temporary stream
179 // the output part must present since we already wrote to the stream
180 if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
181 return false;
183 XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
184 if ( xTempInStream == null )
185 return false;
187 // open input stream
188 // since no mode is provided the result storage must be opened readonly
189 Object pOneArg[] = new Object[1];
190 pOneArg[0] = (Object) xTempInStream;
192 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
193 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
194 if ( xResultStorage == null )
196 m_aTestHelper.Error( "Can't open storage based on input stream!" );
197 return false;
200 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.READ ) )
201 return false;
203 if( !checkSubStorages( xResultStorage, pBytes1, pBigBytes ) )
204 return false;
206 return true;
208 catch( Exception e )
210 m_aTestHelper.Error( "Exception: " + e );
211 return false;
215 private boolean checkSubStorages( XStorage xStorage, byte[] pBytes1, byte[] pBigBytes )
217 XStorage xReadSubStorage1 = m_aTestHelper.openSubStorage( xStorage,
218 "SubStorage1",
219 ElementModes.READ );
221 XStorage xReadSubStorage2 = m_aTestHelper.openSubStorage( xStorage,
222 "SubStorage1",
223 ElementModes.READ );
225 if ( xReadSubStorage1 == null || xReadSubStorage2 == null )
227 m_aTestHelper.Error( "Can't open substorage for reading!" );
228 return false;
231 if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage1, "MediaType3", false, ElementModes.READ ) )
232 return false;
234 if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage2, "MediaType3", false, ElementModes.READ ) )
235 return false;
237 if ( !m_aTestHelper.checkStream( xReadSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
238 return false;
240 if ( !m_aTestHelper.checkStream( xReadSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
241 return false;
243 if ( !m_aTestHelper.checkStream( xReadSubStorage2, "SubStream1", "MediaType1", true, pBytes1 ) )
244 return false;
246 if ( !m_aTestHelper.checkStream( xReadSubStorage2, "BigSubStream1", "MediaType1", true, pBigBytes ) )
247 return false;
249 if ( !m_aTestHelper.disposeStorage( xReadSubStorage1 ) )
250 return false;
252 if ( !m_aTestHelper.disposeStorage( xReadSubStorage2 ) )
253 return false;
255 return true;