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 import com.sun.star.io.XStream;
11 import com.sun.star.io.XInputStream;
12 
13 import com.sun.star.embed.*;
14 
15 import storagetesting.TestHelper;
16 import storagetesting.StorageTest;
17 
18 public class Test02 implements StorageTest {
19 
20 	XMultiServiceFactory m_xMSF;
21 	XSingleServiceFactory m_xStorageFactory;
22 	TestHelper m_aTestHelper;
23 
24 	public Test02( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
25 	{
26 		m_xMSF = xMSF;
27 		m_xStorageFactory = xStorageFactory;
28 		m_aTestHelper = new TestHelper( "Test02: " );
29 	}
30 
31     public boolean test()
32 	{
33 		try
34 		{
35 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
36 			if ( xTempFileStream == null )
37 				return false;
38 
39 			// create storage based on the temporary stream
40 			Object pArgs[] = new Object[2];
41 			pArgs[0] = (Object) xTempFileStream;
42 			pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
43 
44 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
45 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
46 			if ( xTempStorage == null )
47 			{
48 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
49 				return false;
50 			}
51 
52 			// open a new substorage
53 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
54 																	"SubStorage1",
55 																	ElementModes.ELEMENT_WRITE );
56 			if ( xTempSubStorage == null )
57 			{
58 				m_aTestHelper.Error( "Can't create substorage!" );
59 				return false;
60 			}
61 
62 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
63 
64 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
65 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
66 				return false;
67 
68 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
69 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
70 															"MediaType2",
71 															true,
72 															ElementModes.ELEMENT_WRITE ) )
73 				return false;
74 
75 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
76 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
77 															"MediaType3",
78 															false,
79 															ElementModes.ELEMENT_WRITE ) )
80 				return false;
81 
82 			// commit substorage first
83 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
84 				return false;
85 
86 			// commit the root storage so the contents must be stored now
87 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
88 				return false;
89 
90 			// dispose used storage to free resources
91 			// the substorage dispose will be triggered by this call
92 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
93 				return false;
94 
95 
96 			// ================================================
97 			// now check all the written information
98 			// ================================================
99 
100 			// close the output part of the temporary stream
101 			// the output part must present since we already wrote to the stream
102 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
103 				return false;
104 
105 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
106 			if ( xTempInStream == null )
107 				return false;
108 
109 
110 			// open input stream
111 			// since no mode is provided the result storage must be opened readonly
112 			Object pOneArg[] = new Object[1];
113 			pOneArg[0] = (Object) xTempInStream;
114 
115 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
116 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
117 			if ( xResultStorage == null )
118 			{
119 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
120 				return false;
121 			}
122 
123 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType2", true, ElementModes.ELEMENT_READ ) )
124 				return false;
125 
126 			// open existing substorage
127 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
128 																		"SubStorage1",
129 																		ElementModes.ELEMENT_READ );
130 			if ( xResultSubStorage == null )
131 			{
132 				m_aTestHelper.Error( "Can't open existing substorage!" );
133 				return false;
134 			}
135 
136 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType3", false, ElementModes.ELEMENT_READ ) )
137 				return false;
138 
139 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1 ) )
140 				return false;
141 
142 			return true;
143 		}
144 		catch( Exception e )
145 		{
146 			m_aTestHelper.Error( "Exception: " + e );
147 			return false;
148 		}
149     }
150 
151 }
152 
153