2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.comp
.connections
;
21 import com
.sun
.star
.io
.IOException
;
22 import org
.junit
.Test
;
23 import static org
.junit
.Assert
.*;
25 public final class PipedConnection_Test
{
26 private static final int ROUNDS
= 2000;
28 @Test public void test() throws Exception
{
29 PipedConnection rightSide
= new PipedConnection(new Object
[0]);
30 PipedConnection leftSide
= new PipedConnection(new Object
[]{rightSide
});
32 Reader reader
= new Reader(rightSide
);
33 Writer writer
= new Writer(leftSide
);
41 assertTrue(writer
._state
);
42 assertTrue(reader
._state
);
43 assertEquals(ROUNDS
, reader
._rounds
);
46 private static class Reader
extends Thread
{
47 PipedConnection _pipedConnection
;
48 boolean _state
= false;
51 Reader(PipedConnection pipedConnection
) {
52 _pipedConnection
= pipedConnection
;
58 for (byte v
= 0;; v
++) {
59 byte[][] b
= new byte[1][];
60 int n
= _pipedConnection
.read(b
, 1);
65 assertEquals(1, b
[0].length
);
66 assertEquals(v
, b
[0][0]);
69 _pipedConnection
.close();
71 } catch (IOException e
) {
72 throw new RuntimeException(e
);
77 private static class Writer
extends Thread
{
78 PipedConnection _pipedConnection
;
79 boolean _state
= false;
81 Writer(PipedConnection pipedConnection
) {
82 _pipedConnection
= pipedConnection
;
89 for (int i
= 0; i
!= ROUNDS
; ++i
) {
90 byte[] b
= new byte[] { v
++ };
91 _pipedConnection
.write(b
);
92 _pipedConnection
.flush();
94 _pipedConnection
.close();
96 } catch (IOException e
) {
97 throw new RuntimeException(e
);