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 Test13
implements StorageTest
{
37 XMultiServiceFactory m_xMSF
;
38 XSingleServiceFactory m_xStorageFactory
;
39 TestHelper m_aTestHelper
;
41 public Test13( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
44 m_xStorageFactory
= xStorageFactory
;
45 m_aTestHelper
= new TestHelper( aLogWriter
, "Test13: " );
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";
65 String aBigSubStream1Path
= aStreamPrefix
+ "BigSubStream1";
66 String aBigSubStream2Path
= aStreamPrefix
+ "BigSubStream2";
67 String aBigSubStream3Path
= aStreamPrefix
+ "BigSubStream3";
69 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
70 if ( sTempFileURL
== null || sTempFileURL
== "" )
72 m_aTestHelper
.Error( "No valid temporary file was created!" );
76 // create temporary storage based on a previously created temporary file
77 Object pArgs
[] = new Object
[2];
78 pArgs
[0] = (Object
) sTempFileURL
;
79 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
81 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
82 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
83 if ( xTempFileStorage
== null )
85 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
89 byte pBigBytes
[] = new byte[33000];
90 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
91 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
93 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
95 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
97 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType1", true, pBytes1
, true ) )
99 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aBigSubStream1Path
, "MediaType1", true, pBigBytes
, true ) )
102 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
104 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
106 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", false, pBytes2
, true ) )
108 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aBigSubStream2Path
, "MediaType2", false, pBigBytes
, true ) )
111 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
113 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream3Path
, "MediaType2", false, pBytes2
, false ) )
115 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aBigSubStream3Path
, "MediaType2", false, pBigBytes
, false ) )
118 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
119 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
122 ElementModes
.WRITE
) )
125 // commit the root storage so the contents must be stored now
126 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
129 // dispose used storages to free resources
130 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
134 // now reopen the storage,
135 // check all the written and copied information
139 // the temporary file must not be locked any more after storage disposing
140 oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
141 xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
142 if ( xTempFileStorage
== null )
144 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
148 if ( !m_aTestHelper
.checkStorageProperties( xTempFileStorage
, "MediaType3", true, ElementModes
.WRITE
) )
151 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType1", true, pBytes1
) )
154 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aBigSubStream1Path
, "MediaType1", true, pBigBytes
) )
157 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType2", false, pBytes2
) )
160 if ( !m_aTestHelper
.checkStreamH( xTempFileStorage
, aBigSubStream2Path
, "MediaType2", false, pBigBytes
) )
163 if ( !m_aTestHelper
.cantOpenStreamH( xTempFileStorage
, aSubStream3Path
, ElementModes
.READ
) )
166 if ( !m_aTestHelper
.cantOpenStreamH( xTempFileStorage
, aBigSubStream3Path
, ElementModes
.READ
) )
169 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
171 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream1Path
, "MediaType3", true, pBytes2
, true ) )
173 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aBigSubStream1Path
, "MediaType3", true, pBigBytes
, true ) )
177 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
179 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aSubStream2Path
, "MediaType3", true, pBytes1
, false ) )
181 if ( !m_aTestHelper
.WriteBytesToStreamH( xTempFileStorage
, aBigSubStream2Path
, "MediaType3", true, pBigBytes
, false ) )
184 // commit the root storage so the contents must be stored now
185 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
188 // dispose used storages to free resources
189 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
193 // now reopen the storage,
194 // check all the written information
197 // the temporary file must not be locked any more after storage disposing
198 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
199 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
200 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
201 if ( xResultStorage
== null )
203 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
207 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
210 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aSubStream1Path
, "MediaType3", true, pBytes2
) )
212 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aBigSubStream1Path
, "MediaType3", true, pBigBytes
) )
215 // the following stream was not committed last time, so the last change must be lost
216 if ( !m_aTestHelper
.checkStreamH( xResultStorage
, aBigSubStream2Path
, "MediaType2", false, pBigBytes
) )
219 // dispose used storages to free resources
220 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
227 m_aTestHelper
.Error( "Exception: " + e
);