xref: /aoo42x/main/package/qa/storages/Test03.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 import com.sun.star.container.XNameAccess;
13*cdf0e10cSrcweir 
14*cdf0e10cSrcweir import share.LogWriter;
15*cdf0e10cSrcweir import complex.storages.TestHelper;
16*cdf0e10cSrcweir import complex.storages.StorageTest;
17*cdf0e10cSrcweir 
18*cdf0e10cSrcweir public class Test03 implements StorageTest {
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir 	XMultiServiceFactory m_xMSF;
21*cdf0e10cSrcweir 	XSingleServiceFactory m_xStorageFactory;
22*cdf0e10cSrcweir 	TestHelper m_aTestHelper;
23*cdf0e10cSrcweir 
24*cdf0e10cSrcweir 	public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
25*cdf0e10cSrcweir 	{
26*cdf0e10cSrcweir 		m_xMSF = xMSF;
27*cdf0e10cSrcweir 		m_xStorageFactory = xStorageFactory;
28*cdf0e10cSrcweir 		m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
29*cdf0e10cSrcweir 	}
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir     public boolean test()
32*cdf0e10cSrcweir 	{
33*cdf0e10cSrcweir 		try
34*cdf0e10cSrcweir 		{
35*cdf0e10cSrcweir 			// create temporary storage based on arbitrary medium
36*cdf0e10cSrcweir 			// after such a storage is closed it is lost
37*cdf0e10cSrcweir 			Object oTempStorage = m_xStorageFactory.createInstance();
38*cdf0e10cSrcweir 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
39*cdf0e10cSrcweir 			if ( xTempStorage == null )
40*cdf0e10cSrcweir 			{
41*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
42*cdf0e10cSrcweir 				return false;
43*cdf0e10cSrcweir 			}
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 			// open a new substorage
46*cdf0e10cSrcweir 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
47*cdf0e10cSrcweir 																		"SubStorage1",
48*cdf0e10cSrcweir 																		ElementModes.WRITE );
49*cdf0e10cSrcweir 			if ( xTempSubStorage == null )
50*cdf0e10cSrcweir 			{
51*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't create substorage!" );
52*cdf0e10cSrcweir 				return false;
53*cdf0e10cSrcweir 			}
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir             byte pBigBytes[] = new byte[33000];
56*cdf0e10cSrcweir 			for ( int nInd = 0; nInd < 33000; nInd++ )
57*cdf0e10cSrcweir 				pBigBytes[nInd] = (byte)( nInd % 128 );
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
60*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
61*cdf0e10cSrcweir 				return false;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
64*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
65*cdf0e10cSrcweir 				return false;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
70*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
71*cdf0e10cSrcweir 				return false;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76*cdf0e10cSrcweir 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
77*cdf0e10cSrcweir 				return false;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
80*cdf0e10cSrcweir 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
81*cdf0e10cSrcweir 															"MediaType3",
82*cdf0e10cSrcweir 															false,
83*cdf0e10cSrcweir 															ElementModes.WRITE ) )
84*cdf0e10cSrcweir 				return false;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
87*cdf0e10cSrcweir 				return false;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
90*cdf0e10cSrcweir 				return false;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 			// ================================================
93*cdf0e10cSrcweir 			// check storage hyerarchy tree
94*cdf0e10cSrcweir 			// ================================================
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 			// check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
97*cdf0e10cSrcweir 			try {
98*cdf0e10cSrcweir 				xTempStorage.isStorageElement( "does not exist" );
99*cdf0e10cSrcweir 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
100*cdf0e10cSrcweir 				return false;
101*cdf0e10cSrcweir 			}
102*cdf0e10cSrcweir 			catch( com.sun.star.container.NoSuchElementException ne )
103*cdf0e10cSrcweir 			{
104*cdf0e10cSrcweir 			}
105*cdf0e10cSrcweir 			catch( Exception e )
106*cdf0e10cSrcweir 			{
107*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
108*cdf0e10cSrcweir 				return false;
109*cdf0e10cSrcweir 			}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 			try {
112*cdf0e10cSrcweir 				xTempStorage.isStreamElement( "does not exist" );
113*cdf0e10cSrcweir 				m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
114*cdf0e10cSrcweir 				return false;
115*cdf0e10cSrcweir 			}
116*cdf0e10cSrcweir 			catch( com.sun.star.container.NoSuchElementException ne )
117*cdf0e10cSrcweir 			{
118*cdf0e10cSrcweir 			}
119*cdf0e10cSrcweir 			catch( Exception e )
120*cdf0e10cSrcweir 			{
121*cdf0e10cSrcweir 				m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
122*cdf0e10cSrcweir 				return false;
123*cdf0e10cSrcweir 			}
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 			XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
126*cdf0e10cSrcweir 			if ( xRootNameAccess == null )
127*cdf0e10cSrcweir 			{
128*cdf0e10cSrcweir 				m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
129*cdf0e10cSrcweir 				return false;
130*cdf0e10cSrcweir 			}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 			try {
133*cdf0e10cSrcweir 				if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
134*cdf0e10cSrcweir 				{
135*cdf0e10cSrcweir 					m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
136*cdf0e10cSrcweir 					return false;
137*cdf0e10cSrcweir 				}
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 				if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
140*cdf0e10cSrcweir 				{
141*cdf0e10cSrcweir 					m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
142*cdf0e10cSrcweir 					return false;
143*cdf0e10cSrcweir 				}
144*cdf0e10cSrcweir 			}
145*cdf0e10cSrcweir 			catch( Exception e )
146*cdf0e10cSrcweir 			{
147*cdf0e10cSrcweir 				m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
148*cdf0e10cSrcweir 				return false;
149*cdf0e10cSrcweir 			}
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 			// check that root storage contents are represented correctly
153*cdf0e10cSrcweir 			String sRootCont[] = xRootNameAccess.getElementNames();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 			if ( sRootCont.length != 3 )
156*cdf0e10cSrcweir 			{
157*cdf0e10cSrcweir 				m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
158*cdf0e10cSrcweir 				return false;
159*cdf0e10cSrcweir 			}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir             int nFlag = 0;
162*cdf0e10cSrcweir             for ( int nInd = 0; nInd < sRootCont.length; nInd++ )
163*cdf0e10cSrcweir             {
164*cdf0e10cSrcweir                 if ( sRootCont[nInd].equals( "SubStorage1" ) )
165*cdf0e10cSrcweir                     nFlag |= 1;
166*cdf0e10cSrcweir                 else if ( sRootCont[nInd].equals( "SubStream1" ) )
167*cdf0e10cSrcweir                     nFlag |= 2;
168*cdf0e10cSrcweir                 else if ( sRootCont[nInd].equals( "BigSubStream1" ) )
169*cdf0e10cSrcweir                     nFlag |= 4;
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 			if ( nFlag != 7 || !( xRootNameAccess.hasByName( "BigSubStream1" ) && xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
173*cdf0e10cSrcweir 			{
174*cdf0e10cSrcweir 				m_aTestHelper.Error( "Root storage contains wrong list of children!" );
175*cdf0e10cSrcweir 				return false;
176*cdf0e10cSrcweir 			}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 			// get storage through XNameAccess
179*cdf0e10cSrcweir 			XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
180*cdf0e10cSrcweir 			if ( xResultSubStorage == null )
181*cdf0e10cSrcweir 				return false;
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.READ ) )
184*cdf0e10cSrcweir 				return false;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 			XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
187*cdf0e10cSrcweir 			if ( xChildAccess == null )
188*cdf0e10cSrcweir 			{
189*cdf0e10cSrcweir 				m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
190*cdf0e10cSrcweir 				return false;
191*cdf0e10cSrcweir 			}
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			if ( !( xChildAccess.hasByName( "SubStream2" ) && xChildAccess.hasByName( "BigSubStream2" ) )
194*cdf0e10cSrcweir 			  || !xResultSubStorage.isStreamElement( "SubStream2" )
195*cdf0e10cSrcweir 			  || !xResultSubStorage.isStreamElement( "BigSubStream2" ) )
196*cdf0e10cSrcweir 			{
197*cdf0e10cSrcweir 				m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
198*cdf0e10cSrcweir 				return false;
199*cdf0e10cSrcweir 			}
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 			return true;
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 		catch( Exception e )
204*cdf0e10cSrcweir 		{
205*cdf0e10cSrcweir 			m_aTestHelper.Error( "Exception: " + e );
206*cdf0e10cSrcweir 			return false;
207*cdf0e10cSrcweir 		}
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
211*cdf0e10cSrcweir 	{
212*cdf0e10cSrcweir 		try
213*cdf0e10cSrcweir 		{
214*cdf0e10cSrcweir 			Object oStorage = xAccess.getByName( sName );
215*cdf0e10cSrcweir 			XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 			if ( xResult != null )
218*cdf0e10cSrcweir 				return xResult;
219*cdf0e10cSrcweir 			else
220*cdf0e10cSrcweir 				m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
221*cdf0e10cSrcweir 		}
222*cdf0e10cSrcweir 		catch( Exception e )
223*cdf0e10cSrcweir 		{
224*cdf0e10cSrcweir 			m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
225*cdf0e10cSrcweir 		}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 		return null;
228*cdf0e10cSrcweir 	}
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232