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