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
.*;
25 import com
.sun
.star
.container
.XNameAccess
;
27 public class Test03
implements StorageTest
{
29 XMultiServiceFactory m_xMSF
;
30 XSingleServiceFactory m_xStorageFactory
;
31 TestHelper m_aTestHelper
;
33 public Test03( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
36 m_xStorageFactory
= xStorageFactory
;
37 m_aTestHelper
= new TestHelper( "Test03: " );
44 // create temporary storage based on arbitrary medium
45 // after such a storage is closed it is lost
46 Object oTempStorage
= m_xStorageFactory
.createInstance();
47 XStorage xTempStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
48 if ( xTempStorage
== null )
50 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
54 // open a new substorage
55 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
58 if ( xTempSubStorage
== null )
60 m_aTestHelper
.Error( "Can't create substorage!" );
64 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
66 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
67 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
70 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
72 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
73 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
76 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
77 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
80 ElementModes
.WRITE
) )
83 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
86 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage
) )
90 // check storage hierarchy tree
93 // check that isStorageElement() and isStreamElement reacts to nonexistent object correctly
95 xTempStorage
.isStorageElement( "does not exist" );
96 m_aTestHelper
.Error( "Nonexistent element doesn't detected by isStorageElement() call!" );
99 catch( com
.sun
.star
.container
.NoSuchElementException ne
)
104 m_aTestHelper
.Error( "Wrong exception is thrown by isStorageElement() call: " + e
);
109 xTempStorage
.isStreamElement( "does not exist" );
110 m_aTestHelper
.Error( "Nonexistent element doesn't detected by isStreamElement() call!" );
113 catch( com
.sun
.star
.container
.NoSuchElementException ne
)
118 m_aTestHelper
.Error( "Wrong exception is thrown by isStreamElement() call: " + e
);
122 XNameAccess xRootNameAccess
= UnoRuntime
.queryInterface( XNameAccess
.class, xTempStorage
);
123 if ( xRootNameAccess
== null )
125 m_aTestHelper
.Error( "Root storage doesn't support XNameAccess!" );
130 if ( !xTempStorage
.isStorageElement( "SubStorage1" ) || xTempStorage
.isStreamElement( "SubStorage1" ) )
132 m_aTestHelper
.Error( "Child 'SubStorage1' can not be detected as storage!" );
136 if ( xTempStorage
.isStorageElement( "SubStream1" ) || !xTempStorage
.isStreamElement( "SubStream1" ) )
138 m_aTestHelper
.Error( "Child 'SubStream1' can not be detected as stream!" );
144 m_aTestHelper
.Error( "Child's type can not be detected, exception: " + e
);
149 // check that root storage contents are represented correctly
150 String sRootCont
[] = xRootNameAccess
.getElementNames();
152 if ( sRootCont
.length
!= 2 )
154 m_aTestHelper
.Error( "Root storage contains wrong amount of children!" );
158 if ( !( sRootCont
[0].equals( "SubStorage1" ) && sRootCont
[1].equals( "SubStream1" )
159 || sRootCont
[0].equals( "SubStream1" ) && sRootCont
[1].equals( "SubStorage1" ) )
160 || !( xRootNameAccess
.hasByName( "SubStream1" ) && xRootNameAccess
.hasByName( "SubStorage1" ) ) )
162 m_aTestHelper
.Error( "Root storage contains wrong list of children!" );
166 // get storage through XNameAccess
167 XStorage xResultSubStorage
= getStorageFromNameAccess( xRootNameAccess
, "SubStorage1" );
168 if ( xResultSubStorage
== null )
171 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType3", false, ElementModes
.READ
) )
174 XNameAccess xChildAccess
= UnoRuntime
.queryInterface( XNameAccess
.class, xResultSubStorage
);
175 if ( xChildAccess
== null )
177 m_aTestHelper
.Error( "Child storage doesn't support XNameAccess!" );
181 if ( !xChildAccess
.hasByName( "SubStream2" )
182 || !xResultSubStorage
.isStreamElement( "SubStream2" )
183 || xResultSubStorage
.isStorageElement( "SubStream2" ) )
185 m_aTestHelper
.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
193 m_aTestHelper
.Error( "Exception: " + e
);
198 public XStorage
getStorageFromNameAccess( XNameAccess xAccess
, String sName
)
202 Object oStorage
= xAccess
.getByName( sName
);
203 XStorage xResult
= UnoRuntime
.queryInterface( XStorage
.class, oStorage
);
205 if ( xResult
!= null )
208 m_aTestHelper
.Error( "Can't retrieve substorage '" + sName
+ "' through XNameAccess!" );
212 m_aTestHelper
.Error( "Can't retrieve substorage '" + sName
+ "' through XNameAccess, exception: " + e
);
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */