xref: /trunk/main/package/qa/ofopxmlstorages/Test03.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir package complex.ofopxmlstorages;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
4*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
5*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
8*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
9*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
10*cdf0e10cSrcweir 
11*cdf0e10cSrcweir import com.sun.star.embed.*;
12*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
13*cdf0e10cSrcweir import com.sun.star.beans.StringPair;
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir import share.LogWriter;
16*cdf0e10cSrcweir import complex.ofopxmlstorages.TestHelper;
17*cdf0e10cSrcweir import complex.ofopxmlstorages.StorageTest;
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir public class Test03 implements StorageTest {
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
22*cdf0e10cSrcweir     XSingleServiceFactory m_xStorageFactory;
23*cdf0e10cSrcweir     TestHelper m_aTestHelper;
24*cdf0e10cSrcweir 
25*cdf0e10cSrcweir     public Test03( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
26*cdf0e10cSrcweir     {
27*cdf0e10cSrcweir         m_xMSF = xMSF;
28*cdf0e10cSrcweir         m_xStorageFactory = xStorageFactory;
29*cdf0e10cSrcweir         m_aTestHelper = new TestHelper( aLogWriter, "Test03: " );
30*cdf0e10cSrcweir     }
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir     public boolean test()
33*cdf0e10cSrcweir     {
34*cdf0e10cSrcweir         try
35*cdf0e10cSrcweir         {
36*cdf0e10cSrcweir             StringPair[][] aRelations =
37*cdf0e10cSrcweir                 { { new StringPair( "Id", "Num1" ) },
38*cdf0e10cSrcweir                   { new StringPair( "Target", "TargetURLValue" ), new StringPair( "Id", "Num6" ) },
39*cdf0e10cSrcweir                   { new StringPair( "Target", "" ), new StringPair( "Id", "Num7" ) },
40*cdf0e10cSrcweir                   { new StringPair( "Id", "Num2" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
41*cdf0e10cSrcweir                   { new StringPair( "Id", "Num3" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
42*cdf0e10cSrcweir                   { new StringPair( "Id", "Num4" ), new StringPair( "TargetMode", "Internal" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) },
43*cdf0e10cSrcweir                   { new StringPair( "Id", "Num5" ), new StringPair( "TargetMode", "" ), new StringPair( "Type", "unknown" ), new StringPair( "Target", "URL value" ) }
44*cdf0e10cSrcweir                 };
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir             // create temporary storage based on arbitrary medium
47*cdf0e10cSrcweir             // after such a storage is closed it is lost
48*cdf0e10cSrcweir             XStorage xTempStorage = m_aTestHelper.createTempStorage( m_xMSF, m_xStorageFactory );
49*cdf0e10cSrcweir             if ( xTempStorage == null )
50*cdf0e10cSrcweir             {
51*cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
52*cdf0e10cSrcweir                 return false;
53*cdf0e10cSrcweir             }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir             // open a new substorage
56*cdf0e10cSrcweir             XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
57*cdf0e10cSrcweir                                                                         "SubStorage1",
58*cdf0e10cSrcweir                                                                         ElementModes.WRITE );
59*cdf0e10cSrcweir             if ( xTempSubStorage == null )
60*cdf0e10cSrcweir             {
61*cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
62*cdf0e10cSrcweir                 return false;
63*cdf0e10cSrcweir             }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir             byte pBytes1[] = { 1, 1, 1, 1, 1 };
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
68*cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempStorage,
69*cdf0e10cSrcweir                                                         "SubStream1",
70*cdf0e10cSrcweir                                                         "MediaType1",
71*cdf0e10cSrcweir                                                         true,
72*cdf0e10cSrcweir                                                         pBytes1,
73*cdf0e10cSrcweir                                                         aRelations ) )
74*cdf0e10cSrcweir                 return false;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir             byte pBytes2[] = { 2, 2, 2, 2, 2 };
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
79*cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToSubstream( xTempSubStorage,
80*cdf0e10cSrcweir                                                         "SubStream2",
81*cdf0e10cSrcweir                                                         "MediaType2",
82*cdf0e10cSrcweir                                                         false,
83*cdf0e10cSrcweir                                                         pBytes2,
84*cdf0e10cSrcweir                                                         aRelations ) )
85*cdf0e10cSrcweir                 return false;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             // set Relations for storages and check that "IsRoot" and "OpenMode" properties are set correctly
88*cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
89*cdf0e10cSrcweir                                                             false,
90*cdf0e10cSrcweir                                                             ElementModes.WRITE,
91*cdf0e10cSrcweir                                                             aRelations ) )
92*cdf0e10cSrcweir                 return false;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir             if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
95*cdf0e10cSrcweir                 return false;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTempSubStorage ) )
98*cdf0e10cSrcweir                 return false;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir             // ================================================
101*cdf0e10cSrcweir             // check storage hyerarchy tree
102*cdf0e10cSrcweir             // ================================================
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir             // check that isStorageElement() and isStreamElement reacts to nonexisting object correctly
105*cdf0e10cSrcweir             try {
106*cdf0e10cSrcweir                 xTempStorage.isStorageElement( "does not exist" );
107*cdf0e10cSrcweir                 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStorageElement() call!" );
108*cdf0e10cSrcweir                 return false;
109*cdf0e10cSrcweir             }
110*cdf0e10cSrcweir             catch( com.sun.star.container.NoSuchElementException ne )
111*cdf0e10cSrcweir             {
112*cdf0e10cSrcweir             }
113*cdf0e10cSrcweir             catch( Exception e )
114*cdf0e10cSrcweir             {
115*cdf0e10cSrcweir                 m_aTestHelper.Error( "Wrong exception is thrown by isStorageElement() call: " + e );
116*cdf0e10cSrcweir                 return false;
117*cdf0e10cSrcweir             }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir             try {
120*cdf0e10cSrcweir                 xTempStorage.isStreamElement( "does not exist" );
121*cdf0e10cSrcweir                 m_aTestHelper.Error( "Nonexisting element doesn't detected by isStreamElement() call!" );
122*cdf0e10cSrcweir                 return false;
123*cdf0e10cSrcweir             }
124*cdf0e10cSrcweir             catch( com.sun.star.container.NoSuchElementException ne )
125*cdf0e10cSrcweir             {
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir             catch( Exception e )
128*cdf0e10cSrcweir             {
129*cdf0e10cSrcweir                 m_aTestHelper.Error( "Wrong exception is thrown by isStreamElement() call: " + e );
130*cdf0e10cSrcweir                 return false;
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir             XNameAccess xRootNameAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xTempStorage );
134*cdf0e10cSrcweir             if ( xRootNameAccess == null )
135*cdf0e10cSrcweir             {
136*cdf0e10cSrcweir                 m_aTestHelper.Error( "Root storage doesn't support XNameAccess!" );
137*cdf0e10cSrcweir                 return false;
138*cdf0e10cSrcweir             }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir             try {
141*cdf0e10cSrcweir                 if ( !xTempStorage.isStorageElement( "SubStorage1" ) || xTempStorage.isStreamElement( "SubStorage1" ) )
142*cdf0e10cSrcweir                 {
143*cdf0e10cSrcweir                     m_aTestHelper.Error( "Child 'SubStorage1' can not be detected as storage!" );
144*cdf0e10cSrcweir                     return false;
145*cdf0e10cSrcweir                 }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir                 if ( xTempStorage.isStorageElement( "SubStream1" ) || !xTempStorage.isStreamElement( "SubStream1" ) )
148*cdf0e10cSrcweir                 {
149*cdf0e10cSrcweir                     m_aTestHelper.Error( "Child 'SubStream1' can not be detected as stream!" );
150*cdf0e10cSrcweir                     return false;
151*cdf0e10cSrcweir                 }
152*cdf0e10cSrcweir             }
153*cdf0e10cSrcweir             catch( Exception e )
154*cdf0e10cSrcweir             {
155*cdf0e10cSrcweir                 m_aTestHelper.Error( "Child's type can not be detected, exception: " + e );
156*cdf0e10cSrcweir                 return false;
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir             // check that root storage contents are represented correctly
161*cdf0e10cSrcweir             String sRootCont[] = xRootNameAccess.getElementNames();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             if ( sRootCont.length != 2 )
164*cdf0e10cSrcweir             {
165*cdf0e10cSrcweir                 m_aTestHelper.Error( "Root storage contains wrong amount of children!" );
166*cdf0e10cSrcweir                 return false;
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             if ( !( sRootCont[0].equals( "SubStorage1" ) && sRootCont[1].equals( "SubStream1" )
170*cdf0e10cSrcweir                  || sRootCont[0].equals( "SubStream1" ) && sRootCont[1].equals( "SubStorage1" ) )
171*cdf0e10cSrcweir               || !( xRootNameAccess.hasByName( "SubStream1" ) && xRootNameAccess.hasByName( "SubStorage1" ) ) )
172*cdf0e10cSrcweir             {
173*cdf0e10cSrcweir                 m_aTestHelper.Error( "Root storage contains wrong list of children!" );
174*cdf0e10cSrcweir                 return false;
175*cdf0e10cSrcweir             }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir             // get storage through XNameAccess
178*cdf0e10cSrcweir             XStorage xResultSubStorage = getStorageFromNameAccess( xRootNameAccess, "SubStorage1" );
179*cdf0e10cSrcweir             if ( xResultSubStorage == null )
180*cdf0e10cSrcweir                 return false;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage,
183*cdf0e10cSrcweir                                                         false,
184*cdf0e10cSrcweir                                                         ElementModes.READ,
185*cdf0e10cSrcweir                                                         aRelations ) )
186*cdf0e10cSrcweir                 return false;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir             XNameAccess xChildAccess = (XNameAccess) UnoRuntime.queryInterface( XNameAccess.class, xResultSubStorage );
189*cdf0e10cSrcweir             if ( xChildAccess == null )
190*cdf0e10cSrcweir             {
191*cdf0e10cSrcweir                 m_aTestHelper.Error( "Child storage doesn't support XNameAccess!" );
192*cdf0e10cSrcweir                 return false;
193*cdf0e10cSrcweir             }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             if ( !xChildAccess.hasByName( "SubStream2" )
196*cdf0e10cSrcweir               || !xResultSubStorage.isStreamElement( "SubStream2" )
197*cdf0e10cSrcweir               || xResultSubStorage.isStorageElement( "SubStream2" ) )
198*cdf0e10cSrcweir             {
199*cdf0e10cSrcweir                 m_aTestHelper.Error( "'SubStream2' can not be detected as child stream element of 'SubStorage1'!" );
200*cdf0e10cSrcweir                 return false;
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir             return true;
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir         catch( Exception e )
206*cdf0e10cSrcweir         {
207*cdf0e10cSrcweir             m_aTestHelper.Error( "Exception: " + e );
208*cdf0e10cSrcweir             return false;
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     public XStorage getStorageFromNameAccess( XNameAccess xAccess, String sName )
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         try
215*cdf0e10cSrcweir         {
216*cdf0e10cSrcweir             Object oStorage = xAccess.getByName( sName );
217*cdf0e10cSrcweir             XStorage xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStorage );
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir             if ( xResult != null )
220*cdf0e10cSrcweir                 return xResult;
221*cdf0e10cSrcweir             else
222*cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess!" );
223*cdf0e10cSrcweir         }
224*cdf0e10cSrcweir         catch( Exception e )
225*cdf0e10cSrcweir         {
226*cdf0e10cSrcweir             m_aTestHelper.Error( "Can't retrieve substorage '" + sName + "' through XNameAccess, exception: " + e );
227*cdf0e10cSrcweir         }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir         return null;
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234