xref: /aoo42x/main/package/qa/storages/Test07.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 Test07 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test07: " );
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             byte pBigBytes[] = new byte[33000];
73 			for ( int nInd = 0; nInd < 33000; nInd++ )
74 				pBigBytes[nInd] = (byte)( nInd % 128 );
75 
76 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
77 			String sPass1 = "12345";
78 
79 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
80 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", true, pBigBytes, sPass1 ) )
81 				return false;
82 
83 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
84 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes1, sPass1 ) )
85 				return false;
86 
87 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
88 			String sPass2 = "54321";
89 
90 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
91 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream2", "MediaType2", false, pBigBytes, sPass2 ) )
92 				return false;
93 
94 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream2", "MediaType2", false, pBytes2, sPass2 ) )
96 				return false;
97 
98 			// create temporary storage based on a previously created temporary file
99 			Object pArgs[] = new Object[2];
100 			pArgs[0] = (Object) sTempFileURL;
101 			pArgs[1] = new Integer( ElementModes.WRITE );
102 
103 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
104 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
105 			if ( xTempFileStorage == null )
106 			{
107 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
108 				return false;
109 			}
110 
111 			// copy xTempStorage to xTempFileStorage
112 			// xTempFileStorage will be automatically committed
113 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
114 				return false;
115 
116 			// dispose used storages to free resources
117 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
118 				return false;
119 
120 			// ================================================
121 			// now check all the written and copied information
122 			// ================================================
123 
124 			// the temporary file must not be locked any more after storage disposing
125 			pArgs[1] = new Integer( ElementModes.WRITE );
126 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
127 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
128 			if ( xResultStorage == null )
129 			{
130 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
131 				return false;
132 			}
133 
134 			Object o2CopyStorage = m_xStorageFactory.createInstance();
135 			XStorage x2CopyStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, o2CopyStorage );
136 			if ( x2CopyStorage == null )
137 			{
138 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
139 				return false;
140 			}
141 
142 			if ( !m_aTestHelper.copyStorage( xResultStorage, x2CopyStorage ) )
143 				return false;
144 
145 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
146 				return false;
147 
148 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
149 				return false;
150 
151 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream2", "MediaType2", pBytes2, sPass2 ) )
152 				return false;
153 
154 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) )
155 				return false;
156 
157 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
158 				return false;
159 
160 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
161 				return false;
162 
163 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "SubStream2", "MediaType2", pBytes2, sPass2 ) )
164 				return false;
165 
166 			if ( !m_aTestHelper.checkEncrStream( x2CopyStorage, "BigSubStream2", "MediaType2", pBigBytes, sPass2 ) )
167 				return false;
168 
169 			// dispose used storages to free resources
170 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
171 				return false;
172 
173 			return true;
174 		}
175 		catch( Exception e )
176 		{
177 			m_aTestHelper.Error( "Exception: " + e );
178 			return false;
179 		}
180     }
181 
182 }
183 
184