Update git submodules
[LibreOffice.git] / odk / examples / java / Storage / Test04.java
blob535253b19a6ec68108fa39d8a948c4a3545c5b55
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
22 import com.sun.star.uno.UnoRuntime;
23 import com.sun.star.container.XNameAccess;
25 import com.sun.star.embed.*;
27 public class Test04 implements StorageTest {
29 XMultiServiceFactory m_xMSF;
30 XSingleServiceFactory m_xStorageFactory;
31 TestHelper m_aTestHelper;
33 public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
35 m_xMSF = xMSF;
36 m_xStorageFactory = xStorageFactory;
37 m_aTestHelper = new TestHelper( "Test04: " );
40 public boolean test()
42 try
44 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
45 if ( sTempFileURL == null || sTempFileURL.equals("") )
47 m_aTestHelper.Error( "No valid temporary file was created!" );
48 return false;
51 // create temporary storage based on arbitrary medium
52 // after such a storage is closed it is lost
53 Object oTempStorage = m_xStorageFactory.createInstance();
54 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
55 if ( xTempStorage == null )
57 m_aTestHelper.Error( "Can't create temporary storage representation!" );
58 return false;
61 // open substorages and create streams there
63 // first substorage of the root storage
64 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
65 "SubStorage1",
66 ElementModes.WRITE );
67 if ( xTempSubStorage1 == null )
69 m_aTestHelper.Error( "Can't create substorage!" );
70 return false;
73 byte pBytes1[] = { 1, 1, 1, 1, 1 };
75 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
77 return false;
79 // second substorage of the root storage
80 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
81 "SubStorage2",
82 ElementModes.WRITE );
83 if ( xTempSubStorage2 == null )
85 m_aTestHelper.Error( "Can't create substorage!" );
86 return false;
89 byte pBytes2[] = { 2, 2, 2, 2, 2 };
91 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
92 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
93 return false;
95 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
96 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
97 "MediaType3",
98 true,
99 ElementModes.READWRITE ) )
100 return false;
102 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
103 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
104 "MediaType4",
105 false,
106 ElementModes.WRITE ) )
107 return false;
109 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
110 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
111 "MediaType5",
112 false,
113 ElementModes.WRITE ) )
114 return false;
116 // create temporary storage based on a previously created temporary file
117 Object pArgs[] = new Object[2];
118 pArgs[0] = sTempFileURL;
119 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
121 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
122 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
123 if ( xTempFileStorage == null )
125 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
126 return false;
129 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
130 return false;
132 // if storage is not committed before disposing all the changes will be lost
133 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
134 return false;
136 // a storage must be disposed before moving/removing otherwise the access will be denied
137 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
138 return false;
140 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
141 return false;
143 // SubStorage2 must be removed and disposed now
146 xTempSubStorage2.isStreamElement( "SubStream2" );
147 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
148 return false;
150 catch( com.sun.star.lang.DisposedException de )
153 catch( Exception e )
155 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
156 return false;
159 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
160 return false;
162 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
163 return false;
165 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
166 return false;
168 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
169 return false;
171 // dispose used storages to free resources
172 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
173 return false;
176 // now check all the written and copied information
179 // the temporary file must not be locked any more after storage disposing
180 pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
181 Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
182 XStorage xResStorage = UnoRuntime.queryInterface( XStorage.class, oResStorage );
183 if ( xResStorage == null )
185 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
186 return false;
189 // open and check SubStorage1
190 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
191 "SubStorage1",
192 ElementModes.READ );
193 if ( xResSubStorage1 == null )
195 m_aTestHelper.Error( "Can't open existing substorage!" );
196 return false;
199 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
200 return false;
203 // open and check SubStorage2
204 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
205 "SubStorage2",
206 ElementModes.READ );
207 if ( xResSubStorage2 == null )
209 m_aTestHelper.Error( "Can't open existing substorage!" );
210 return false;
213 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
214 return false;
217 // check all the result streams
219 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1 ) )
220 return false;
222 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1 ) )
223 return false;
225 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1 ) )
226 return false;
228 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2 ) )
229 return false;
231 // the storage must be disposed before removing
232 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
233 return false;
235 // remove element and check that it was removed completely
236 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
237 return false;
241 XNameAccess xResAccess = UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
242 if ( xResAccess.hasByName( "SubStorage2" ) )
243 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
245 catch( Exception e )
247 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
248 return false;
253 xResSubStorage2.isStreamElement( "SubStream2" );
255 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
256 return false;
258 catch( com.sun.star.lang.DisposedException de )
261 catch( Exception e )
263 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
264 return false;
267 // dispose used storages to free resources
268 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
269 return false;
271 return true;
273 catch( Exception e )
275 m_aTestHelper.Error( "Exception: " + e );
276 return false;
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */