bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test04.java
blob78af017897ab1a686327ad8e234b0ff3f0be99da
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;
24 import com.sun.star.lang.DisposedException;
26 import com.sun.star.bridge.XUnoUrlResolver;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XInterface;
30 import com.sun.star.container.XNameAccess;
32 import com.sun.star.embed.*;
34 import share.LogWriter;
35 import complex.storages.TestHelper;
36 import complex.storages.StorageTest;
38 public class Test04 implements StorageTest {
40 XMultiServiceFactory m_xMSF;
41 XSingleServiceFactory m_xStorageFactory;
42 TestHelper m_aTestHelper;
44 public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
46 m_xMSF = xMSF;
47 m_xStorageFactory = xStorageFactory;
48 m_aTestHelper = new TestHelper( aLogWriter, "Test04: " );
51 public boolean test()
53 try
55 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
56 if ( sTempFileURL == null || sTempFileURL == "" )
58 m_aTestHelper.Error( "No valid temporary file was created!" );
59 return false;
62 // create temporary storage based on arbitrary medium
63 // after such a storage is closed it is lost
64 Object oTempStorage = m_xStorageFactory.createInstance();
65 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
66 if ( xTempStorage == null )
68 m_aTestHelper.Error( "Can't create temporary storage representation!" );
69 return false;
72 // open substorages and create streams there
74 // first substorage of the root storage
75 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
76 "SubStorage1",
77 ElementModes.WRITE );
78 if ( xTempSubStorage1 == null )
80 m_aTestHelper.Error( "Can't create substorage!" );
81 return false;
84 byte pBigBytes[] = new byte[33000];
85 for ( int nInd = 0; nInd < 33000; nInd++ )
86 pBigBytes[nInd] = (byte)( nInd % 128 );
88 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
90 return false;
92 byte pBytes1[] = { 1, 1, 1, 1, 1 };
94 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
96 return false;
98 // second substorage of the root storage
99 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
100 "SubStorage2",
101 ElementModes.WRITE );
102 if ( xTempSubStorage2 == null )
104 m_aTestHelper.Error( "Can't create substorage!" );
105 return false;
108 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
109 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
110 return false;
112 byte pBytes2[] = { 2, 2, 2, 2, 2 };
114 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
115 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
116 return false;
118 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
119 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
120 "MediaType3",
121 true,
122 ElementModes.WRITE ) )
123 return false;
125 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
126 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
127 "MediaType4",
128 false,
129 ElementModes.WRITE ) )
130 return false;
132 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
133 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
134 "MediaType5",
135 false,
136 ElementModes.WRITE ) )
137 return false;
139 // create temporary storage based on a previously created temporary file
140 Object pArgs[] = new Object[2];
141 pArgs[0] = (Object) sTempFileURL;
142 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
144 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
145 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
146 if ( xTempFileStorage == null )
148 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
149 return false;
152 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
153 return false;
155 // if storage is not committed before disposing all the changes will be lost
156 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
157 return false;
159 // a storage must be disposed before moving/removing otherwise the access will be denied
160 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
161 return false;
163 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
164 return false;
166 // SubStorage2 must be removed and disposed now
169 xTempSubStorage2.isStreamElement( "SubStream2" );
170 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
171 return false;
173 catch( com.sun.star.lang.DisposedException de )
176 catch( Exception e )
178 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
179 return false;
182 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
183 return false;
185 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
186 return false;
188 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
189 return false;
191 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
192 return false;
194 if ( !m_aTestHelper.renameElement( xTempFileStorage, "BigSubStream1", "BigSubStream1_copy" ) )
195 return false;
197 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "BigSubStream1", xTempFileStorage ) )
198 return false;
200 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
201 return false;
203 // dispose used storages to free resources
204 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
205 return false;
208 // now check all the written and copied information
211 // the temporary file must not be locked any more after storage disposing
212 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
213 Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
214 XStorage xResStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResStorage );
215 if ( xResStorage == null )
217 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
218 return false;
221 // open and check SubStorage1
222 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
223 "SubStorage1",
224 ElementModes.READ );
225 if ( xResSubStorage1 == null )
227 m_aTestHelper.Error( "Can't open existing substorage!" );
228 return false;
231 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
232 return false;
235 // open and check SubStorage2
236 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
237 "SubStorage2",
238 ElementModes.READ );
239 if ( xResSubStorage2 == null )
241 m_aTestHelper.Error( "Can't open existing substorage!" );
242 return false;
245 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
246 return false;
249 // check all the result streams
251 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
252 return false;
254 if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
255 return false;
257 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", true, pBytes1 ) )
258 return false;
260 if ( !m_aTestHelper.checkStream( xResStorage, "BigSubStream1_copy", "MediaType1", true, pBigBytes ) )
261 return false;
263 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
264 return false;
266 if ( !m_aTestHelper.checkStream( xResSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
267 return false;
269 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
270 return false;
272 if ( !m_aTestHelper.checkStream( xResSubStorage2, "BigSubStream2", "MediaType2", false, pBigBytes ) )
273 return false;
275 // the storage must be disposed before removing
276 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
277 return false;
279 // remove element and check that it was removed completely
280 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
281 return false;
285 XNameAccess xResAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
286 if ( xResAccess.hasByName( "SubStorage2" ) )
287 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
289 catch( Exception e )
291 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
292 return false;
297 xResSubStorage2.isStreamElement( "SubStream2" );
299 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
300 return false;
302 catch( com.sun.star.lang.DisposedException de )
305 catch( Exception e )
307 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
308 return false;
311 // dispose used storages to free resources
312 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
313 return false;
315 return true;
317 catch( Exception e )
319 m_aTestHelper.Error( "Exception: " + e );
320 return false;