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
;
21 import com
.sun
.star
.uno
.UnoRuntime
;
22 import com
.sun
.star
.container
.XNameAccess
;
24 import com
.sun
.star
.embed
.*;
26 public class Test04
implements StorageTest
{
28 XMultiServiceFactory m_xMSF
;
29 XSingleServiceFactory m_xStorageFactory
;
30 TestHelper m_aTestHelper
;
32 public Test04( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
35 m_xStorageFactory
= xStorageFactory
;
36 m_aTestHelper
= new TestHelper( "Test04: " );
43 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
44 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
46 m_aTestHelper
.Error( "No valid temporary file was created!" );
50 // create temporary storage based on arbitrary medium
51 // after such a storage is closed it is lost
52 Object oTempStorage
= m_xStorageFactory
.createInstance();
53 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
54 if ( xTempStorage
== null )
56 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
60 // open substorages and create streams there
62 // first substorage of the root storage
63 XStorage xTempSubStorage1
= m_aTestHelper
.openSubStorage( xTempStorage
,
66 if ( xTempSubStorage1
== null )
68 m_aTestHelper
.Error( "Can't create substorage!" );
72 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
74 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
75 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
78 // second substorage of the root storage
79 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
82 if ( xTempSubStorage2
== null )
84 m_aTestHelper
.Error( "Can't create substorage!" );
88 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
90 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
98 ElementModes
.READWRITE
) )
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
105 ElementModes
.WRITE
) )
108 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
112 ElementModes
.WRITE
) )
115 // create temporary storage based on a previously created temporary file
116 Object pArgs
[] = new Object
[2];
117 pArgs
[0] = sTempFileURL
;
118 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
120 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
121 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
122 if ( xTempFileStorage
== null )
124 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
128 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
131 // if storage is not committed before disposing all the changes will be lost
132 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
135 // a storage must be disposed before moving/removing otherwise the access will be denied
136 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
139 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
142 // SubStorage2 must be removed and disposed now
145 xTempSubStorage2
.isStreamElement( "SubStream2" );
146 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
149 catch( com
.sun
.star
.lang
.DisposedException de
)
154 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
158 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
161 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
164 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
167 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
170 // dispose used storages to free resources
171 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
175 // now check all the written and copied information
178 // the temporary file must not be locked any more after storage disposing
179 pArgs
[1] = Integer
.valueOf( ElementModes
.READWRITE
);
180 Object oResStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
181 XStorage xResStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResStorage
);
182 if ( xResStorage
== null )
184 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
188 // open and check SubStorage1
189 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
192 if ( xResSubStorage1
== null )
194 m_aTestHelper
.Error( "Can't open existing substorage!" );
198 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
, "MediaType4", false, ElementModes
.READ
) )
202 // open and check SubStorage2
203 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
206 if ( xResSubStorage2
== null )
208 m_aTestHelper
.Error( "Can't open existing substorage!" );
212 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
, "MediaType5", false, ElementModes
.READ
) )
216 // check all the result streams
218 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", pBytes1
) )
221 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", pBytes1
) )
224 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", pBytes1
) )
227 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", pBytes2
) )
230 // the storage must be disposed before removing
231 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
234 // remove element and check that it was removed completelly
235 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
240 XNameAccess xResAccess
= UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
241 if ( xResAccess
.hasByName( "SubStorage2" ) )
242 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
246 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
252 xResSubStorage2
.isStreamElement( "SubStream2" );
254 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
257 catch( com
.sun
.star
.lang
.DisposedException de
)
262 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
266 // dispose used storages to free resources
267 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
274 m_aTestHelper
.Error( "Exception: " + e
);