xref: /trunk/main/package/qa/storages/Test09.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 package complex.storages;
2 
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
6 
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
10 
11 import com.sun.star.embed.*;
12 
13 import share.LogWriter;
14 import complex.storages.TestHelper;
15 import complex.storages.StorageTest;
16 
17 public class Test09 implements StorageTest {
18 
19     XMultiServiceFactory m_xMSF;
20     XSingleServiceFactory m_xStorageFactory;
21     TestHelper m_aTestHelper;
22 
23     public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
24     {
25         m_xMSF = xMSF;
26         m_xStorageFactory = xStorageFactory;
27         m_aTestHelper = new TestHelper( aLogWriter, "Test09: " );
28     }
29 
30     public boolean test()
31     {
32         try
33         {
34 
35             // create temporary storage based on arbitrary medium
36             // after such a storage is closed it is lost
37             Object oTempStorage = m_xStorageFactory.createInstance();
38             XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
39             if ( xTempStorage == null )
40             {
41                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
42                 return false;
43             }
44 
45             String sPass1 = "123";
46             String sPass2 = "321";
47             byte pBytes[] = { 1, 1, 1, 1, 1 };
48             byte pBigBytes[] = new byte[33000];
49             for ( int nInd = 0; nInd < 33000; nInd++ )
50                 pBigBytes[nInd] = (byte)( nInd % 128 );
51 
52             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
53             // the stream will not be encrypted
54             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, sPass1 ) )
55                 return false;
56             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", false, pBigBytes, sPass1 ) )
57                 return false;
58 
59             // create temporary file
60             String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
61             if ( sTempFileURL == null || sTempFileURL == "" )
62             {
63                 m_aTestHelper.Error( "No valid temporary file was created!" );
64                 return false;
65             }
66 
67             // create temporary storage based on a previously created temporary file
68             Object pArgs[] = new Object[2];
69             pArgs[0] = (Object) sTempFileURL;
70             pArgs[1] = new Integer( ElementModes.WRITE );
71 
72             Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
73             XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
74             if ( xTempFileStorage == null )
75             {
76                 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
77                 return false;
78             }
79 
80             // copy xTempStorage to xTempFileStorage
81             // xTempFileStorage will be automatically commited
82             if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
83                 return false;
84 
85             // change password of the substream of new storage based on file
86             int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", sPass1, sPass2 );
87             if ( nResult == 0 )
88                 return false; // test failed
89             else if ( nResult == -1 )
90                 return true; // tested optional feature is not supported
91 
92             // change password of the substream of new storage based on file
93             nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "BigSubStream1", sPass1, sPass2 );
94             if ( nResult == 0 )
95                 return false; // test failed
96             else if ( nResult == -1 )
97                 return true; // tested optional feature is not supported
98 
99             if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
100                 return false;
101 
102             // dispose used storages to free resources
103             if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
104                 return false;
105 
106             // ================================================
107             // now check all the written and copied information
108             // ================================================
109 
110             // the temporary file must not be locked any more after storage disposing
111             pArgs[1] = new Integer( ElementModes.READ );
112             Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
113             XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
114             if ( xResultStorage == null )
115             {
116                 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
117                 return false;
118             }
119 
120             if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, sPass2 ) )
121                 return false;
122             if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass2 ) )
123                 return false;
124 
125             // dispose used storages to free resources
126             if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
127                 return false;
128 
129             return true;
130         }
131         catch( Exception e )
132         {
133             m_aTestHelper.Error( "Exception: " + e );
134             return false;
135         }
136     }
137 }
138 
139