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