xref: /aoo41x/main/package/qa/storages/Test13.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 
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 Test13 implements StorageTest {
18 
19 	XMultiServiceFactory m_xMSF;
20 	XSingleServiceFactory m_xStorageFactory;
21 	TestHelper m_aTestHelper;
22 
23 	public Test13( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
24 	{
25 		m_xMSF = xMSF;
26 		m_xStorageFactory = xStorageFactory;
27 		m_aTestHelper = new TestHelper( aLogWriter, "Test13: " );
28 	}
29 
30     public boolean test()
31 	{
32 		String aStreamPrefix = "";
33 		for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
34 			if ( !testForPath( aStreamPrefix ) )
35 				return false;
36 
37 		return true;
38 	}
39 
40 	public boolean testForPath( String aStreamPrefix )
41 	{
42 		try
43 		{
44 			String aSubStream1Path = aStreamPrefix + "SubStream1";
45 			String aSubStream2Path = aStreamPrefix + "SubStream2";
46 			String aSubStream3Path = aStreamPrefix + "SubStream3";
47 			String aBigSubStream1Path = aStreamPrefix + "BigSubStream1";
48 			String aBigSubStream2Path = aStreamPrefix + "BigSubStream2";
49 			String aBigSubStream3Path = aStreamPrefix + "BigSubStream3";
50 
51 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
52 			if ( sTempFileURL == null || sTempFileURL == "" )
53 			{
54 				m_aTestHelper.Error( "No valid temporary file was created!" );
55 				return false;
56 			}
57 
58 			// create temporary storage based on a previously created temporary file
59 			Object pArgs[] = new Object[2];
60 			pArgs[0] = (Object) sTempFileURL;
61 			pArgs[1] = new Integer( ElementModes.WRITE );
62 
63 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
64 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
65 			if ( xTempFileStorage == null )
66 			{
67 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
68 				return false;
69 			}
70 
71             byte pBigBytes[] = new byte[33000];
72 			for ( int nInd = 0; nInd < 33000; nInd++ )
73 				pBigBytes[nInd] = (byte)( nInd % 128 );
74 
75 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
76 
77 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
78 			// and commit
79 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, true ) )
80 				return false;
81 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream1Path, "MediaType1", true, pBigBytes, true ) )
82 				return false;
83 
84 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
85 
86 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
87 			// and commit
88 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, true ) )
89 				return false;
90             if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream2Path, "MediaType2", false, pBigBytes, true ) )
91 				return false;
92 
93 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
94 			// and don't commit
95 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream3Path, "MediaType2", false, pBytes2, false ) )
96 				return false;
97 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream3Path, "MediaType2", false, pBigBytes, false ) )
98 				return false;
99 
100 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
101 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
102 															"MediaType3",
103 															true,
104 															ElementModes.WRITE ) )
105 				return false;
106 
107 			// commit the root storage so the contents must be stored now
108 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
109 				return false;
110 
111 			// dispose used storages to free resources
112 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
113 				return false;
114 
115 			// ================================================
116 			// now reopen the storage,
117 			// check all the written and copied information
118 			// and change it
119 			// ================================================
120 
121 			// the temporary file must not be locked any more after storage disposing
122 			oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
123 			xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
124 			if ( xTempFileStorage == null )
125 			{
126 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
127 				return false;
128 			}
129 
130 			if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
131 				return false;
132 
133 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1 ) )
134 				return false;
135 
136 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aBigSubStream1Path, "MediaType1", true, pBigBytes ) )
137 				return false;
138 
139 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2 ) )
140 				return false;
141 
142 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aBigSubStream2Path, "MediaType2", false, pBigBytes ) )
143 				return false;
144 
145 			if ( !m_aTestHelper.cantOpenStreamH( xTempFileStorage, aSubStream3Path, ElementModes.READ ) )
146 				return false;
147 
148 			if ( !m_aTestHelper.cantOpenStreamH( xTempFileStorage, aBigSubStream3Path, ElementModes.READ ) )
149 				return false;
150 
151 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
152 			// and commit
153 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream1Path, "MediaType3", true, pBytes2, true ) )
154 				return false;
155 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream1Path, "MediaType3", true, pBigBytes, true ) )
156 				return false;
157 
158 
159 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
160 			// and don't commit
161 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType3", true, pBytes1, false ) )
162 				return false;
163 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aBigSubStream2Path, "MediaType3", true, pBigBytes, false ) )
164 				return false;
165 
166 			// commit the root storage so the contents must be stored now
167 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
168 				return false;
169 
170 			// dispose used storages to free resources
171 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
172 				return false;
173 
174 			// ================================================
175 			// now reopen the storage,
176 			// check all the written information
177 			// ================================================
178 
179 			// the temporary file must not be locked any more after storage disposing
180 			pArgs[1] = new Integer( ElementModes.READ );
181 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
182 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
183 			if ( xResultStorage == null )
184 			{
185 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
186 				return false;
187 			}
188 
189 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
190 				return false;
191 
192 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream1Path, "MediaType3", true, pBytes2 ) )
193 				return false;
194 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aBigSubStream1Path, "MediaType3", true, pBigBytes ) )
195 				return false;
196 
197 			// the following stream was not commited last time, so the last change must be lost
198 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aBigSubStream2Path, "MediaType2", false, pBigBytes ) )
199 				return false;
200 
201 			// dispose used storages to free resources
202 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
203 				return false;
204 
205 			return true;
206 		}
207 		catch( Exception e )
208 		{
209 			m_aTestHelper.Error( "Exception: " + e );
210 			return false;
211 		}
212     }
213 
214 }
215 
216