1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.lang
.XMultiServiceFactory
;
21 import com
.sun
.star
.lang
.XSingleServiceFactory
;
22 import com
.sun
.star
.uno
.UnoRuntime
;
23 import com
.sun
.star
.container
.XNameAccess
;
25 import com
.sun
.star
.embed
.*;
27 public class Test04
implements StorageTest
{
29 XMultiServiceFactory m_xMSF
;
30 XSingleServiceFactory m_xStorageFactory
;
31 TestHelper m_aTestHelper
;
33 public Test04( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
36 m_xStorageFactory
= xStorageFactory
;
37 m_aTestHelper
= new TestHelper( "Test04: " );
44 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
45 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
47 m_aTestHelper
.Error( "No valid temporary file was created!" );
51 // create temporary storage based on arbitrary medium
52 // after such a storage is closed it is lost
53 Object oTempStorage
= m_xStorageFactory
.createInstance();
54 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
55 if ( xTempStorage
== null )
57 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
61 // open substorages and create streams there
63 // first substorage of the root storage
64 XStorage xTempSubStorage1
= m_aTestHelper
.openSubStorage( xTempStorage
,
67 if ( xTempSubStorage1
== null )
69 m_aTestHelper
.Error( "Can't create substorage!" );
73 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
75 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
, "SubStream1", "MediaType1", true, pBytes1
) )
79 // second substorage of the root storage
80 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
83 if ( xTempSubStorage2
== null )
85 m_aTestHelper
.Error( "Can't create substorage!" );
89 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
91 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
92 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
, "SubStream2", "MediaType2", false, pBytes2
) )
95 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
96 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
99 ElementModes
.READWRITE
) )
102 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
103 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
106 ElementModes
.WRITE
) )
109 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
110 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
113 ElementModes
.WRITE
) )
116 // create temporary storage based on a previously created temporary file
117 Object pArgs
[] = new Object
[2];
118 pArgs
[0] = sTempFileURL
;
119 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
121 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
122 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
123 if ( xTempFileStorage
== null )
125 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
129 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
132 // if storage is not committed before disposing all the changes will be lost
133 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
136 // a storage must be disposed before moving/removing otherwise the access will be denied
137 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
140 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
143 // SubStorage2 must be removed and disposed now
146 xTempSubStorage2
.isStreamElement( "SubStream2" );
147 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
150 catch( com
.sun
.star
.lang
.DisposedException de
)
155 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
159 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
162 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
165 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
168 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
171 // dispose used storages to free resources
172 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
176 // now check all the written and copied information
179 // the temporary file must not be locked any more after storage disposing
180 pArgs
[1] = Integer
.valueOf( ElementModes
.READWRITE
);
181 Object oResStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
182 XStorage xResStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResStorage
);
183 if ( xResStorage
== null )
185 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
189 // open and check SubStorage1
190 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
193 if ( xResSubStorage1
== null )
195 m_aTestHelper
.Error( "Can't open existing substorage!" );
199 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
, "MediaType4", false, ElementModes
.READ
) )
203 // open and check SubStorage2
204 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
207 if ( xResSubStorage2
== null )
209 m_aTestHelper
.Error( "Can't open existing substorage!" );
213 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
, "MediaType5", false, ElementModes
.READ
) )
217 // check all the result streams
219 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", pBytes1
) )
222 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", pBytes1
) )
225 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", pBytes1
) )
228 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", pBytes2
) )
231 // the storage must be disposed before removing
232 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
235 // remove element and check that it was removed completely
236 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
241 XNameAccess xResAccess
= UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
242 if ( xResAccess
.hasByName( "SubStorage2" ) )
243 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
247 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
253 xResSubStorage2
.isStreamElement( "SubStream2" );
255 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
258 catch( com
.sun
.star
.lang
.DisposedException de
)
263 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
267 // dispose used storages to free resources
268 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
275 m_aTestHelper
.Error( "Exception: " + e
);
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */