1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
16 #include <com/sun/star/io/NotConnectedException.hpp>
17 #include <com/sun/star/io/TextInputStream.hpp>
18 #include <com/sun/star/io/XInputStream.hpp>
19 #include <com/sun/star/io/XTextInputStream2.hpp>
20 #include <com/sun/star/uno/Sequence.hxx>
21 #include <com/sun/star/uno/Reference.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppunit/TestAssert.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
26 #include <osl/mutex.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
29 #include <unotest/bootstrapfixturebase.hxx>
33 class Input
: public cppu::WeakImplHelper
<css::io::XInputStream
> {
35 Input(): open_(true), index_(0) {}
38 virtual ~Input() override
{}
40 sal_Int32 SAL_CALL
readBytes(css::uno::Sequence
<sal_Int8
> &, sal_Int32
)
42 { CPPUNIT_FAIL("readLine is supposed to call readSomeBytes instead"); }
44 sal_Int32 SAL_CALL
readSomeBytes(
45 css::uno::Sequence
<sal_Int8
> & aData
, sal_Int32 nMaxBytesToRead
) override
47 assert(nMaxBytesToRead
>= 0);
48 osl::MutexGuard
g(mutex_
);
50 assert(index_
>= 0 && index_
<= SIZE
);
51 sal_Int32 n
= std::min
<sal_Int32
>(
52 std::min
<sal_Int32
>(nMaxBytesToRead
, 2), SIZE
- index_
);
53 assert(n
>= 0 && n
<= SIZE
- index_
);
55 std::memcpy(aData
.getArray(), data
+ index_
, n
);
57 assert(index_
>= 0 && index_
<= SIZE
);
61 void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip
) override
63 assert(nBytesToSkip
>= 0);
64 osl::MutexGuard
g(mutex_
);
66 assert(index_
>= 0 && index_
<= SIZE
);
67 index_
+= std::min
<sal_Int32
>(nBytesToSkip
, SIZE
- index_
);
68 assert(index_
>= 0 && index_
<= SIZE
);
71 sal_Int32 SAL_CALL
available() override
73 osl::MutexGuard
g(mutex_
);
75 assert(index_
>= 0 && index_
<= SIZE
);
79 void SAL_CALL
closeInput() override
81 osl::MutexGuard
g(mutex_
);
88 throw css::io::NotConnectedException(
89 "test input stream already closed");
93 static sal_Int32
const SIZE
= 9;
94 static char const data
[SIZE
];
101 char const Input::data
[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
103 class Test
: public test::BootstrapFixtureBase
{
105 CPPUNIT_TEST_SUITE(Test
);
106 CPPUNIT_TEST(testReadLine
);
107 CPPUNIT_TEST_SUITE_END();
112 void Test::testReadLine() {
113 css::uno::Reference
<css::io::XTextInputStream2
> s(
114 css::io::TextInputStream::create(getComponentContext()));
115 s
->setInputStream(new Input
);
116 OUString
l(s
->readLine());
117 CPPUNIT_ASSERT_EQUAL(OUString("123456789"), l
);
120 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
124 CPPUNIT_PLUGIN_IMPLEMENT();
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */