xref: /aoo42x/main/package/qa/storages/Test14.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 Test14 implements StorageTest {
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
20*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
21*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
22*cdf0e10cSrcweir 
23*cdf0e10cSrcweir 	public Test14( 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, "Test14: " );
28*cdf0e10cSrcweir 	}
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir     public boolean test()
31*cdf0e10cSrcweir 	{
32*cdf0e10cSrcweir 		String aStreamPrefix = "";
33*cdf0e10cSrcweir 		for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
34*cdf0e10cSrcweir 			if ( !testForPath( aStreamPrefix ) )
35*cdf0e10cSrcweir 				return false;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 		return true;
38*cdf0e10cSrcweir 	}
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 	public boolean testForPath( String aStreamPrefix )
41*cdf0e10cSrcweir 	{
42*cdf0e10cSrcweir 		try
43*cdf0e10cSrcweir 		{
44*cdf0e10cSrcweir 			String aSubStream1Path = aStreamPrefix + "SubStream1";
45*cdf0e10cSrcweir 			String aSubStream2Path = aStreamPrefix + "SubStream2";
46*cdf0e10cSrcweir 			String aSubStream3Path = aStreamPrefix + "SubStream3";
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
49*cdf0e10cSrcweir 			if ( sTempFileURL == null || sTempFileURL == "" )
50*cdf0e10cSrcweir 			{
51*cdf0e10cSrcweir 				m_aTestHelper.Error( "No valid temporary file was created!" );
52*cdf0e10cSrcweir 				return false;
53*cdf0e10cSrcweir 			}
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 			// create temporary storage based on a previously created temporary file
56*cdf0e10cSrcweir 			Object pArgs[] = new Object[2];
57*cdf0e10cSrcweir 			pArgs[0] = (Object) sTempFileURL;
58*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.WRITE );
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
61*cdf0e10cSrcweir 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
62*cdf0e10cSrcweir 			if ( xTempFileStorage == null )
63*cdf0e10cSrcweir 			{
64*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
65*cdf0e10cSrcweir 				return false;
66*cdf0e10cSrcweir 			}
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
69*cdf0e10cSrcweir 			String sPass1 = "12345";
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
72*cdf0e10cSrcweir 			// and commit
73*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, sPass1, true ) )
74*cdf0e10cSrcweir 				return false;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
77*cdf0e10cSrcweir 			String sPass2 = "54321";
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
80*cdf0e10cSrcweir 			// and commit
81*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) )
82*cdf0e10cSrcweir 				return false;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
85*cdf0e10cSrcweir 			// and don't commit
86*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType2", false, pBytes2, sPass2, false ) )
87*cdf0e10cSrcweir 				return false;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
91*cdf0e10cSrcweir 															"MediaType3",
92*cdf0e10cSrcweir 															true,
93*cdf0e10cSrcweir 															ElementModes.WRITE ) )
94*cdf0e10cSrcweir 				return false;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
97*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
98*cdf0e10cSrcweir 				return false;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 			// dispose used storages to free resources
101*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
102*cdf0e10cSrcweir 				return false;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 			// ================================================
105*cdf0e10cSrcweir 			// now reopen the storage,
106*cdf0e10cSrcweir 			// check all the written and copied information
107*cdf0e10cSrcweir 			// and change it
108*cdf0e10cSrcweir 			// ================================================
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
111*cdf0e10cSrcweir 			oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
112*cdf0e10cSrcweir 			xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
113*cdf0e10cSrcweir 			if ( xTempFileStorage == null )
114*cdf0e10cSrcweir 			{
115*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
116*cdf0e10cSrcweir 				return false;
117*cdf0e10cSrcweir 			}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
120*cdf0e10cSrcweir 				return false;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) )
123*cdf0e10cSrcweir 				return false;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", pBytes2, sPass2 ) )
126*cdf0e10cSrcweir 				return false;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 			if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream3Path, ElementModes.READ, sPass2 ) )
129*cdf0e10cSrcweir 				return false;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
132*cdf0e10cSrcweir 			// and commit
133*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType3", true, pBytes2, sPass1, true ) )
134*cdf0e10cSrcweir 				return false;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
137*cdf0e10cSrcweir 			// and don't commit
138*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType3", true, pBytes1, sPass2, false ) )
139*cdf0e10cSrcweir 				return false;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 			// commit the root storage so the contents must be stored now
142*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
143*cdf0e10cSrcweir 				return false;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			// dispose used storages to free resources
146*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
147*cdf0e10cSrcweir 				return false;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 			// ================================================
150*cdf0e10cSrcweir 			// now reopen the storage,
151*cdf0e10cSrcweir 			// check all the written information
152*cdf0e10cSrcweir 			// ================================================
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 			// the temporary file must not be locked any more after storage disposing
155*cdf0e10cSrcweir 			pArgs[1] = new Integer( ElementModes.READ );
156*cdf0e10cSrcweir 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
157*cdf0e10cSrcweir 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
158*cdf0e10cSrcweir 			if ( xResultStorage == null )
159*cdf0e10cSrcweir 			{
160*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
161*cdf0e10cSrcweir 				return false;
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
165*cdf0e10cSrcweir 				return false;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream1Path, "MediaType3", pBytes2, sPass1 ) )
168*cdf0e10cSrcweir 				return false;
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 			// the following stream was not commited last time, so the last change must be lost
171*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream2Path, "MediaType2", pBytes2, sPass2 ) )
172*cdf0e10cSrcweir 				return false;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 			// dispose used storages to free resources
175*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
176*cdf0e10cSrcweir 				return false;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 			return true;
179*cdf0e10cSrcweir 		}
180*cdf0e10cSrcweir 		catch( Exception e )
181*cdf0e10cSrcweir 		{
182*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
183*cdf0e10cSrcweir 			return false;
184*cdf0e10cSrcweir 		}
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189