1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.lang
.XMultiServiceFactory
;
21 import com
.sun
.star
.lang
.XSingleServiceFactory
;
23 import com
.sun
.star
.uno
.UnoRuntime
;
24 import com
.sun
.star
.embed
.*;
26 public class Test01
implements StorageTest
{
28 XMultiServiceFactory m_xMSF
;
29 XSingleServiceFactory m_xStorageFactory
;
30 TestHelper m_aTestHelper
;
32 public Test01( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
35 m_xStorageFactory
= xStorageFactory
;
36 m_aTestHelper
= new TestHelper( "Test01: " );
43 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
44 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
46 m_aTestHelper
.Error( "No valid temporary file was created!" );
50 // create temporary storage based on arbitrary medium
51 // after such a storage is closed it is lost
52 Object oTempStorage
= m_xStorageFactory
.createInstance();
53 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
54 if ( xTempStorage
== null )
56 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
60 // open a new substorage
61 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
64 if ( xTempSubStorage
== null )
66 m_aTestHelper
.Error( "Can't create substorage!" );
70 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
72 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
73 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
76 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
78 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
79 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
82 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
83 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
86 ElementModes
.READWRITE
) )
89 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
93 ElementModes
.WRITE
) )
96 // create temporary storage based on a previously created temporary file
97 Object pArgs
[] = new Object
[2];
98 pArgs
[0] = sTempFileURL
;
99 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
101 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
102 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
103 if ( xTempFileStorage
== null )
105 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
109 // copy xTempStorage to xTempFileStorage
110 // xTempFileStorage will be automatically committed
111 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
114 // dispose used storages to free resources
115 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
119 // now check all the written and copied information
122 // the temporary file must not be locked any more after storage disposing
123 pArgs
[1] = Integer
.valueOf( ElementModes
.READWRITE
);
124 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
125 XStorage xResultStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
126 if ( xResultStorage
== null )
128 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
132 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READWRITE
) )
135 // open existing substorage
136 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
139 if ( xResultSubStorage
== null )
141 m_aTestHelper
.Error( "Can't open existing substorage!" );
145 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType4", false, ElementModes
.READ
) )
148 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
) )
151 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
154 // dispose used storages to free resources
155 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
162 m_aTestHelper
.Error( "Exception: " + e
);
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */