xref: /aoo4110/main/package/qa/storages/Test14.java (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 
32 import com.sun.star.embed.*;
33 
34 import share.LogWriter;
35 import complex.storages.TestHelper;
36 import complex.storages.StorageTest;
37 
38 public class Test14 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test14( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test14( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test14: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		String aStreamPrefix = "";
54 		for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
55 			if ( !testForPath( aStreamPrefix ) )
56 				return false;
57 
58 		return true;
59 	}
60 
testForPath( String aStreamPrefix )61 	public boolean testForPath( String aStreamPrefix )
62 	{
63 		try
64 		{
65 			String aSubStream1Path = aStreamPrefix + "SubStream1";
66 			String aSubStream2Path = aStreamPrefix + "SubStream2";
67 			String aSubStream3Path = aStreamPrefix + "SubStream3";
68 
69 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
70 			if ( sTempFileURL == null || sTempFileURL == "" )
71 			{
72 				m_aTestHelper.Error( "No valid temporary file was created!" );
73 				return false;
74 			}
75 
76 			// create temporary storage based on a previously created temporary file
77 			Object pArgs[] = new Object[2];
78 			pArgs[0] = (Object) sTempFileURL;
79 			pArgs[1] = new Integer( ElementModes.WRITE );
80 
81 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
82 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
83 			if ( xTempFileStorage == null )
84 			{
85 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
86 				return false;
87 			}
88 
89 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
90 			String sPass1 = "12345";
91 
92 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
93 			// and commit
94 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, sPass1, true ) )
95 				return false;
96 
97 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
98 			String sPass2 = "54321";
99 
100 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
101 			// and commit
102 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) )
103 				return false;
104 
105 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
106 			// and don't commit
107 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType2", false, pBytes2, sPass2, false ) )
108 				return false;
109 
110 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
111 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
112 															"MediaType3",
113 															true,
114 															ElementModes.WRITE ) )
115 				return false;
116 
117 			// commit the root storage so the contents must be stored now
118 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
119 				return false;
120 
121 			// dispose used storages to free resources
122 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
123 				return false;
124 
125 			// ================================================
126 			// now reopen the storage,
127 			// check all the written and copied information
128 			// and change it
129 			// ================================================
130 
131 			// the temporary file must not be locked any more after storage disposing
132 			oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
133 			xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
134 			if ( xTempFileStorage == null )
135 			{
136 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
137 				return false;
138 			}
139 
140 			if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
141 				return false;
142 
143 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) )
144 				return false;
145 
146 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", pBytes2, sPass2 ) )
147 				return false;
148 
149 			if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream3Path, ElementModes.READ, sPass2 ) )
150 				return false;
151 
152 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
153 			// and commit
154 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType3", true, pBytes2, sPass1, true ) )
155 				return false;
156 
157 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
158 			// and don't commit
159 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType3", true, pBytes1, sPass2, false ) )
160 				return false;
161 
162 			// commit the root storage so the contents must be stored now
163 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
164 				return false;
165 
166 			// dispose used storages to free resources
167 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
168 				return false;
169 
170 			// ================================================
171 			// now reopen the storage,
172 			// check all the written information
173 			// ================================================
174 
175 			// the temporary file must not be locked any more after storage disposing
176 			pArgs[1] = new Integer( ElementModes.READ );
177 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
178 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
179 			if ( xResultStorage == null )
180 			{
181 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
182 				return false;
183 			}
184 
185 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
186 				return false;
187 
188 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream1Path, "MediaType3", pBytes2, sPass1 ) )
189 				return false;
190 
191 			// the following stream was not commited last time, so the last change must be lost
192 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream2Path, "MediaType2", pBytes2, sPass2 ) )
193 				return false;
194 
195 			// dispose used storages to free resources
196 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
197 				return false;
198 
199 			return true;
200 		}
201 		catch( Exception e )
202 		{
203 			m_aTestHelper.Error( "Exception: " + e );
204 			return false;
205 		}
206     }
207 
208 }
209 
210