//TODO: figure out why the normal assertTest with the non mocked PoolingClientConnectionManager interferes with the mock PoolingClientConnectionManager
//TODO: jmockit to overcome easymock limitations
package net.kittyandbear;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class HttpClientFactoryTest
{
HttpClientFactory testerDefault;
@Before
public void setUp() throws Exception
{
testerDefault = HttpClientFactory.getInstance();
}
@Test
public void testGetMaxConnectionsPerRouteMock()
{
//Given
PoolingClientConnectionManager mock = EasyMock.createMock( PoolingClientConnectionManager.class);
int testMax = 1;
expect( mock.getMaxTotal()).andReturn(testMax);
replay( mock );
//When
HttpClientFactory testerMock = HttpClientFactory.getInstance();
testerMock.setPoolingClientConnectionManager(mock);
testerMock.getMaxConnectionsOverall();
verify( mock );
}
@Test
public void testGetInstance()
{
assertNotNull(HttpClientFactory.getInstance());
assertNotNull(HttpClientFactory.getStaticHttpClient());
}
@Test
public void testGetStaticHttpClient()
{
assertNotNull(HttpClientFactory.getStaticHttpClient());
}
/*
@Test
public void testSetMaxConnectionsPerRoute()
{
testerDefault.setMaxConnectionsPerRoute(1);
assertEquals(1, testerDefault.getMaxConnectionsPerRoute());
testerDefault.setMaxConnectionsPerRoute(999);
assertEquals(999, testerDefault.getMaxConnectionsPerRoute());
}
@Test
public void testSetMaxConnectionsOverall()
{
testerDefault.setMaxConnectionsOverall(1);
assertEquals(1, testerDefault.getMaxConnectionsOverall());
testerDefault.setMaxConnectionsOverall(999);
assertEquals(999, testerDefault.getMaxConnectionsOverall());
}
@Test
public void testSetMaxConnectionsPerRouteZero()
{
try
{
testerDefault.setMaxConnectionsPerRoute(0);
}
catch (IllegalArgumentException e)
{
return;
}
fail("ERROR: should throw IllegalArgumentException about parameter being less than 1");
}
@Test
public void testSetMaxConnectionsOverallZero()
{
try
{
testerDefault.setMaxConnectionsOverall(0);
}
catch (IllegalArgumentException e)
{
return;
}
fail("ERROR: should throw IllegalArgumentException about parameter being less than 1");
}
//INCOMPLETE
@Ignore
@Test
public void testSetMaxConnectionsPerRouteSynchronized()
{
testerDefault.setMaxConnectionsOverall(1);
assertEquals(1, testerDefault.getMaxConnectionsOverall());
// SlowThread threadOne = new SlowThread(5000, 5000);
FastThread threadTwo = new FastThread(2000, 2000);
threadTwo.run();
Thread thread1 = new Thread(new RunnableThread(), "thread1");
Thread thread2 = new Thread(new RunnableThread(), "thread2");
thread1.start();
thread2.start();
try
{
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread());
// threadOne.run();
assertEquals(500, testerDefault.getMaxConnectionsOverall());
}
class RunnableThread implements Runnable
{
Thread runner;
public RunnableThread()
{
}
public RunnableThread(String threadName)
{
runner = new Thread(this, threadName);
System.out.println(runner.getName());
runner.start();
}
public void run()
{
System.out.println(Thread.currentThread());
}
}
class FastThread implements Runnable
{
int maxConnections;
int waitInMilliseconds;
FastThread(int maxConnections, int waitInMilliseconds)
{
this.maxConnections = maxConnections;
this.waitInMilliseconds = waitInMilliseconds;
}
public void run()
{
try
{
System.out.println("FAST sleeping " + waitInMilliseconds);
Thread.sleep(waitInMilliseconds);
testerDefault.setMaxConnectionsOverall(maxConnections);
}
catch (InterruptedException e)
{
System.err.println(Thread.currentThread().getName() + " interrupted");
}
}
}
*/
} // end class