Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / io / qa / textinputstream.cxx
blob5a58391460f45caf815f7670ac371fbc15b34875
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
12 #include <algorithm>
13 #include <cassert>
14 #include <cstring>
15 #include <exception>
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>
35 namespace {
37 class Input: public cppu::WeakImplHelper<css::io::XInputStream> {
38 public:
39 Input(): open_(true), index_(0) {}
41 private:
42 virtual ~Input() {}
44 sal_Int32 SAL_CALL readBytes(css::uno::Sequence<sal_Int8> &, sal_Int32)
45 throw (
46 css::io::NotConnectedException,
47 css::io::BufferSizeExceededException, css::io::IOException,
48 css::uno::RuntimeException, std::exception)
49 override
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)
54 throw (
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_);
61 checkClosed();
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_);
66 aData.realloc(n);
67 std::memcpy(aData.getArray(), data + index_, n);
68 index_ += n;
69 assert(index_ >= 0 && index_ <= SIZE);
70 return n;
73 void SAL_CALL skipBytes(sal_Int32 nBytesToSkip)
74 throw (
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_);
81 checkClosed();
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()
88 throw (
89 css::io::NotConnectedException, css::io::IOException,
90 css::uno::RuntimeException, std::exception) override
92 osl::MutexGuard g(mutex_);
93 checkClosed();
94 assert(index_ >= 0 && index_ <= SIZE);
95 return SIZE - index_;
98 void SAL_CALL closeInput()
99 throw (
100 css::io::NotConnectedException, css::io::IOException,
101 css::uno::RuntimeException, std::exception) override
103 osl::MutexGuard g(mutex_);
104 checkClosed();
105 open_ = true;
108 void checkClosed() {
109 if (!open_) {
110 throw css::io::NotConnectedException(
111 "test input stream already closed");
115 static sal_Int32 const SIZE = 9;
116 static char const data[SIZE];
118 osl::Mutex mutex_;
119 bool open_;
120 sal_Int32 index_;
123 char const Input::data[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
125 class Test: public test::BootstrapFixtureBase {
126 private:
127 CPPUNIT_TEST_SUITE(Test);
128 CPPUNIT_TEST(testReadLine);
129 CPPUNIT_TEST_SUITE_END();
131 void testReadLine();
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: */