12be43276SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32be43276SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42be43276SAndrew Rist * or more contributor license agreements. See the NOTICE file 52be43276SAndrew Rist * distributed with this work for additional information 62be43276SAndrew Rist * regarding copyright ownership. The ASF licenses this file 72be43276SAndrew Rist * to you under the Apache License, Version 2.0 (the 82be43276SAndrew Rist * "License"); you may not use this file except in compliance 92be43276SAndrew Rist * with the License. You may obtain a copy of the License at 102be43276SAndrew Rist * 112be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 122be43276SAndrew Rist * 132be43276SAndrew Rist * Unless required by applicable law or agreed to in writing, 142be43276SAndrew Rist * software distributed under the License is distributed on an 152be43276SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162be43276SAndrew Rist * KIND, either express or implied. See the License for the 172be43276SAndrew Rist * specific language governing permissions and limitations 182be43276SAndrew Rist * under the License. 192be43276SAndrew Rist * 202be43276SAndrew Rist *************************************************************/ 212be43276SAndrew Rist 222be43276SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.comp.connections; 25cdf0e10cSrcweir 26*9cbd97ceSDamjan Jovanovic import org.junit.Test; 27*9cbd97ceSDamjan Jovanovic import static org.junit.Assert.*; 28cdf0e10cSrcweir 29*9cbd97ceSDamjan Jovanovic public final class PipedConnection_Test { 30*9cbd97ceSDamjan Jovanovic @Test test()31cdf0e10cSrcweir public void test() throws Exception { 32cdf0e10cSrcweir PipedConnection rightSide = new PipedConnection(new Object[0]); 33cdf0e10cSrcweir PipedConnection leftSide = new PipedConnection(new Object[]{rightSide}); 34cdf0e10cSrcweir 35cdf0e10cSrcweir byte theByte[] = new byte[1]; 36cdf0e10cSrcweir 37cdf0e10cSrcweir Reader reader = new Reader(rightSide, theByte); 38cdf0e10cSrcweir Writer writer = new Writer(leftSide, theByte, reader); 39cdf0e10cSrcweir 40cdf0e10cSrcweir reader.start(); 41cdf0e10cSrcweir writer.start(); 42cdf0e10cSrcweir 43cdf0e10cSrcweir Thread.sleep(2000); 44cdf0e10cSrcweir 45cdf0e10cSrcweir writer.term(); 46cdf0e10cSrcweir writer.join(); 47cdf0e10cSrcweir 48cdf0e10cSrcweir reader.join(); 49cdf0e10cSrcweir 50*9cbd97ceSDamjan Jovanovic assertTrue("", writer._state && reader._state); 51cdf0e10cSrcweir } 52cdf0e10cSrcweir 53cdf0e10cSrcweir static class Reader extends Thread { 54cdf0e10cSrcweir PipedConnection _pipedConnection; 55cdf0e10cSrcweir byte _theByte[]; 56cdf0e10cSrcweir boolean _quit; 57cdf0e10cSrcweir boolean _state = false; 58cdf0e10cSrcweir Reader(PipedConnection pipedConnection, byte theByte[])59cdf0e10cSrcweir Reader(PipedConnection pipedConnection, byte theByte[]) { 60cdf0e10cSrcweir _pipedConnection = pipedConnection; 61cdf0e10cSrcweir _theByte = theByte; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir run()64cdf0e10cSrcweir public void run() { 65cdf0e10cSrcweir try { 66cdf0e10cSrcweir byte bytes[][] = new byte[1][]; 67cdf0e10cSrcweir 68cdf0e10cSrcweir while(!_quit) { 69cdf0e10cSrcweir int read = _pipedConnection.read(bytes, 1); 70cdf0e10cSrcweir 71cdf0e10cSrcweir if(read == 1) { 72cdf0e10cSrcweir // System.err.println("read :" + bytes[0][0]); 73cdf0e10cSrcweir 74cdf0e10cSrcweir if(_theByte[0] != bytes[0][0]) 75cdf0e10cSrcweir throw new NullPointerException(); 76cdf0e10cSrcweir 77cdf0e10cSrcweir synchronized(this) { 78cdf0e10cSrcweir notifyAll(); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } 81cdf0e10cSrcweir else 82cdf0e10cSrcweir _quit = true; // EOF 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir _pipedConnection.close(); 86cdf0e10cSrcweir _state = true; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir catch(com.sun.star.io.IOException ioException) { 89cdf0e10cSrcweir System.err.println("#### Reader - unexpected:" + ioException); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir static class Writer extends Thread { 96cdf0e10cSrcweir PipedConnection _pipedConnection; 97cdf0e10cSrcweir byte _theByte[]; 98cdf0e10cSrcweir Reader _reader; 99cdf0e10cSrcweir boolean _quit; 100cdf0e10cSrcweir boolean _state = false; 101cdf0e10cSrcweir Writer(PipedConnection pipedConnection, byte theByte[], Reader reader)102cdf0e10cSrcweir Writer(PipedConnection pipedConnection, byte theByte[], Reader reader) { 103cdf0e10cSrcweir _pipedConnection = pipedConnection; 104cdf0e10cSrcweir _reader = reader; 105cdf0e10cSrcweir _theByte = theByte; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir run()108cdf0e10cSrcweir public void run() { 109cdf0e10cSrcweir try { 110cdf0e10cSrcweir while(!_quit) { 111cdf0e10cSrcweir synchronized(_reader) { 112cdf0e10cSrcweir _pipedConnection.write(_theByte); 113cdf0e10cSrcweir _pipedConnection.flush(); 114cdf0e10cSrcweir // System.err.println("written :" + _theByte[0]); 115cdf0e10cSrcweir 116cdf0e10cSrcweir _reader.wait(); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir ++ _theByte[0]; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir _pipedConnection.close(); 122cdf0e10cSrcweir 123cdf0e10cSrcweir _state = true; 124cdf0e10cSrcweir } 125cdf0e10cSrcweir catch(com.sun.star.io.IOException ioException) { 126cdf0e10cSrcweir System.err.println("#### Writer:" + ioException); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir catch(InterruptedException interruptedException) { 129cdf0e10cSrcweir System.err.println("#### Writer:" + interruptedException); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir } 132cdf0e10cSrcweir term()133cdf0e10cSrcweir public void term() { 134cdf0e10cSrcweir _quit = true; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir } 137cdf0e10cSrcweir } 138