fix toolbar import
[ooovba.git] / package / qa / storages / Test03.java
blobc347021851d52bb84a52d4ddd5388454a6799fd8
1 package complex.storages;
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
11 import com.sun.star.embed.*;
12 import com.sun.star.container.XNameAccess;
14 import share.LogWriter;
15 import complex.storages.TestHelper;
16 import complex.storages.StorageTest;
18 public class Test03 implements StorageTest {
20 XMultiServiceFactory m_xMSF;
21 XSingleServiceFactory m_xStorageFactory;
22 TestHelper m_aTestHelper;
24 public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
26 m_xMSF = xMSF;
27 m_xStorageFactory = xStorageFactory;
28 m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
31 public boolean test()
33 try
35 // create temporary storage based on arbitrary medium
36 // after such a storage is closed it is lost
37 Object oTempStorage = m_xStorageFactory.createInstance();
38 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
39 if ( xTempStorage == null )
41 m_aTestHelper.Error( "Can't create temporary storage representation!" );
42 return false;
45 // open a new substorage
46 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
47 "SubStorage1",
48 ElementModes.WRITE );
49 if ( xTempSubStorage == null )
51 m_aTestHelper.Error( "Can't create substorage!" );
52 return false;
55 byte pBytes1[] = { 1, 1, 1, 1, 1 };
57 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
58 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
59 return false;
61 byte pBytes2[] = { 2, 2, 2, 2, 2 };
63 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
64 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
65 return false;
67 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
68 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
69 "MediaType3",
70 false,
71 ElementModes.WRITE ) )
72 return false;
74 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
75 return false;
77 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
78 return false;
80 // ================================================
81 // check storage hyerarchy tree
82 // ================================================
84 // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
85 try {
86 xTempStorage.isStorageElement( "does not exist" );
87 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
88 return false;
90 catch( com.sun.star.container.NoSuchElementException ne )
93 catch( Exception e )
95 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
96 return false;
99 try {
100 xTempStorage.isStreamElement( "does not exist" );
101 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
102 return false;
104 catch( com.sun.star.container.NoSuchElementException ne )
107 catch( Exception e )
109 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
110 return false;
113 XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
114 if ( xRootNameAccess == null )
116 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
117 return false;
120 try {
121 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
123 m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
124 return false;
127 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
129 m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
130 return false;
133 catch( Exception e )
135 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
136 return false;
140 // check that root storage contents are represented correctly
141 String sRootCont[] = xRootNameAccess.getElementNames();
143 if ( sRootCont.length != 2 )
145 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
146 return false;
149 if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
150 || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
151 || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
153 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
154 return false;
157 // get storage through XNameAccess
158 XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
159 if ( xResultSubStorage == null )
160 return false;
162 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
163 return false;
165 XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
166 if ( xChildAccess == null )
168 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
169 return false;
172 if ( !xChildAccess.hasByName( "SubStream2" )
173 || !xResultSubStorage.isStreamElement( "SubStream2" )
174 || xResultSubStorage.isStorageElement( "SubStream2" ) )
176 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
177 return false;
180 return true;
182 catch( Exception e )
184 m_aTestHelper.Error( "Exception: " + e );
185 return false;
189 public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
193 Object oStorage = xAccess.getByName( sName );
194 XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
196 if ( xResult != null )
197 return xResult;
198 else
199 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
201 catch( Exception e )
203 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
206 return null;