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 complexlib.ComplexTestCase;
27 
28 public final class PipedConnection_Test extends ComplexTestCase {
getTestObjectName()29     public String getTestObjectName() {
30         return getClass().getName();
31     }
32 
getTestMethodNames()33     public String[] getTestMethodNames() {
34         return new String[] { "test" };
35     }
36 
test()37     public void test() throws Exception {
38         PipedConnection rightSide = new PipedConnection(new Object[0]);
39         PipedConnection leftSide = new PipedConnection(new Object[]{rightSide});
40 
41         byte theByte[] = new byte[1];
42 
43         Reader reader = new Reader(rightSide, theByte);
44         Writer writer = new Writer(leftSide, theByte, reader);
45 
46         reader.start();
47         writer.start();
48 
49         Thread.sleep(2000);
50 
51         writer.term();
52         writer.join();
53 
54         reader.join();
55 
56         assure("", writer._state && reader._state);
57     }
58 
59     static class Reader extends Thread {
60         PipedConnection _pipedConnection;
61         byte _theByte[];
62         boolean _quit;
63         boolean _state = false;
64 
Reader(PipedConnection pipedConnection, byte theByte[])65         Reader(PipedConnection pipedConnection, byte theByte[]) {
66             _pipedConnection = pipedConnection;
67             _theByte = theByte;
68         }
69 
run()70         public void run() {
71             try {
72                 byte bytes[][] = new byte[1][];
73 
74                 while(!_quit) {
75                     int read = _pipedConnection.read(bytes, 1);
76 
77                     if(read == 1) {
78 //                          System.err.println("read :" + bytes[0][0]);
79 
80                         if(_theByte[0] != bytes[0][0])
81                             throw new NullPointerException();
82 
83                         synchronized(this) {
84                             notifyAll();
85                         }
86                     }
87                     else
88                         _quit = true; // EOF
89                 }
90 
91                 _pipedConnection.close();
92                 _state = true;
93             }
94             catch(com.sun.star.io.IOException ioException) {
95                 System.err.println("#### Reader - unexpected:" + ioException);
96             }
97 
98         }
99     }
100 
101     static class Writer extends Thread {
102         PipedConnection _pipedConnection;
103         byte _theByte[];
104         Reader _reader;
105         boolean _quit;
106         boolean _state = false;
107 
Writer(PipedConnection pipedConnection, byte theByte[], Reader reader)108         Writer(PipedConnection pipedConnection, byte theByte[], Reader reader) {
109             _pipedConnection = pipedConnection;
110             _reader = reader;
111             _theByte = theByte;
112         }
113 
run()114         public void run() {
115             try {
116                 while(!_quit) {
117                     synchronized(_reader) {
118                         _pipedConnection.write(_theByte);
119                         _pipedConnection.flush();
120 //                          System.err.println("written :" + _theByte[0]);
121 
122                         _reader.wait();
123                     }
124                     ++ _theByte[0];
125                 }
126 
127                 _pipedConnection.close();
128 
129                 _state = true;
130             }
131             catch(com.sun.star.io.IOException ioException) {
132                 System.err.println("#### Writer:" + ioException);
133             }
134             catch(InterruptedException interruptedException) {
135                 System.err.println("#### Writer:" + interruptedException);
136             }
137         }
138 
term()139         public void term() {
140             _quit = true;
141         }
142     }
143 }
144