tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / odk / examples / java / Storage / Test05.java
blob29cf99d529672132923116a01e6690255ba9a3dc
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;
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 )
36 m_xMSF = xMSF;
37 m_xStorageFactory = xStorageFactory;
38 m_aTestHelper = new TestHelper( "Test05: " );
41 public boolean test()
43 try
45 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
46 if ( sTempFileURL == null || sTempFileURL.equals("") )
48 m_aTestHelper.Error( "No valid temporary file was created!" );
49 return false;
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!" );
62 return false;
65 // open a new substorage
66 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempFileStorage,
67 "SubStorage1",
68 ElementModes.WRITE );
69 if ( xTempSubStorage == null )
71 m_aTestHelper.Error( "Can't create substorage!" );
72 return false;
75 // open a new substorage
76 XStorage xSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
77 "SubSubStorage1",
78 ElementModes.WRITE );
79 if ( xSubSubStorage == null )
81 m_aTestHelper.Error( "Can't create substorage!" );
82 return false;
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 ) )
90 return false;
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 ) )
96 return false;
98 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
99 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
100 "MediaType3",
101 true,
102 ElementModes.WRITE ) )
103 return false;
105 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
106 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
107 "MediaType4",
108 false,
109 ElementModes.WRITE ) )
110 return false;
112 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
113 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xSubSubStorage,
114 "MediaType5",
115 false,
116 ElementModes.WRITE ) )
117 return false;
120 // commit all the storages
121 if ( !m_aTestHelper.commitStorage( xSubSubStorage ) )
122 return false;
124 if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
125 return false;
127 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
128 return false;
130 // try to open an opened substorage, open call must fail
131 if ( !m_aTestHelper.cantOpenStorage( xTempFileStorage, "SubStorage1" ) )
132 return false;
135 // reopen created streams
136 XStream xSubStream1 = m_aTestHelper.OpenStream( xSubSubStorage,
137 "SubStream1",
138 ElementModes.READWRITE | ElementModes.NOCREATE );
139 XStream xSubStream2 = m_aTestHelper.OpenStream( xSubSubStorage,
140 "SubStream2",
141 ElementModes.READ | ElementModes.NOCREATE );
142 if ( xSubStream1 == null || xSubStream2 == null )
143 return false;
145 // it should be possible to have more than one copy of stream for reading
146 XStream xSubStream2clone = m_aTestHelper.OpenStream( xSubSubStorage,
147 "SubStream2",
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 ) )
154 return false;
156 // the second stream can not be open for writing
157 if ( !m_aTestHelper.cantOpenStream( xSubSubStorage, "SubStream2", ElementModes.WRITE ) )
158 return false;
161 // dispose xTestSubStorage, all the subtree must be disposed
162 if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
163 return false;
165 // check that subtree was disposed correctly
168 xSubSubStorage.isStreamElement( "SubStream1" );
169 m_aTestHelper.Error( "Substorage was not disposed!" );
170 return false;
172 catch ( com.sun.star.lang.DisposedException de )
174 catch ( Exception e )
176 m_aTestHelper.Error( "Wrong exception is thrown by disposed storage: " + e );
177 return false;
182 xSubStream1.getInputStream();
183 m_aTestHelper.Error( "Writeable substream was not disposed!" );
184 return false;
186 catch ( com.sun.star.lang.DisposedException de )
188 catch ( Exception e )
190 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
191 return false;
196 xSubStream2.getInputStream();
197 m_aTestHelper.Error( "Readonly substream was not disposed!" );
198 return false;
200 catch ( com.sun.star.lang.DisposedException de )
202 catch ( Exception e )
204 m_aTestHelper.Error( "Wrong exception is thrown by disposed stream: " + e );
205 return false;
209 // dispose root storage
210 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
211 return false;
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!" );
224 return false;
227 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
228 return false;
230 // open existing substorage
231 XStorage xResSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
232 "SubStorage1",
233 ElementModes.READ );
234 if ( xResSubStorage == null )
236 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
237 return false;
240 if ( !m_aTestHelper.checkStorageProperties( xResSubStorage, "MediaType4", false, ElementModes.READ ) )
241 return false;
243 // open existing substorage
244 XStorage xResSubSubStorage = m_aTestHelper.openSubStorage( xResSubStorage,
245 "SubSubStorage1",
246 ElementModes.READ );
247 if ( xResSubSubStorage == null )
249 m_aTestHelper.Error( "Can't open existing substorage 'SubSubStorage'!" );
250 return false;
253 if ( !m_aTestHelper.checkStorageProperties( xResSubSubStorage, "MediaType5", false, ElementModes.READ ) )
254 return false;
256 // check substreams
257 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
258 return false;
260 if ( !m_aTestHelper.checkStream( xResSubSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
261 return false;
263 // dispose used storages to free resources
264 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
265 return false;
267 return true;
269 catch( Exception e )
271 m_aTestHelper.Error( "Exception: " + e );
272 return false;
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */