xref: /trunk/main/package/qa/storages/Test18.java (revision cdf0e10c)
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.embed.*;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import share.LogWriter;
14*cdf0e10cSrcweir import complex.storages.TestHelper;
15*cdf0e10cSrcweir import complex.storages.StorageTest;
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir public class Test18 implements StorageTest {
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
20*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
21*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
22*cdf0e10cSrcweir 
23*cdf0e10cSrcweir 	public Test18( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
24*cdf0e10cSrcweir 	{
25*cdf0e10cSrcweir 		m_xMSF = xMSF;
26*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
27*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test18: " );
28*cdf0e10cSrcweir 	}
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir     public boolean test()
31*cdf0e10cSrcweir 	{
32*cdf0e10cSrcweir 		try
33*cdf0e10cSrcweir 		{
34*cdf0e10cSrcweir             // test the default value of Compressed property
35*cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
36*cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
37*cdf0e10cSrcweir 			{
38*cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
39*cdf0e10cSrcweir 				return false;
40*cdf0e10cSrcweir 			}
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
43*cdf0e10cSrcweir 			// after such a storage is closed it is lost
44*cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
45*cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
46*cdf0e10cSrcweir 			if ( xTempStorage == null )
47*cdf0e10cSrcweir 			{
48*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
49*cdf0e10cSrcweir 				return false;
50*cdf0e10cSrcweir 			}
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 			// open a new substorage
53*cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
54*cdf0e10cSrcweir 																		"SubStorage1",
55*cdf0e10cSrcweir 																		ElementModes.WRITE );
56*cdf0e10cSrcweir 			if ( xTempSubStorage == null )
57*cdf0e10cSrcweir 			{
58*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
59*cdf0e10cSrcweir 				return false;
60*cdf0e10cSrcweir 			}
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
65*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream1", "image/jpeg", pBytes1 ) )
66*cdf0e10cSrcweir 				return false;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
69*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream2", "image/png", pBytes1 ) )
70*cdf0e10cSrcweir 				return false;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
73*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream3", "image/gif", pBytes1 ) )
74*cdf0e10cSrcweir 				return false;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
77*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstreamDefaultCompressed( xTempSubStorage, "SubStream4", "MediaType1", pBytes1 ) )
78*cdf0e10cSrcweir 				return false;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
81*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
82*cdf0e10cSrcweir 															"MediaType3",
83*cdf0e10cSrcweir 															true,
84*cdf0e10cSrcweir 															ElementModes.WRITE ) )
85*cdf0e10cSrcweir 				return false;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
89*cdf0e10cSrcweir 															"MediaType4",
90*cdf0e10cSrcweir 															false,
91*cdf0e10cSrcweir 															ElementModes.WRITE ) )
92*cdf0e10cSrcweir 				return false;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
95*cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
96*cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
97*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
100*cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
101*cdf0e10cSrcweir 			if ( xTempFileStorage == null )
102*cdf0e10cSrcweir 			{
103*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
104*cdf0e10cSrcweir 				return false;
105*cdf0e10cSrcweir 			}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 			// copy xTempStorage to xTempFileStorage
108*cdf0e10cSrcweir 			// xTempFileStorage will be automatically commited
109*cdf0e10cSrcweir 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
110*cdf0e10cSrcweir 				return false;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 			// dispose used storages to free resources
113*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
114*cdf0e10cSrcweir 				return false;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 			// ================================================
117*cdf0e10cSrcweir 			// now check all the written and copied information
118*cdf0e10cSrcweir 			// ================================================
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
121*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
122*cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
123*cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
124*cdf0e10cSrcweir 			if ( xResultStorage == null )
125*cdf0e10cSrcweir 			{
126*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
127*cdf0e10cSrcweir 				return false;
128*cdf0e10cSrcweir 			}
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.WRITE ) )
131*cdf0e10cSrcweir 				return false;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 			// open existing substorage
134*cdf0e10cSrcweir 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
135*cdf0e10cSrcweir 																		"SubStorage1",
136*cdf0e10cSrcweir 																		ElementModes.READ );
137*cdf0e10cSrcweir 			if ( xResultSubStorage == null )
138*cdf0e10cSrcweir 			{
139*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't open existing substorage!" );
140*cdf0e10cSrcweir 				return false;
141*cdf0e10cSrcweir 			}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
144*cdf0e10cSrcweir 				return false;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "image/jpeg", false, pBytes1 ) )
147*cdf0e10cSrcweir 				return false;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "image/png", false, pBytes1 ) )
150*cdf0e10cSrcweir 				return false;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "image/gif", false, pBytes1 ) )
153*cdf0e10cSrcweir 				return false;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream4", "MediaType1", true, pBytes1 ) )
156*cdf0e10cSrcweir 				return false;
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 			// dispose used storages to free resources
159*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
160*cdf0e10cSrcweir 				return false;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 			return true;
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 		catch( Exception e )
165*cdf0e10cSrcweir 		{
166*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
167*cdf0e10cSrcweir 			return false;
168*cdf0e10cSrcweir 		}
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173