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
.ofopxmlstorages
;
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
.*;
33 import com
.sun
.star
.beans
.StringPair
;
35 import share
.LogWriter
;
36 import complex
.ofopxmlstorages
.TestHelper
;
37 import complex
.ofopxmlstorages
.StorageTest
;
39 public class Test04
implements StorageTest
{
41 XMultiServiceFactory m_xMSF
;
42 XSingleServiceFactory m_xStorageFactory
;
43 TestHelper m_aTestHelper
;
45 public Test04( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
48 m_xStorageFactory
= xStorageFactory
;
49 m_aTestHelper
= new TestHelper( aLogWriter
, "Test04: " );
54 StringPair
[][] aRelations1
=
55 { { new StringPair( "Id", "Num1" ) },
56 { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
57 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
58 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
59 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
60 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
61 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
64 StringPair
[][] aRelations2
=
65 { { new StringPair( "Id", "Num1" ) },
66 { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
67 { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
68 { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
69 { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
70 { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
71 { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
76 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
77 if ( sTempFileURL
== null || sTempFileURL
== "" )
79 m_aTestHelper
.Error( "No valid temporary file was created!" );
83 // create temporary storage based on arbitrary medium
84 // after such a storage is closed it is lost
85 XStorage xTempStorage
= m_aTestHelper
.createTempStorage( m_xMSF
, m_xStorageFactory
);
86 if ( xTempStorage
== null )
88 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
92 // open substorages and create streams there
94 // first substorage of the root storage
95 XStorage xTempSubStorage1
= m_aTestHelper
.openSubStorage( xTempStorage
,
98 if ( xTempSubStorage1
== null )
100 m_aTestHelper
.Error( "Can't create substorage!" );
104 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
106 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
107 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage1
,
115 // second substorage of the root storage
116 XStorage xTempSubStorage2
= m_aTestHelper
.openSubStorage( xTempStorage
,
118 ElementModes
.WRITE
);
119 if ( xTempSubStorage2
== null )
121 m_aTestHelper
.Error( "Can't create substorage!" );
125 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
127 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
128 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage2
,
136 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
137 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
143 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
144 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage1
,
150 // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
151 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage2
,
157 // create temporary storage based on a previously created temporary file
158 XStorage xTempFileStorage
= m_aTestHelper
.createStorageFromURL( m_xStorageFactory
,
160 ElementModes
.WRITE
);
161 if ( xTempFileStorage
== null )
163 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
167 if ( !m_aTestHelper
.copyElementTo( xTempStorage
, "SubStorage1", xTempFileStorage
) )
170 // if storage is not committed before disposing all the changes will be lost
171 if ( !m_aTestHelper
.commitStorage( xTempSubStorage2
) )
174 // a storage must be disposed before moving/removing otherwise the access will be denied
175 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage2
) )
178 if ( !m_aTestHelper
.moveElementTo( xTempStorage
, "SubStorage2", xTempFileStorage
) )
181 // SubStorage2 must be removed and disposed now
184 xTempSubStorage2
.isStreamElement( "SubStream2" );
185 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
188 catch( com
.sun
.star
.lang
.DisposedException de
)
193 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
197 if ( !m_aTestHelper
.copyElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
200 if ( !m_aTestHelper
.renameElement( xTempFileStorage
, "SubStream1", "SubStream1_copy" ) )
203 if ( !m_aTestHelper
.moveElementTo( xTempSubStorage1
, "SubStream1", xTempFileStorage
) )
206 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
209 // dispose used storages to free resources
210 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
214 // now check all the written and copied information
217 // the temporary file must not be locked any more after storage disposing
218 XStorage xResStorage
= m_aTestHelper
.createStorageFromURL( m_xStorageFactory
,
220 ElementModes
.WRITE
);
222 if ( xResStorage
== null )
224 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
228 // open and check SubStorage1
229 XStorage xResSubStorage1
= m_aTestHelper
.openSubStorage( xResStorage
,
232 if ( xResSubStorage1
== null )
234 m_aTestHelper
.Error( "Can't open existing substorage!" );
238 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage1
,
245 // open and check SubStorage2
246 XStorage xResSubStorage2
= m_aTestHelper
.openSubStorage( xResStorage
,
249 if ( xResSubStorage2
== null )
251 m_aTestHelper
.Error( "Can't open existing substorage!" );
255 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage2
,
262 // check all the result streams
264 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1", "MediaType1", pBytes1
, aRelations1
) )
267 if ( !m_aTestHelper
.checkStream( xResStorage
, "SubStream1_copy", "MediaType1", pBytes1
, aRelations1
) )
270 if ( !m_aTestHelper
.checkStream( xResSubStorage1
, "SubStream1", "MediaType1", pBytes1
, aRelations1
) )
273 if ( !m_aTestHelper
.checkStream( xResSubStorage2
, "SubStream2", "MediaType2", pBytes2
, aRelations2
) )
276 // the storage must be disposed before removing
277 if ( !m_aTestHelper
.disposeStorage( xResSubStorage2
) )
280 // remove element and check that it was removed completely
281 if ( !m_aTestHelper
.removeElement( xResStorage
, "SubStorage2" ) )
286 XNameAccess xResAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xResStorage
);
287 if ( xResAccess
.hasByName( "SubStorage2" ) )
288 m_aTestHelper
.Error( "SubStorage2 must be removed already!" );
292 m_aTestHelper
.Error( "Can't get access to root storage, exception: " + e
);
298 xResSubStorage2
.isStreamElement( "SubStream2" );
300 m_aTestHelper
.Error( "SubStorage2 must be disposed already!" );
303 catch( com
.sun
.star
.lang
.DisposedException de
)
308 m_aTestHelper
.Error( "Wrong exception in case of disposed storage, exception: " + e
);
312 // dispose used storages to free resources
313 if ( !m_aTestHelper
.disposeStorage( xResStorage
) )
320 m_aTestHelper
.Error( "Exception: " + e
);