Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <sal/config.h>
11 :
12 : #include <algorithm>
13 : #include <cassert>
14 : #include <cstring>
15 : #include <exception>
16 :
17 : #include "com/sun/star/io/BufferSizeExceededException.hdl"
18 : #include "com/sun/star/io/IOException.hdl"
19 : #include "com/sun/star/io/NotConnectedException.hdl"
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.hdl"
26 : #include <cppuhelper/implbase1.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>
34 :
35 : namespace {
36 :
37 : class Input: public cppu::WeakImplHelper1<css::io::XInputStream> {
38 : public:
39 1 : Input(): open_(true), index_(0) {}
40 :
41 : private:
42 2 : virtual ~Input() {}
43 :
44 0 : 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 : SAL_OVERRIDE
50 0 : { CPPUNIT_FAIL("readLine is supposed to call readSomeBytes instead"); return 0;}
51 :
52 6 : 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) SAL_OVERRIDE
58 : {
59 : assert(nMaxBytesToRead >= 0);
60 6 : osl::MutexGuard g(mutex_);
61 6 : checkClosed();
62 : assert(index_ >= 0 && index_ <= SIZE);
63 : sal_Int32 n = std::min<sal_Int32>(
64 6 : std::min<sal_Int32>(nMaxBytesToRead, 2), SIZE - index_);
65 : assert(n >= 0 && n <= SIZE - index_);
66 6 : aData.realloc(n);
67 6 : std::memcpy(aData.getArray(), data + index_, n);
68 6 : index_ += n;
69 : assert(index_ >= 0 && index_ <= SIZE);
70 6 : return n;
71 : }
72 :
73 0 : 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) SAL_OVERRIDE
78 : {
79 : assert(nBytesToSkip >= 0);
80 0 : osl::MutexGuard g(mutex_);
81 0 : checkClosed();
82 : assert(index_ >= 0 && index_ <= SIZE);
83 0 : index_ += std::min<sal_Int32>(nBytesToSkip, SIZE - index_);
84 0 : assert(index_ >= 0 && index_ <= SIZE);
85 0 : }
86 :
87 0 : sal_Int32 SAL_CALL available()
88 : throw (
89 : css::io::NotConnectedException, css::io::IOException,
90 : css::uno::RuntimeException, std::exception) SAL_OVERRIDE
91 : {
92 0 : osl::MutexGuard g(mutex_);
93 0 : checkClosed();
94 : assert(index_ >= 0 && index_ <= SIZE);
95 0 : return SIZE - index_;
96 : }
97 :
98 0 : void SAL_CALL closeInput()
99 : throw (
100 : css::io::NotConnectedException, css::io::IOException,
101 : css::uno::RuntimeException, std::exception) SAL_OVERRIDE
102 : {
103 0 : osl::MutexGuard g(mutex_);
104 0 : checkClosed();
105 0 : open_ = true;
106 0 : }
107 :
108 6 : void checkClosed() {
109 6 : if (!open_) {
110 : throw css::io::NotConnectedException(
111 0 : "test input stream already closed");
112 : }
113 6 : }
114 :
115 : static sal_Int32 const SIZE = 9;
116 : static char const data[SIZE];
117 :
118 : osl::Mutex mutex_;
119 : bool open_;
120 : sal_Int32 index_;
121 : };
122 :
123 : char const Input::data[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
124 :
125 3 : class Test: public test::BootstrapFixtureBase {
126 : private:
127 2 : CPPUNIT_TEST_SUITE(Test);
128 1 : CPPUNIT_TEST(testReadLine);
129 5 : CPPUNIT_TEST_SUITE_END();
130 :
131 : void testReadLine();
132 : };
133 :
134 1 : void Test::testReadLine() {
135 : css::uno::Reference<css::io::XTextInputStream2> s(
136 1 : css::io::TextInputStream::create(getComponentContext()));
137 1 : s->setInputStream(new Input);
138 2 : rtl::OUString l(s->readLine());
139 2 : CPPUNIT_ASSERT_EQUAL(rtl::OUString("123456789"), l);
140 1 : }
141 :
142 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
143 :
144 : }
145 :
146 4 : CPPUNIT_PLUGIN_IMPLEMENT();
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|