merge the formfield patch from ooo-build
[ooovba.git] / jurt / test / com / sun / star / comp / connections / PipedConnection_Test.java
blobb8a77aab1255f0d43951d54e0c49341740d51f61
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PipedConnection_Test.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.comp.connections;
33 import complexlib.ComplexTestCase;
35 public final class PipedConnection_Test extends ComplexTestCase {
36 public String getTestObjectName() {
37 return getClass().getName();
40 public String[] getTestMethodNames() {
41 return new String[] { "test" };
44 public void test() throws Exception {
45 PipedConnection rightSide = new PipedConnection(new Object[0]);
46 PipedConnection leftSide = new PipedConnection(new Object[]{rightSide});
48 byte theByte[] = new byte[1];
50 Reader reader = new Reader(rightSide, theByte);
51 Writer writer = new Writer(leftSide, theByte, reader);
53 reader.start();
54 writer.start();
56 Thread.sleep(2000);
58 writer.term();
59 writer.join();
61 reader.join();
63 assure("", writer._state && reader._state);
66 static class Reader extends Thread {
67 PipedConnection _pipedConnection;
68 byte _theByte[];
69 boolean _quit;
70 boolean _state = false;
72 Reader(PipedConnection pipedConnection, byte theByte[]) {
73 _pipedConnection = pipedConnection;
74 _theByte = theByte;
77 public void run() {
78 try {
79 byte bytes[][] = new byte[1][];
81 while(!_quit) {
82 int read = _pipedConnection.read(bytes, 1);
84 if(read == 1) {
85 // System.err.println("read :" + bytes[0][0]);
87 if(_theByte[0] != bytes[0][0])
88 throw new NullPointerException();
90 synchronized(this) {
91 notifyAll();
94 else
95 _quit = true; // EOF
98 _pipedConnection.close();
99 _state = true;
101 catch(com.sun.star.io.IOException ioException) {
102 System.err.println("#### Reader - unexpected:" + ioException);
108 static class Writer extends Thread {
109 PipedConnection _pipedConnection;
110 byte _theByte[];
111 Reader _reader;
112 boolean _quit;
113 boolean _state = false;
115 Writer(PipedConnection pipedConnection, byte theByte[], Reader reader) {
116 _pipedConnection = pipedConnection;
117 _reader = reader;
118 _theByte = theByte;
121 public void run() {
122 try {
123 while(!_quit) {
124 synchronized(_reader) {
125 _pipedConnection.write(_theByte);
126 _pipedConnection.flush();
127 // System.err.println("written :" + _theByte[0]);
129 _reader.wait();
131 ++ _theByte[0];
134 _pipedConnection.close();
136 _state = true;
138 catch(com.sun.star.io.IOException ioException) {
139 System.err.println("#### Writer:" + ioException);
141 catch(InterruptedException interruptedException) {
142 System.err.println("#### Writer:" + interruptedException);
146 public void term() {
147 _quit = true;