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 Test01
implements StorageTest
{
37 XMultiServiceFactory m_xMSF
;
38 XSingleServiceFactory m_xStorageFactory
;
39 TestHelper m_aTestHelper
;
41 public Test01( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
44 m_xStorageFactory
= xStorageFactory
;
45 m_aTestHelper
= new TestHelper( aLogWriter
, "Test01: " );
52 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
53 if ( sTempFileURL
== null || sTempFileURL
== "" )
55 m_aTestHelper
.Error( "No valid temporary file was created!" );
59 // create temporary storage based on arbitrary medium
60 // after such a storage is closed it is lost
61 Object oTempStorage
= m_xStorageFactory
.createInstance();
62 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
63 if ( xTempStorage
== null )
65 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
69 // open a new substorage
70 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
73 if ( xTempSubStorage
== null )
75 m_aTestHelper
.Error( "Can't create substorage!" );
79 byte pBigBytes
[] = new byte[33000];
80 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
81 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
83 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
84 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
87 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
91 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
93 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
94 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
97 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
99 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
100 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
103 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
104 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
107 ElementModes
.WRITE
) )
110 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
111 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
114 ElementModes
.WRITE
) )
117 // create temporary storage based on a previously created temporary file
118 Object pArgs
[] = new Object
[2];
119 pArgs
[0] = (Object
) sTempFileURL
;
120 pArgs
[1] = new Integer( ElementModes
.WRITE
);
122 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
123 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
124 if ( xTempFileStorage
== null )
126 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
130 // copy xTempStorage to xTempFileStorage
131 // xTempFileStorage will be automatically commited
132 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
135 // dispose used storages to free resources
136 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
139 // ================================================
140 // now check all the written and copied information
141 // ================================================
143 // the temporary file must not be locked any more after storage disposing
144 pArgs
[1] = new Integer( ElementModes
.WRITE
);
145 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
146 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
147 if ( xResultStorage
== null )
149 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
153 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.WRITE
) )
156 // open existing substorage
157 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
160 if ( xResultSubStorage
== null )
162 m_aTestHelper
.Error( "Can't open existing substorage!" );
166 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType4", false, ElementModes
.READ
) )
169 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
172 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
175 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
178 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
181 // dispose used storages to free resources
182 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
189 m_aTestHelper
.Error( "Exception: " + e
);