xref: /aoo42x/main/package/qa/storages/Test09.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 Test09 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test09: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		try
54 		{
55 
56 			// create temporary storage based on arbitrary medium
57 			// after such a storage is closed it is lost
58 			Object oTempStorage = m_xStorageFactory.createInstance();
59 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
60 			if ( xTempStorage == null )
61 			{
62 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
63 				return false;
64 			}
65 
66 			String sPass1 = "123";
67 			String sPass2 = "321";
68 			byte pBytes[] = { 1, 1, 1, 1, 1 };
69             byte pBigBytes[] = new byte[33000];
70 			for ( int nInd = 0; nInd < 33000; nInd++ )
71 				pBigBytes[nInd] = (byte)( nInd % 128 );
72 
73 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
74 			// the stream will not be encrypted
75 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, sPass1 ) )
76 				return false;
77             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "BigSubStream1", "MediaType1", false, pBigBytes, sPass1 ) )
78 				return false;
79 
80 			// create temporary file
81 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
82 			if ( sTempFileURL == null || sTempFileURL == "" )
83 			{
84 				m_aTestHelper.Error( "No valid temporary file was created!" );
85 				return false;
86 			}
87 
88 			// create temporary storage based on a previously created temporary file
89 			Object pArgs[] = new Object[2];
90 			pArgs[0] = (Object) sTempFileURL;
91 			pArgs[1] = new Integer( ElementModes.WRITE );
92 
93 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
94 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
95 			if ( xTempFileStorage == null )
96 			{
97 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
98 				return false;
99 			}
100 
101 			// copy xTempStorage to xTempFileStorage
102 			// xTempFileStorage will be automatically committed
103 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
104 				return false;
105 
106 			// change password of the substream of new storage based on file
107 			int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", sPass1, sPass2 );
108 			if ( nResult == 0 )
109 				return false; // test failed
110 			else if ( nResult == -1 )
111 				return true; // tested optional feature is not supported
112 
113 			// change password of the substream of new storage based on file
114 			nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "BigSubStream1", sPass1, sPass2 );
115 			if ( nResult == 0 )
116 				return false; // test failed
117 			else if ( nResult == -1 )
118 				return true; // tested optional feature is not supported
119 
120 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
121 				return false;
122 
123 			// dispose used storages to free resources
124 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
125 				return false;
126 
127 			// ================================================
128 			// now check all the written and copied information
129 			// ================================================
130 
131 			// the temporary file must not be locked any more after storage disposing
132 			pArgs[1] = new Integer( ElementModes.READ );
133 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
134 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
135 			if ( xResultStorage == null )
136 			{
137 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
138 				return false;
139 			}
140 
141 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, sPass2 ) )
142 				return false;
143 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass2 ) )
144 				return false;
145 
146 			// dispose used storages to free resources
147 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
148 				return false;
149 
150 			return true;
151 		}
152 		catch( Exception e )
153 		{
154 			m_aTestHelper.Error( "Exception: " + e );
155 			return false;
156 		}
157     }
158 }
159 
160