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>
17 #include "com/sun/star/io/BufferSizeExceededException.hpp"
18 #include "com/sun/star/io/IOException.hpp"
19 #include "com/sun/star/io/NotConnectedException.hpp"
20 #include <com/sun/star/io/TextInputStream.hpp>
21 #include <com/sun/star/io/XInputStream.hpp>
22 #include <com/sun/star/io/XTextInputStream2.hpp>
23 #include "com/sun/star/uno/Sequence.hxx"
24 #include <com/sun/star/uno/Reference.hxx>
25 #include "com/sun/star/uno/RuntimeException.hpp"
26 #include <cppuhelper/implbase.hxx>
27 #include <cppunit/TestAssert.h>
28 #include <cppunit/extensions/HelperMacros.h>
29 #include <cppunit/plugin/TestPlugIn.h>
30 #include <osl/mutex.hxx>
31 #include <rtl/ustring.hxx>
32 #include <sal/types.h>
33 #include <unotest/bootstrapfixturebase.hxx>
37 class Input
: public cppu::WeakImplHelper
<css::io::XInputStream
> {
39 Input(): open_(true), index_(0) {}
44 sal_Int32 SAL_CALL
readBytes(css::uno::Sequence
<sal_Int8
> &, sal_Int32
)
46 css::io::NotConnectedException
,
47 css::io::BufferSizeExceededException
, css::io::IOException
,
48 css::uno::RuntimeException
, std::exception
)
50 { CPPUNIT_FAIL("readLine is supposed to call readSomeBytes instead"); return 0;}
52 sal_Int32 SAL_CALL
readSomeBytes(
53 css::uno::Sequence
<sal_Int8
> & aData
, sal_Int32 nMaxBytesToRead
)
55 css::io::NotConnectedException
,
56 css::io::BufferSizeExceededException
, css::io::IOException
,
57 css::uno::RuntimeException
, ::std::exception
) override
59 assert(nMaxBytesToRead
>= 0);
60 osl::MutexGuard
g(mutex_
);
62 assert(index_
>= 0 && index_
<= SIZE
);
63 sal_Int32 n
= std::min
<sal_Int32
>(
64 std::min
<sal_Int32
>(nMaxBytesToRead
, 2), SIZE
- index_
);
65 assert(n
>= 0 && n
<= SIZE
- index_
);
67 std::memcpy(aData
.getArray(), data
+ index_
, n
);
69 assert(index_
>= 0 && index_
<= SIZE
);
73 void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip
)
75 css::io::NotConnectedException
,
76 css::io::BufferSizeExceededException
, css::io::IOException
,
77 css::uno::RuntimeException
, std::exception
) override
79 assert(nBytesToSkip
>= 0);
80 osl::MutexGuard
g(mutex_
);
82 assert(index_
>= 0 && index_
<= SIZE
);
83 index_
+= std::min
<sal_Int32
>(nBytesToSkip
, SIZE
- index_
);
84 assert(index_
>= 0 && index_
<= SIZE
);
87 sal_Int32 SAL_CALL
available()
89 css::io::NotConnectedException
, css::io::IOException
,
90 css::uno::RuntimeException
, std::exception
) override
92 osl::MutexGuard
g(mutex_
);
94 assert(index_
>= 0 && index_
<= SIZE
);
98 void SAL_CALL
closeInput()
100 css::io::NotConnectedException
, css::io::IOException
,
101 css::uno::RuntimeException
, std::exception
) override
103 osl::MutexGuard
g(mutex_
);
110 throw css::io::NotConnectedException(
111 "test input stream already closed");
115 static sal_Int32
const SIZE
= 9;
116 static char const data
[SIZE
];
123 char const Input::data
[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
125 class Test
: public test::BootstrapFixtureBase
{
127 CPPUNIT_TEST_SUITE(Test
);
128 CPPUNIT_TEST(testReadLine
);
129 CPPUNIT_TEST_SUITE_END();
134 void Test::testReadLine() {
135 css::uno::Reference
<css::io::XTextInputStream2
> s(
136 css::io::TextInputStream::create(getComponentContext()));
137 s
->setInputStream(new Input
);
138 rtl::OUString
l(s
->readLine());
139 CPPUNIT_ASSERT_EQUAL(rtl::OUString("123456789"), l
);
142 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
146 CPPUNIT_PLUGIN_IMPLEMENT();
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */