KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > ConnectionPoolTest


1 package org.sapia.ubik.net;
2
3 import junit.framework.TestCase;
4
5 import java.io.IOException JavaDoc;
6
7 import java.net.Socket JavaDoc;
8
9 import java.rmi.RemoteException JavaDoc;
10
11
12 /**
13  * @author Yanick Duchesne
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class ConnectionPoolTest extends TestCase {
21   /**
22    * Constructor for ConnectionPoolTest.
23    * @param arg0
24    */

25   public ConnectionPoolTest(String JavaDoc arg0) {
26     super(arg0);
27   }
28
29   public void testAcquireNoMaxSize() throws Exception JavaDoc {
30     ConnectionPool pool = new ConnectionPool("test", 9999,
31         new TestConnectionFactory(), new DefaultClientSocketFactory(), -1);
32
33     for (int i = 0; i < 100; i++) {
34       pool.acquire();
35     }
36   }
37
38   public void testAcquireWithMaxSize() throws Exception JavaDoc {
39     ConnectionPool pool = new ConnectionPool("test", 9999,
40         new TestConnectionFactory(), new DefaultClientSocketFactory(), 3);
41
42     pool.acquire(100);
43     pool.acquire(100);
44     pool.acquire(100);
45
46     try {
47       pool.acquire(100);
48       throw new Exception JavaDoc("new connection should not have been created");
49     } catch (Exception JavaDoc e) {
50       // ok;
51
}
52
53     super.assertEquals(3, pool.getCount());
54   }
55
56   public void testRelease() throws Exception JavaDoc {
57     ConnectionPool pool = new ConnectionPool("test", 9999,
58         new TestConnectionFactory(), new DefaultClientSocketFactory(), 3);
59
60     Connection conn;
61     pool.acquire(100);
62     pool.acquire(100);
63     conn = pool.acquire(100);
64     pool.release(conn);
65     pool.acquire(100);
66     super.assertEquals(3, pool.getCount());
67   }
68
69   /*////////////////////////////////////////////////////////////////////
70                               INNER CLASSES
71   ////////////////////////////////////////////////////////////////////*/

72   static class TestConnection implements Connection {
73     TCPAddress _id = new TCPAddress("test", 8888);
74
75     /**
76     * @see org.sapia.ubik.net.Connection#close()
77     */

78     public void close() {
79     }
80
81     /**
82      * @see org.sapia.ubik.net.Connection#getServerAddress()()
83      */

84     public ServerAddress getServerAddress() {
85       return _id;
86     }
87
88     /**
89      * @see org.sapia.ubik.net.Connection#receive()
90      */

91     public Object JavaDoc receive()
92       throws IOException JavaDoc, ClassNotFoundException JavaDoc, RemoteException JavaDoc {
93       return "ACK";
94     }
95
96     /**
97      * @see org.sapia.ubik.net.Connection#send(Object)
98      */

99     public void send(Object JavaDoc o) throws IOException JavaDoc, RemoteException JavaDoc {
100     }
101   }
102
103   static class TestConnectionFactory implements ConnectionFactory {
104     /**
105      * @see org.sapia.ubik.net.ConnectionFactory#newConnection(Socket)
106      */

107     public Connection newConnection(Socket JavaDoc sock)
108       throws IOException JavaDoc, UnsupportedOperationException JavaDoc {
109       return new TestConnection();
110     }
111
112     /**
113      * @see org.sapia.ubik.net.ConnectionFactory#newConnection(String, int)
114      */

115     public Connection newConnection(String JavaDoc host, int port)
116       throws IOException JavaDoc {
117       return new TestConnection();
118     }
119   }
120 }
121
Popular Tags