Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Storage / Test04.java
blob2390a9140992a844350bd2c4efd43e0272a4bfc7
1 /*
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;
21 import com.sun.star.uno.UnoRuntime;
22 import com.sun.star.container.XNameAccess;
24 import com.sun.star.embed.*;
26 public class Test04 implements StorageTest {
28 XMultiServiceFactory m_xMSF;
29 XSingleServiceFactory m_xStorageFactory;
30 TestHelper m_aTestHelper;
32 public Test04( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
34 m_xMSF = xMSF;
35 m_xStorageFactory = xStorageFactory;
36 m_aTestHelper = new TestHelper( "Test04: " );
39 public boolean test()
41 try
43 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
44 if ( sTempFileURL == null || sTempFileURL.equals("") )
46 m_aTestHelper.Error( "No valid temporary file was created!" );
47 return false;
50 // create temporary storage based on arbitrary medium
51 // after such a storage is closed it is lost
52 Object oTempStorage = m_xStorageFactory.createInstance();
53 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
54 if ( xTempStorage == null )
56 m_aTestHelper.Error( "Can't create temporary storage representation!" );
57 return false;
60 // open substorages and create streams there
62 // first substorage of the root storage
63 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
64 "SubStorage1",
65 ElementModes.WRITE );
66 if ( xTempSubStorage1 == null )
68 m_aTestHelper.Error( "Can't create substorage!" );
69 return false;
72 byte pBytes1[] = { 1, 1, 1, 1, 1 };
74 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
75 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
76 return false;
78 // second substorage of the root storage
79 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
80 "SubStorage2",
81 ElementModes.WRITE );
82 if ( xTempSubStorage2 == null )
84 m_aTestHelper.Error( "Can't create substorage!" );
85 return false;
88 byte pBytes2[] = { 2, 2, 2, 2, 2 };
90 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage2, "SubStream2", "MediaType2", false, pBytes2 ) )
92 return false;
94 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
95 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
96 "MediaType3",
97 true,
98 ElementModes.READWRITE ) )
99 return false;
101 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
102 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage1,
103 "MediaType4",
104 false,
105 ElementModes.WRITE ) )
106 return false;
108 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage2,
110 "MediaType5",
111 false,
112 ElementModes.WRITE ) )
113 return false;
115 // create temporary storage based on a previously created temporary file
116 Object pArgs[] = new Object[2];
117 pArgs[0] = sTempFileURL;
118 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
120 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
121 XStorage xTempFileStorage = UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
122 if ( xTempFileStorage == null )
124 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
125 return false;
128 if ( !m_aTestHelper.copyElementTo( xTempStorage, "SubStorage1", xTempFileStorage ) )
129 return false;
131 // if storage is not committed before disposing all the changes will be lost
132 if ( !m_aTestHelper.commitStorage( xTempSubStorage2 ) )
133 return false;
135 // a storage must be disposed before moving/removing otherwise the access will be denied
136 if ( !m_aTestHelper.disposeStorage( xTempSubStorage2 ) )
137 return false;
139 if ( !m_aTestHelper.moveElementTo( xTempStorage, "SubStorage2", xTempFileStorage ) )
140 return false;
142 // SubStorage2 must be removed and disposed now
145 xTempSubStorage2.isStreamElement( "SubStream2" );
146 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
147 return false;
149 catch( com.sun.star.lang.DisposedException de )
152 catch( Exception e )
154 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
155 return false;
158 if ( !m_aTestHelper.copyElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
159 return false;
161 if ( !m_aTestHelper.renameElement( xTempFileStorage, "SubStream1", "SubStream1_copy" ) )
162 return false;
164 if ( !m_aTestHelper.moveElementTo( xTempSubStorage1, "SubStream1", xTempFileStorage ) )
165 return false;
167 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
168 return false;
170 // dispose used storages to free resources
171 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
172 return false;
175 // now check all the written and copied information
178 // the temporary file must not be locked any more after storage disposing
179 pArgs[1] = Integer.valueOf( ElementModes.READWRITE );
180 Object oResStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
181 XStorage xResStorage = UnoRuntime.queryInterface( XStorage.class, oResStorage );
182 if ( xResStorage == null )
184 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
185 return false;
188 // open and check SubStorage1
189 XStorage xResSubStorage1 = m_aTestHelper.openSubStorage( xResStorage,
190 "SubStorage1",
191 ElementModes.READ );
192 if ( xResSubStorage1 == null )
194 m_aTestHelper.Error( "Can't open existing substorage!" );
195 return false;
198 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage1, "MediaType4", false, ElementModes.READ ) )
199 return false;
202 // open and check SubStorage2
203 XStorage xResSubStorage2 = m_aTestHelper.openSubStorage( xResStorage,
204 "SubStorage2",
205 ElementModes.READ );
206 if ( xResSubStorage2 == null )
208 m_aTestHelper.Error( "Can't open existing substorage!" );
209 return false;
212 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage2, "MediaType5", false, ElementModes.READ ) )
213 return false;
216 // check all the result streams
218 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1", "MediaType1", pBytes1 ) )
219 return false;
221 if ( !m_aTestHelper.checkStream( xResStorage, "SubStream1_copy", "MediaType1", pBytes1 ) )
222 return false;
224 if ( !m_aTestHelper.checkStream( xResSubStorage1, "SubStream1", "MediaType1", pBytes1 ) )
225 return false;
227 if ( !m_aTestHelper.checkStream( xResSubStorage2, "SubStream2", "MediaType2", pBytes2 ) )
228 return false;
230 // the storage must be disposed before removing
231 if ( !m_aTestHelper.disposeStorage( xResSubStorage2 ) )
232 return false;
234 // remove element and check that it was removed completelly
235 if ( !m_aTestHelper.removeElement( xResStorage, "SubStorage2" ) )
236 return false;
240 XNameAccess xResAccess = UnoRuntime.queryInterface( XNameAccess.class, xResStorage );
241 if ( xResAccess.hasByName( "SubStorage2" ) )
242 m_aTestHelper.Error( "SubStorage2 must be removed already!" );
244 catch( Exception e )
246 m_aTestHelper.Error( "Can't get access to root storage, exception: " + e );
247 return false;
252 xResSubStorage2.isStreamElement( "SubStream2" );
254 m_aTestHelper.Error( "SubStorage2 must be disposed already!" );
255 return false;
257 catch( com.sun.star.lang.DisposedException de )
260 catch( Exception e )
262 m_aTestHelper.Error( "Wrong exception in case of disposed storage, exception: " + e );
263 return false;
266 // dispose used storages to free resources
267 if ( !m_aTestHelper.disposeStorage( xResStorage ) )
268 return false;
270 return true;
272 catch( Exception e )
274 m_aTestHelper.Error( "Exception: " + e );
275 return false;