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
)
47 m_xStorageFactory
= xStorageFactory
;
48 m_aTestHelper
= new TestHelper( aLogWriter
, "Test04: " );
55 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
56 if ( sTempFileURL
== null || sTempFileURL
== "" )
58 m_aTestHelper
.Error( "No valid temporary file was created!" );
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!" );
72 // open substorages and create streams there
74 // first substorage of the root storage
75 XStorage xTempSubStorage1
= m_aTestHelper
.openSubStorage( xTempStorage
,
78 if ( xTempSubStorage1
== null )
80 m_aTestHelper
.Error( "Can't create substorage!" );
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
) )
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
) )
98 // second substorage of the root storage
99 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
101 ElementModes
.WRITE
);
102 if ( xTempSubStorage2
== null )
104 m_aTestHelper
.Error( "Can't create substorage!" );
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
) )
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
) )
118 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
119 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
122 ElementModes
.WRITE
) )
125 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
126 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
129 ElementModes
.WRITE
) )
132 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
133 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
136 ElementModes
.WRITE
) )
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!" );
152 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
155 // if storage is not committed before disposing all the changes will be lost
156 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
159 // a storage must be disposed before moving/removing otherwise the access will be denied
160 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
163 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
166 // SubStorage2 must be removed and disposed now
169 xTempSubStorage2
.isStreamElement( "SubStream2" );
170 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
173 catch( com
.sun
.star
.lang
.DisposedException de
)
178 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
182 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
185 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
188 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
191 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "BigSubStream1", xTempFileStorage
) )
194 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "BigSubStream1", "BigSubStream1_copy" ) )
197 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "BigSubStream1", xTempFileStorage
) )
200 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
203 // dispose used storages to free resources
204 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
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!" );
221 // open and check SubStorage1
222 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
225 if ( xResSubStorage1
== null )
227 m_aTestHelper
.Error( "Can't open existing substorage!" );
231 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
, "MediaType4", false, ElementModes
.READ
) )
235 // open and check SubStorage2
236 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
239 if ( xResSubStorage2
== null )
241 m_aTestHelper
.Error( "Can't open existing substorage!" );
245 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
, "MediaType5", false, ElementModes
.READ
) )
249 // check all the result streams
251 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
254 if ( !m_aTestHelper
.checkStream( xResStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
257 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", true, pBytes1
) )
260 if ( !m_aTestHelper
.checkStream( xResStorage
, "BigSubStream1_copy", "MediaType1", true, pBigBytes
) )
263 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
266 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
269 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
272 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
275 // the storage must be disposed before removing
276 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
279 // remove element and check that it was removed completely
280 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
285 XNameAccess xResAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
286 if ( xResAccess
.hasByName( "SubStorage2" ) )
287 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
291 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
297 xResSubStorage2
.isStreamElement( "SubStream2" );
299 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
302 catch( com
.sun
.star
.lang
.DisposedException de
)
307 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
311 // dispose used storages to free resources
312 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
319 m_aTestHelper
.Error( "Exception: " + e
);