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 import com
.sun
.star
.lang
.XMultiServiceFactory
;
20 import com
.sun
.star
.lang
.XSingleServiceFactory
;
22 import com
.sun
.star
.uno
.UnoRuntime
;
23 import com
.sun
.star
.embed
.*;
25 public class Test01
implements StorageTest
{
27 XMultiServiceFactory m_xMSF
;
28 XSingleServiceFactory m_xStorageFactory
;
29 TestHelper m_aTestHelper
;
31 public Test01( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
34 m_xStorageFactory
= xStorageFactory
;
35 m_aTestHelper
= new TestHelper( "Test01: " );
42 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
43 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
45 m_aTestHelper
.Error( "No valid temporary file was created!" );
49 // create temporary storage based on arbitrary medium
50 // after such a storage is closed it is lost
51 Object oTempStorage
= m_xStorageFactory
.createInstance();
52 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
53 if ( xTempStorage
== null )
55 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
59 // open a new substorage
60 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
63 if ( xTempSubStorage
== null )
65 m_aTestHelper
.Error( "Can't create substorage!" );
69 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
71 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
72 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
75 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
77 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
81 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
82 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
85 ElementModes
.READWRITE
) )
88 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
89 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
92 ElementModes
.WRITE
) )
95 // create temporary storage based on a previously created temporary file
96 Object pArgs
[] = new Object
[2];
97 pArgs
[0] = sTempFileURL
;
98 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
100 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
101 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
102 if ( xTempFileStorage
== null )
104 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
108 // copy xTempStorage to xTempFileStorage
109 // xTempFileStorage will be automatically committed
110 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
113 // dispose used storages to free resources
114 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
118 // now check all the written and copied information
121 // the temporary file must not be locked any more after storage disposing
122 pArgs
[1] = Integer
.valueOf( ElementModes
.READWRITE
);
123 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
124 XStorage xResultStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
125 if ( xResultStorage
== null )
127 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
131 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READWRITE
) )
134 // open existing substorage
135 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
138 if ( xResultSubStorage
== null )
140 m_aTestHelper
.Error( "Can't open existing substorage!" );
144 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType4", false, ElementModes
.READ
) )
147 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
) )
150 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
153 // dispose used storages to free resources
154 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
161 m_aTestHelper
.Error( "Exception: " + e
);