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