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_i29321 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i29321( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i29321( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i29321: " );
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 			// open a new substorage
85 			XStorage xTempSubSubStorage = m_aTestHelper.openSubStorage( xTempSubStorage,
86 																		"SubSubStorage1",
87 																		ElementModes.WRITE );
88 			if ( xTempSubSubStorage == null )
89 			{
90 				m_aTestHelper.Error( "Can't create substorage!" );
91 				return false;
92 			}
93 
94 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
95 
96 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
97 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage, "Stream1", "MediaType1", true, pBytes1 ) )
98 				return false;
99 
100 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
101 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage, "SubStream1", "MediaType2", true, pBytes1 ) )
102 				return false;
103 
104 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
105 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubSubStorage, "SubSubStream1", "MediaType3", true, pBytes1 ) )
106 				return false;
107 
108 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
109 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
110 															"MediaType4",
111 															true,
112 															ElementModes.WRITE ) )
113 				return false;
114 
115 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
116 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
117 															"MediaType5",
118 															false,
119 															ElementModes.WRITE ) )
120 				return false;
121 
122 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
123 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubSubStorage,
124 															"MediaType6",
125 															false,
126 															ElementModes.WRITE ) )
127 				return false;
128 
129 			// ================================================
130 			// commit the storages twice to test the bug scenario
131 			// ================================================
132 
133 			// commit lowlevel substorage first
134 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
135 				return false;
136 
137 			// commit substorage
138 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
139 				return false;
140 
141 			// commit substorage to let the renaming take place
142 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
143 				return false;
144 
145 			// commit lowlevel substorage first
146 			if ( !m_aTestHelper.commitStorage( xTempSubSubStorage ) )
147 				return false;
148 
149 			// commit substorage
150 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
151 				return false;
152 
153 			// commit substorage to let the renaming take place
154 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
155 				return false;
156 
157 			// ================================================
158 			// check the storages and streams without closing
159 			// ================================================
160 
161 			if ( !m_aTestHelper.checkStorageProperties( xTempSubSubStorage, "MediaType6", false, ElementModes.WRITE ) )
162 				return false;
163 
164 			if ( !m_aTestHelper.checkStorageProperties( xTempSubStorage, "MediaType5", false, ElementModes.WRITE ) )
165 				return false;
166 
167 			if ( !m_aTestHelper.checkStorageProperties( xTempStorage, "MediaType4", true, ElementModes.WRITE ) )
168 				return false;
169 
170 			if ( !m_aTestHelper.checkStream( xTempSubSubStorage, "SubSubStream1", "MediaType3", true, pBytes1 ) )
171 				return false;
172 
173 			if ( !m_aTestHelper.checkStream( xTempSubStorage, "SubStream1", "MediaType2", true, pBytes1 ) )
174 				return false;
175 
176 			if ( !m_aTestHelper.checkStream( xTempStorage, "Stream1", "MediaType1", true, pBytes1 ) )
177 				return false;
178 
179 			// the root storage is based on the temporary stream so it can be left undisposed, since it does not lock
180 			// any resource, later the garbage collector will release the object and it must die by refcount
181 
182 			return true;
183 		}
184 		catch( Exception e )
185 		{
186 			m_aTestHelper.Error( "Exception: " + e );
187 			return false;
188 		}
189     }
190 }
191 
192