update dev300-m58
[ooovba.git] / package / qa / storages / Test01.java
blobb722c3a4ec24071a1189bdb6e3780d5ce1ce7a91
1 package complex.storages;
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
11 import com.sun.star.embed.*;
13 import share.LogWriter;
14 import complex.storages.TestHelper;
15 import complex.storages.StorageTest;
17 public class Test01 implements StorageTest {
19 XMultiServiceFactory m_xMSF;
20 XSingleServiceFactory m_xStorageFactory;
21 TestHelper m_aTestHelper;
23 public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
25 m_xMSF = xMSF;
26 m_xStorageFactory = xStorageFactory;
27 m_aTestHelper = new TestHelper( aLogWriter, "Test01: " );
30 public boolean test()
32 try
34 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
35 if ( sTempFileURL == null || sTempFileURL == "" )
37 m_aTestHelper.Error( "No valid temporary file was created!" );
38 return false;
41 // create temporary storage based on arbitrary medium
42 // after such a storage is closed it is lost
43 Object oTempStorage = m_xStorageFactory.createInstance();
44 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
45 if ( xTempStorage == null )
47 m_aTestHelper.Error( "Can't create temporary storage representation!" );
48 return false;
51 // open a new substorage
52 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
53 "SubStorage1",
54 ElementModes.WRITE );
55 if ( xTempSubStorage == null )
57 m_aTestHelper.Error( "Can't create substorage!" );
58 return false;
61 byte pBigBytes[] = new byte[33000];
62 for ( int nInd = 0; nInd < 33000; nInd++ )
63 pBigBytes[nInd] = (byte)( nInd % 128 );
65 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
66 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
67 return false;
69 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
70 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
71 return false;
73 byte pBytes1[] = { 1, 1, 1, 1, 1 };
75 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
76 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
77 return false;
79 byte pBytes2[] = { 2, 2, 2, 2, 2 };
81 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
82 if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
83 return false;
85 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
86 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
87 "MediaType3",
88 true,
89 ElementModes.WRITE ) )
90 return false;
92 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
93 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
94 "MediaType4",
95 false,
96 ElementModes.WRITE ) )
97 return false;
99 // create temporary storage based on a previously created temporary file
100 Object pArgs[] = new Object[2];
101 pArgs[0] = (Object) sTempFileURL;
102 pArgs[1] = new Integer( ElementModes.WRITE );
104 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
105 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
106 if ( xTempFileStorage == null )
108 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
109 return false;
112 // copy xTempStorage to xTempFileStorage
113 // xTempFileStorage will be automatically commited
114 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
115 return false;
117 // dispose used storages to free resources
118 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
119 return false;
121 // ================================================
122 // now check all the written and copied information
123 // ================================================
125 // the temporary file must not be locked any more after storage disposing
126 pArgs[1] = new Integer( ElementModes.WRITE );
127 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
128 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
129 if ( xResultStorage == null )
131 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
132 return false;
135 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.WRITE ) )
136 return false;
138 // open existing substorage
139 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
140 "SubStorage1",
141 ElementModes.READ );
142 if ( xResultSubStorage == null )
144 m_aTestHelper.Error( "Can't open existing substorage!" );
145 return false;
148 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
149 return false;
151 if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
152 return false;
154 if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
155 return false;
157 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
158 return false;
160 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
161 return false;
163 // dispose used storages to free resources
164 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
165 return false;
167 return true;
169 catch( Exception e )
171 m_aTestHelper.Error( "Exception: " + e );
172 return false;