xref: /AOO41X/main/sot/qa/complex/olesimplestorage/Test01.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 package complex.olesimplestorage;
2 
3 
4 
5 import com.sun.star.lang.XMultiServiceFactory;
6 import com.sun.star.io.XInputStream;
7 import com.sun.star.io.XOutputStream;
8 import com.sun.star.io.XTempFile;
9 import com.sun.star.embed.XOLESimpleStorage;
10 import com.sun.star.uno.UnoRuntime;
11 
12 import java.util.Random;
13 
14 
15 public class Test01 implements OLESimpleStorageTest
16 {
17     XMultiServiceFactory m_xMSF = null;
18     TestHelper m_aTestHelper = null;
19     final int pStreamCnt = 5;
20     final int pBytesCnt = 10;
21 
22     public Test01 ( XMultiServiceFactory xMSF )
23     {
24         m_xMSF = xMSF;
25         m_aTestHelper = new TestHelper ("Test01: ");
26     }
27 
28     public boolean test ()
29     {
30         try
31         {
32             //create a new temporary stream
33             Object oTempFile = m_xMSF.createInstance ( "com.sun.star.io.TempFile" );
34             XTempFile xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile);
35             m_aTestHelper.Message ( "A new temporary stream created." );
36 
37             //create OLESimpleStorage based on it
38             Object pArgs[] = new Object[2];
39             pArgs[0] = (Object) xTempFile;
40             pArgs[1] = new Boolean( true );
41             Object oOLESimpleStorage = m_xMSF.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs );
42             XOLESimpleStorage xOLESimpleStorage = UnoRuntime.queryInterface(XOLESimpleStorage.class, oOLESimpleStorage);
43             m_aTestHelper.Message ( "OLESimpleStorage based on XStream created." );
44 
45             //fill it with some streams
46             Object oStream[] = new Object[pStreamCnt];
47             byte pBytesIn[][][] = new byte [pStreamCnt][1][pBytesCnt];
48             byte pBytesOut[][] = new byte [pStreamCnt][pBytesCnt];
49             XTempFile xTempStream[] = new XTempFile[pStreamCnt];
50             Random oRandom = new Random ();
51             final String sSubStreamPrefix = "SubStream";
52             for ( int i = 0; i < pStreamCnt; i++ )
53             {
54                 oRandom.nextBytes (pBytesOut[i]);
55                 oStream[i] = m_xMSF.createInstance ( "com.sun.star.io.TempFile" );
56                 xTempStream[i] = UnoRuntime.queryInterface(XTempFile.class, oStream[i]);
57                 xTempStream[i].getOutputStream ().writeBytes (pBytesOut[i]);
58                 xTempStream[i].seek (0);
59                 m_aTestHelper.Message ( "Substream " + i + " initialized." );
60                 if (xOLESimpleStorage.hasByName (sSubStreamPrefix + i))
61                 {
62                     xOLESimpleStorage.replaceByName ( sSubStreamPrefix + i, xTempStream[i] );
63                 }
64                 else
65                 {
66                     xOLESimpleStorage.insertByName ( sSubStreamPrefix + i, xTempStream[i] );
67                     m_aTestHelper.Message ( "Substream " + i + " inserted." );
68                 }
69             }
70 
71             //commit the storage and close it
72             xOLESimpleStorage.commit ();
73             m_aTestHelper.Message ( "Storage commited." );
74             xOLESimpleStorage.dispose ();
75             for ( int i = 0; i < pStreamCnt; ++i )
76             {
77                 xTempStream[i].setRemoveFile ( true );
78                 xTempStream[i].getInputStream ().closeInput ();
79                 xTempStream[i].getOutputStream ().closeOutput ();
80             }
81             m_aTestHelper.Message ( "Storage closed." );
82 
83             //open the same stream with the constructor for inputstream
84             pArgs[0] = (Object)xTempFile.getInputStream ();
85             oOLESimpleStorage = m_xMSF.createInstanceWithArguments ( "com.sun.star.embed.OLESimpleStorage", pArgs );
86             xOLESimpleStorage = UnoRuntime.queryInterface(XOLESimpleStorage.class, oOLESimpleStorage);
87             m_aTestHelper.Message ( "Storage reopened, based on XInputStream." );
88 
89             //check that all the streams contain correct information
90             m_aTestHelper.Message ( "Checking data contained in all the substreams..." );
91             for ( int i = 0; i < pStreamCnt; ++i )
92             {
93                 if ( xOLESimpleStorage.hasByName (sSubStreamPrefix + i) )
94                 {
95                     xTempStream[i] = UnoRuntime.queryInterface(XTempFile.class, xOLESimpleStorage.getByName(sSubStreamPrefix + i));
96                     xTempStream[i].seek (0);
97                     xTempStream[i].getInputStream ().readBytes (pBytesIn[i], pBytesIn[i][0].length + 1 );
98                     for ( int j = 0; j < pBytesCnt; ++j )
99                     {
100                         if ( pBytesIn[i][0][j] != pBytesOut[i][j] )
101                         {
102                             m_aTestHelper.Error ( "Stream " + i + " byte " + j + ": INCORRECT DATA!");
103                             return false;
104                         }
105                         else
106                         {
107                             m_aTestHelper.Message ( "Stream " + i + " byte " + j + ":  CORRECT." );
108                         }
109                     }
110                 }
111                 else
112                 {
113                     m_aTestHelper.Error( "Stream " + i + " is lost!");
114                     return false;
115                 }
116             }
117             m_aTestHelper.Message ( "All substreams contain correct data. SUCCESS." );
118         }
119         catch ( Exception e )
120         {
121             m_aTestHelper.Error ( "Exception: " + e );
122             return false;
123         }
124         return true;
125     }
126 }
127