1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.lang
.XMultiServiceFactory
;
21 import com
.sun
.star
.lang
.XSingleServiceFactory
;
23 import com
.sun
.star
.uno
.UnoRuntime
;
24 import com
.sun
.star
.io
.XStream
;
26 import com
.sun
.star
.embed
.*;
28 public class Test05
implements StorageTest
{
30 XMultiServiceFactory m_xMSF
;
31 XSingleServiceFactory m_xStorageFactory
;
32 TestHelper m_aTestHelper
;
34 public Test05( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
)
37 m_xStorageFactory
= xStorageFactory
;
38 m_aTestHelper
= new TestHelper( "Test05: " );
45 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
46 if ( sTempFileURL
== null || sTempFileURL
.equals("") )
48 m_aTestHelper
.Error( "No valid temporary file was created!" );
52 // create temporary storage based on a previously created temporary file
53 Object pArgs
[] = new Object
[2];
54 pArgs
[0] = sTempFileURL
;
55 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
57 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
58 XStorage xTempFileStorage
= UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
59 if ( xTempFileStorage
== null )
61 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
65 // open a new substorage
66 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempFileStorage
,
69 if ( xTempSubStorage
== null )
71 m_aTestHelper
.Error( "Can't create substorage!" );
75 // open a new substorage
76 XStorage xSubSubStorage
= m_aTestHelper
.openSubStorage( xTempSubStorage
,
79 if ( xSubSubStorage
== null )
81 m_aTestHelper
.Error( "Can't create substorage!" );
86 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
88 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
89 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "SubStream1", "MediaType1", true, pBytes1
) )
92 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
94 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 if ( !m_aTestHelper
.WriteBytesToSubstream( xSubSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
98 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
99 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempFileStorage
,
102 ElementModes
.WRITE
) )
105 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
106 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
109 ElementModes
.WRITE
) )
112 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
113 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xSubSubStorage
,
116 ElementModes
.WRITE
) )
120 // commit all the storages
121 if ( !m_aTestHelper
.commitStorage( xSubSubStorage
) )
124 if ( !m_aTestHelper
.commitStorage( xTempSubStorage
) )
127 if ( !m_aTestHelper
.commitStorage( xTempFileStorage
) )
130 // try to open an opened substorage, open call must fail
131 if ( !m_aTestHelper
.cantOpenStorage( xTempFileStorage
, "SubStorage1" ) )
135 // reopen created streams
136 XStream xSubStream1
= m_aTestHelper
.OpenStream( xSubSubStorage
,
138 ElementModes
.READWRITE
| ElementModes
.NOCREATE
);
139 XStream xSubStream2
= m_aTestHelper
.OpenStream( xSubSubStorage
,
141 ElementModes
.READ
| ElementModes
.NOCREATE
);
142 if ( xSubStream1
== null || xSubStream2
== null )
145 // it should be possible to have more than one copy of stream for reading
146 XStream xSubStream2clone
= m_aTestHelper
.OpenStream( xSubSubStorage
,
148 ElementModes
.READ
| ElementModes
.NOCREATE
);
151 // so now the first stream can not be open neither for reading nor for writing
152 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream1", ElementModes
.WRITE
)
153 || !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream1", ElementModes
.READ
) )
156 // the second stream can not be open for writing
157 if ( !m_aTestHelper
.cantOpenStream( xSubSubStorage
, "SubStream2", ElementModes
.WRITE
) )
161 // dispose xTestSubStorage, all the subtree must be disposed
162 if ( !m_aTestHelper
.disposeStorage( xTempSubStorage
) )
165 // check that subtree was disposed correctly
168 xSubSubStorage
.isStreamElement( "SubStream1" );
169 m_aTestHelper
.Error( "Substorage was not disposed!" );
172 catch ( com
.sun
.star
.lang
.DisposedException de
)
174 catch ( Exception e
)
176 m_aTestHelper
.Error( "Wrong exception is thrown by disposed storage: " + e
);
182 xSubStream1
.getInputStream();
183 m_aTestHelper
.Error( "Writeable substream was not disposed!" );
186 catch ( com
.sun
.star
.lang
.DisposedException de
)
188 catch ( Exception e
)
190 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
196 xSubStream2
.getInputStream();
197 m_aTestHelper
.Error( "Readonly substream was not disposed!" );
200 catch ( com
.sun
.star
.lang
.DisposedException de
)
202 catch ( Exception e
)
204 m_aTestHelper
.Error( "Wrong exception is thrown by disposed stream: " + e
);
209 // dispose root storage
210 if ( !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
215 // now check all the written and copied information
218 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
219 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
220 XStorage xResultStorage
= UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
221 if ( xResultStorage
== null )
223 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
227 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType3", true, ElementModes
.READ
) )
230 // open existing substorage
231 XStorage xResSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
234 if ( xResSubStorage
== null )
236 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
240 if ( !m_aTestHelper
.checkStorageProperties( xResSubStorage
, "MediaType4", false, ElementModes
.READ
) )
243 // open existing substorage
244 XStorage xResSubSubStorage
= m_aTestHelper
.openSubStorage( xResSubStorage
,
247 if ( xResSubSubStorage
== null )
249 m_aTestHelper
.Error( "Can't open existing substorage 'SubSubStorage'!" );
253 if ( !m_aTestHelper
.checkStorageProperties( xResSubSubStorage
, "MediaType5", false, ElementModes
.READ
) )
257 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream1", "MediaType1", pBytes1
) )
260 if ( !m_aTestHelper
.checkStream( xResSubSubStorage
, "SubStream2", "MediaType2", pBytes2
) )
263 // dispose used storages to free resources
264 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
271 m_aTestHelper
.Error( "Exception: " + e
);
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */