Update ooo320-m1
[ooovba.git] / package / qa / storages / Test03.java
blob01b9039dd318622cb301f2739501b5083c798783
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 pBigBytes[] = new byte[33000];
56 for ( int nInd = 0; nInd < 33000; nInd++ )
57 pBigBytes[nInd] = (byte)( nInd % 128 );
59 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
60 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
61 return false;
63 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
64 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
65 return false;
67 byte pBytes1[] = { 1, 1, 1, 1, 1 };
69 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
70 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
71 return false;
73 byte pBytes2[] = { 2, 2, 2, 2, 2 };
75 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
77 return false;
79 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
80 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
81 "MediaType3",
82 false,
83 ElementModes.WRITE ) )
84 return false;
86 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
87 return false;
89 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
90 return false;
92 // ================================================
93 // check storage hyerarchy tree
94 // ================================================
96 // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
97 try {
98 xTempStorage.isStorageElement( "does not exist" );
99 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
100 return false;
102 catch( com.sun.star.container.NoSuchElementException ne )
105 catch( Exception e )
107 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
108 return false;
111 try {
112 xTempStorage.isStreamElement( "does not exist" );
113 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
114 return false;
116 catch( com.sun.star.container.NoSuchElementException ne )
119 catch( Exception e )
121 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
122 return false;
125 XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
126 if ( xRootNameAccess == null )
128 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
129 return false;
132 try {
133 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
135 m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
136 return false;
139 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
141 m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
142 return false;
145 catch( Exception e )
147 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
148 return false;
152 // check that root storage contents are represented correctly
153 String sRootCont[] = xRootNameAccess.getElementNames();
155 if ( sRootCont.length != 3 )
157 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
158 return false;
161 int nFlag = 0;
162 for ( int nInd = 0; nInd < sRootCont.length; nInd++ )
164 if ( sRootCont[nInd].equals( "SubStorage1" ) )
165 nFlag |= 1;
166 else if ( sRootCont[nInd].equals( "SubStream1" ) )
167 nFlag |= 2;
168 else if ( sRootCont[nInd].equals( "BigSubStream1" ) )
169 nFlag |= 4;
172 if ( nFlag != 7 || !( xRootNameAccess.hasByName( "BigSubStream1" ) && xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
174 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
175 return false;
178 // get storage through XNameAccess
179 XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
180 if ( xResultSubStorage == null )
181 return false;
183 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
184 return false;
186 XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
187 if ( xChildAccess == null )
189 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
190 return false;
193 if ( !( xChildAccess.hasByName( "SubStream2" ) && xChildAccess.hasByName( "BigSubStream2" ) )
194 || !xResultSubStorage.isStreamElement( "SubStream2" )
195 || !xResultSubStorage.isStreamElement( "BigSubStream2" ) )
197 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
198 return false;
201 return true;
203 catch( Exception e )
205 m_aTestHelper.Error( "Exception: " + e );
206 return false;
210 public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
214 Object oStorage = xAccess.getByName( sName );
215 XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
217 if ( xResult != null )
218 return xResult;
219 else
220 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
222 catch( Exception e )
224 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
227 return null;