xref: /aoo41x/main/package/qa/storages/Test10.java (revision cdf0e10c)
1*cdf0e10cSrcweir package complex.storages;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
4*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
5*cdf0e10cSrcweir 
6*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
7*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
8*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
9*cdf0e10cSrcweir 
10*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
11*cdf0e10cSrcweir import com.sun.star.io.XStream;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import com.sun.star.embed.*;
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir import share.LogWriter;
16*cdf0e10cSrcweir import complex.storages.TestHelper;
17*cdf0e10cSrcweir import complex.storages.StorageTest;
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir public class Test10 implements StorageTest {
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
22*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
23*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
24*cdf0e10cSrcweir 
25*cdf0e10cSrcweir 	public Test10( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
26*cdf0e10cSrcweir 	{
27*cdf0e10cSrcweir 		m_xMSF = xMSF;
28*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
29*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test10: " );
30*cdf0e10cSrcweir 	}
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir     public boolean test()
33*cdf0e10cSrcweir 	{
34*cdf0e10cSrcweir 		try
35*cdf0e10cSrcweir 		{
36*cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
37*cdf0e10cSrcweir 			// after such a storage is closed it is lost
38*cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
39*cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
40*cdf0e10cSrcweir 			if ( xTempStorage == null )
41*cdf0e10cSrcweir 			{
42*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
43*cdf0e10cSrcweir 				return false;
44*cdf0e10cSrcweir 			}
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
47*cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
48*cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
53*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
54*cdf0e10cSrcweir 				return false;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
57*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
58*cdf0e10cSrcweir 				return false;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 			// open a new substorage
62*cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
63*cdf0e10cSrcweir 																		"SubStorage1",
64*cdf0e10cSrcweir 																		ElementModes.WRITE );
65*cdf0e10cSrcweir 			if ( xTempSubStorage == null )
66*cdf0e10cSrcweir 			{
67*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
68*cdf0e10cSrcweir 				return false;
69*cdf0e10cSrcweir 			}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
74*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", true, pBytes2 ) )
75*cdf0e10cSrcweir 				return false;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
78*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", true, pBigBytes ) )
79*cdf0e10cSrcweir 				return false;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
82*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
83*cdf0e10cSrcweir 															"MediaType3",
84*cdf0e10cSrcweir 															true,
85*cdf0e10cSrcweir 															ElementModes.WRITE ) )
86*cdf0e10cSrcweir 				return false;
87*cdf0e10cSrcweir 
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 			// ==============================
95*cdf0e10cSrcweir 			// check cloning at current state
96*cdf0e10cSrcweir 			// ==============================
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 			// the new storage still was not commited so the clone must be empty
99*cdf0e10cSrcweir 			XStorage xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" );
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 			if ( xClonedSubStorage == null )
102*cdf0e10cSrcweir 			{
103*cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of clone is empty!" );
104*cdf0e10cSrcweir 				return false;
105*cdf0e10cSrcweir 			}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 			XNameAccess xClonedNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xClonedSubStorage );
108*cdf0e10cSrcweir 			if ( xClonedNameAccess == null )
109*cdf0e10cSrcweir 			{
110*cdf0e10cSrcweir 				m_aTestHelper.Error( "XNameAccess is not implemented by the clone!" );
111*cdf0e10cSrcweir 				return false;
112*cdf0e10cSrcweir 			}
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "", true, ElementModes.WRITE ) )
115*cdf0e10cSrcweir 				return false;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 			if ( xClonedNameAccess.hasElements() )
118*cdf0e10cSrcweir 			{
119*cdf0e10cSrcweir 				m_aTestHelper.Error( "The new substorage still was not commited so it must be empty!" );
120*cdf0e10cSrcweir 				return false;
121*cdf0e10cSrcweir 			}
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xClonedSubStorage ) )
124*cdf0e10cSrcweir 				return false;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 			xClonedSubStorage = null;
127*cdf0e10cSrcweir 			xClonedNameAccess = null;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 			// the new stream was opened, written and closed, that means flashed
130*cdf0e10cSrcweir 			// so the clone must contain all the information
131*cdf0e10cSrcweir 			XStream xClonedSubStream = m_aTestHelper.cloneSubStream( xTempStorage, "SubStream1" );
132*cdf0e10cSrcweir 			if ( !m_aTestHelper.InternalCheckStream( xClonedSubStream, "SubStream1", "MediaType1", true, pBytes1, true ) )
133*cdf0e10cSrcweir 				return false;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 			XStream xClonedBigSubStream = m_aTestHelper.cloneSubStream( xTempStorage, "BigSubStream1" );
136*cdf0e10cSrcweir 			if ( !m_aTestHelper.InternalCheckStream( xClonedBigSubStream, "BigSubStream1", "MediaType1", true, pBigBytes, true ) )
137*cdf0e10cSrcweir 				return false;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStream( xClonedSubStream, "SubStream1" ) )
140*cdf0e10cSrcweir 				return false;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStream( xClonedBigSubStream, "BigSubStream1" ) )
143*cdf0e10cSrcweir 				return false;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			// ==============================
146*cdf0e10cSrcweir 			// commit substorage and check cloning
147*cdf0e10cSrcweir 			// ==============================
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
150*cdf0e10cSrcweir 				return false;
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 			xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xStorageFactory, xTempStorage, "SubStorage1" );
153*cdf0e10cSrcweir 			if ( xClonedSubStorage == null )
154*cdf0e10cSrcweir 			{
155*cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of clone is empty!" );
156*cdf0e10cSrcweir 				return false;
157*cdf0e10cSrcweir 			}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage, "MediaType4", true, ElementModes.WRITE ) )
160*cdf0e10cSrcweir 				return false;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xClonedSubStorage, "SubStream2", "MediaType2", true, pBytes2 ) )
163*cdf0e10cSrcweir 				return false;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xClonedSubStorage, "BigSubStream2", "MediaType2", true, pBigBytes ) )
166*cdf0e10cSrcweir 				return false;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 			XStorage xCloneOfRoot = m_aTestHelper.cloneStorage( m_xStorageFactory, xTempStorage );
169*cdf0e10cSrcweir 			if ( xCloneOfRoot == null )
170*cdf0e10cSrcweir 			{
171*cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of root clone is empty!" );
172*cdf0e10cSrcweir 				return false;
173*cdf0e10cSrcweir 			}
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 			XNameAccess xCloneOfRootNA = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xCloneOfRoot );
176*cdf0e10cSrcweir 			if ( xCloneOfRootNA == null )
177*cdf0e10cSrcweir 			{
178*cdf0e10cSrcweir 				m_aTestHelper.Error( "XNameAccess is not implemented by the root clone!" );
179*cdf0e10cSrcweir 				return false;
180*cdf0e10cSrcweir 			}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 			if ( xCloneOfRootNA.hasElements() )
183*cdf0e10cSrcweir 			{
184*cdf0e10cSrcweir 				m_aTestHelper.Error( "The root storage still was not commited so it's clone must be empty!" );
185*cdf0e10cSrcweir 				return false;
186*cdf0e10cSrcweir 			}
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xCloneOfRoot ) )
189*cdf0e10cSrcweir 				return false;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 			xCloneOfRoot = null;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			// ==============================
194*cdf0e10cSrcweir 			// commit root storage and check cloning
195*cdf0e10cSrcweir 			// ==============================
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
198*cdf0e10cSrcweir 				return false;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 			xCloneOfRoot = m_aTestHelper.cloneStorage( m_xStorageFactory, xTempStorage );
201*cdf0e10cSrcweir 			if ( xCloneOfRoot == null )
202*cdf0e10cSrcweir 			{
203*cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of root clone is empty!" );
204*cdf0e10cSrcweir 				return false;
205*cdf0e10cSrcweir 			}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 			XStorage xSubStorageOfClone = xCloneOfRoot.openStorageElement( "SubStorage1", ElementModes.READ );
208*cdf0e10cSrcweir 			if ( xSubStorageOfClone == null )
209*cdf0e10cSrcweir 			{
210*cdf0e10cSrcweir 				m_aTestHelper.Error( "The result of root clone is wrong!" );
211*cdf0e10cSrcweir 				return false;
212*cdf0e10cSrcweir 			}
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xSubStorageOfClone, "MediaType4", false, ElementModes.READ ) )
215*cdf0e10cSrcweir 				return false;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xSubStorageOfClone, "SubStream2", "MediaType2", true, pBytes2 ) )
218*cdf0e10cSrcweir 				return false;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStream( xSubStorageOfClone, "BigSubStream2", "MediaType2", true, pBigBytes ) )
221*cdf0e10cSrcweir 				return false;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 			return true;
224*cdf0e10cSrcweir 		}
225*cdf0e10cSrcweir 		catch( Exception e )
226*cdf0e10cSrcweir 		{
227*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
228*cdf0e10cSrcweir 			return false;
229*cdf0e10cSrcweir 		}
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233