Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test03.java
blobf1d5d415067f3cac09d3038607fddbe886f8e64d
1 package storagetesting;
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 storagetesting.TestHelper;
15 import storagetesting.StorageTest;
17 public class Test03 implements StorageTest {
19 XMultiServiceFactory m_xMSF;
20 XSingleServiceFactory m_xStorageFactory;
21 TestHelper m_aTestHelper;
23 public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
25 m_xMSF = xMSF;
26 m_xStorageFactory = xStorageFactory;
27 m_aTestHelper = new TestHelper( "Test03: " );
30 public boolean test()
32 try
34 // create temporary storage based on arbitrary medium
35 // after such a storage is closed it is lost
36 Object oTempStorage = m_xStorageFactory.createInstance();
37 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
38 if ( xTempStorage == null )
40 m_aTestHelper.Error( "Can't create temporary storage representation!" );
41 return false;
44 // open a new substorage
45 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
46 "SubStorage1",
47 ElementModes.ELEMENT_WRITE );
48 if ( xTempSubStorage == null )
50 m_aTestHelper.Error( "Can't create substorage!" );
51 return false;
54 byte pBytes1[] = { 1, 1, 1, 1, 1 };
56 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
57 if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
58 return false;
60 byte pBytes2[] = { 2, 2, 2, 2, 2 };
62 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
63 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
64 return false;
66 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
67 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
68 "MediaType3",
69 false,
70 ElementModes.ELEMENT_WRITE ) )
71 return false;
73 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
74 return false;
76 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
77 return false;
79 // ================================================
80 // check storage hyerarchy tree
81 // ================================================
83 // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
84 try {
85 xTempStorage.isStorageElement( "does not exist" );
86 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
87 return false;
89 catch( com.sun.star.container.NoSuchElementException ne )
92 catch( Exception e )
94 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
95 return false;
98 try {
99 xTempStorage.isStreamElement( "does not exist" );
100 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
101 return false;
103 catch( com.sun.star.container.NoSuchElementException ne )
106 catch( Exception e )
108 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
109 return false;
112 XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
113 if ( xRootNameAccess == null )
115 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
116 return false;
119 try {
120 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
122 m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
123 return false;
126 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
128 m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
129 return false;
132 catch( Exception e )
134 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
135 return false;
139 // check that root storage contents are represented correctly
140 String sRootCont[] = xRootNameAccess.getElementNames();
142 if ( sRootCont.length != 2 )
144 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
145 return false;
148 if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
149 || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
150 || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
152 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
153 return false;
156 // get storage through XNameAccess
157 XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
158 if ( xResultSubStorage == null )
159 return false;
161 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.ELEMENT_READ ) )
162 return false;
164 XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
165 if ( xChildAccess == null )
167 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
168 return false;
171 if ( !xChildAccess.hasByName( "SubStream2" )
172 || !xResultSubStorage.isStreamElement( "SubStream2" )
173 || xResultSubStorage.isStorageElement( "SubStream2" ) )
175 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
176 return false;
179 return true;
181 catch( Exception e )
183 m_aTestHelper.Error( "Exception: " + e );
184 return false;
188 public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
192 Object oStorage = xAccess.getByName( sName );
193 XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
195 if ( xResult != null )
196 return xResult;
197 else
198 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
200 catch( Exception e )
202 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
205 return null;