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 package complex
.storages
;
21 import com
.sun
.star
.uno
.XInterface
;
22 import com
.sun
.star
.lang
.XMultiServiceFactory
;
23 import com
.sun
.star
.lang
.XSingleServiceFactory
;
25 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
27 import com
.sun
.star
.uno
.XInterface
;
28 import com
.sun
.star
.io
.XStream
;
30 import com
.sun
.star
.embed
.*;
32 import share
.LogWriter
;
33 import complex
.storages
.TestHelper
;
34 import complex
.storages
.StorageTest
;
36 public class Test05
implements StorageTest
{
38 XMultiServiceFactory m_xMSF
;
39 XSingleServiceFactory m_xStorageFactory
;
40 TestHelper m_aTestHelper
;
42 public Test05( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
45 m_xStorageFactory
= xStorageFactory
;
46 m_aTestHelper
= new TestHelper( aLogWriter
, "Test05: " );
53 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
54 if ( sTempFileURL
== null || sTempFileURL
== "" )
56 m_aTestHelper
.Error( "No valid temporary file was created!" );
60 // create temporary storage based on a previously created temporary file
61 Object pArgs
[] = new Object
[2];
62 pArgs
[0] = (Object
) sTempFileURL
;
63 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
65 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
66 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
67 if ( xTempFileStorage
== null )
69 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
73 // open a new substorage
74 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempFileStorage
,
77 if ( xTempSubStorage
== null )
79 m_aTestHelper
.Error( "Can't create substorage!" );
83 // open a new substorage
84 XStorage xSubSubStorage
= m_aTestHelper
.openSubStorage( xTempSubStorage
,
87 if ( xSubSubStorage
== null )
89 m_aTestHelper
.Error( "Can't create substorage!" );
93 byte pBigBytes
[] = new byte[33000];
94 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
95 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
97 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
101 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
102 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
105 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
107 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
108 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
111 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
113 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
114 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
117 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
118 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
121 ElementModes
.WRITE
) )
124 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
125 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
128 ElementModes
.WRITE
) )
131 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
132 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xSubSubStorage
,
135 ElementModes
.WRITE
) )
139 // commit all the storages
140 if ( !m_aTestHelper
.commitStorage( xSubSubStorage
) )
143 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
146 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
149 // try to open an opened substorage, open call must fail
150 if ( !m_aTestHelper
.cantOpenStorage( xTempFileStorage
, "SubStorage1" ) )
154 // reopen created streams
155 XStream xSubStream1
= m_aTestHelper
.OpenStream( xSubSubStorage
,
157 ElementModes
.WRITE
| ElementModes
.NOCREATE
);
158 XStream xBigSubStream1
= m_aTestHelper
.OpenStream( xSubSubStorage
,
160 ElementModes
.WRITE
| ElementModes
.NOCREATE
);
161 XStream xSubStream2
= m_aTestHelper
.OpenStream( xSubSubStorage
,
163 ElementModes
.READ
| ElementModes
.NOCREATE
);
164 XStream xBigSubStream2
= m_aTestHelper
.OpenStream( xSubSubStorage
,
166 ElementModes
.READ
| ElementModes
.NOCREATE
);
168 if ( xSubStream1
== null || xBigSubStream1
== null || xSubStream2
== null || xBigSubStream2
== null )
171 // it should be possible to have more than one copy of stream for reading
172 XStream xSubStream2clone
= m_aTestHelper
.OpenStream( xSubSubStorage
,
174 ElementModes
.READ
| ElementModes
.NOCREATE
);
175 XStream xBigSubStream2clone
= m_aTestHelper
.OpenStream( xSubSubStorage
,
177 ElementModes
.READ
| ElementModes
.NOCREATE
);
178 if ( xSubStream2clone
== null || xBigSubStream2clone
== null )
182 // so now the first streams can not be open neither for reading nor for writing
183 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream1", ElementModes
.WRITE
)
184 || !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream1", ElementModes
.READ
)
185 || !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "BigSubStream1", ElementModes
.WRITE
)
186 || !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "BigSubStream1", ElementModes
.READ
) )
189 // the second streams can not be open for writing
190 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream2", ElementModes
.WRITE
)
191 || !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "BigSubStream2", ElementModes
.WRITE
) )
195 // dispose xTestSubStorage, all the subtree must be disposed
196 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage
) )
199 // check that subtree was disposed correctly
202 xSubSubStorage
.isStreamElement( "SubStream1" );
203 m_aTestHelper
.Error( "Substorage was not disposed!" );
206 catch ( com
.sun
.star
.lang
.DisposedException de
)
208 catch ( Exception e
)
210 m_aTestHelper
.Error( "Wrong exception is thrown by disposed storage: " + e
);
216 xSubStream1
.getInputStream();
217 m_aTestHelper
.Error( "Writeable substream was not disposed!" );
220 catch ( com
.sun
.star
.lang
.DisposedException de
)
222 catch ( Exception e
)
224 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
230 xSubStream2
.getInputStream();
231 m_aTestHelper
.Error( "Readonly substream was not disposed!" );
234 catch ( com
.sun
.star
.lang
.DisposedException de
)
236 catch ( Exception e
)
238 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
243 // dispose root storage
244 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
249 // now check all the written and copied information
252 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
253 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
254 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
255 if ( xResultStorage
== null )
257 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
261 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
264 // open existing substorage
265 XStorage xResSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
268 if ( xResSubStorage
== null )
270 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
274 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage
, "MediaType4", false, ElementModes
.READ
) )
277 // open existing substorage
278 XStorage xResSubSubStorage
= m_aTestHelper
.openSubStorage( xResSubStorage
,
281 if ( xResSubSubStorage
== null )
283 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
287 if ( !m_aTestHelper
.checkStorageProperties( xResSubSubStorage
, "MediaType5", false, ElementModes
.READ
) )
291 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
294 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
) )
297 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
300 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
303 // dispose used storages to free resources
304 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
311 m_aTestHelper
.Error( "Exception: " + e
);