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