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.ofopxmlstorages;
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.ofopxmlstorages.TestHelper;
40 import complex.ofopxmlstorages.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 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
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.WRITE );
88 			XStorage xTempSubStorage2 = m_aTestHelper.openSubStorage( xTempStorage,
89 																	"SubStorage2",
90 																	ElementModes.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.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.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 			XStorage xTargetStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
211 			if ( xTargetStorage == null )
212 			{
213 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
214 				return false;
215 			}
216 
217 			// open a new substorage
218 			XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
219 																	"SubStorage1",
220 																	ElementModes.WRITE );
221 			if ( xTargetSubStorage == null )
222 			{
223 				m_aTestHelper.Error( "Can't create substorage!" );
224 				return false;
225 			}
226 
227 			// in case of copying of nonexistent element an exception must be thrown
228 			try
229 			{
230 				xTempStorage.copyElementTo( "Nonexistent element", xTargetStorage, "Target" );
231 				m_aTestHelper.Error( "An exception must be thrown in case of copying of nonexisting element!" );
232 				return false;
233 			}
234 			catch( com.sun.star.container.NoSuchElementException ne )
235 			{}
236 			catch( Exception e )
237 			{
238 				m_aTestHelper.Error( "Unexpected excepion in case of copying of nonexistent element: " + e );
239 				return false;
240 			}
241 
242 			// in case of moving of nonexistent element an exception must be thrown
243 			try
244 			{
245 				xTempStorage.moveElementTo( "Nonexistent element", xTargetStorage, "Target" );
246 				m_aTestHelper.Error( "An exception must be thrown in case of moving of nonexisting element!" );
247 				return false;
248 			}
249 			catch( com.sun.star.container.NoSuchElementException ne )
250 			{}
251 			catch( Exception e )
252 			{
253 				m_aTestHelper.Error( "Unexpected excepion in case of moving of nonexistent element: " + e );
254 				return false;
255 			}
256 
257 			// in case target for copying already exists an exception must be thrown
258 			try
259 			{
260 				xTempStorage.copyElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
261 				m_aTestHelper.Error( "An exception must be thrown in case target for copying already exists!" );
262 				return false;
263 			}
264 			catch( com.sun.star.container.ElementExistException ee )
265 			{}
266 			catch( Exception e )
267 			{
268 				m_aTestHelper.Error( "Unexpected excepion in case target for copying already exists: " + e );
269 				return false;
270 			}
271 
272 			// in case target for moving already exists an exception must be thrown
273 			try
274 			{
275 				xTempStorage.moveElementTo( "SubStorage1", xTargetStorage, "SubStorage1" );
276 				m_aTestHelper.Error( "An exception must be thrown in case target for moving already exists!" );
277 				return false;
278 			}
279 			catch( com.sun.star.container.ElementExistException ee )
280 			{}
281 			catch( Exception e )
282 			{
283 				m_aTestHelper.Error( "Unexpected excepion in case target for moving already exists: " + e );
284 				return false;
285 			}
286 
287 
288 			return true;
289 		}
290 		catch( Exception e )
291 		{
292 			m_aTestHelper.Error( "Exception: " + e );
293 			return false;
294 		}
295     }
296 
297 }
298 
299