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