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