xref: /trunk/main/comphelper/qa/complex/comphelper/SequenceOutputStreamUnitTest.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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 
23 package complex.comphelper;
24 
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.uno.UnoRuntime;
27 
28 import com.sun.star.io.XSequenceOutputStream;
29 import com.sun.star.io.XSeekableInputStream;
30 
31 import java.util.Random;
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.openoffice.test.OfficeConnection;
38 import static org.junit.Assert.*;
39 
40 /* Document.
41  */
42 
43 class TestHelper
44 {
45     // LogWriter m_aLogWriter;
46     String m_sTestPrefix;
47 
48     /** Creates a new instance of TestHelper
49      * @param sTestPrefix
50      */
TestHelper( String sTestPrefix )51     public TestHelper ( String sTestPrefix ) {
52         m_sTestPrefix = sTestPrefix;
53     }
54 
Error( String sError )55     public void Error ( String sError ) {
56         System.out.println ( m_sTestPrefix + "Error: " + sError );
57     }
58 
Message( String sMessage )59     public void Message ( String sMessage ) {
60         System.out.println ( m_sTestPrefix + sMessage );
61     }
62 }
63 
64 public class SequenceOutputStreamUnitTest {
65     private XMultiServiceFactory m_xMSF = null;
66 
67     TestHelper m_aTestHelper = null;
68 
69 //    public static String getShortTestDescription() {
70 //        return "tests the SequenceOutput/InputStream implementations";
71 //    }
72 
before()73     @Before public void before() {
74         try {
75             m_xMSF = getMSF();
76             m_aTestHelper = new TestHelper ( "Test01: ");
77         } catch (Exception e) {
78             fail ("Cannot create service factory!");
79         }
80         if (m_xMSF==null) {
81             fail ("Cannot create service factory!");
82         }
83     }
84 
after()85     @After public void after() {
86         m_xMSF = null;
87     }
88 
89 //    @Test public void ExecuteTest01() {
90 //        Test01 aTest = new Test01 (m_xMSF);
91 //        assertTrue( "Test01 failed!", aTest.test() );
92 //    }
93 
test()94     @Test public void test () {
95         try {
96             final int nBytesCnt = 20;
97 
98             //create SequenceOutputStream
99             Object oSequenceOutputStream = m_xMSF.createInstance (
100                     "com.sun.star.io.SequenceOutputStream" );
101             XSequenceOutputStream xSeqOutStream =
102                     UnoRuntime.queryInterface (
103                     XSequenceOutputStream.class, oSequenceOutputStream );
104             m_aTestHelper.Message ( "SequenceOutputStream created." );
105 
106             //write something to the stream
107             byte pBytesOriginal[] = new byte [nBytesCnt];
108             Random oRandom = new Random();
109             oRandom.nextBytes (pBytesOriginal);
110             xSeqOutStream.writeBytes (pBytesOriginal);
111             byte pBytesWritten[] = xSeqOutStream.getWrittenBytes ();
112             m_aTestHelper.Message ( "SeuenceOutputStream filled." );
113 
114             //create SequenceInputstream
115             Object pArgs[] = new Object[1];
116             pArgs[0] = pBytesWritten;
117             Object oSequenceInputStream = m_xMSF.createInstanceWithArguments (
118                     "com.sun.star.io.SequenceInputStream", pArgs );
119             XSeekableInputStream xSeekableInStream =
120                     UnoRuntime.queryInterface (
121                     XSeekableInputStream.class, oSequenceInputStream );
122             m_aTestHelper.Message ( "SequenceInputStream created." );
123 
124             //read from the stream
125             byte pBytesRead[][] = new byte [1][nBytesCnt];
126             xSeekableInStream.readBytes ( pBytesRead, pBytesRead[0].length + 1 );
127             m_aTestHelper.Message ( "Read from SequenceInputStream." );
128 
129             //close the streams
130             xSeqOutStream.closeOutput ();
131             xSeekableInStream.closeInput ();
132             m_aTestHelper.Message ( "Both streams closed." );
133 
134             //compare the original, written and read arrys
135             for ( int i = 0; i < nBytesCnt; ++i ) {
136                 if ( pBytesOriginal[i] != pBytesWritten[i] ) {
137                     m_aTestHelper.Error ( "Written array not identical to " +
138                             "original array. Position: " + i );
139                     return /* false */;
140                 } else if ( pBytesOriginal[i] != pBytesRead[0][i] ) {
141                     m_aTestHelper.Error ( "Read array not identical to original " +
142                             "array. Position: " + i );
143                     return /* false */;
144                 }
145             }
146             m_aTestHelper.Message ( "All data correct." );
147         } catch ( Exception e ) {
148             m_aTestHelper.Error ( "Exception: " + e );
149             return /* false */;
150         }
151         return /* true */;
152     }
153 
getMSF()154     private XMultiServiceFactory getMSF()
155     {
156         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
157         return xMSF1;
158     }
159 
160     // setup and close connections
setUpConnection()161     @BeforeClass public static void setUpConnection() throws Exception {
162         System.out.println("setUpConnection()");
163         connection.setUp();
164     }
165 
tearDownConnection()166     @AfterClass public static void tearDownConnection()
167         throws InterruptedException, com.sun.star.uno.Exception
168     {
169         System.out.println("tearDownConnection()");
170         connection.tearDown();
171     }
172 
173     private static final OfficeConnection connection = new OfficeConnection();
174 }
175