Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Storage / Test05.java
blob571251bb276dc94a4a265b479be54b1aa444c0ab
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;
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 )
35 m_xMSF = xMSF;
36 m_xStorageFactory = xStorageFactory;
37 m_aTestHelper = new TestHelper( "Test05: " );
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 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!" );
61 return false;
64 // open a new substorage
65 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
66 "SubStorage1",
67 ElementModes.WRITE );
68 if ( xTempSubStorage == null )
70 m_aTestHelper.Error( "Can't create substorage!" );
71 return false;
74 // open a new substorage
75 XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
76 "SubSubStorage1",
77 ElementModes.WRITE );
78 if ( xSubSubStorage == null )
80 m_aTestHelper.Error( "Can't create substorage!" );
81 return false;
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 ) )
89 return false;
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 ) )
95 return false;
97 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
99 "MediaType3",
100 true,
101 ElementModes.WRITE ) )
102 return false;
104 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
105 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
106 "MediaType4",
107 false,
108 ElementModes.WRITE ) )
109 return false;
111 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
112 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
113 "MediaType5",
114 false,
115 ElementModes.WRITE ) )
116 return false;
119 // commit all the storages
120 if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
121 return false;
123 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
124 return false;
126 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
127 return false;
129 // try to open an opened substorage, open call must fail
130 if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
131 return false;
134 // reopen created streams
135 XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
136 "SubStream1",
137 ElementModes.READWRITE | ElementModes.NOCREATE );
138 XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
139 "SubStream2",
140 ElementModes.READ | ElementModes.NOCREATE );
141 if ( xSubStream1 == null || xSubStream2 == null )
142 return false;
144 // it should be possible to have more than one copy of stream for reading
145 XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
146 "SubStream2",
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 ) )
153 return false;
155 // the second stream can not be open for writing
156 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE ) )
157 return false;
160 // dispose xTestSubStorage, all the subtree must be disposed
161 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
162 return false;
164 // check that subtree was disposed correctly
167 xSubSubStorage.isStreamElement( "SubStream1" );
168 m_aTestHelper.Error( "Substorage was not disposed!" );
169 return false;
171 catch ( com.sun.star.lang.DisposedException de )
173 catch ( Exception e )
175 m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
176 return false;
181 xSubStream1.getInputStream();
182 m_aTestHelper.Error( "Writeable substream was not disposed!" );
183 return false;
185 catch ( com.sun.star.lang.DisposedException de )
187 catch ( Exception e )
189 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
190 return false;
195 xSubStream2.getInputStream();
196 m_aTestHelper.Error( "Readonly substream was not disposed!" );
197 return false;
199 catch ( com.sun.star.lang.DisposedException de )
201 catch ( Exception e )
203 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
204 return false;
208 // dispose root storage
209 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
210 return false;
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!" );
223 return false;
226 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
227 return false;
229 // open existing substorage
230 XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
231 "SubStorage1",
232 ElementModes.READ );
233 if ( xResSubStorage == null )
235 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
236 return false;
239 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.READ ) )
240 return false;
242 // open existing substorage
243 XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
244 "SubSubStorage1",
245 ElementModes.READ );
246 if ( xResSubSubStorage == null )
248 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
249 return false;
252 if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.READ ) )
253 return false;
255 // check substreams
256 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
257 return false;
259 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
260 return false;
262 // dispose used storages to free resources
263 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
264 return false;
266 return true;
268 catch( Exception e )
270 m_aTestHelper.Error( "Exception: " + e );
271 return false;