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.ofopxmlstorages;
23 
24 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.lang.XSingleServiceFactory;
26 
27 import com.sun.star.bridge.XUnoUrlResolver;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.XInterface;
30 
31 import com.sun.star.container.XNameAccess;
32 import com.sun.star.io.XStream;
33 
34 import com.sun.star.embed.*;
35 import com.sun.star.beans.StringPair;
36 
37 import share.LogWriter;
38 import complex.ofopxmlstorages.TestHelper;
39 import complex.ofopxmlstorages.StorageTest;
40 
41 public class Test07 implements StorageTest {
42 
43 	XMultiServiceFactory m_xMSF;
44 	XSingleServiceFactory m_xStorageFactory;
45 	TestHelper m_aTestHelper;
46 
Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )47 	public Test07( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
48 	{
49 		m_xMSF = xMSF;
50 		m_xStorageFactory = xStorageFactory;
51 		m_aTestHelper = new TestHelper( aLogWriter, "Test07: " );
52 	}
53 
test()54     public boolean test()
55 	{
56 		StringPair[][] aRelations1 =
57 			{ { new StringPair( "Id", "Num1" ) },
58 			  { new StringPair( "Target", "TargetURLValue1" ), new StringPair( "Id", "Num6" ) },
59 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
60 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
61 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
62 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal1" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value 1" ) },
63 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown1" ), new StringPair( "Target", "URL value1" ) }
64 			};
65 
66 		StringPair[][] aRelations2 =
67 			{ { new StringPair( "Id", "Num1" ) },
68 			  { new StringPair( "Target", "TargetURLValue2" ), new StringPair( "Id", "Num6" ) },
69 			  { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
70 			  { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
71 			  { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown2" ), new StringPair( "Target", "URL value 2" ) },
72 			  { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal2" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
73 			  { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
74 			};
75 
76 		try
77 		{
78 			// create temporary storage based on arbitrary medium
79 			// after such a storage is closed it is lost
80 			XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
81 			if ( xTempStorage == null )
82 			{
83 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
84 				return false;
85 			}
86 
87 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
88 
89 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
90 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage,
91 														"SubStream1",
92 														"MediaType1",
93 														true,
94 														pBytes1,
95 														aRelations1 ) )
96 				return false;
97 
98 
99 			// open a new substorage
100 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
101 																		"SubStorage1",
102 																		ElementModes.WRITE );
103 			if ( xTempSubStorage == null )
104 			{
105 				m_aTestHelper.Error( "Can't create substorage!" );
106 				return false;
107 			}
108 
109 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
110 
111 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
112 			if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
113 														"SubStream2",
114 														"MediaType2",
115 														true,
116 														pBytes2,
117 														aRelations2 ) )
118 				return false;
119 
120 			// set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
121 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
122 															true,
123 															ElementModes.WRITE,
124 															aRelations2 ) )
125 				return false;
126 
127 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
128 															false,
129 															ElementModes.WRITE,
130 															aRelations2 ) )
131 				return false;
132 
133 			// ==============================
134 			// check cloning at current state
135 			// ==============================
136 
137 			// the new storage still was not commited so the clone must be empty
138 			XStorage xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xMSF, m_xStorageFactory, xTempStorage, "SubStorage1" );
139 
140 			if ( xClonedSubStorage == null )
141 			{
142 				m_aTestHelper.Error( "The result of clone is empty!" );
143 				return false;
144 			}
145 
146 			XNameAccess xClonedNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xClonedSubStorage );
147 			if ( xClonedNameAccess == null )
148 			{
149 				m_aTestHelper.Error( "XNameAccess is not implemented by the clone!" );
150 				return false;
151 			}
152 
153 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage,
154 														true,
155 														ElementModes.WRITE,
156 														new StringPair[0][0] ) )
157 				return false;
158 
159 			if ( xClonedNameAccess.hasElements() )
160 			{
161 				m_aTestHelper.Error( "The new substorage still was not commited so it must be empty!" );
162 				return false;
163 			}
164 
165 			if ( !m_aTestHelper.disposeStorage( xClonedSubStorage ) )
166 				return false;
167 
168 			xClonedSubStorage = null;
169 			xClonedNameAccess = null;
170 
171 			// the new stream was opened, written and closed, that means flashed
172 			// so the clone must contain all the information
173 			XStream xClonedSubStream = m_aTestHelper.cloneSubStream( xTempStorage, "SubStream1" );
174 			if ( !m_aTestHelper.InternalCheckStream( xClonedSubStream,
175 													"SubStream1",
176 													"MediaType1",
177 													pBytes1,
178 													aRelations1 ) )
179 				return false;
180 
181 			if ( !m_aTestHelper.disposeStream( xClonedSubStream, "SubStream1" ) )
182 				return false;
183 
184 			// ==============================
185 			// commit substorage and check cloning
186 			// ==============================
187 
188 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
189 				return false;
190 
191 			xClonedSubStorage = m_aTestHelper.cloneSubStorage( m_xMSF, m_xStorageFactory, xTempStorage, "SubStorage1" );
192 			if ( xClonedSubStorage == null )
193 			{
194 				m_aTestHelper.Error( "The result of clone is empty!" );
195 				return false;
196 			}
197 
198 			if ( !m_aTestHelper.checkStorageProperties( xClonedSubStorage,
199 														true,
200 														ElementModes.WRITE,
201 														aRelations2 ) )
202 				return false;
203 
204 			if ( !m_aTestHelper.checkStream( xClonedSubStorage,
205 											"SubStream2",
206 											"MediaType2",
207 											pBytes2,
208 											aRelations2 ) )
209 				return false;
210 
211 			XStorage xCloneOfRoot = m_aTestHelper.cloneStorage( m_xMSF, m_xStorageFactory, xTempStorage );
212 			if ( xCloneOfRoot == null )
213 			{
214 				m_aTestHelper.Error( "The result of root clone is empty!" );
215 				return false;
216 			}
217 
218 			XNameAccess xCloneOfRootNA = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xCloneOfRoot );
219 			if ( xCloneOfRootNA == null )
220 			{
221 				m_aTestHelper.Error( "XNameAccess is not implemented by the root clone!" );
222 				return false;
223 			}
224 
225 			if ( xCloneOfRootNA.hasElements() )
226 			{
227 				m_aTestHelper.Error( "The root storage still was not commited so it's clone must be empty!" );
228 				return false;
229 			}
230 
231 			if ( !m_aTestHelper.disposeStorage( xCloneOfRoot ) )
232 				return false;
233 
234 			xCloneOfRoot = null;
235 
236 			// ==============================
237 			// commit root storage and check cloning
238 			// ==============================
239 
240 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
241 				return false;
242 
243 			xCloneOfRoot = m_aTestHelper.cloneStorage( m_xMSF, m_xStorageFactory, xTempStorage );
244 			if ( xCloneOfRoot == null )
245 			{
246 				m_aTestHelper.Error( "The result of root clone is empty!" );
247 				return false;
248 			}
249 
250 			XStorage xSubStorageOfClone = xCloneOfRoot.openStorageElement( "SubStorage1", ElementModes.READ );
251 			if ( xSubStorageOfClone == null )
252 			{
253 				m_aTestHelper.Error( "The result of root clone is wrong!" );
254 				return false;
255 			}
256 
257 			if ( !m_aTestHelper.checkStorageProperties( xSubStorageOfClone,
258 														false,
259 														ElementModes.READ,
260 														aRelations2 ) )
261 				return false;
262 
263 			if ( !m_aTestHelper.checkStream( xSubStorageOfClone,
264 											"SubStream2",
265 											"MediaType2",
266 											pBytes2,
267 											aRelations2 ) )
268 				return false;
269 
270 			return true;
271 		}
272 		catch( Exception e )
273 		{
274 			m_aTestHelper.Error( "Exception: " + e );
275 			return false;
276 		}
277     }
278 }
279 
280