Skip to content

Commit 1af24b6

Browse files
author
Nicola Ken Barozzi
committed
Testcases for new classes from Avalon excalibur.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140305 13f79535-47bb-0310-9956-ffa450edef68
1 parent e44faf9 commit 1af24b6

18 files changed

Lines changed: 2214 additions & 0 deletions
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
/*
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//io/src/test/org/apache/commons/io/DemuxTestCase.java,v 1.1 2002/07/08 22:19:10 nicolaken Exp $
3+
* $Revision: 1.1 $
4+
* $Date: 2002/07/08 22:19:10 $
5+
*
6+
* ====================================================================
7+
*
8+
* The Apache Software License, Version 1.1
9+
*
10+
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11+
* reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
*
17+
* 1. Redistributions of source code must retain the above copyright
18+
* notice, this list of conditions and the following disclaimer.
19+
*
20+
* 2. Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimer in
22+
* the documentation and/or other materials provided with the
23+
* distribution.
24+
*
25+
* 3. The end-user documentation included with the redistribution, if
26+
* any, must include the following acknowlegement:
27+
* "This product includes software developed by the
28+
* Apache Software Foundation (http://www.apache.org/)."
29+
* Alternately, this acknowlegement may appear in the software itself,
30+
* if and wherever such third-party acknowlegements normally appear.
31+
*
32+
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
33+
* Foundation" must not be used to endorse or promote products derived
34+
* from this software without prior written permission. For written
35+
* permission, please contact apache@apache.org.
36+
*
37+
* 5. Products derived from this software may not be called "Apache"
38+
* nor may "Apache" appear in their names without prior written
39+
* permission of the Apache Group.
40+
*
41+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52+
* SUCH DAMAGE.
53+
* ====================================================================
54+
*
55+
* This software consists of voluntary contributions made by many
56+
* individuals on behalf of the Apache Software Foundation. For more
57+
* information on the Apache Software Foundation, please see
58+
* <http://www.apache.org/>.
59+
*
60+
*/
61+
62+
package org.apache.commons.io;
63+
64+
import java.io.ByteArrayInputStream;
65+
import java.io.ByteArrayOutputStream;
66+
import java.io.IOException;
67+
import java.io.InputStream;
68+
import java.io.OutputStream;
69+
import java.util.HashMap;
70+
import java.util.Iterator;
71+
import java.util.Random;
72+
import junit.framework.TestCase;
73+
import org.apache.avalon.excalibur.io.DemuxInputStream;
74+
import org.apache.avalon.excalibur.io.DemuxOutputStream;
75+
76+
/**
77+
* Basic unit tests for the multiplexing streams.
78+
*
79+
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
80+
*/
81+
public final class DemuxTestCase
82+
extends TestCase
83+
{
84+
private static final String T1 = "Thread1";
85+
private static final String T2 = "Thread2";
86+
private static final String T3 = "Thread3";
87+
private static final String T4 = "Thread4";
88+
89+
private static final String DATA1 = "Data for thread1";
90+
private static final String DATA2 = "Data for thread2";
91+
private static final String DATA3 = "Data for thread3";
92+
private static final String DATA4 = "Data for thread4";
93+
94+
private static final Random c_random = new Random();
95+
private final HashMap m_outputMap = new HashMap();
96+
private final HashMap m_threadMap = new HashMap();
97+
98+
public DemuxTestCase( final String name )
99+
{
100+
super( name );
101+
}
102+
103+
private String getOutput( final String threadName )
104+
throws IOException
105+
{
106+
final ByteArrayOutputStream output =
107+
(ByteArrayOutputStream)m_outputMap.get( threadName );
108+
assertNotNull( "getOutput()", output );
109+
110+
return output.toString();
111+
}
112+
113+
private String getInput( final String threadName )
114+
throws IOException
115+
{
116+
final ReaderThread thread = (ReaderThread)m_threadMap.get( threadName );
117+
assertNotNull( "getInput()", thread );
118+
119+
return thread.getData();
120+
}
121+
122+
private void doStart()
123+
throws Exception
124+
{
125+
final Iterator iterator = m_threadMap.keySet().iterator();
126+
while( iterator.hasNext() )
127+
{
128+
final String name = (String)iterator.next();
129+
final Thread thread = (Thread)m_threadMap.get( name );
130+
thread.start();
131+
}
132+
}
133+
134+
private void doJoin()
135+
throws Exception
136+
{
137+
final Iterator iterator = m_threadMap.keySet().iterator();
138+
while( iterator.hasNext() )
139+
{
140+
final String name = (String)iterator.next();
141+
final Thread thread = (Thread)m_threadMap.get( name );
142+
thread.join();
143+
}
144+
}
145+
146+
private void startWriter( final String name,
147+
final String data,
148+
final DemuxOutputStream demux )
149+
throws Exception
150+
{
151+
final ByteArrayOutputStream output = new ByteArrayOutputStream();
152+
m_outputMap.put( name, output );
153+
final WriterThread thread =
154+
new WriterThread( name, data, output, demux );
155+
m_threadMap.put( name, thread );
156+
}
157+
158+
private void startReader( final String name,
159+
final String data,
160+
final DemuxInputStream demux )
161+
throws Exception
162+
{
163+
final ByteArrayInputStream input = new ByteArrayInputStream( data.getBytes() );
164+
final ReaderThread thread = new ReaderThread( name, input, demux );
165+
m_threadMap.put( name, thread );
166+
}
167+
168+
public void testOutputStream()
169+
throws Exception
170+
{
171+
final DemuxOutputStream output = new DemuxOutputStream();
172+
startWriter( T1, DATA1, output );
173+
startWriter( T2, DATA2, output );
174+
startWriter( T3, DATA3, output );
175+
startWriter( T4, DATA4, output );
176+
177+
doStart();
178+
doJoin();
179+
180+
assertEquals( "Data1", DATA1, getOutput( T1 ) );
181+
assertEquals( "Data2", DATA2, getOutput( T2 ) );
182+
assertEquals( "Data3", DATA3, getOutput( T3 ) );
183+
assertEquals( "Data4", DATA4, getOutput( T4 ) );
184+
}
185+
186+
public void testInputStream()
187+
throws Exception
188+
{
189+
final DemuxInputStream input = new DemuxInputStream();
190+
startReader( T1, DATA1, input );
191+
startReader( T2, DATA2, input );
192+
startReader( T3, DATA3, input );
193+
startReader( T4, DATA4, input );
194+
195+
doStart();
196+
doJoin();
197+
198+
assertEquals( "Data1", DATA1, getInput( T1 ) );
199+
assertEquals( "Data2", DATA2, getInput( T2 ) );
200+
assertEquals( "Data3", DATA3, getInput( T3 ) );
201+
assertEquals( "Data4", DATA4, getInput( T4 ) );
202+
}
203+
204+
private static class ReaderThread
205+
extends Thread
206+
{
207+
private final StringBuffer m_buffer = new StringBuffer();
208+
private final InputStream m_input;
209+
private final DemuxInputStream m_demux;
210+
211+
ReaderThread( final String name,
212+
final InputStream input,
213+
final DemuxInputStream demux )
214+
{
215+
super( name );
216+
m_input = input;
217+
m_demux = demux;
218+
}
219+
220+
public String getData()
221+
{
222+
return m_buffer.toString();
223+
}
224+
225+
public void run()
226+
{
227+
m_demux.bindStream( m_input );
228+
229+
try
230+
{
231+
int ch = m_demux.read();
232+
while( -1 != ch )
233+
{
234+
//System.out.println( "Reading: " + (char)ch );
235+
m_buffer.append( (char)ch );
236+
237+
final int sleepTime = Math.abs( c_random.nextInt() % 10 );
238+
Thread.sleep( sleepTime );
239+
ch = m_demux.read();
240+
}
241+
}
242+
catch( final Exception e )
243+
{
244+
e.printStackTrace();
245+
}
246+
}
247+
}
248+
249+
private static class WriterThread
250+
extends Thread
251+
{
252+
private final byte[] m_data;
253+
private final OutputStream m_output;
254+
private final DemuxOutputStream m_demux;
255+
256+
WriterThread( final String name,
257+
final String data,
258+
final OutputStream output,
259+
final DemuxOutputStream demux )
260+
{
261+
super( name );
262+
m_output = output;
263+
m_demux = demux;
264+
m_data = data.getBytes();
265+
}
266+
267+
public void run()
268+
{
269+
m_demux.bindStream( m_output );
270+
for( int i = 0; i < m_data.length; i++ )
271+
{
272+
try
273+
{
274+
//System.out.println( "Writing: " + (char)m_data[ i ] );
275+
m_demux.write( m_data[ i ] );
276+
final int sleepTime = Math.abs( c_random.nextInt() % 10 );
277+
Thread.sleep( sleepTime );
278+
}
279+
catch( final Exception e )
280+
{
281+
e.printStackTrace();
282+
}
283+
}
284+
}
285+
}
286+
}

0 commit comments

Comments
 (0)