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
)
26 m_xStorageFactory
= xStorageFactory
;
27 m_aTestHelper
= new TestHelper( "Test05: " );
34 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
35 if ( sTempFileURL
== null || sTempFileURL
== "" )
37 m_aTestHelper
.Error( "No valid temporary file was created!" );
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!" );
54 // open a new substorage
55 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempFileStorage
,
57 ElementModes
.ELEMENT_WRITE
);
58 if ( xTempSubStorage
== null )
60 m_aTestHelper
.Error( "Can't create substorage!" );
64 // open a new substorage
65 XStorage xSubSubStorage
= m_aTestHelper
.openSubStorage( xTempSubStorage
,
67 ElementModes
.ELEMENT_WRITE
);
68 if ( xSubSubStorage
== null )
70 m_aTestHelper
.Error( "Can't create substorage!" );
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
) )
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
) )
87 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
91 ElementModes
.ELEMENT_WRITE
) )
94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
98 ElementModes
.ELEMENT_WRITE
) )
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xSubSubStorage
,
105 ElementModes
.ELEMENT_WRITE
) )
109 // commit all the storages
110 if ( !m_aTestHelper
.commitStorage( xSubSubStorage
) )
113 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
116 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
119 // try to open an opened substorage, open call must fail
120 if ( !m_aTestHelper
.cantOpenStorage( xTempFileStorage
, "SubStorage1" ) )
124 // reopen created streams
125 XStream xSubStream1
= m_aTestHelper
.OpenStream( xSubSubStorage
,
127 ElementModes
.ELEMENT_READWRITE
| ElementModes
.ELEMENT_NOCREATE
);
128 XStream xSubStream2
= m_aTestHelper
.OpenStream( xSubSubStorage
,
130 ElementModes
.ELEMENT_READ
| ElementModes
.ELEMENT_NOCREATE
);
131 if ( xSubStream1
== null || xSubStream2
== null )
134 // it should be possible to have more then one copy of stream for reading
135 XStream xSubStream2clone
= m_aTestHelper
.OpenStream( xSubSubStorage
,
137 ElementModes
.ELEMENT_READ
| ElementModes
.ELEMENT_NOCREATE
);
138 if ( xSubStream2
== null )
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
) )
147 // the second stream can not be open for writing
148 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream2", ElementModes
.ELEMENT_WRITE
) )
152 // dispose xTestSubStorage, all the subtree must be disposed
153 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage
) )
156 // check that subtree was disposed correctly
159 xSubSubStorage
.isStreamElement( "SubStream1" );
160 m_aTestHelper
.Error( "Substorage was not disposed!" );
163 catch ( com
.sun
.star
.lang
.DisposedException de
)
165 catch ( Exception e
)
167 m_aTestHelper
.Error( "Wrong exception is thrown by disposed storage: " + e
);
173 xSubStream1
.getInputStream();
174 m_aTestHelper
.Error( "Writeable substream was not disposed!" );
177 catch ( com
.sun
.star
.lang
.DisposedException de
)
179 catch ( Exception e
)
181 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
187 xSubStream2
.getInputStream();
188 m_aTestHelper
.Error( "Readonly substream was not disposed!" );
191 catch ( com
.sun
.star
.lang
.DisposedException de
)
193 catch ( Exception e
)
195 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
200 // dispose root storage
201 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
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!" );
218 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.ELEMENT_READ
) )
221 // open existing substorage
222 XStorage xResSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
224 ElementModes
.ELEMENT_READ
);
225 if ( xResSubStorage
== null )
227 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
231 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage
, "MediaType4", false, ElementModes
.ELEMENT_READ
) )
234 // open existing substorage
235 XStorage xResSubSubStorage
= m_aTestHelper
.openSubStorage( xResSubStorage
,
237 ElementModes
.ELEMENT_READ
);
238 if ( xResSubSubStorage
== null )
240 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
244 if ( !m_aTestHelper
.checkStorageProperties( xResSubSubStorage
, "MediaType5", false, ElementModes
.ELEMENT_READ
) )
248 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream1", "MediaType1", pBytes1
) )
251 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
254 // dispose used storages to free resources
255 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
262 m_aTestHelper
.Error( "Exception: " + e
);