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