xref: /trunk/main/package/qa/storages/Test01.java (revision a893be29)
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 Test01 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test01: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		try
54 		{
55 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
56 			if ( sTempFileURL == null || sTempFileURL == "" )
57 			{
58 				m_aTestHelper.Error( "No valid temporary file was created!" );
59 				return false;
60 			}
61 
62 			// create temporary storage based on arbitrary medium
63 			// after such a storage is closed it is lost
64 			Object oTempStorage = m_xStorageFactory.createInstance();
65 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
66 			if ( xTempStorage == null )
67 			{
68 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
69 				return false;
70 			}
71 
72 			// open a new substorage
73 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
74 																		"SubStorage1",
75 																		ElementModes.WRITE );
76 			if ( xTempSubStorage == null )
77 			{
78 				m_aTestHelper.Error( "Can't create substorage!" );
79 				return false;
80 			}
81 
82             byte pBigBytes[] = new byte[33000];
83 			for ( int nInd = 0; nInd < 33000; nInd++ )
84 				pBigBytes[nInd] = (byte)( nInd % 128 );
85 
86 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
87 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
88 				return false;
89 
90 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
92 				return false;
93 
94 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
95 
96 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
97 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
98 				return false;
99 
100 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
101 
102 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
103 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
104 				return false;
105 
106 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
107 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
108 															"MediaType3",
109 															true,
110 															ElementModes.WRITE ) )
111 				return false;
112 
113 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
114 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
115 															"MediaType4",
116 															false,
117 															ElementModes.WRITE ) )
118 				return false;
119 
120 			// create temporary storage based on a previously created temporary file
121 			Object pArgs[] = new Object[2];
122 			pArgs[0] = (Object) sTempFileURL;
123 			pArgs[1] = new Integer( ElementModes.WRITE );
124 
125 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
126 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
127 			if ( xTempFileStorage == null )
128 			{
129 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
130 				return false;
131 			}
132 
133 			// copy xTempStorage to xTempFileStorage
134 			// xTempFileStorage will be automatically committed
135 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
136 				return false;
137 
138 			// dispose used storages to free resources
139 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
140 				return false;
141 
142 			// ================================================
143 			// now check all the written and copied information
144 			// ================================================
145 
146 			// the temporary file must not be locked any more after storage disposing
147 			pArgs[1] = new Integer( ElementModes.WRITE );
148 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
149 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
150 			if ( xResultStorage == null )
151 			{
152 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
153 				return false;
154 			}
155 
156 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.WRITE ) )
157 				return false;
158 
159 			// open existing substorage
160 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
161 																		"SubStorage1",
162 																		ElementModes.READ );
163 			if ( xResultSubStorage == null )
164 			{
165 				m_aTestHelper.Error( "Can't open existing substorage!" );
166 				return false;
167 			}
168 
169 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
170 				return false;
171 
172 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes ) )
173 				return false;
174 
175 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
176 				return false;
177 
178 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
179 				return false;
180 
181 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
182 				return false;
183 
184 			// dispose used storages to free resources
185 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
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 }
198 
199