From 6f112bd40fe78141de865934145d81b1258b36de Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Tue, 20 Dec 2022 22:20:23 +0800 Subject: [PATCH] tdf#97362 Convert comphelper SequenceOutputStreamUnitTest to Python Signed-off-by: Chenxiong Qi Change-Id: I72826fc8180fcb147ff84b10b88c16a4084799ec Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145690 Tested-by: Jenkins Reviewed-by: Hossein --- comphelper/JunitTest_comphelper_complex.mk | 2 - comphelper/Module_comphelper.mk | 1 + comphelper/PythonTest_comphelper_python.mk | 13 ++ .../SequenceOutputStreamUnitTest.java | 132 ------------------ comphelper/qa/complex/comphelper_all.sce | 1 - .../qa/python/test_sequence_output_stream.py | 84 +++++++++++ 6 files changed, 98 insertions(+), 135 deletions(-) create mode 100644 comphelper/PythonTest_comphelper_python.mk delete mode 100644 comphelper/qa/complex/comphelper/SequenceOutputStreamUnitTest.java create mode 100644 comphelper/qa/python/test_sequence_output_stream.py diff --git a/comphelper/JunitTest_comphelper_complex.mk b/comphelper/JunitTest_comphelper_complex.mk index 1171eb1cd232..e35857b35b07 100644 --- a/comphelper/JunitTest_comphelper_complex.mk +++ b/comphelper/JunitTest_comphelper_complex.mk @@ -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 \ )) diff --git a/comphelper/Module_comphelper.mk b/comphelper/Module_comphelper.mk index 7541a59f1641..c483e50b0cc6 100644 --- a/comphelper/Module_comphelper.mk +++ b/comphelper/Module_comphelper.mk @@ -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,\ diff --git a/comphelper/PythonTest_comphelper_python.mk b/comphelper/PythonTest_comphelper_python.mk new file mode 100644 index 000000000000..e020deb9899c --- /dev/null +++ b/comphelper/PythonTest_comphelper_python.mk @@ -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: diff --git a/comphelper/qa/complex/comphelper/SequenceOutputStreamUnitTest.java b/comphelper/qa/complex/comphelper/SequenceOutputStreamUnitTest.java deleted file mode 100644 index 03906134b9f7..000000000000 --- a/comphelper/qa/complex/comphelper/SequenceOutputStreamUnitTest.java +++ /dev/null @@ -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(); -} diff --git a/comphelper/qa/complex/comphelper_all.sce b/comphelper/qa/complex/comphelper_all.sce index 5ee5f941f94a..656850ab0712 100644 --- a/comphelper/qa/complex/comphelper_all.sce +++ b/comphelper/qa/complex/comphelper_all.sce @@ -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 diff --git a/comphelper/qa/python/test_sequence_output_stream.py b/comphelper/qa/python/test_sequence_output_stream.py new file mode 100644 index 000000000000..6f1294960e29 --- /dev/null +++ b/comphelper/qa/python/test_sequence_output_stream.py @@ -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: