xref: /aoo42x/main/package/qa/storages/Test12.java (revision cdf0e10c)
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 import com.sun.star.io.XStream;
11 import com.sun.star.io.XInputStream;
12 
13 import com.sun.star.embed.*;
14 
15 import share.LogWriter;
16 import complex.storages.TestHelper;
17 import complex.storages.StorageTest;
18 
19 public class Test12 implements StorageTest {
20 
21 	XMultiServiceFactory m_xMSF;
22 	XSingleServiceFactory m_xStorageFactory;
23 	TestHelper m_aTestHelper;
24 
25 	public Test12( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
26 	{
27 		m_xMSF = xMSF;
28 		m_xStorageFactory = xStorageFactory;
29 		m_aTestHelper = new TestHelper( aLogWriter, "Test12: " );
30 	}
31 
32     public boolean test()
33 	{
34 		try
35 		{
36 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
37 			if ( xTempFileStream == null )
38 				return false;
39 
40 			// create storage based on the temporary stream
41 			Object pArgs[] = new Object[2];
42 			pArgs[0] = (Object) xTempFileStream;
43 			pArgs[1] = new Integer( ElementModes.WRITE );
44 
45 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
46 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
47 			if ( xTempStorage == null )
48 			{
49 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
50 				return false;
51 			}
52 
53 			// open a new substorage
54 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
55 																	"SubStorage1",
56 																	ElementModes.WRITE );
57 			if ( xTempSubStorage == null )
58 			{
59 				m_aTestHelper.Error( "Can't create substorage!" );
60 				return false;
61 			}
62 
63             byte pBigBytes[] = new byte[33000];
64 			for ( int nInd = 0; nInd < 33000; nInd++ )
65 				pBigBytes[nInd] = (byte)( nInd % 128 );
66 
67 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
68 
69 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
70 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
71 				return false;
72 
73 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
74 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
75 				return false;
76 
77 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
78 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
79 															"MediaType2",
80 															true,
81 															ElementModes.WRITE ) )
82 				return false;
83 
84 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
85 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
86 															"MediaType3",
87 															false,
88 															ElementModes.WRITE ) )
89 				return false;
90 
91 			// commit substorage first
92 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
93 				return false;
94 
95 			// commit the root storage so the contents must be stored now
96 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
97 				return false;
98 
99 			// dispose substorage
100 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
101 				return false;
102 
103 			// ================================================
104 			// check substorage
105 			// ================================================
106 
107 			if ( !checkSubStorages( xTempStorage, pBytes1, pBigBytes ) )
108 				return false;
109 
110 			// dispose used storage to free resources
111 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
112 				return false;
113 
114 			// ================================================
115 			// now check all the written information with readwrite access
116 			// ================================================
117 
118 			Object oResWriteStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
119 			XStorage xResWriteStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResWriteStorage );
120 			if ( xResWriteStorage == null )
121 			{
122 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
123 				return false;
124 			}
125 
126 			if ( !m_aTestHelper.checkStorageProperties( xResWriteStorage, "MediaType2", true, ElementModes.WRITE ) )
127 				return false;
128 
129 			if( !checkSubStorages( xResWriteStorage, pBytes1, pBigBytes ) )
130 				return false;
131 
132 			// try to open for writing after opening for reading
133 			XStorage xResWSubStorage = m_aTestHelper.openSubStorage( xResWriteStorage,
134 																	"SubStorage1",
135 																	ElementModes.WRITE );
136 			if ( xResWSubStorage == null )
137 			{
138 				m_aTestHelper.Error( "Can't open substorage for writing after it was opened for reading!" );
139 				return false;
140 			}
141 
142 			if ( !m_aTestHelper.checkStorageProperties( xResWSubStorage, "MediaType3", false, ElementModes.WRITE ) )
143 				return false;
144 
145 			if ( !m_aTestHelper.checkStream( xResWSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
146 				return false;
147 
148 			if ( !m_aTestHelper.checkStream( xResWSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
149 				return false;
150 
151 			// dispose used storage to free resources
152 			if ( !m_aTestHelper.disposeStorage( xResWriteStorage ) )
153 				return false;
154 
155 
156 			// ================================================
157 			// now check all the written information with readonly access
158 			// ================================================
159 
160 			// close the output part of the temporary stream
161 			// the output part must present since we already wrote to the stream
162 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
163 				return false;
164 
165 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
166 			if ( xTempInStream == null )
167 				return false;
168 
169 			// open input stream
170 			// since no mode is provided the result storage must be opened readonly
171 			Object pOneArg[] = new Object[1];
172 			pOneArg[0] = (Object) xTempInStream;
173 
174 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
175 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
176 			if ( xResultStorage == null )
177 			{
178 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
179 				return false;
180 			}
181 
182 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.READ ) )
183 				return false;
184 
185 			if( !checkSubStorages( xResultStorage, pBytes1, pBigBytes ) )
186 				return false;
187 
188 			return true;
189 		}
190 		catch( Exception e )
191 		{
192 			m_aTestHelper.Error( "Exception: " + e );
193 			return false;
194 		}
195     }
196 
197 	private boolean checkSubStorages( XStorage xStorage, byte[] pBytes1, byte[] pBigBytes )
198 	{
199 		XStorage xReadSubStorage1 = m_aTestHelper.openSubStorage( xStorage,
200 																"SubStorage1",
201 																ElementModes.READ );
202 
203 		XStorage xReadSubStorage2 = m_aTestHelper.openSubStorage( xStorage,
204 																"SubStorage1",
205 																ElementModes.READ );
206 
207 		if ( xReadSubStorage1 == null || xReadSubStorage2 == null )
208 		{
209 			m_aTestHelper.Error( "Can't open substorage for reading!" );
210 			return false;
211 		}
212 
213 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage1, "MediaType3", false, ElementModes.READ ) )
214 			return false;
215 
216 		if ( !m_aTestHelper.checkStorageProperties( xReadSubStorage2, "MediaType3", false, ElementModes.READ ) )
217 			return false;
218 
219 		if ( !m_aTestHelper.checkStream( xReadSubStorage1, "SubStream1", "MediaType1", true, pBytes1 ) )
220 			return false;
221 
222 		if ( !m_aTestHelper.checkStream( xReadSubStorage1, "BigSubStream1", "MediaType1", true, pBigBytes ) )
223 			return false;
224 
225 		if ( !m_aTestHelper.checkStream( xReadSubStorage2, "SubStream1", "MediaType1", true, pBytes1 ) )
226 			return false;
227 
228 		if ( !m_aTestHelper.checkStream( xReadSubStorage2, "BigSubStream1", "MediaType1", true, pBigBytes ) )
229 			return false;
230 
231 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage1 ) )
232 			return false;
233 
234 		if ( !m_aTestHelper.disposeStorage( xReadSubStorage2 ) )
235 			return false;
236 
237 		return true;
238 	}
239 }
240 
241