jicarilla-sandbox/platform/plumbing/impl/src/test/org/jicarilla/plumbing/test ConnectorTestCase.java,NONE,1.1 DefaultStageTestCase.java,NONE,1.1 NoopSinkTestCase.java,NONE,1.1 PostProcessorTestCase.java,NONE,1.1 PreProcessorTestCase.java,NONE,1.1 SimpleAlternatorTestCase.java,NONE,1.1 SimpleCollectorTestCase.java,NONE,1.1 SimpleMulticasterTestCase.java,NONE,1.1 SimpleScreenerTestCase.java,NONE,1.1 SimpleSinkTestCase.java,NONE,1.1 SimpleSourceTestCase.java,NONE,1.1 SimpleStageTestCase.java,NONE,1.1
Leo Simons <[email protected]>
| Newsgroups | gmane.comp.java.jicarilla.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/jicarilla/jicarilla-sandbox/platform/plumbing/impl/src/test/org/jicarilla/plumbing/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30323/plumbing/impl/src/test/org/jicarilla/plumbing/test
Added Files:
ConnectorTestCase.java DefaultStageTestCase.java
NoopSinkTestCase.java PostProcessorTestCase.java
PreProcessorTestCase.java SimpleAlternatorTestCase.java
SimpleCollectorTestCase.java SimpleMulticasterTestCase.java
SimpleScreenerTestCase.java SimpleSinkTestCase.java
SimpleSourceTestCase.java SimpleStageTestCase.java
Log Message:
s/o.j.f.p/o.j.p/
--- NEW FILE: NoopSinkTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import org.jicarilla.plumbing.NoopSink;
import org.jicarilla.plumbing.NoopSink;
import junit.framework.TestCase;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* NoopSink.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: NoopSinkTestCase.java,v 1.1 2004/03/22 21:20:14 lsimons Exp $
*/
public class NoopSinkTestCase extends TestCase
{
public void testPut() throws Throwable
{
new NoopSink().put( this );
new NoopSink().put( null );
}
public void testOffeR() throws Throwable
{
assertTrue( new NoopSink().offer( this, 10 ) );
assertTrue( new NoopSink().offer( null, 10 ) );
}
}
--- NEW FILE: SimpleScreenerTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import org.jicarilla.plumbing.NoopSink;
import org.jicarilla.framework.Selector;
import org.jicarilla.plumbing.SimpleScreener;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.SimpleScreener;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleScreener.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleScreenerTestCase.java,v 1.1 2003/11/18 16:09:16 lsimons
* Exp $
*/
public class SimpleScreenerTestCase extends TestCase
{
public static class CountingSink implements Sink
{
public int put = 0;
public int offered = 0;
public static int sput = 0;
public static int soffered = 0;
public void put( Object o ) throws InterruptedException
{
put++;
sput++;
}
public boolean offer( Object o, long l ) throws InterruptedException
{
offered++;
soffered++;
return true;
}
}
public static class YesSelector implements Selector
{
public boolean select( Object object )
{
return true;
}
}
public static class NoSelector implements Selector
{
public boolean select( Object object )
{
return false;
}
}
public void testConstructor()
{
new SimpleScreener();
assertEquals( 0, new Screener().getSinks().size() );
assertEquals( 0, new Screener().getSize() );
}
public void testAdd()
{
Screener sc = new Screener();
NoopSink s = new NoopSink();
sc.addSink( new NoSelector(), s );
assertEquals( 1, sc.getSinks().size() );
assertEquals( sc.getSinks().size(), sc.getSize() );
NoopSink s2 = new NoopSink();
sc.addSink( new NoSelector(), s2 );
assertEquals( 2, sc.getSinks().size() );
Throwable t = null;
try
{
sc.addSink( null, s2 );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
t = null;
try
{
sc.addSink( new NoSelector(), null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
public void testPut() throws InterruptedException
{
Screener sc = new Screener();
CountingSink s = new CountingSink();
sc.addSink( new NoSelector(), s );
CountingSink s2 = new CountingSink();
sc.addSink( new YesSelector(), s2 );
sc.put( new Object() );
sc.put( new Object() );
sc.put( new Object() );
sc.put( new Object() );
assertEquals( 4, s2.put );
assertEquals( 4, CountingSink.sput );
}
public void testExhaustedPut() throws Exception
{
Screener sc = new Screener();
CountingSink s = new CountingSink();
sc.addSink( new NoSelector(), s );
CountingSink s2 = new CountingSink();
sc.addSink( new NoSelector(), s2 );
CountingSink s3 = new CountingSink();
sc.addSink( new NoSelector(), s3 );
CountingSink s4 = new CountingSink();
sc.addSink( new NoSelector(), s4 );
CountingSink.sput = 0;
sc.put( new Object() );
sc.put( new Object() );
sc.put( new Object() );
sc.put( new Object() );
assertEquals( 4, s4.put );
assertEquals( 4, CountingSink.sput );
}
public void testOffer() throws InterruptedException
{
Screener sc = new Screener();
CountingSink s = new CountingSink();
sc.addSink( new NoSelector(), s );
CountingSink s2 = new CountingSink();
sc.addSink( new YesSelector(), s2 );
CountingSink.soffered = 0;
sc.offer( new Object(), 10 );
sc.offer( new Object(), 10 );
sc.offer( new Object(), 10 );
sc.offer( new Object(), 10 );
assertEquals( 4, s2.offered );
assertEquals( 4, CountingSink.soffered );
}
public void testOffer2() throws InterruptedException
{
Screener sc = new Screener();
CountingSink s = new CountingSink();
sc.addSink( new NoSelector(), s );
CountingSink s2 = new CountingSink();
sc.addSink( new NoSelector(), s2 );
CountingSink s3 = new CountingSink();
sc.addSink( new NoSelector(), s3 );
CountingSink s4 = new CountingSink();
sc.addSink( new YesSelector(), s4 );
CountingSink s5 = new CountingSink();
sc.addSink( new NoSelector(), s5 );
CountingSink.soffered = 0;
sc.offer( new Object(), 10 );
sc.offer( new Object(), 10 );
sc.offer( new Object(), 10 );
sc.offer( new Object(), 10 );
assertEquals( 0, s2.offered );
assertEquals( 4, s4.offered );
assertEquals( 4, CountingSink.soffered );
}
public static class Screener extends SimpleScreener
{
public List getSinks()
{
List sinks = new ArrayList();
sinks.addAll( super.m_switch.values() );
return sinks;
}
public int getSize()
{
return super.m_switch.size();
}
}
}
--- NEW FILE: ConnectorTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.Executor;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import EDU.oswego.cs.dl.util.concurrent.ThreadedExecutor;
import org.jicarilla.plumbing.Connector;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Source;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Source;
import junit.framework.TestCase;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* Connector.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: ConnectorTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons Exp
* $
*/
public class ConnectorTestCase extends TestCase
{
public static class AccessibleConnector extends Connector
{
public AccessibleConnector( Executor ex, Source source, Sink sink )
{
super( source, sink, ex );
}
public Source getSource()
{
return m_source;
}
public Sink getSink()
{
return m_sink;
}
public Executor getExecutor()
{
return m_executor;
}
}
ThreadedExecutor tx = new ThreadedExecutor();
LinkedQueue q = new LinkedQueue();
public static class MockSource implements Source
{
Object o = new Object();
int takes = 0;
public Object take() throws InterruptedException
{
takes++;
return o;
}
public Object poll( long l ) throws InterruptedException
{
takes++;
return o;
}
};
public static class MockSink implements Sink
{
Object o = null;
int puts = 0;
public void put( Object obj ) throws InterruptedException
{
puts++;
o = obj;
}
public boolean offer( Object obj, long l )
throws InterruptedException
{
puts++;
o = obj;
return true;
}
}
MockSource source = new MockSource();
MockSink sink = new MockSink();
public AccessibleConnector c =
new AccessibleConnector( tx, source, sink );
public void testConstructor()
{
assertEquals( sink, c.getSink() );
assertEquals( source, c.getSource() );
assertEquals( tx, c.getExecutor() );
Throwable t = null;
try
{
new AccessibleConnector( null, source, sink );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
t = null;
try
{
new AccessibleConnector( tx, null, sink );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
t = null;
try
{
new AccessibleConnector( tx, source, null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
public void testWork() throws Throwable
{
c.initialize();
Thread.sleep( 400 );
c.dispose();
Thread.sleep( 400 );
assertEquals( source.o, sink.o );
assertTrue( source.takes > 2 );
assertTrue( sink.puts > 2 );
}
}
--- NEW FILE: PreProcessorTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.Channel;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.PostProcessor;
import org.jicarilla.plumbing.PreProcessor;
import org.jicarilla.plumbing.SimpleSink;
import org.jicarilla.plumbing.SimpleStage;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.PostProcessor;
import org.jicarilla.plumbing.PreProcessor;
import org.jicarilla.plumbing.SimpleSink;
import org.jicarilla.plumbing.SimpleStage;
import junit.framework.TestCase;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* PreProcessor.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: PreProcessorTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
* Exp $
*/
public class PreProcessorTestCase extends TestCase
{
public final static class Processor extends PreProcessor
{
public int processed = 0;
public Processor( Channel channel, Sink errorHandler )
{
super( channel, errorHandler );
}
public List getStages()
{
return m_stages;
}
public void process( Object o ) throws InterruptedException
{
super.process( o );
processed++;
}
}
LinkedQueue c = new LinkedQueue();
LinkedQueue l = new LinkedQueue();
SimpleSink e = new SimpleSink( l );
public void testConstructor()
{
new PostProcessor( c, e );
}
public void testAdd()
{
Processor p = new Processor( c, e );
SimpleStage s = new SimpleStage( new LinkedQueue() );
p.addStage( s );
assertEquals( s, p.getStages().get( 0 ) );
SimpleStage s1 = new SimpleStage( new LinkedQueue() );
p.addStage( s1 );
SimpleStage s2 = new SimpleStage( new LinkedQueue() );
p.addStage( s2 );
SimpleStage s3 = new SimpleStage( new LinkedQueue() );
p.addStage( s3 );
SimpleStage s4 = new SimpleStage( new LinkedQueue() );
p.addStage( s4 );
assertEquals( s1, p.getStages().get( 1 ) );
assertEquals( s2, p.getStages().get( 2 ) );
assertEquals( s3, p.getStages().get( 3 ) );
assertEquals( s4, p.getStages().get( 4 ) );
Throwable t = null;
try
{
p.addStage( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
boolean running = true;
public void testConcurrentAdd() throws InterruptedException
{
final Processor p = new Processor( c, e );
Thread[] threads = new Thread[5];
for( int i = 0; i < 5; i++ )
{
threads[i] =
new Thread( new Runnable()
{
public void run()
{
while( running )
{
try
{
Thread.sleep( 100 );
}
catch( InterruptedException e1 )
{
break;
}
SimpleStage s = new SimpleStage(
new LinkedQueue() );
p.addStage( s );
Thread.yield();
}
}
} );
}
for( int i = 0; i < 5; i++ )
{
threads[i].start();
}
Thread.sleep( 1000 );
running = false;
for( int i = 0; i < 5; i++ )
{
threads[i].interrupt();
}
}
public void testPut() throws InterruptedException
{
final Processor p = new Processor( c, e );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
assertEquals( 4, p.processed );
}
public void testOffer() throws InterruptedException
{
final Processor p = new Processor( c, e );
p.offer( new Object(), 100 );
p.offer( new Object(), 100 );
p.offer( new Object(), 100 );
p.offer( new Object(), 100 );
assertEquals( 4, p.processed );
}
public final static class CountingStage extends SimpleStage
{
public int put = 0;
public int taken = 0;
public static int sput = 0;
public static int staken = 0;
public CountingStage()
{
super( new LinkedQueue() );
}
public void put( Object o ) throws InterruptedException
{
super.put( o );
put++;
sput++;
}
public Object take() throws InterruptedException
{
taken++;
staken++;
return super.take();
}
}
public void testProcess() throws InterruptedException
{
final Processor p = new Processor( c, e );
CountingStage s1 = new CountingStage();
p.addStage( s1 );
CountingStage s2 = new CountingStage();
p.addStage( s2 );
CountingStage s3 = new CountingStage();
p.addStage( s3 );
CountingStage s4 = new CountingStage();
p.addStage( s4 );
p.process( new Object() );
p.process( new Object() );
p.process( new Object() );
p.process( new Object() );
assertEquals( 4, s1.put );
assertEquals( 4, s1.taken );
assertEquals( 4, s2.put );
assertEquals( 4, s2.taken );
assertEquals( 4, s3.put );
assertEquals( 4, s3.taken );
assertEquals( 4, s4.put );
assertEquals( 4, s4.taken );
assertEquals( 4 * 4, CountingStage.sput );
assertEquals( 4 * 4, CountingStage.staken );
}
}
--- NEW FILE: DefaultStageTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.Channel;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.DefaultStage;
import org.jicarilla.plumbing.SimpleSink;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.DefaultStage;
import org.jicarilla.plumbing.SimpleSink;
import junit.framework.TestCase;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* DefaultStage.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: DefaultStageTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
* Exp $
*/
public class DefaultStageTestCase extends TestCase
{
public final static class Stage extends DefaultStage
{
public Stage( Channel channel, Sink errorHandler )
{
super( channel, errorHandler );
}
public void handleError( Object o )
throws InterruptedException
{
super.handleError( o );
}
}
public void testConstructor()
{
LinkedQueue c = new LinkedQueue();
SimpleSink e = new SimpleSink( new LinkedQueue() );
new DefaultStage( c, e );
new DefaultStage( c, null );
}
public void testHandleError() throws Exception
{
LinkedQueue c = new LinkedQueue();
LinkedQueue l = new LinkedQueue();
SimpleSink e = new SimpleSink( l );
Stage s = new Stage( c, e );
Object o = new Object();
s.handleError( o );
assertEquals( o, l.take() );
s = new Stage( c, null );
s.handleError( o );
assertNull( l.poll( 100 ) );
}
}
--- NEW FILE: SimpleCollectorTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.SimpleCollector;
import org.jicarilla.plumbing.SimpleSource;
import org.jicarilla.plumbing.SimpleCollector;
import org.jicarilla.plumbing.SimpleSource;
import junit.framework.TestCase;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleCollector.
*
* @todo this test just deadlocked on me. Figure out what's going on.
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleCollectorTestCase.java,v 1.1 2003/11/18 16:09:16 lsimons
* Exp $
*/
public class SimpleCollectorTestCase extends TestCase
{
public void testAdd()
{
Collector c = new Collector();
LinkedQueue l = new LinkedQueue();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
assertEquals( c.getSources().get( 0 ), s );
assertEquals( c.getSources().size(), 1 );
assertEquals( c.getSources().size(), c.getSize() );
LinkedQueue l2 = new LinkedQueue();
SimpleSource s2 = new SimpleSource( l2 );
c.addSource( s2 );
Throwable t = null;
try
{
c.addSource( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
boolean running = true;
public void testConcurrentAdd() throws InterruptedException
{
final Collector c = new Collector();
Thread[] threads = new Thread[5];
for( int i = 0; i < 5; i++ )
{
threads[i] =
new Thread( new Runnable()
{
public void run()
{
while( running )
{
try
{
Thread.sleep( 100 );
}
catch( InterruptedException e1 )
{
break;
}
LinkedQueue l = new LinkedQueue();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
Thread.yield();
}
}
} );
}
for( int i = 0; i < 5; i++ )
{
threads[i].start();
}
Thread.sleep( 1000 );
running = false;
for( int i = 0; i < 5; i++ )
{
threads[i].interrupt();
}
}
public void testTake() throws InterruptedException
{
Collector c = new Collector();
LinkedQueue l = new Q();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
l.put( o1 );
l.put( o2 );
l.put( o3 );
l.put( o4 );
assertEquals( o1, c.take() );
assertEquals( o2, c.take() );
assertEquals( o3, c.take() );
assertEquals( o4, c.take() );
LinkedQueue l2 = new LinkedQueue();
SimpleSource s2 = new SimpleSource( l2 );
c.addSource( s2 );
l2.put( o3 );
assertEquals( o3, c.take() );
l.put( null );
l2.put( o1 );
assertEquals( o1, c.take() );
}
boolean interrupted = false;
public void testBlockOnTake() throws InterruptedException
{
final Collector c = new Collector();
final LinkedQueue l = new LinkedQueue();
final SimpleSource s = new SimpleSource( l );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
c.addSource( s );
Thread thread = new Thread( new Runnable()
{
public void run()
{
try
{
c.take(); // should block, then be interrupted
}
catch( InterruptedException ie )
{
interrupted = true;
}
}
} );
thread.start();
Thread.sleep( 1000 );
thread.interrupt();
Thread.sleep( 1000 );
assertTrue( interrupted );
}
public static class Q extends LinkedQueue
{
public void put( Object o ) throws InterruptedException
{
if( o == null )
{
super.insert( o );
}
else
{
super.put( o );
}
}
}
public void testPoll() throws InterruptedException
{
Collector c = new Collector();
LinkedQueue l = new LinkedQueue();
SimpleSource s = new SimpleSource( l );
c.addSource( s );
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
l.put( o1 );
l.put( o2 );
l.put( o3 );
l.put( o4 );
assertEquals( o1, c.poll( 10 ) );
assertEquals( o2, c.poll( 20 ) );
assertEquals( o3, c.poll( 100 ) );
assertEquals( o4, c.poll( 50 ) );
assertNull( c.poll( 100 ) );
}
public static class Collector extends SimpleCollector
{
public List getSources()
{
return m_sources;
}
public int getSize()
{
return m_sources.size();
}
}
}
--- NEW FILE: SimpleMulticasterTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import org.jicarilla.plumbing.NoopSink;
import org.jicarilla.plumbing.SimpleMulticaster;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.NoopSink;
import org.jicarilla.plumbing.SimpleMulticaster;
import junit.framework.TestCase;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleMulticaster.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleMulticasterTestCase.java,v 1.1 2003/11/18 16:09:16
* lsimons Exp $
*/
public class SimpleMulticasterTestCase extends TestCase
{
public static class CountingSink implements Sink
{
public int put = 0;
public int offered = 0;
public static int sput = 0;
public static int soffered = 0;
public void put( Object o ) throws InterruptedException
{
put++;
sput++;
}
public boolean offer( Object o, long l ) throws InterruptedException
{
offered++;
soffered++;
return true;
}
}
public void testConstructor()
{
new SimpleMulticaster();
assertEquals( 0, new Multicaster().getSinks().size() );
assertEquals( 0, new Multicaster().getSize() );
}
public void testAdd()
{
Multicaster m = new Multicaster();
NoopSink s = new NoopSink();
m.addSink( s );
assertEquals( m.getSinks().get( 0 ), s );
NoopSink s2 = new NoopSink();
m.addSink( s2 );
assertEquals( m.getSinks().get( 1 ), s2 );
assertEquals( m.getSinks().size(), m.getSize() );
Throwable t = null;
try
{
m.addSink( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
boolean running = true;
public void testConcurrentAdd() throws InterruptedException
{
final Multicaster m = new Multicaster();
Thread[] threads = new Thread[5];
for( int i = 0; i < 5; i++ )
{
threads[i] =
new Thread( new Runnable()
{
public void run()
{
while( running )
{
try
{
Thread.sleep( 100 );
}
catch( InterruptedException e1 )
{
break;
}
NoopSink s = new NoopSink();
m.addSink( s );
Thread.yield();
}
}
} );
}
for( int i = 0; i < 5; i++ )
{
threads[i].start();
}
Thread.sleep( 1000 );
running = false;
for( int i = 0; i < 5; i++ )
{
threads[i].interrupt();
}
}
public void testPut() throws InterruptedException
{
Multicaster m = new Multicaster();
CountingSink s1 = new CountingSink();
CountingSink s2 = new CountingSink();
CountingSink s3 = new CountingSink();
m.addSink( s1 );
m.addSink( s2 );
m.addSink( s3 );
m.put( new CloneMe() );
m.put( new Object() );
m.put( new DontCloneMe() );
m.put( new Object() );
assertEquals( 12, CountingSink.sput );
assertEquals( 4, s1.put );
}
public void testOffer() throws InterruptedException
{
Multicaster m = new Multicaster();
CountingSink s1 = new CountingSink();
CountingSink s2 = new CountingSink();
CountingSink s3 = new CountingSink();
m.addSink( s1 );
m.addSink( s2 );
m.addSink( s3 );
assertTrue( m.offer( new CloneMe(), 10 ) );
m.offer( new Object(), 10 );
assertTrue( m.offer( new Object(), 10 ) );
assertEquals( 9, CountingSink.soffered );
assertEquals( 3, s1.offered );
}
public static class Multicaster extends SimpleMulticaster
{
public List getSinks()
{
return m_sinks;
}
public int getSize()
{
return m_sinks.size();
}
}
public static class CloneMe implements Cloneable
{
public Object clone() throws CloneNotSupportedException
{ return super.clone(); }
}
public static class DontCloneMe implements Cloneable
{
protected Object clone() throws CloneNotSupportedException
{ return super.clone(); }
}
}
--- NEW FILE: SimpleSourceTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.SimpleSource;
import org.jicarilla.plumbing.SimpleSource;
import junit.framework.TestCase;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleSource.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleSourceTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
* Exp $
*/
public class SimpleSourceTestCase extends TestCase
{
public void testConstructor()
{
new SimpleSource( new LinkedQueue() );
Throwable t = null;
try
{
new SimpleSource( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
public void testTake() throws Exception
{
LinkedQueue l = new LinkedQueue();
Object o = new Object();
l.put( o );
l.put( new Object() );
l.put( new Object() );
l.put( new Object() );
l.put( new Object() );
SimpleSource s = new SimpleSource( l );
assertEquals( o, s.take() );
}
public void testPoll() throws Exception
{
LinkedQueue l = new LinkedQueue();
Object o = new Object();
l.put( o );
l.put( new Object() );
l.put( new Object() );
l.put( new Object() );
l.put( new Object() );
SimpleSource s = new SimpleSource( l );
assertEquals( o, s.poll( 100 ) );
}
}
--- NEW FILE: SimpleStageTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.SimpleStage;
import junit.framework.TestCase;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleStage.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleStageTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons Exp
* $
*/
public class SimpleStageTestCase extends TestCase
{
LinkedQueue l = new LinkedQueue();
SimpleStage s = new SimpleStage( l );
public void testConstructor()
{
LinkedQueue l = new LinkedQueue();
new SimpleStage( l );
Throwable t = null;
try
{
new SimpleStage( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
public void testPutPoll() throws Throwable
{
Object o = new Object();
s.put( o );
assertEquals( o, s.poll( 100 ) );
}
public void testOfferPeek() throws Throwable
{
Object o = new Object();
s.offer( o, 100 );
assertEquals( o, s.peek() );
}
public void testPutTake() throws Throwable
{
Object o = new Object();
s.put( o );
assertEquals( o, s.peek() );
assertEquals( o, s.take() );
}
}
--- NEW FILE: SimpleAlternatorTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import org.jicarilla.plumbing.NoopSink;
import org.jicarilla.plumbing.SimpleAlternator;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.NoopSink;
import junit.framework.TestCase;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleAlternator.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleAlternatorTestCase.java,v 1.1 2003/11/18 16:09:16
* lsimons Exp $
*/
public class SimpleAlternatorTestCase extends TestCase
{
public static class CountingSink implements Sink
{
public int put = 0;
public int offered = 0;
public static int sput = 0;
public static int soffered = 0;
public void put( Object o ) throws InterruptedException
{
put++;
sput++;
}
public boolean offer( Object o, long l ) throws InterruptedException
{
offered++;
soffered++;
return true;
}
}
public void testConstructor()
{
new SimpleAlternator();
assertEquals( 0, new Alternator().getSinks().size() );
assertEquals( 0, new Alternator().getSize() );
}
public void testAdd()
{
Alternator a = new Alternator();
NoopSink s = new NoopSink();
a.addSink( s );
assertEquals( a.getSinks().get( 0 ), s );
assertEquals( a.getSinks().get( Alternator.DEFAULT_PRIORITY - 1 ), s );
NoopSink s2 = new NoopSink();
a.addSink( s2, 2 );
assertEquals( a.getSinks().get( Alternator.DEFAULT_PRIORITY ), s2 );
assertEquals( a.getSinks().get( Alternator.DEFAULT_PRIORITY + 1 ), s2 );
assertEquals( a.getSinks().size(), Alternator.DEFAULT_PRIORITY + 2 );
assertEquals( a.getSinks().size(), a.getSize() );
Throwable t = null;
try
{
a.addSink( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
t = null;
try
{
a.addSink( null, 10000 );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
t = null;
try
{
a.addSink( null, 0 );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
boolean running = true;
public void testConcurrentAdd() throws InterruptedException
{
final Alternator a = new Alternator();
Thread[] threads = new Thread[5];
for( int i = 0; i < 5; i++ )
{
threads[i] =
new Thread( new Runnable()
{
public void run()
{
while( running )
{
try
{
Thread.sleep( 100 );
}
catch( InterruptedException e1 )
{
break;
}
NoopSink s = new NoopSink();
a.addSink( s, 3 );
Thread.yield();
}
}
} );
}
for( int i = 0; i < 5; i++ )
{
threads[i].start();
}
Thread.sleep( 1000 );
running = false;
for( int i = 0; i < 5; i++ )
{
threads[i].interrupt();
}
}
public void testPut() throws InterruptedException
{
Alternator a = new Alternator();
CountingSink s1 = new CountingSink();
CountingSink s2 = new CountingSink();
CountingSink s3 = new CountingSink();
a.addSink( s1 );
a.addSink( s2 );
a.addSink( s3 );
a.put( new Object() );
a.put( new Object() );
a.put( new Object() );
a.put( new Object() );
assertEquals( 4, CountingSink.sput );
assertEquals( 4, s1.put + s2.put + s3.put );
}
public void testOffer() throws InterruptedException
{
Alternator a = new Alternator();
CountingSink s1 = new CountingSink();
CountingSink s2 = new CountingSink();
CountingSink s3 = new CountingSink();
a.addSink( s1 );
a.addSink( s2 );
a.addSink( s3 );
a.offer( new Object(), 10 );
a.offer( new Object(), 10 );
a.offer( new Object(), 10 );
assertEquals( 3, CountingSink.soffered );
assertEquals( 3, s1.offered + s2.offered + s3.offered );
}
public static class Alternator extends SimpleAlternator
{
public List getSinks()
{
return m_sinks;
}
public int getSize()
{
return m_sinks.size();
}
}
}
--- NEW FILE: SimpleSinkTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.SimpleSink;
import org.jicarilla.plumbing.SimpleSink;
import junit.framework.TestCase;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* SimpleSink.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: SimpleSinkTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons Exp
* $
*/
public class SimpleSinkTestCase extends TestCase
{
public void testConstructor()
{
new SimpleSink( new LinkedQueue() );
Throwable t = null;
try
{
new SimpleSink( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
public void testPut() throws InterruptedException
{
SimpleSink sink = new SimpleSink( new LinkedQueue() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
sink.put( new Object() );
}
public void testOffer() throws InterruptedException
{
SimpleSink sink = new SimpleSink( new LinkedQueue() );
assertTrue( sink.offer( new Object(), 10 ) );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
sink.offer( new Object(), 10 );
}
}
--- NEW FILE: PostProcessorTestCase.java ---
/* ====================================================================
The Jicarilla Software License
Copyright (c) 2003 Leo Simons.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.plumbing.test;
import EDU.oswego.cs.dl.util.concurrent.Channel;
import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
import org.jicarilla.plumbing.PostProcessor;
import org.jicarilla.plumbing.SimpleSink;
import org.jicarilla.plumbing.SimpleStage;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.Sink;
import org.jicarilla.plumbing.SimpleStage;
import junit.framework.TestCase;
import java.util.List;
/**
* <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
* PostProcessor.
*
* @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
* @version $Id: PostProcessorTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
* Exp $
*/
public class PostProcessorTestCase extends TestCase
{
public final static class Processor extends PostProcessor
{
public int processed = 0;
public Processor( Channel channel, Sink errorHandler )
{
super( channel, errorHandler );
}
public List getStages()
{
return m_stages;
}
public void process( Object o ) throws InterruptedException
{
super.process( o );
processed++;
}
}
LinkedQueue c;
LinkedQueue l;
SimpleSink e;
public void setUp()
{
c = new LinkedQueue();;
l = new LinkedQueue();
e = new SimpleSink( l );
}
public void testConstructor()
{
new PostProcessor( c, e );
}
public void testAdd()
{
Processor p = new Processor( c, e );
SimpleStage s = new SimpleStage( new LinkedQueue() );
p.addStage( s );
assertEquals( s, p.getStages().get( 0 ) );
SimpleStage s1 = new SimpleStage( new LinkedQueue() );
p.addStage( s1 );
SimpleStage s2 = new SimpleStage( new LinkedQueue() );
p.addStage( s2 );
SimpleStage s3 = new SimpleStage( new LinkedQueue() );
p.addStage( s3 );
SimpleStage s4 = new SimpleStage( new LinkedQueue() );
p.addStage( s4 );
assertEquals( s1, p.getStages().get( 1 ) );
assertEquals( s2, p.getStages().get( 2 ) );
assertEquals( s3, p.getStages().get( 3 ) );
assertEquals( s4, p.getStages().get( 4 ) );
Throwable t = null;
try
{
p.addStage( null );
}
catch( AssertionError ae )
{
t = ae;
}
assertNotNull( t );
}
boolean running = true;
public void testConcurrentAdd() throws InterruptedException
{
final Processor p = new Processor( c, e );
Thread[] threads = new Thread[5];
for( int i = 0; i < 5; i++ )
{
threads[i] =
new Thread( new Runnable()
{
public void run()
{
while( running )
{
try
{
Thread.sleep( 100 );
}
catch( InterruptedException e1 )
{
break;
}
SimpleStage s = new SimpleStage(
new LinkedQueue() );
p.addStage( s );
Thread.yield();
}
}
} );
}
for( int i = 0; i < 5; i++ )
{
threads[i].start();
}
Thread.sleep( 1000 );
running = false;
for( int i = 0; i < 5; i++ )
{
threads[i].interrupt();
}
}
public void testPut() throws InterruptedException
{
final Processor p = new Processor( c, e );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
assertEquals( 0, p.processed );
}
public void testOffer() throws InterruptedException
{
final Processor p = new Processor( c, e );
p.offer( new Object(), 100 );
p.offer( new Object(), 100 );
p.offer( new Object(), 100 );
p.offer( new Object(), 100 );
assertEquals( 0, p.processed );
}
public void testTake() throws InterruptedException
{
final Processor p = new Processor( c, e );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
p.take();
p.take();
p.take();
p.take();
assertEquals( 4, p.processed );
}
public void testPoll() throws InterruptedException
{
final Processor p = new Processor( c, e );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
p.put( new Object() );
p.poll( 100 );
p.poll( 100 );
p.poll( 100 );
p.poll( 100 );
assertEquals( 4, p.processed );
}
public final static class CountingStage extends SimpleStage
{
public int put = 0;
public int taken = 0;
public static int sput = 0;
public static int staken = 0;
public CountingStage()
{
super( new LinkedQueue() );
}
public void put( Object o ) throws InterruptedException
{
super.put( o );
put++;
sput++;
}
public Object take() throws InterruptedException
{
taken++;
staken++;
return super.take();
}
}
public void testProcess() throws InterruptedException
{
final Processor p = new Processor( c, e );
CountingStage s1 = new CountingStage();
p.addStage( s1 );
CountingStage s2 = new CountingStage();
p.addStage( s2 );
CountingStage s3 = new CountingStage();
p.addStage( s3 );
CountingStage s4 = new CountingStage();
p.addStage( s4 );
p.process( new Object() );
p.process( new Object() );
p.process( new Object() );
p.process( new Object() );
assertEquals( 4, s1.put );
assertEquals( 4, s1.taken );
assertEquals( 4, s2.put );
assertEquals( 4, s2.taken );
assertEquals( 4, s3.put );
assertEquals( 4, s3.taken );
assertEquals( 4, s4.put );
assertEquals( 4, s4.taken );
assertEquals( 4 * 4, CountingStage.sput );
assertEquals( 4 * 4, CountingStage.staken );
}
}
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click