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
;
25 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
27 import com
.sun
.star
.uno
.XInterface
;
29 import com
.sun
.star
.embed
.*;
31 import share
.LogWriter
;
32 import complex
.storages
.TestHelper
;
33 import complex
.storages
.StorageTest
;
35 public class Test14
implements StorageTest
{
37 XMultiServiceFactory m_xMSF
;
38 XSingleServiceFactory m_xStorageFactory
;
39 TestHelper m_aTestHelper
;
41 public Test14( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
44 m_xStorageFactory
= xStorageFactory
;
45 m_aTestHelper
= new TestHelper( aLogWriter
, "Test14: " );
50 String aStreamPrefix
= "";
51 for ( int nInd
= 0; nInd
< 4; ++nInd
, aStreamPrefix
+= "SubStorage" + nInd
)
52 if ( !testForPath( aStreamPrefix
) )
58 public boolean testForPath( String aStreamPrefix
)
62 String aSubStream1Path
= aStreamPrefix
+ "SubStream1";
63 String aSubStream2Path
= aStreamPrefix
+ "SubStream2";
64 String aSubStream3Path
= aStreamPrefix
+ "SubStream3";
66 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
67 if ( sTempFileURL
== null || sTempFileURL
== "" )
69 m_aTestHelper
.Error( "No valid temporary file was created!" );
73 // create temporary storage based on a previously created temporary file
74 Object pArgs
[] = new Object
[2];
75 pArgs
[0] = (Object
) sTempFileURL
;
76 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
78 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
79 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
80 if ( xTempFileStorage
== null )
82 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
86 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
87 String sPass1
= "12345";
89 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
91 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType1", true, pBytes1
, sPass1
, true ) )
94 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
95 String sPass2
= "54321";
97 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
99 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", false, pBytes2
, sPass2
, true ) )
102 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
104 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType2", false, pBytes2
, sPass2
, false ) )
107 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
108 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
111 ElementModes
.WRITE
) )
114 // commit the root storage so the contents must be stored now
115 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
118 // dispose used storages to free resources
119 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
123 // now reopen the storage,
124 // check all the written and copied information
128 // the temporary file must not be locked any more after storage disposing
129 oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
130 xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
131 if ( xTempFileStorage
== null )
133 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
137 if ( !m_aTestHelper
.checkStorageProperties( xTempFileStorage
, "MediaType3", true, ElementModes
.WRITE
) )
140 if ( !m_aTestHelper
.checkEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType1", pBytes1
, sPass1
) )
143 if ( !m_aTestHelper
.checkEncrStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", pBytes2
, sPass2
) )
146 if ( !m_aTestHelper
.cantOpenEncrStreamH( xTempFileStorage
, aSubStream3Path
, ElementModes
.READ
, sPass2
) )
149 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
151 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType3", true, pBytes2
, sPass1
, true ) )
154 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
156 if ( !m_aTestHelper
.WriteBytesToEncrStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType3", true, pBytes1
, sPass2
, false ) )
159 // commit the root storage so the contents must be stored now
160 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
163 // dispose used storages to free resources
164 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
168 // now reopen the storage,
169 // check all the written information
172 // the temporary file must not be locked any more after storage disposing
173 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
174 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
175 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
176 if ( xResultStorage
== null )
178 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
182 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
185 if ( !m_aTestHelper
.checkEncrStreamH( xResultStorage
, aSubStream1Path
, "MediaType3", pBytes2
, sPass1
) )
188 // the following stream was not committed last time, so the last change must be lost
189 if ( !m_aTestHelper
.checkEncrStreamH( xResultStorage
, aSubStream2Path
, "MediaType2", pBytes2
, sPass2
) )
192 // dispose used storages to free resources
193 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
200 m_aTestHelper
.Error( "Exception: " + e
);