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