Update ooo320-m1
[ooovba.git] / odk / examples / java / Storage / Test01.java
blob7c9de41c37bee481f23d4eb46316a5c82dc7f601
1 package storagetesting;
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
11 import com.sun.star.embed.*;
13 import storagetesting.TestHelper;
14 import storagetesting.StorageTest;
16 public class Test01 implements StorageTest {
18 XMultiServiceFactory m_xMSF;
19 XSingleServiceFactory m_xStorageFactory;
20 TestHelper m_aTestHelper;
22 public Test01( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
24 m_xMSF = xMSF;
25 m_xStorageFactory = xStorageFactory;
26 m_aTestHelper = new TestHelper( "Test01: " );
29 public boolean test()
31 try
33 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
34 if ( sTempFileURL == null || sTempFileURL == "" )
36 m_aTestHelper.Error( "No valid temporary file was created!" );
37 return false;
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 )
46 m_aTestHelper.Error( "Can't create temporary storage representation!" );
47 return false;
50 // open a new substorage
51 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
52 "SubStorage1",
53 ElementModes.ELEMENT_WRITE );
54 if ( xTempSubStorage == null )
56 m_aTestHelper.Error( "Can't create substorage!" );
57 return false;
60 byte pBytes1[] = { 1, 1, 1, 1, 1 };
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;
66 byte pBytes2[] = { 2, 2, 2, 2, 2 };
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;
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;
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;
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 );
91 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
92 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
93 if ( xTempFileStorage == null )
95 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
96 return false;
99 // copy xTempStorage to xTempFileStorage
100 // xTempFileStorage will be automatically commited
101 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
102 return false;
104 // dispose used storages to free resources
105 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
106 return false;
108 // ================================================
109 // now check all the written and copied information
110 // ================================================
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 )
118 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
119 return false;
122 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.ELEMENT_READWRITE ) )
123 return false;
125 // open existing substorage
126 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
127 "SubStorage1",
128 ElementModes.ELEMENT_READ );
129 if ( xResultSubStorage == null )
131 m_aTestHelper.Error( "Can't open existing substorage!" );
132 return false;
135 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.ELEMENT_READ ) )
136 return false;
138 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
139 return false;
141 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
142 return false;
144 // dispose used storages to free resources
145 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
146 return false;
148 return true;
150 catch( Exception e )
152 m_aTestHelper.Error( "Exception: " + e );
153 return false;