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
.*;
30 import com
.sun
.star
.container
.XNameAccess
;
32 import share
.LogWriter
;
33 import complex
.storages
.TestHelper
;
34 import complex
.storages
.StorageTest
;
36 public class Test03
implements StorageTest
{
38 XMultiServiceFactory m_xMSF
;
39 XSingleServiceFactory m_xStorageFactory
;
40 TestHelper m_aTestHelper
;
42 public Test03( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
45 m_xStorageFactory
= xStorageFactory
;
46 m_aTestHelper
= new TestHelper( aLogWriter
, "Test03: " );
53 // create temporary storage based on arbitrary medium
54 // after such a storage is closed it is lost
55 Object oTempStorage
= m_xStorageFactory
.createInstance();
56 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
57 if ( xTempStorage
== null )
59 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
63 // open a new substorage
64 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
67 if ( xTempSubStorage
== null )
69 m_aTestHelper
.Error( "Can't create substorage!" );
73 byte pBigBytes
[] = new byte[33000];
74 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
75 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
77 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
81 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
85 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
87 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
91 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
93 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
94 if ( !m_aTestHelper
.WriteBytesToSubstream( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
97 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
101 ElementModes
.WRITE
) )
104 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
107 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage
) )
111 // check storage hierarchy tree
114 // check that isStorageElement() and isStreamElement reacts to nonexistent object correctly
116 xTempStorage
.isStorageElement( "does not exist" );
117 m_aTestHelper
.Error( "Nonexistent element doesn't detected by isStorageElement() call!" );
120 catch( com
.sun
.star
.container
.NoSuchElementException ne
)
125 m_aTestHelper
.Error( "Wrong exception is thrown by isStorageElement() call: " + e
);
130 xTempStorage
.isStreamElement( "does not exist" );
131 m_aTestHelper
.Error( "Nonexistent element doesn't detected by isStreamElement() call!" );
134 catch( com
.sun
.star
.container
.NoSuchElementException ne
)
139 m_aTestHelper
.Error( "Wrong exception is thrown by isStreamElement() call: " + e
);
143 XNameAccess xRootNameAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xTempStorage
);
144 if ( xRootNameAccess
== null )
146 m_aTestHelper
.Error( "Root storage doesn't support XNameAccess!" );
151 if ( !xTempStorage
.isStorageElement( "SubStorage1" ) || xTempStorage
.isStreamElement( "SubStorage1" ) )
153 m_aTestHelper
.Error( "Child 'SubStorage1' can not be detected as storage!" );
157 if ( xTempStorage
.isStorageElement( "SubStream1" ) || !xTempStorage
.isStreamElement( "SubStream1" ) )
159 m_aTestHelper
.Error( "Child 'SubStream1' can not be detected as stream!" );
165 m_aTestHelper
.Error( "Child's type can not be detected, exception: " + e
);
170 // check that root storage contents are represented correctly
171 String sRootCont
[] = xRootNameAccess
.getElementNames();
173 if ( sRootCont
.length
!= 3 )
175 m_aTestHelper
.Error( "Root storage contains wrong amount of children!" );
180 for ( int nInd
= 0; nInd
< sRootCont
.length
; nInd
++ )
182 if ( sRootCont
[nInd
].equals( "SubStorage1" ) )
184 else if ( sRootCont
[nInd
].equals( "SubStream1" ) )
186 else if ( sRootCont
[nInd
].equals( "BigSubStream1" ) )
190 if ( nFlag
!= 7 || !( xRootNameAccess
.hasByName( "BigSubStream1" ) && xRootNameAccess
.hasByName( "SubStream1" ) && xRootNameAccess
.hasByName( "SubStorage1" ) ) )
192 m_aTestHelper
.Error( "Root storage contains wrong list of children!" );
196 // get storage through XNameAccess
197 XStorage xResultSubStorage
= getStorageFromNameAccess( xRootNameAccess
, "SubStorage1" );
198 if ( xResultSubStorage
== null )
201 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType3", false, ElementModes
.READ
) )
204 XNameAccess xChildAccess
= (XNameAccess
) UnoRuntime
.queryInterface( XNameAccess
.class, xResultSubStorage
);
205 if ( xChildAccess
== null )
207 m_aTestHelper
.Error( "Child storage doesn't support XNameAccess!" );
211 if ( !( xChildAccess
.hasByName( "SubStream2" ) && xChildAccess
.hasByName( "BigSubStream2" ) )
212 || !xResultSubStorage
.isStreamElement( "SubStream2" )
213 || !xResultSubStorage
.isStreamElement( "BigSubStream2" ) )
215 m_aTestHelper
.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
223 m_aTestHelper
.Error( "Exception: " + e
);
228 public XStorage
getStorageFromNameAccess( XNameAccess xAccess
, String sName
)
232 Object oStorage
= xAccess
.getByName( sName
);
233 XStorage xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oStorage
);
235 if ( xResult
!= null )
238 m_aTestHelper
.Error( "Can't retrieve substorage '" + sName
+ "' through XNameAccess!" );
242 m_aTestHelper
.Error( "Can't retrieve substorage '" + sName
+ "' through XNameAccess, exception: " + e
);