xref: /aoo42x/main/package/qa/storages/Test14.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 Test14 implements StorageTest {
18 
19 	XMultiServiceFactory m_xMSF;
20 	XSingleServiceFactory m_xStorageFactory;
21 	TestHelper m_aTestHelper;
22 
23 	public Test14( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
24 	{
25 		m_xMSF = xMSF;
26 		m_xStorageFactory = xStorageFactory;
27 		m_aTestHelper = new TestHelper( aLogWriter, "Test14: " );
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 
48 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
49 			if ( sTempFileURL == null || sTempFileURL == "" )
50 			{
51 				m_aTestHelper.Error( "No valid temporary file was created!" );
52 				return false;
53 			}
54 
55 			// create temporary storage based on a previously created temporary file
56 			Object pArgs[] = new Object[2];
57 			pArgs[0] = (Object) sTempFileURL;
58 			pArgs[1] = new Integer( ElementModes.WRITE );
59 
60 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
61 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
62 			if ( xTempFileStorage == null )
63 			{
64 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
65 				return false;
66 			}
67 
68 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
69 			String sPass1 = "12345";
70 
71 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
72 			// and commit
73 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, sPass1, true ) )
74 				return false;
75 
76 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
77 			String sPass2 = "54321";
78 
79 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
80 			// and commit
81 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) )
82 				return false;
83 
84 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
85 			// and don't commit
86 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType2", false, pBytes2, sPass2, false ) )
87 				return false;
88 
89 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
90 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
91 															"MediaType3",
92 															true,
93 															ElementModes.WRITE ) )
94 				return false;
95 
96 			// commit the root storage so the contents must be stored now
97 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
98 				return false;
99 
100 			// dispose used storages to free resources
101 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
102 				return false;
103 
104 			// ================================================
105 			// now reopen the storage,
106 			// check all the written and copied information
107 			// and change it
108 			// ================================================
109 
110 			// the temporary file must not be locked any more after storage disposing
111 			oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
112 			xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
113 			if ( xTempFileStorage == null )
114 			{
115 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
116 				return false;
117 			}
118 
119 			if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
120 				return false;
121 
122 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) )
123 				return false;
124 
125 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", pBytes2, sPass2 ) )
126 				return false;
127 
128 			if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream3Path, ElementModes.READ, sPass2 ) )
129 				return false;
130 
131 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
132 			// and commit
133 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType3", true, pBytes2, sPass1, true ) )
134 				return false;
135 
136 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
137 			// and don't commit
138 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType3", true, pBytes1, sPass2, false ) )
139 				return false;
140 
141 			// commit the root storage so the contents must be stored now
142 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
143 				return false;
144 
145 			// dispose used storages to free resources
146 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
147 				return false;
148 
149 			// ================================================
150 			// now reopen the storage,
151 			// check all the written information
152 			// ================================================
153 
154 			// the temporary file must not be locked any more after storage disposing
155 			pArgs[1] = new Integer( ElementModes.READ );
156 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
157 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
158 			if ( xResultStorage == null )
159 			{
160 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
161 				return false;
162 			}
163 
164 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
165 				return false;
166 
167 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream1Path, "MediaType3", pBytes2, sPass1 ) )
168 				return false;
169 
170 			// the following stream was not commited last time, so the last change must be lost
171 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream2Path, "MediaType2", pBytes2, sPass2 ) )
172 				return false;
173 
174 			// dispose used storages to free resources
175 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
176 				return false;
177 
178 			return true;
179 		}
180 		catch( Exception e )
181 		{
182 			m_aTestHelper.Error( "Exception: " + e );
183 			return false;
184 		}
185     }
186 
187 }
188 
189