xref: /AOO41X/main/odk/examples/java/Storage/Test06.java (revision ae15d43ae9bc0d57b88b38bfa728519a0f7db121)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package storagetesting;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 
32 import com.sun.star.lang.IllegalArgumentException;
33 import com.sun.star.container.NoSuchElementException;
34 import com.sun.star.container.ElementExistException;
35 
36 import com.sun.star.embed.*;
37 
38 import storagetesting.TestHelper;
39 import storagetesting.StorageTest;
40 
41 public class Test06 implements StorageTest {
42 
43     XMultiServiceFactory m_xMSF;
44     XSingleServiceFactory m_xStorageFactory;
45     TestHelper m_aTestHelper;
46 
Test06( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )47     public Test06( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
48     {
49         m_xMSF = xMSF;
50         m_xStorageFactory = xStorageFactory;
51         m_aTestHelper = new TestHelper( "Test06: " );
52     }
53 
test()54     public boolean test()
55     {
56         try
57         {
58             // create temporary storage based on arbitrary medium
59             // after such a storage is closed it is lost
60             Object oTempStorage = m_xStorageFactory.createInstance();
61             XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
62             if ( xTempStorage == null )
63             {
64                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
65                 return false;
66             }
67 
68             try
69             {
70                 xTempStorage.copyToStorage( null );
71                 m_aTestHelper.Error( "The method must throw an exception because of illegal parameter!" );
72                 return false;
73             }
74             catch( com.sun.star.lang.IllegalArgumentException iae )
75             {}
76             catch( com.sun.star.uno.Exception ue )
77             {}
78             catch( Exception e )
79             {
80                 m_aTestHelper.Error( "Unexpected excepion because of illegal parameter : " + e );
81                 return false;
82             }
83 
84             // open new substorages
85             XStorage xTempSubStorage1 = m_aTestHelper.openSubStorage( xTempStorage,
86                                                                     "SubStorage1",
87                                                                     ElementModes.ELEMENT_WRITE );
88             XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
89                                                                     "SubStorage2",
90                                                                     ElementModes.ELEMENT_WRITE );
91             if ( xTempSubStorage1 == null || xTempSubStorage2 == null )
92             {
93                 m_aTestHelper.Error( "Can't create substorage!" );
94                 return false;
95             }
96 
97             // in case stream is open for reading it must exist
98             try
99             {
100                 xTempSubStorage1.openStreamElement( "NonExistingStream", ElementModes.ELEMENT_READ );
101                 m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent stream for reading!" );
102                 return false;
103             }
104             catch( com.sun.star.uno.Exception ue )
105             {}
106             catch( Exception e )
107             {
108                 m_aTestHelper.Error( "Unexpected excepion in case of try to open nonexistent stream for reading : " + e );
109                 return false;
110             }
111 
112             // in case a storage is open for reading it must exist
113             try
114             {
115                 xTempSubStorage1.openStreamElement( "NonExistingStorage", ElementModes.ELEMENT_READ );
116                 m_aTestHelper.Error( "The method must throw an exception in case of try to open nonexistent storage for reading!" );
117                 return false;
118             }
119             catch( com.sun.star.uno.Exception ue )
120             {}
121             catch( Exception e )
122             {
123                 m_aTestHelper.Error( "Unexpected excepion in case of try to open nonexistent storage for reading : " + e );
124                 return false;
125             }
126 
127             // in case of removing nonexistent element an exception must be thrown
128             try
129             {
130                 xTempSubStorage1.removeElement( "NonExistingElement" );
131                 m_aTestHelper.Error( "An exception must be thrown in case of removing nonexistent element!" );
132                 return false;
133             }
134             catch( com.sun.star.container.NoSuchElementException ne )
135             {}
136             catch( Exception e )
137             {
138                 m_aTestHelper.Error( "Unexpected excepion in case of try to remove nonexistent element : " + e );
139                 return false;
140             }
141 
142             // in case of renaming of nonexistent element an exception must be thrown
143             try
144             {
145                 xTempSubStorage1.renameElement( "NonExistingElement", "NewName" );
146                 m_aTestHelper.Error( "An exception must be thrown in case of renaming nonexistent element!" );
147                 return false;
148             }
149             catch( com.sun.star.container.NoSuchElementException ne )
150             {}
151             catch( Exception e )
152             {
153                 m_aTestHelper.Error( "Unexpected excepion in case of try to rename nonexistent element : " + e );
154                 return false;
155             }
156 
157             // in case of renaming to a name of existent element an exception must be thrown
158             try
159             {
160                 xTempStorage.renameElement( "SubStorage1", "SubStorage2" );
161                 m_aTestHelper.Error( "An exception must be thrown in case of renaming to the name of existent element!" );
162                 return false;
163             }
164             catch( com.sun.star.container.ElementExistException ee )
165             {}
166             catch( Exception e )
167             {
168                 m_aTestHelper.Error( "Unexpected excepion in case of try to rename to the name of existent element : " + e );
169                 return false;
170             }
171 
172             // in case of copying target storage must be provided
173             try
174             {
175                 xTempStorage.copyElementTo( "SubStorage1", null, "SubStorage1" );
176                 m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for copying!" );
177                 return false;
178             }
179             catch( com.sun.star.lang.IllegalArgumentException iae )
180             {}
181             catch( com.sun.star.uno.Exception ue )
182             {}
183             catch( Exception e )
184             {
185                 m_aTestHelper.Error( "Unexpected excepion in case empty reference is provieded as target for copying : " + e );
186                 return false;
187             }
188 
189             // in case of moving target storage must be provided
190             try
191             {
192                 xTempStorage.moveElementTo( "SubStorage1", null, "SubStorage1" );
193                 m_aTestHelper.Error( "An exception must be thrown in case empty reference is provided as target for moving!" );
194                 return false;
195             }
196             catch( com.sun.star.lang.IllegalArgumentException iae )
197             {}
198             catch( com.sun.star.uno.Exception ue )
199             {}
200             catch( Exception e )
201             {
202                 m_aTestHelper.Error( "Unexpected excepion in case empty reference is provieded as target for moving : " + e );
203                 return false;
204             }
205 
206 
207             // prepare target for further testings
208 
209             // create new temporary storage based on arbitrary medium
210             Object oTargetStorage = m_xStorageFactory.createInstance();
211             XStorage xTargetStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTargetStorage );
212             if ( xTargetStorage == null )
213             {
214                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
215                 return false;
216             }
217 
218             // open a new substorage
219             XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
220                                                                     "SubStorage1",
221                                                                     ElementModes.ELEMENT_WRITE );
222             if ( xTargetSubStorage == null )
223             {
224                 m_aTestHelper.Error( "Can't create substorage!" );
225                 return false;
226             }
227 
228             // in case of copying of nonexistent element an exception must be thrown
229             try
230             {
231                 xTempStorage.copyElementTo( "Nonexistent element", xTargetStorage, "Target" );
232                 m_aTestHelper.Error( "An exception must be thrown in case of copying of nonexisting element!" );
233                 return false;
234             }
235             catch( com.sun.star.container.NoSuchElementException ne )
236             {}
237             catch( Exception e )
238             {
239                 m_aTestHelper.Error( "Unexpected excepion in case of copying of nonexistent element: " + e );
240                 return false;
241             }
242 
243             // in case of moving of nonexistent element an exception must be thrown
244             try
245             {
246                 xTempStorage.moveElementTo( "Nonexistent element", xTargetStorage, "Target" );
247                 m_aTestHelper.Error( "An exception must be thrown in case of moving of nonexisting element!" );
248                 return false;
249             }
250             catch( com.sun.star.container.NoSuchElementException ne )
251             {}
252             catch( Exception e )
253             {
254                 m_aTestHelper.Error( "Unexpected excepion in case of moving of nonexistent element: " + e );
255                 return false;
256             }
257 
258             // in case target for copying already exists an exception must be thrown
259             try
260             {
261                 xTempStorage.copyElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
262                 m_aTestHelper.Error( "An exception must be thrown in case target for copying already exists!" );
263                 return false;
264             }
265             catch( com.sun.star.container.ElementExistException ee )
266             {}
267             catch( Exception e )
268             {
269                 m_aTestHelper.Error( "Unexpected excepion in case target for copying already exists: " + e );
270                 return false;
271             }
272 
273             // in case target for moving already exists an exception must be thrown
274             try
275             {
276                 xTempStorage.moveElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
277                 m_aTestHelper.Error( "An exception must be thrown in case target for moving already exists!" );
278                 return false;
279             }
280             catch( com.sun.star.container.ElementExistException ee )
281             {}
282             catch( Exception e )
283             {
284                 m_aTestHelper.Error( "Unexpected excepion in case target for moving already exists: " + e );
285                 return false;
286             }
287 
288 
289             return true;
290         }
291         catch( Exception e )
292         {
293             m_aTestHelper.Error( "Exception: " + e );
294             return false;
295         }
296     }
297 
298 }
299 
300