1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package com.sun.star.comp.connections; 29 30 import complexlib.ComplexTestCase; 31 32 public final class PipedConnection_Test extends ComplexTestCase { 33 public String getTestObjectName() { 34 return getClass().getName(); 35 } 36 37 public String[] getTestMethodNames() { 38 return new String[] { "test" }; 39 } 40 41 public void test() throws Exception { 42 PipedConnection rightSide = new PipedConnection(new Object[0]); 43 PipedConnection leftSide = new PipedConnection(new Object[]{rightSide}); 44 45 byte theByte[] = new byte[1]; 46 47 Reader reader = new Reader(rightSide, theByte); 48 Writer writer = new Writer(leftSide, theByte, reader); 49 50 reader.start(); 51 writer.start(); 52 53 Thread.sleep(2000); 54 55 writer.term(); 56 writer.join(); 57 58 reader.join(); 59 60 assure("", writer._state && reader._state); 61 } 62 63 static class Reader extends Thread { 64 PipedConnection _pipedConnection; 65 byte _theByte[]; 66 boolean _quit; 67 boolean _state = false; 68 69 Reader(PipedConnection pipedConnection, byte theByte[]) { 70 _pipedConnection = pipedConnection; 71 _theByte = theByte; 72 } 73 74 public void run() { 75 try { 76 byte bytes[][] = new byte[1][]; 77 78 while(!_quit) { 79 int read = _pipedConnection.read(bytes, 1); 80 81 if(read == 1) { 82 // System.err.println("read :" + bytes[0][0]); 83 84 if(_theByte[0] != bytes[0][0]) 85 throw new NullPointerException(); 86 87 synchronized(this) { 88 notifyAll(); 89 } 90 } 91 else 92 _quit = true; // EOF 93 } 94 95 _pipedConnection.close(); 96 _state = true; 97 } 98 catch(com.sun.star.io.IOException ioException) { 99 System.err.println("#### Reader - unexpected:" + ioException); 100 } 101 102 } 103 } 104 105 static class Writer extends Thread { 106 PipedConnection _pipedConnection; 107 byte _theByte[]; 108 Reader _reader; 109 boolean _quit; 110 boolean _state = false; 111 112 Writer(PipedConnection pipedConnection, byte theByte[], Reader reader) { 113 _pipedConnection = pipedConnection; 114 _reader = reader; 115 _theByte = theByte; 116 } 117 118 public void run() { 119 try { 120 while(!_quit) { 121 synchronized(_reader) { 122 _pipedConnection.write(_theByte); 123 _pipedConnection.flush(); 124 // System.err.println("written :" + _theByte[0]); 125 126 _reader.wait(); 127 } 128 ++ _theByte[0]; 129 } 130 131 _pipedConnection.close(); 132 133 _state = true; 134 } 135 catch(com.sun.star.io.IOException ioException) { 136 System.err.println("#### Writer:" + ioException); 137 } 138 catch(InterruptedException interruptedException) { 139 System.err.println("#### Writer:" + interruptedException); 140 } 141 } 142 143 public void term() { 144 _quit = true; 145 } 146 } 147 } 148