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 Test01 implements StorageTest {
17 
18 	XMultiServiceFactory m_xMSF;
19 	XSingleServiceFactory m_xStorageFactory;
20 	TestHelper m_aTestHelper;
21 
22 	public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
23 	{
24 		m_xMSF = xMSF;
25 		m_xStorageFactory = xStorageFactory;
26 		m_aTestHelper = new TestHelper( "Test01: " );
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 			// open a new substorage
51 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
52 																		"SubStorage1",
53 																		ElementModes.ELEMENT_WRITE );
54 			if ( xTempSubStorage == null )
55 			{
56 				m_aTestHelper.Error( "Can't create substorage!" );
57 				return false;
58 			}
59 
60 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
61 
62 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
63 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
64 				return false;
65 
66 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
67 
68 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
69 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
70 				return false;
71 
72 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
73 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
74 															"MediaType3",
75 															true,
76 															ElementModes.ELEMENT_READWRITE ) )
77 				return false;
78 
79 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
80 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
81 															"MediaType4",
82 															false,
83 															ElementModes.ELEMENT_WRITE ) )
84 				return false;
85 
86 			// create temporary storage based on a previously created temporary file
87 			Object pArgs[] = new Object[2];
88 			pArgs[0] = (Object) sTempFileURL;
89 			pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
90 
91 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
92 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
93 			if ( xTempFileStorage == null )
94 			{
95 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
96 				return false;
97 			}
98 
99 			// copy xTempStorage to xTempFileStorage
100 			// xTempFileStorage will be automatically commited
101 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
102 				return false;
103 
104 			// dispose used storages to free resources
105 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
106 				return false;
107 
108 			// ================================================
109 			// now check all the written and copied information
110 			// ================================================
111 
112 			// the temporary file must not be locked any more after storage disposing
113 			pArgs[1] = new Integer( ElementModes.ELEMENT_READWRITE );
114 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
115 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
116 			if ( xResultStorage == null )
117 			{
118 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
119 				return false;
120 			}
121 
122 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.ELEMENT_READWRITE ) )
123 				return false;
124 
125 			// open existing substorage
126 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
127 																		"SubStorage1",
128 																		ElementModes.ELEMENT_READ );
129 			if ( xResultSubStorage == null )
130 			{
131 				m_aTestHelper.Error( "Can't open existing substorage!" );
132 				return false;
133 			}
134 
135 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.ELEMENT_READ ) )
136 				return false;
137 
138 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
139 				return false;
140 
141 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
142 				return false;
143 
144 			// dispose used storages to free resources
145 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
146 				return false;
147 
148 			return true;
149 		}
150 		catch( Exception e )
151 		{
152 			m_aTestHelper.Error( "Exception: " + e );
153 			return false;
154 		}
155     }
156 
157 }
158 
159