172a8d698SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
372a8d698SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
472a8d698SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
572a8d698SAndrew Rist  * distributed with this work for additional information
672a8d698SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
772a8d698SAndrew Rist  * to you under the Apache License, Version 2.0 (the
872a8d698SAndrew Rist  * "License"); you may not use this file except in compliance
972a8d698SAndrew Rist  * with the License.  You may obtain a copy of the License at
1072a8d698SAndrew Rist  *
1172a8d698SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1272a8d698SAndrew Rist  *
1372a8d698SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1472a8d698SAndrew Rist  * software distributed under the License is distributed on an
1572a8d698SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1672a8d698SAndrew Rist  * KIND, either express or implied.  See the License for the
1772a8d698SAndrew Rist  * specific language governing permissions and limitations
1872a8d698SAndrew Rist  * under the License.
1972a8d698SAndrew Rist  *
2072a8d698SAndrew Rist  *************************************************************/
2172a8d698SAndrew Rist 
2272a8d698SAndrew Rist 
23cdf0e10cSrcweir package complex.comphelper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.io.XSequenceOutputStream;
29cdf0e10cSrcweir import com.sun.star.io.XSeekableInputStream;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir import java.util.Random;
32cdf0e10cSrcweir import org.junit.After;
33cdf0e10cSrcweir import org.junit.AfterClass;
34cdf0e10cSrcweir import org.junit.Before;
35cdf0e10cSrcweir import org.junit.BeforeClass;
36cdf0e10cSrcweir import org.junit.Test;
37cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
38cdf0e10cSrcweir import static org.junit.Assert.*;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /* Document.
41cdf0e10cSrcweir  */
42cdf0e10cSrcweir 
43cdf0e10cSrcweir class TestHelper
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     // LogWriter m_aLogWriter;
46cdf0e10cSrcweir     String m_sTestPrefix;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     /** Creates a new instance of TestHelper
49cdf0e10cSrcweir      * @param sTestPrefix
50cdf0e10cSrcweir      */
TestHelper( String sTestPrefix )51cdf0e10cSrcweir     public TestHelper ( String sTestPrefix ) {
52cdf0e10cSrcweir         m_sTestPrefix = sTestPrefix;
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
Error( String sError )55cdf0e10cSrcweir     public void Error ( String sError ) {
56cdf0e10cSrcweir         System.out.println ( m_sTestPrefix + "Error: " + sError );
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
Message( String sMessage )59cdf0e10cSrcweir     public void Message ( String sMessage ) {
60cdf0e10cSrcweir         System.out.println ( m_sTestPrefix + sMessage );
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64*2062e9d7SDamjan Jovanovic public class SequenceOutputStreamUnitTest {
65cdf0e10cSrcweir     private XMultiServiceFactory m_xMSF = null;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     TestHelper m_aTestHelper = null;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir //    public static String getShortTestDescription() {
70cdf0e10cSrcweir //        return "tests the SequenceOutput/InputStream implementations";
71cdf0e10cSrcweir //    }
72cdf0e10cSrcweir 
before()73cdf0e10cSrcweir     @Before public void before() {
74cdf0e10cSrcweir         try {
75cdf0e10cSrcweir             m_xMSF = getMSF();
76cdf0e10cSrcweir             m_aTestHelper = new TestHelper ( "Test01: ");
77cdf0e10cSrcweir         } catch (Exception e) {
78cdf0e10cSrcweir             fail ("Cannot create service factory!");
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         if (m_xMSF==null) {
81cdf0e10cSrcweir             fail ("Cannot create service factory!");
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
after()85cdf0e10cSrcweir     @After public void after() {
86cdf0e10cSrcweir         m_xMSF = null;
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir //    @Test public void ExecuteTest01() {
90cdf0e10cSrcweir //        Test01 aTest = new Test01 (m_xMSF);
91cdf0e10cSrcweir //        assertTrue( "Test01 failed!", aTest.test() );
92cdf0e10cSrcweir //    }
93cdf0e10cSrcweir 
test()94cdf0e10cSrcweir     @Test public void test () {
95cdf0e10cSrcweir         try {
96cdf0e10cSrcweir             final int nBytesCnt = 20;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             //create SequenceOutputStream
99cdf0e10cSrcweir             Object oSequenceOutputStream = m_xMSF.createInstance (
100cdf0e10cSrcweir                     "com.sun.star.io.SequenceOutputStream" );
101cdf0e10cSrcweir             XSequenceOutputStream xSeqOutStream =
102cdf0e10cSrcweir                     UnoRuntime.queryInterface (
103cdf0e10cSrcweir                     XSequenceOutputStream.class, oSequenceOutputStream );
104cdf0e10cSrcweir             m_aTestHelper.Message ( "SequenceOutputStream created." );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir             //write something to the stream
107cdf0e10cSrcweir             byte pBytesOriginal[] = new byte [nBytesCnt];
108cdf0e10cSrcweir             Random oRandom = new Random();
109cdf0e10cSrcweir             oRandom.nextBytes (pBytesOriginal);
110cdf0e10cSrcweir             xSeqOutStream.writeBytes (pBytesOriginal);
111cdf0e10cSrcweir             byte pBytesWritten[] = xSeqOutStream.getWrittenBytes ();
112cdf0e10cSrcweir             m_aTestHelper.Message ( "SeuenceOutputStream filled." );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             //create SequenceInputstream
115cdf0e10cSrcweir             Object pArgs[] = new Object[1];
116cdf0e10cSrcweir             pArgs[0] = pBytesWritten;
117cdf0e10cSrcweir             Object oSequenceInputStream = m_xMSF.createInstanceWithArguments (
118cdf0e10cSrcweir                     "com.sun.star.io.SequenceInputStream", pArgs );
119cdf0e10cSrcweir             XSeekableInputStream xSeekableInStream =
120cdf0e10cSrcweir                     UnoRuntime.queryInterface (
121cdf0e10cSrcweir                     XSeekableInputStream.class, oSequenceInputStream );
122cdf0e10cSrcweir             m_aTestHelper.Message ( "SequenceInputStream created." );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             //read from the stream
125cdf0e10cSrcweir             byte pBytesRead[][] = new byte [1][nBytesCnt];
126cdf0e10cSrcweir             xSeekableInStream.readBytes ( pBytesRead, pBytesRead[0].length + 1 );
127cdf0e10cSrcweir             m_aTestHelper.Message ( "Read from SequenceInputStream." );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             //close the streams
130cdf0e10cSrcweir             xSeqOutStream.closeOutput ();
131cdf0e10cSrcweir             xSeekableInStream.closeInput ();
132cdf0e10cSrcweir             m_aTestHelper.Message ( "Both streams closed." );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir             //compare the original, written and read arrys
135cdf0e10cSrcweir             for ( int i = 0; i < nBytesCnt; ++i ) {
136cdf0e10cSrcweir                 if ( pBytesOriginal[i] != pBytesWritten[i] ) {
137cdf0e10cSrcweir                     m_aTestHelper.Error ( "Written array not identical to " +
138cdf0e10cSrcweir                             "original array. Position: " + i );
139cdf0e10cSrcweir                     return /* false */;
140cdf0e10cSrcweir                 } else if ( pBytesOriginal[i] != pBytesRead[0][i] ) {
141cdf0e10cSrcweir                     m_aTestHelper.Error ( "Read array not identical to original " +
142cdf0e10cSrcweir                             "array. Position: " + i );
143cdf0e10cSrcweir                     return /* false */;
144cdf0e10cSrcweir                 }
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir             m_aTestHelper.Message ( "All data correct." );
147cdf0e10cSrcweir         } catch ( Exception e ) {
148cdf0e10cSrcweir             m_aTestHelper.Error ( "Exception: " + e );
149cdf0e10cSrcweir             return /* false */;
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         return /* true */;
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
getMSF()154cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
157cdf0e10cSrcweir         return xMSF1;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     // setup and close connections
setUpConnection()161cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
162cdf0e10cSrcweir         System.out.println("setUpConnection()");
163cdf0e10cSrcweir         connection.setUp();
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
tearDownConnection()166cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
167cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         System.out.println("tearDownConnection()");
170cdf0e10cSrcweir         connection.tearDown();
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
174*2062e9d7SDamjan Jovanovic }
175