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