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