1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import com.sun.star.io.XStream;
32 import com.sun.star.io.XInputStream;
33 
34 import com.sun.star.embed.*;
35 
36 import share.LogWriter;
37 import complex.storages.TestHelper;
38 import complex.storages.StorageTest;
39 
40 public class RegressionTest_i35095 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i35095( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i35095( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i35095: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58 			if ( xTempFileStream == null )
59 				return false;
60 
61 			// create storage based on the temporary stream
62 			Object pArgs[] = new Object[2];
63 			pArgs[0] = (Object) xTempFileStream;
64 			pArgs[1] = new Integer( ElementModes.WRITE );
65 
66 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68 			if ( xTempStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 				return false;
72 			}
73 
74 			byte pBytes[] = new byte[36000];
75 			for ( int nInd = 0; nInd < 36000; nInd++ )
76 				pBytes[nInd] = (byte)( nInd % 128 );
77 
78 			String sPass = "12345";
79 
80 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
81 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes, sPass ) )
82 				return false;
83 
84 			// open a new substorage
85 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
86 																		"SubStorage1",
87 																		ElementModes.WRITE );
88 			if ( xTempSubStorage == null )
89 			{
90 				m_aTestHelper.Error( "Can't create substorage!" );
91 				return false;
92 			}
93 
94 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes, sPass ) )
96 				return false;
97 
98 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
99 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
100 															"MediaType3",
101 															true,
102 															ElementModes.WRITE ) )
103 				return false;
104 
105 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
106 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
107 															"MediaType4",
108 															false,
109 															ElementModes.WRITE ) )
110 				return false;
111 
112 			// commit substorage first
113 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
114 				return false;
115 
116 			// commit the root storage so the contents must be stored now
117 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
118 				return false;
119 
120 			// dispose used storage to free resources
121 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
122 				return false;
123 
124 			// ================================================
125 			// now check all the written information
126 			// and the raw stream contents
127 			// ================================================
128 
129 			// close the output part of the temporary stream
130 			// the output part must present since we already wrote to the stream
131 			if ( !m_aTestHelper.closeOutput( xTempFileStream ) )
132 				return false;
133 
134 			XInputStream xTempInStream = m_aTestHelper.getInputStream( xTempFileStream );
135 			if ( xTempInStream == null )
136 				return false;
137 
138 			// open input stream
139 			// since no mode is provided the result storage must be opened readonly
140 			Object pOneArg[] = new Object[1];
141 			pOneArg[0] = (Object) xTempInStream;
142 
143 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pOneArg );
144 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
145 			if ( xResultStorage == null )
146 			{
147 				m_aTestHelper.Error( "Can't open storage based on input stream!" );
148 				return false;
149 			}
150 
151 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
152 				return false;
153 
154 			// open existing substorage
155 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
156 																		"SubStorage1",
157 																		ElementModes.READ );
158 			if ( xResultSubStorage == null )
159 			{
160 				m_aTestHelper.Error( "Can't open existing substorage!" );
161 				return false;
162 			}
163 
164 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType4", false, ElementModes.READ ) )
165 				return false;
166 
167 			if ( !m_aTestHelper.compareRawMethodsOnEncrStream( xResultStorage, "SubStream1" ) )
168 				return false;
169 
170 			if ( !m_aTestHelper.compareRawMethodsOnEncrStream( xResultSubStorage, "SubStream2" ) )
171 				return false;
172 
173 			// dispose used storages to free resources
174 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
175 				return false;
176 
177 			return true;
178 		}
179 		catch( Exception e )
180 		{
181 			m_aTestHelper.Error( "Exception: " + e );
182 			return false;
183 		}
184     }
185 
186 }
187 
188