2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com
.sun
.star
.lang
.XMultiServiceFactory
;
20 import com
.sun
.star
.lang
.XSingleServiceFactory
;
22 import com
.sun
.star
.uno
.UnoRuntime
;
23 import com
.sun
.star
.io
.XStream
;
25 import com
.sun
.star
.embed
.*;
27 public class Test05
implements StorageTest
{
29 XMultiServiceFactory m_xMSF
;
30 XSingleServiceFactory m_xStorageFactory
;
31 TestHelper m_aTestHelper
;
33 public Test05( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
36 m_xStorageFactory
= xStorageFactory
;
37 m_aTestHelper
= new TestHelper( "Test05: " );
44 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
45 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
47 m_aTestHelper
.Error( "No valid temporary file was created!" );
51 // create temporary storage based on a previously created temporary file
52 Object pArgs
[] = new Object
[2];
53 pArgs
[0] = sTempFileURL
;
54 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
56 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
57 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
58 if ( xTempFileStorage
== null )
60 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
64 // open a new substorage
65 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempFileStorage
,
68 if ( xTempSubStorage
== null )
70 m_aTestHelper
.Error( "Can't create substorage!" );
74 // open a new substorage
75 XStorage xSubSubStorage
= m_aTestHelper
.openSubStorage( xTempSubStorage
,
78 if ( xSubSubStorage
== null )
80 m_aTestHelper
.Error( "Can't create substorage!" );
85 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
87 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
88 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
91 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
93 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
94 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
97 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
101 ElementModes
.WRITE
) )
104 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
108 ElementModes
.WRITE
) )
111 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
112 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xSubSubStorage
,
115 ElementModes
.WRITE
) )
119 // commit all the storages
120 if ( !m_aTestHelper
.commitStorage( xSubSubStorage
) )
123 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
126 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
129 // try to open an opened substorage, open call must fail
130 if ( !m_aTestHelper
.cantOpenStorage( xTempFileStorage
, "SubStorage1" ) )
134 // reopen created streams
135 XStream xSubStream1
= m_aTestHelper
.OpenStream( xSubSubStorage
,
137 ElementModes
.READWRITE
| ElementModes
.NOCREATE
);
138 XStream xSubStream2
= m_aTestHelper
.OpenStream( xSubSubStorage
,
140 ElementModes
.READ
| ElementModes
.NOCREATE
);
141 if ( xSubStream1
== null || xSubStream2
== null )
144 // it should be possible to have more than one copy of stream for reading
145 XStream xSubStream2clone
= m_aTestHelper
.OpenStream( xSubSubStorage
,
147 ElementModes
.READ
| ElementModes
.NOCREATE
);
150 // so now the first stream can not be open neither for reading nor for writing
151 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream1", ElementModes
.WRITE
)
152 || !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream1", ElementModes
.READ
) )
155 // the second stream can not be open for writing
156 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream2", ElementModes
.WRITE
) )
160 // dispose xTestSubStorage, all the subtree must be disposed
161 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage
) )
164 // check that subtree was disposed correctly
167 xSubSubStorage
.isStreamElement( "SubStream1" );
168 m_aTestHelper
.Error( "Substorage was not disposed!" );
171 catch ( com
.sun
.star
.lang
.DisposedException de
)
173 catch ( Exception e
)
175 m_aTestHelper
.Error( "Wrong exception is thrown by disposed storage: " + e
);
181 xSubStream1
.getInputStream();
182 m_aTestHelper
.Error( "Writeable substream was not disposed!" );
185 catch ( com
.sun
.star
.lang
.DisposedException de
)
187 catch ( Exception e
)
189 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
195 xSubStream2
.getInputStream();
196 m_aTestHelper
.Error( "Readonly substream was not disposed!" );
199 catch ( com
.sun
.star
.lang
.DisposedException de
)
201 catch ( Exception e
)
203 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
208 // dispose root storage
209 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
214 // now check all the written and copied information
217 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
218 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
219 XStorage xResultStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
220 if ( xResultStorage
== null )
222 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
226 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
229 // open existing substorage
230 XStorage xResSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
233 if ( xResSubStorage
== null )
235 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
239 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage
, "MediaType4", false, ElementModes
.READ
) )
242 // open existing substorage
243 XStorage xResSubSubStorage
= m_aTestHelper
.openSubStorage( xResSubStorage
,
246 if ( xResSubSubStorage
== null )
248 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
252 if ( !m_aTestHelper
.checkStorageProperties( xResSubSubStorage
, "MediaType5", false, ElementModes
.READ
) )
256 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream1", "MediaType1", pBytes1
) )
259 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
262 // dispose used storages to free resources
263 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
270 m_aTestHelper
.Error( "Exception: " + e
);