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