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