Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test05.java
blob263e2d2561c71489fa9fcb3e0cbf52b8ceb8894a
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;
10 import com.sun.star.io.XStream;
12 import com.sun.star.embed.*;
14 import storagetesting.TestHelper;
15 import storagetesting.StorageTest;
17 public class Test05 implements StorageTest {
19 XMultiServiceFactory m_xMSF;
20 XSingleServiceFactory m_xStorageFactory;
21 TestHelper m_aTestHelper;
23 public Test05( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
25 m_xMSF = xMSF;
26 m_xStorageFactory = xStorageFactory;
27 m_aTestHelper = new TestHelper( "Test05: " );
30 public boolean test()
32 try
34 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
35 if ( sTempFileURL == null || sTempFileURL == "" )
37 m_aTestHelper.Error( "No valid temporary file was created!" );
38 return false;
41 // create temporary storage based on a previously created temporary file
42 Object pArgs[] = new Object[2];
43 pArgs[0] = (Object) sTempFileURL;
44 pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
46 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
47 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
48 if ( xTempFileStorage == null )
50 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
51 return false;
54 // open a new substorage
55 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
56 "SubStorage1",
57 ElementModes.ELEMENT_WRITE );
58 if ( xTempSubStorage == null )
60 m_aTestHelper.Error( "Can't create substorage!" );
61 return false;
64 // open a new substorage
65 XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
66 "SubSubStorage1",
67 ElementModes.ELEMENT_WRITE );
68 if ( xSubSubStorage == null )
70 m_aTestHelper.Error( "Can't create substorage!" );
71 return false;
75 byte pBytes1[] = { 1, 1, 1, 1, 1 };
77 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
79 return false;
81 byte pBytes2[] = { 2, 2, 2, 2, 2 };
83 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
84 if ( !m_aTestHelper.WriteBytesToSubstream( xSubSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
85 return false;
87 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
89 "MediaType3",
90 true,
91 ElementModes.ELEMENT_WRITE ) )
92 return false;
94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
96 "MediaType4",
97 false,
98 ElementModes.ELEMENT_WRITE ) )
99 return false;
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
103 "MediaType5",
104 false,
105 ElementModes.ELEMENT_WRITE ) )
106 return false;
109 // commit all the storages
110 if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
111 return false;
113 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
114 return false;
116 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
117 return false;
119 // try to open an opened substorage, open call must fail
120 if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
121 return false;
124 // reopen created streams
125 XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
126 "SubStream1",
127 ElementModes.ELEMENT_READWRITE | ElementModes.ELEMENT_NOCREATE );
128 XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
129 "SubStream2",
130 ElementModes.ELEMENT_READ | ElementModes.ELEMENT_NOCREATE );
131 if ( xSubStream1 == null || xSubStream2 == null )
132 return false;
134 // it should be possible to have more then one copy of stream for reading
135 XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
136 "SubStream2",
137 ElementModes.ELEMENT_READ | ElementModes.ELEMENT_NOCREATE );
138 if ( xSubStream2 == null )
139 return false;
142 // so now the first stream can not be open neither for reading nor for writing
143 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.ELEMENT_WRITE )
144 || !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream1", ElementModes.ELEMENT_READ ) )
145 return false;
147 // the second stream can not be open for writing
148 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.ELEMENT_WRITE ) )
149 return false;
152 // dispose xTestSubStorage, all the subtree must be disposed
153 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
154 return false;
156 // check that subtree was disposed correctly
159 xSubSubStorage.isStreamElement( "SubStream1" );
160 m_aTestHelper.Error( "Substorage was not disposed!" );
161 return false;
163 catch ( com.sun.star.lang.DisposedException de )
165 catch ( Exception e )
167 m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
168 return false;
173 xSubStream1.getInputStream();
174 m_aTestHelper.Error( "Writeable substream was not disposed!" );
175 return false;
177 catch ( com.sun.star.lang.DisposedException de )
179 catch ( Exception e )
181 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
182 return false;
187 xSubStream2.getInputStream();
188 m_aTestHelper.Error( "Readonly substream was not disposed!" );
189 return false;
191 catch ( com.sun.star.lang.DisposedException de )
193 catch ( Exception e )
195 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
196 return false;
200 // dispose root storage
201 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
202 return false;
205 // ================================================
206 // now check all the written and copied information
207 // ================================================
209 pArgs[1] = new Integer( ElementModes.ELEMENT_READ );
210 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
211 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
212 if ( xResultStorage == null )
214 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
215 return false;
218 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.ELEMENT_READ ) )
219 return false;
221 // open existing substorage
222 XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
223 "SubStorage1",
224 ElementModes.ELEMENT_READ );
225 if ( xResSubStorage == null )
227 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
228 return false;
231 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.ELEMENT_READ ) )
232 return false;
234 // open existing substorage
235 XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
236 "SubSubStorage1",
237 ElementModes.ELEMENT_READ );
238 if ( xResSubSubStorage == null )
240 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
241 return false;
244 if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.ELEMENT_READ ) )
245 return false;
247 // check substreams
248 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
249 return false;
251 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
252 return false;
254 // dispose used storages to free resources
255 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
256 return false;
258 return true;
260 catch( Exception e )
262 m_aTestHelper.Error( "Exception: " + e );
263 return false;