1 package storagetesting;
2 
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
6 
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
10 
11 import com.sun.star.embed.*;
12 
13 import storagetesting.TestHelper;
14 import storagetesting.StorageTest;
15 
16 public class Test09 implements StorageTest {
17 
18 	XMultiServiceFactory m_xMSF;
19 	XSingleServiceFactory m_xStorageFactory;
20 	TestHelper m_aTestHelper;
21 
22 	public Test09( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
23 	{
24 		m_xMSF = xMSF;
25 		m_xStorageFactory = xStorageFactory;
26 		m_aTestHelper = new TestHelper( "Test09: " );
27 	}
28 
29     public boolean test()
30 	{
31 		try
32 		{
33 
34 			// create temporary storage based on arbitrary medium
35 			// after such a storage is closed it is lost
36 			Object oTempStorage = m_xStorageFactory.createInstance();
37 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
38 			if ( xTempStorage == null )
39 			{
40 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
41 				return false;
42 			}
43 
44 			byte pPass1[] = { 1, 2, 3 };
45 			byte pPass2[] = { 3, 2, 1 };
46 			byte pBytes[] = { 1, 1, 1, 1, 1 };
47 
48 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
49 			// the stream will not be encrypted
50 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", false, pBytes, pPass1 ) )
51 				return false;
52 
53 			// create temporary file
54 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
55 			if ( sTempFileURL == null || sTempFileURL == "" )
56 			{
57 				m_aTestHelper.Error( "No valid temporary file was created!" );
58 				return false;
59 			}
60 
61 			// create temporary storage based on a previously created temporary file
62 			Object pArgs[] = new Object[2];
63 			pArgs[0] = (Object) sTempFileURL;
64 			pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
65 
66 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
68 			if ( xTempFileStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
71 				return false;
72 			}
73 
74 			// copy xTempStorage to xTempFileStorage
75 			// xTempFileStorage will be automatically commited
76 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
77 				return false;
78 
79 			// change password of the substream of new storage based on file
80 			int nResult = m_aTestHelper.ChangeStreamPass( xTempFileStorage, "SubStream1", pPass1, pPass2 );
81 			if ( nResult == 0 )
82 				return false; // test failed
83 			else if ( nResult == -1 )
84 				return true; // tested optional feature is not supported
85 
86 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
87 				return false;
88 
89 			// dispose used storages to free resources
90 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
91 				return false;
92 
93 			// ================================================
94 			// now check all the written and copied information
95 			// ================================================
96 
97 			// the temporary file must not be locked any more after storage disposing
98 			pArgs[1] = new Integer( ElementModes.ELEMENT_READ );
99 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
100 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
101 			if ( xResultStorage == null )
102 			{
103 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
104 				return false;
105 			}
106 
107 			if ( !m_aTestHelper.checkEncrStream( xResultStorage, "SubStream1", "MediaType1", pBytes, pPass2 ) )
108 				return false;
109 
110 			// dispose used storages to free resources
111 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
112 				return false;
113 
114 			return true;
115 		}
116 		catch( Exception e )
117 		{
118 			m_aTestHelper.Error( "Exception: " + e );
119 			return false;
120 		}
121     }
122 }
123 
124