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