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