Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Storage / Test06.java
blobd3b1cb097fd6f21ceaa0f421aa6c9ccbc8354a86
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.embed.*;
25 public class Test06 implements StorageTest {
27 XMultiServiceFactory m_xMSF;
28 XSingleServiceFactory m_xStorageFactory;
29 TestHelper m_aTestHelper;
31 public Test06( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
33 m_xMSF = xMSF;
34 m_xStorageFactory = xStorageFactory;
35 m_aTestHelper = new TestHelper( "Test06: " );
38 public boolean test()
40 try
42 // create temporary storage based on arbitrary medium
43 // after such a storage is closed it is lost
44 Object oTempStorage = m_xStorageFactory.createInstance();
45 XStorage xTempStorage = UnoRuntime.queryInterface( XStorage.class, oTempStorage );
46 if ( xTempStorage == null )
48 m_aTestHelper.Error( "Can't create temporary storage representation!" );
49 return false;
52 try
54 xTempStorage.copyToStorage( null );
55 m_aTestHelper.Error( "The method must throw an exception because of illegal parameter!" );
56 return false;
58 catch( com.sun.star.lang.IllegalArgumentException iae )
60 catch( com.sun.star.uno.Exception ue )
62 catch( Exception e )
64 m_aTestHelper.Error( "Unexpected excepion because of illegal parameter : " + e );
65 return false;
68 // open new substorages
69 XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
70 "SubStorage1",
71 ElementModes.WRITE );
72 XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
73 "SubStorage2",
74 ElementModes.WRITE );
75 if ( xTempSubStorage1 == null || xTempSubStorage2 == null )
77 m_aTestHelper.Error( "Can't create substorage!" );
78 return false;
81 // in case stream is open for reading it must exist
82 try
84 xTempSubStorage1.openStreamElement( "NonExistingStream", ElementModes.READ );
85 m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent stream for reading!" );
86 return false;
88 catch( com.sun.star.uno.Exception ue )
90 catch( Exception e )
92 m_aTestHelper.Error( "Unexpected excepion in case of try to open nonexistent stream for reading : " + e );
93 return false;
96 // in case a storage is open for reading it must exist
97 try
99 xTempSubStorage1.openStreamElement( "NonExistingStorage", ElementModes.READ );
100 m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent storage for reading!" );
101 return false;
103 catch( com.sun.star.uno.Exception ue )
105 catch( Exception e )
107 m_aTestHelper.Error( "Unexpected excepion in case of try to open nonexistent storage for reading : " + e );
108 return false;
111 // in case of removing nonexistent element an exception must be thrown
114 xTempSubStorage1.removeElement( "NonExistingElement" );
115 m_aTestHelper.Error( "An exception must be thrown in case of removing nonexistent element!" );
116 return false;
118 catch( com.sun.star.container.NoSuchElementException ne )
120 catch( Exception e )
122 m_aTestHelper.Error( "Unexpected excepion in case of try to remove nonexistent element : " + e );
123 return false;
126 // in case of renaming of nonexistent element an exception must be thrown
129 xTempSubStorage1.renameElement( "NonExistingElement", "NewName" );
130 m_aTestHelper.Error( "An exception must be thrown in case of renaming nonexistent element!" );
131 return false;
133 catch( com.sun.star.container.NoSuchElementException ne )
135 catch( Exception e )
137 m_aTestHelper.Error( "Unexpected excepion in case of try to rename nonexistent element : " + e );
138 return false;
141 // in case of renaming to a name of existent element an exception must be thrown
144 xTempStorage.renameElement( "SubStorage1", "SubStorage2" );
145 m_aTestHelper.Error( "An exception must be thrown in case of renaming to the name of existent element!" );
146 return false;
148 catch( com.sun.star.container.ElementExistException ee )
150 catch( Exception e )
152 m_aTestHelper.Error( "Unexpected excepion in case of try to rename to the name of existent element : " + e );
153 return false;
156 // in case of copying target storage must be provided
159 xTempStorage.copyElementTo( "SubStorage1", null, "SubStorage1" );
160 m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for copying!" );
161 return false;
163 catch( com.sun.star.lang.IllegalArgumentException iae )
165 catch( com.sun.star.uno.Exception ue )
167 catch( Exception e )
169 m_aTestHelper.Error( "Unexpected excepion in case empty reference is provieded as target for copying : " + e );
170 return false;
173 // in case of moving target storage must be provided
176 xTempStorage.moveElementTo( "SubStorage1", null, "SubStorage1" );
177 m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for moving!" );
178 return false;
180 catch( com.sun.star.lang.IllegalArgumentException iae )
182 catch( com.sun.star.uno.Exception ue )
184 catch( Exception e )
186 m_aTestHelper.Error( "Unexpected excepion in case empty reference is provieded as target for moving : " + e );
187 return false;
191 // prepare target for further testings
193 // create new temporary storage based on arbitrary medium
194 Object oTargetStorage = m_xStorageFactory.createInstance();
195 XStorage xTargetStorage = UnoRuntime.queryInterface( XStorage.class, oTargetStorage );
196 if ( xTargetStorage == null )
198 m_aTestHelper.Error( "Can't create temporary storage representation!" );
199 return false;
202 // open a new substorage
203 XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
204 "SubStorage1",
205 ElementModes.WRITE );
206 if ( xTargetSubStorage == null )
208 m_aTestHelper.Error( "Can't create substorage!" );
209 return false;
212 // in case of copying of nonexistent element an exception must be thrown
215 xTempStorage.copyElementTo( "Nonexistent element", xTargetStorage, "Target" );
216 m_aTestHelper.Error( "An exception must be thrown in case of copying of nonexisting element!" );
217 return false;
219 catch( com.sun.star.container.NoSuchElementException ne )
221 catch( Exception e )
223 m_aTestHelper.Error( "Unexpected excepion in case of copying of nonexistent element: " + e );
224 return false;
227 // in case of moving of nonexistent element an exception must be thrown
230 xTempStorage.moveElementTo( "Nonexistent element", xTargetStorage, "Target" );
231 m_aTestHelper.Error( "An exception must be thrown in case of moving of nonexisting element!" );
232 return false;
234 catch( com.sun.star.container.NoSuchElementException ne )
236 catch( Exception e )
238 m_aTestHelper.Error( "Unexpected excepion in case of moving of nonexistent element: " + e );
239 return false;
242 // in case target for copying already exists an exception must be thrown
245 xTempStorage.copyElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
246 m_aTestHelper.Error( "An exception must be thrown in case target for copying already exists!" );
247 return false;
249 catch( com.sun.star.container.ElementExistException ee )
251 catch( Exception e )
253 m_aTestHelper.Error( "Unexpected excepion in case target for copying already exists: " + e );
254 return false;
257 // in case target for moving already exists an exception must be thrown
260 xTempStorage.moveElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
261 m_aTestHelper.Error( "An exception must be thrown in case target for moving already exists!" );
262 return false;
264 catch( com.sun.star.container.ElementExistException ee )
266 catch( Exception e )
268 m_aTestHelper.Error( "Unexpected excepion in case target for moving already exists: " + e );
269 return false;
273 return true;
275 catch( Exception e )
277 m_aTestHelper.Error( "Exception: " + e );
278 return false;