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