bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test03.java
blob8e7c1ee304f0367dea9c8037ac6128796e9fa657
1 /*
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 )
44 m_xMSF = xMSF;
45 m_xStorageFactory = xStorageFactory;
46 m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
49 public boolean test()
51 try
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!" );
60 return false;
63 // open a new substorage
64 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
65 "SubStorage1",
66 ElementModes.WRITE );
67 if ( xTempSubStorage == null )
69 m_aTestHelper.Error( "Can't create substorage!" );
70 return false;
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 ) )
79 return false;
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 ) )
83 return false;
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 ) )
89 return false;
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 ) )
95 return false;
97 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
99 "MediaType3",
100 false,
101 ElementModes.WRITE ) )
102 return false;
104 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
105 return false;
107 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
108 return false;
111 // check storage hierarchy tree
114 // check that isStorageElement() and isStreamElement reacts to nonexistent object correctly
115 try {
116 xTempStorage.isStorageElement( "does not exist" );
117 m_aTestHelper.Error( "Nonexistent element doesn't detected by isStorageElement() call!" );
118 return false;
120 catch( com.sun.star.container.NoSuchElementException ne )
123 catch( Exception e )
125 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
126 return false;
129 try {
130 xTempStorage.isStreamElement( "does not exist" );
131 m_aTestHelper.Error( "Nonexistent element doesn't detected by isStreamElement() call!" );
132 return false;
134 catch( com.sun.star.container.NoSuchElementException ne )
137 catch( Exception e )
139 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
140 return false;
143 XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
144 if ( xRootNameAccess == null )
146 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
147 return false;
150 try {
151 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
153 m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
154 return false;
157 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
159 m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
160 return false;
163 catch( Exception e )
165 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
166 return false;
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!" );
176 return false;
179 int nFlag = 0;
180 for ( int nInd = 0; nInd < sRootCont.length; nInd++ )
182 if ( sRootCont[nInd].equals( "SubStorage1" ) )
183 nFlag |= 1;
184 else if ( sRootCont[nInd].equals( "SubStream1" ) )
185 nFlag |= 2;
186 else if ( sRootCont[nInd].equals( "BigSubStream1" ) )
187 nFlag |= 4;
190 if ( nFlag != 7 || !( xRootNameAccess.hasByName( "BigSubStream1" ) && xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
192 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
193 return false;
196 // get storage through XNameAccess
197 XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
198 if ( xResultSubStorage == null )
199 return false;
201 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
202 return false;
204 XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
205 if ( xChildAccess == null )
207 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
208 return false;
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'!" );
216 return false;
219 return true;
221 catch( Exception e )
223 m_aTestHelper.Error( "Exception: " + e );
224 return false;
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 )
236 return xResult;
237 else
238 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
240 catch( Exception e )
242 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
245 return null;