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