View Javadoc

1   // Copyright (C) 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.tools.tcpproxy;
23  
24  import junit.framework.TestCase;
25  
26  import net.grinder.testutility.AssertUtilities;
27  
28  
29  /**
30   * Unit test case for {@link CompositeFilter}.
31   *
32   * @author Philip Aston
33   */
34  public class TestCompositeFilter extends TestCase {
35  
36    private final MyFilterStubFactory m_filter1StubFactory =
37      new MyFilterStubFactory();
38  
39    private final TCPProxyFilter m_filter1 = m_filter1StubFactory.getStub();
40  
41    final MyFilterStubFactory m_filter2StubFactory =
42      new MyFilterStubFactory();
43  
44    final TCPProxyFilter m_filter2 = m_filter2StubFactory.getStub();
45  
46    private final ConnectionDetails m_connectionDetails =
47      new ConnectionDetails(new EndPoint("foo", 1),
48                            new EndPoint("bah", 2),
49                            false);
50  
51    public void testGetFilters() throws Exception {
52      final CompositeFilter composite = new CompositeFilter();
53  
54      assertEquals(0, composite.getFilters().length);
55  
56      composite.add(m_filter1);
57  
58      AssertUtilities.assertArraysEqual(
59        new Object[] { m_filter1 }, composite.getFilters());
60  
61      composite.add(m_filter2);
62  
63      AssertUtilities.assertArraysEqual(
64        new Object[] { m_filter1, m_filter2 }, composite.getFilters());
65    }
66  
67    public void testHandle() throws Exception {
68      final CompositeFilter composite = new CompositeFilter();
69  
70      final byte[] buffer = new byte[100];
71      final byte[] buffer2 = new byte[20];
72  
73      assertNull(composite.handle(m_connectionDetails, buffer, 10));
74  
75      composite.add(m_filter1);
76      composite.add(m_filter2);
77      composite.add(m_filter1);
78  
79      m_filter2StubFactory.setResult(buffer2);
80  
81      composite.handle(m_connectionDetails, buffer, 10);
82  
83      m_filter1StubFactory.assertSuccess("handle",
84                                         m_connectionDetails,
85                                         buffer,
86                                         new Integer(10));
87  
88      m_filter2StubFactory.assertSuccess("handle",
89                                         m_connectionDetails,
90                                         buffer,
91                                         new Integer(100));
92  
93      m_filter1StubFactory.assertSuccess("handle",
94                                         m_connectionDetails,
95                                         buffer2,
96                                         new Integer(20));
97  
98      m_filter1StubFactory.assertNoMoreCalls();
99      m_filter2StubFactory.assertNoMoreCalls();
100 
101     m_filter2StubFactory.setResult(null);
102 
103     composite.handle(m_connectionDetails, buffer, 10);
104 
105     m_filter1StubFactory.assertSuccess("handle",
106                                        m_connectionDetails,
107                                        buffer,
108                                        new Integer(10));
109 
110     m_filter2StubFactory.assertSuccess("handle",
111                                        m_connectionDetails,
112                                        buffer,
113                                        new Integer(100));
114 
115     m_filter1StubFactory.assertSuccess("handle",
116                                        m_connectionDetails,
117                                        buffer,
118                                        new Integer(100));
119   }
120 
121   public void testConnectionOpened() throws Exception {
122     final CompositeFilter composite = new CompositeFilter();
123     composite.add(m_filter1);
124     composite.add(m_filter2);
125     composite.add(m_filter1);
126 
127     composite.connectionOpened(m_connectionDetails);
128 
129     m_filter1StubFactory.assertSuccess("connectionOpened", m_connectionDetails);
130     m_filter2StubFactory.assertSuccess("connectionOpened", m_connectionDetails);
131     m_filter1StubFactory.assertSuccess("connectionOpened", m_connectionDetails);
132     m_filter1StubFactory.assertNoMoreCalls();
133     m_filter2StubFactory.assertNoMoreCalls();
134   }
135 
136   public void testConnectionClosed() throws Exception {
137     final CompositeFilter composite = new CompositeFilter();
138     composite.add(m_filter1);
139     composite.add(m_filter2);
140     composite.add(m_filter1);
141 
142     composite.connectionClosed(m_connectionDetails);
143 
144     m_filter1StubFactory.assertSuccess("connectionClosed", m_connectionDetails);
145     m_filter2StubFactory.assertSuccess("connectionClosed", m_connectionDetails);
146     m_filter1StubFactory.assertSuccess("connectionClosed", m_connectionDetails);
147     m_filter1StubFactory.assertNoMoreCalls();
148     m_filter2StubFactory.assertNoMoreCalls();
149   }
150 
151   public void testToString() throws Exception {
152     final CompositeFilter composite = new CompositeFilter();
153     composite.add(m_filter1);
154     composite.add(m_filter2);
155     composite.add(m_filter1);
156 
157     final String s = composite.toString();
158     assertNotNull(s);
159   }
160 }