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_i46848 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i46848( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i46848( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i46848: " );
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 			// open a new substorage
75 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
76 																	"SubStorage1",
77 																	ElementModes.WRITE );
78 			if ( xTempSubStorage == null )
79 			{
80 				m_aTestHelper.Error( "Can't create substorage!" );
81 				return false;
82 			}
83 
84 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
85 
86 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
87 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
88 				return false;
89 
90 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
91 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
92 															"MediaType2",
93 															true,
94 															ElementModes.WRITE ) )
95 				return false;
96 
97 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
98 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
99 															"MediaType3",
100 															false,
101 															ElementModes.WRITE ) )
102 				return false;
103 
104 			// commit substorage first
105 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
106 				return false;
107 
108 			// commit the root storage so the contents must be stored now
109 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
110 				return false;
111 
112 			// dispose substorage
113 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
114 				return false;
115 
116 			// dispose the temporary storage
117 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
118 				return false;
119 
120 			// ================================================
121 			// create a new storage based on the stream and change the mediatype of the substorage
122 			// as described in the bug description
123 			// ================================================
124 
125 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
126 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
127 			if ( xTempStorage == null )
128 			{
129 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
130 				return false;
131 			}
132 
133 			// open the substorage
134 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
135 															"SubStorage1",
136 															ElementModes.WRITE );
137 			if ( xTempSubStorage == null )
138 			{
139 				m_aTestHelper.Error( "Can't create substorage!" );
140 				return false;
141 			}
142 
143 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
144 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
145 															"ChangedMediaType3",
146 															false,
147 															ElementModes.WRITE ) )
148 				return false;
149 
150 			// commit substorage first
151 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
152 				return false;
153 
154 			// commit the root storage so the contents must be stored now
155 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
156 				return false;
157 
158 			// dispose substorage
159 			if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
160 				return false;
161 
162 			// dispose the temporary storage
163 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
164 				return false;
165 
166 			// ================================================
167 			// create a new readonly storage based on the stream and check the contents
168 			// ================================================
169 
170 			pArgs[1] = new Integer( ElementModes.READ );
171 			oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
172 			xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
173 			if ( xTempStorage == null )
174 			{
175 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
176 				return false;
177 			}
178 
179 			// open the substorage
180 			xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
181 															"SubStorage1",
182 															ElementModes.READ );
183 			if ( xTempSubStorage == null )
184 			{
185 				m_aTestHelper.Error( "Can't create substorage!" );
186 				return false;
187 			}
188 
189 			if ( !m_aTestHelper.checkStorageProperties( xTempStorage, "MediaType2", true, ElementModes.READ ) )
190 				return false;
191 
192 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage, "ChangedMediaType3", false, ElementModes.READ ) )
193 				return false;
194 
195 			// the MediaType and the contents must be up to date
196 			if ( !m_aTestHelper.checkStream( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1 ) )
197 				return false;
198 
199 			// dispose used storage to free resources
200 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
201 				return false;
202 
203 			return true;
204 		}
205 		catch( Exception e )
206 		{
207 			m_aTestHelper.Error( "Exception: " + e );
208 			return false;
209 		}
210     }
211 }
212 
213