tdf#97362 Convert comphelper SequenceOutputStreamUnitTest to Python
Signed-off-by: Chenxiong Qi <qcxhome@gmail.com> Change-Id: I72826fc8180fcb147ff84b10b88c16a4084799ec Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145690 Tested-by: Jenkins Reviewed-by: Hossein <hossein@libreoffice.org>
This commit is contained in:
parent
af1ca684bb
commit
6f112bd40f
6 changed files with 98 additions and 135 deletions
|
@ -23,11 +23,9 @@ $(eval $(call gb_JunitTest_use_unoapi_jars,comphelper_complex))
|
|||
|
||||
$(eval $(call gb_JunitTest_add_sourcefiles,comphelper_complex,\
|
||||
comphelper/qa/complex/comphelper/Map \
|
||||
comphelper/qa/complex/comphelper/SequenceOutputStreamUnitTest \
|
||||
))
|
||||
|
||||
$(eval $(call gb_JunitTest_add_classes,comphelper_complex,\
|
||||
complex.comphelper.SequenceOutputStreamUnitTest \
|
||||
complex.comphelper.Map \
|
||||
))
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ $(eval $(call gb_Module_add_targets,comphelper,\
|
|||
|
||||
$(eval $(call gb_Module_add_subsequentcheck_targets,comphelper,\
|
||||
JunitTest_comphelper_complex \
|
||||
PythonTest_comphelper_python \
|
||||
))
|
||||
|
||||
$(eval $(call gb_Module_add_check_targets,comphelper,\
|
||||
|
|
13
comphelper/PythonTest_comphelper_python.mk
Normal file
13
comphelper/PythonTest_comphelper_python.mk
Normal file
|
@ -0,0 +1,13 @@
|
|||
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
|
||||
#
|
||||
# This file is part of the LibreOffice project.
|
||||
#
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
#
|
||||
$(eval $(call gb_PythonTest_PythonTest,comphelper_python))
|
||||
$(eval $(call gb_PythonTest_add_modules,comphelper_python,$(SRCDIR)/comphelper/qa/python,\
|
||||
test_sequence_output_stream \
|
||||
))
|
||||
# vim: set noet sw=4 ts=4:
|
|
@ -1,132 +0,0 @@
|
|||
/*
|
||||
* This file is part of the LibreOffice project.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* This file incorporates work covered by the following license notice:
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed
|
||||
* with this work for additional information regarding copyright
|
||||
* ownership. The ASF licenses this file to you under the Apache
|
||||
* License, Version 2.0 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
||||
*/
|
||||
package complex.comphelper;
|
||||
|
||||
import com.sun.star.lang.XMultiServiceFactory;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
|
||||
import com.sun.star.io.XSequenceOutputStream;
|
||||
import com.sun.star.io.XSeekableInputStream;
|
||||
|
||||
import java.util.Random;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.openoffice.test.OfficeConnection;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/* Document.
|
||||
*/
|
||||
|
||||
public class SequenceOutputStreamUnitTest
|
||||
{
|
||||
private XMultiServiceFactory m_xMSF = null;
|
||||
|
||||
@Before public void before() {
|
||||
try {
|
||||
m_xMSF = getMSF();
|
||||
} catch (Exception e) {
|
||||
fail ("Cannot create service factory!");
|
||||
}
|
||||
if (m_xMSF==null) {
|
||||
fail ("Cannot create service factory!");
|
||||
}
|
||||
}
|
||||
|
||||
@After public void after() {
|
||||
m_xMSF = null;
|
||||
}
|
||||
|
||||
@Test public void test () {
|
||||
try {
|
||||
final int nBytesCnt = 20;
|
||||
|
||||
//create SequenceOutputStream
|
||||
Object oSequenceOutputStream = m_xMSF.createInstance (
|
||||
"com.sun.star.io.SequenceOutputStream" );
|
||||
XSequenceOutputStream xSeqOutStream =
|
||||
UnoRuntime.queryInterface (
|
||||
XSequenceOutputStream.class, oSequenceOutputStream );
|
||||
|
||||
//write something to the stream
|
||||
byte pBytesOriginal[] = new byte [nBytesCnt];
|
||||
Random oRandom = new Random();
|
||||
oRandom.nextBytes (pBytesOriginal);
|
||||
xSeqOutStream.writeBytes (pBytesOriginal);
|
||||
|
||||
// Append the same content once again
|
||||
xSeqOutStream.writeBytes (pBytesOriginal);
|
||||
|
||||
byte pBytesWritten[] = xSeqOutStream.getWrittenBytes ();
|
||||
assertTrue( "SequenceOutputStream::getWrittenBytes() - wrong amount of bytes returned",
|
||||
pBytesWritten.length == nBytesCnt * 2 );
|
||||
|
||||
//create SequenceInputstream
|
||||
Object pArgs[] = new Object[1];
|
||||
pArgs[0] = pBytesWritten;
|
||||
Object oSequenceInputStream = m_xMSF.createInstanceWithArguments (
|
||||
"com.sun.star.io.SequenceInputStream", pArgs );
|
||||
XSeekableInputStream xSeekableInStream =
|
||||
UnoRuntime.queryInterface (
|
||||
XSeekableInputStream.class, oSequenceInputStream );
|
||||
|
||||
//read from the stream
|
||||
byte pBytesRead[][] = new byte [1][nBytesCnt*2];
|
||||
int nBytesCountRead = xSeekableInStream.readBytes ( pBytesRead, pBytesRead[0].length + 1 );
|
||||
|
||||
assertTrue( "SequenceInputStream::readBytes() - wrong amount of bytes returned " + pBytesRead[0].length + " vs " + (nBytesCountRead),
|
||||
pBytesRead[0].length == nBytesCountRead);
|
||||
|
||||
//close the streams
|
||||
xSeqOutStream.closeOutput ();
|
||||
xSeekableInStream.closeInput ();
|
||||
|
||||
//compare the original, written and read arrays
|
||||
for ( int i = 0; i < nBytesCnt * 2; ++i ) {
|
||||
assertTrue( "Written array not identical to original array. Position: " + i,
|
||||
pBytesOriginal[i % nBytesCnt] == pBytesWritten[i] );
|
||||
assertTrue( "Read array not identical to original array. Position: " + i,
|
||||
pBytesOriginal[i % nBytesCnt] == pBytesRead[0][i] );
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
fail ( "Exception: " + e );
|
||||
}
|
||||
}
|
||||
|
||||
private static XMultiServiceFactory getMSF()
|
||||
{
|
||||
return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
|
||||
}
|
||||
|
||||
// setup and close connections
|
||||
@BeforeClass public static void setUpConnection() throws Exception {
|
||||
System.out.println("setUpConnection()");
|
||||
connection.setUp();
|
||||
}
|
||||
|
||||
@AfterClass public static void tearDownConnection()
|
||||
throws InterruptedException, com.sun.star.uno.Exception
|
||||
{
|
||||
System.out.println("tearDownConnection()");
|
||||
connection.tearDown();
|
||||
}
|
||||
|
||||
private static final OfficeConnection connection = new OfficeConnection();
|
||||
}
|
|
@ -15,5 +15,4 @@
|
|||
# except in compliance with the License. You may obtain a copy of
|
||||
# the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
||||
#
|
||||
-o complex.comphelper.SequenceOutputStreamUnitTest
|
||||
-o complex.comphelper.Map
|
||||
|
|
84
comphelper/qa/python/test_sequence_output_stream.py
Normal file
84
comphelper/qa/python/test_sequence_output_stream.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
|
||||
#
|
||||
# This file is part of the LibreOffice project.
|
||||
#
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
#
|
||||
|
||||
import unittest
|
||||
import uno
|
||||
|
||||
from org.libreoffice.unotest import UnoInProcess
|
||||
|
||||
|
||||
class TestSequenceOutputStream(unittest.TestCase):
|
||||
"""Test com.sun.star.io.SequenceOutputStream"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._uno = UnoInProcess()
|
||||
cls._uno.setUp()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls._uno.tearDown()
|
||||
|
||||
def setUp(self):
|
||||
self.data = uno.ByteSequence(b"some data")
|
||||
try:
|
||||
self.service_manager = self._uno.getContext().getServiceManager()
|
||||
except:
|
||||
raise RuntimeError("Cannot create service factory!")
|
||||
if self.service_manager is None:
|
||||
raise RuntimeError("Cannot create service factory!")
|
||||
|
||||
def test_stream(self):
|
||||
try:
|
||||
seq_output_stream = self.service_manager.createInstance(
|
||||
"com.sun.star.io.SequenceOutputStream"
|
||||
)
|
||||
seq_output_stream.writeBytes(self.data)
|
||||
|
||||
# Append the same content once again
|
||||
seq_output_stream.writeBytes(self.data)
|
||||
|
||||
written_bytes = seq_output_stream.getWrittenBytes()
|
||||
|
||||
self.assertEqual(
|
||||
len(self.data) * 2,
|
||||
len(written_bytes),
|
||||
"SequenceOutputStream::getWrittenBytes() - wrong amount of bytes returned",
|
||||
)
|
||||
|
||||
# create SequenceInputstream
|
||||
seq_input_stream = self.service_manager.createInstanceWithArguments(
|
||||
"com.sun.star.io.SequenceInputStream", (written_bytes,)
|
||||
)
|
||||
|
||||
# read from the stream
|
||||
nbytes_read, read_bytes = seq_input_stream.readBytes(None, len(self.data) * 2 + 1)
|
||||
self.assertEqual(
|
||||
len(self.data) * 2,
|
||||
nbytes_read,
|
||||
"SequenceInputStream::readBytes() - "
|
||||
f"wrong amount of bytes returned {len(self.data) * 2} vs {nbytes_read}",
|
||||
)
|
||||
|
||||
# close the streams
|
||||
seq_output_stream.closeOutput()
|
||||
seq_input_stream.closeInput()
|
||||
|
||||
expected = uno.ByteSequence(self.data.value * 2)
|
||||
self.assertEqual(expected, written_bytes, "Written array not identical to original.")
|
||||
self.assertEqual(expected, read_bytes, "Read array not identical to original.")
|
||||
except Exception as e:
|
||||
self.fail(f"Exception: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
# vim: set shiftwidth=4 softtabstop=4 expandtab:
|
Loading…
Reference in a new issue