1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.integration.spring;
21  
22  import java.util.Arrays;
23  import java.util.LinkedList;
24  
25  import org.apache.mina.common.DefaultIoFilterChainBuilder;
26  import org.apache.mina.common.IoFilter;
27  import org.apache.mina.common.IoFilterChain;
28  import org.easymock.MockControl;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * Tests {@link DefaultIoFilterChainBuilderFactoryBean}.
34   *
35   * @author The Apache Directory Project (mina-dev@directory.apache.org)
36   * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $
37   */
38  public class DefaultIoFilterChainBuilderFactoryBeanTest extends TestCase {
39      MockControl mockChain;
40  
41      IoFilterChain chain;
42  
43      IoFilter[] filters;
44  
45      protected void setUp() throws Exception {
46          super.setUp();
47  
48          mockChain = MockControl.createControl(IoFilterChain.class);
49          chain = (IoFilterChain) mockChain.getMock();
50          filters = new IoFilter[] {
51                  (IoFilter) MockControl.createControl(IoFilter.class).getMock(),
52                  (IoFilter) MockControl.createControl(IoFilter.class).getMock(),
53                  (IoFilter) MockControl.createControl(IoFilter.class).getMock() };
54      }
55  
56      public void testUnnamedFilters() throws Exception {
57          chain.addLast("prefix0", filters[0]);
58          chain.addLast("prefix1", filters[1]);
59          chain.addLast("prefix2", filters[2]);
60  
61          mockChain.replay();
62  
63          DefaultIoFilterChainBuilderFactoryBean factory = new DefaultIoFilterChainBuilderFactoryBean();
64          factory.setFilters(Arrays.asList(filters));
65          factory.setFilterNamePrefix("prefix");
66          DefaultIoFilterChainBuilder builder = (DefaultIoFilterChainBuilder) factory
67                  .createInstance();
68          builder.buildFilterChain(chain);
69  
70          mockChain.verify();
71      }
72  
73      @SuppressWarnings("unchecked")
74      public void testIllegalObjectsInFilterList() throws Exception {
75          LinkedList mappings = new LinkedList();
76          mappings.add(new IoFilterMapping("f0", filters[0]));
77          mappings.add(new Object());
78          DefaultIoFilterChainBuilderFactoryBean factory = new DefaultIoFilterChainBuilderFactoryBean();
79          try {
80              factory.setFilters(mappings);
81              fail("Illegal object in list of filters. IllegalArgumentException expected.");
82          } catch (IllegalArgumentException iae) {
83          }
84      }
85  
86      @SuppressWarnings("unchecked")
87      public void testNamedAndUnnamedFilters() throws Exception {
88          LinkedList mappings = new LinkedList();
89          mappings.add(new IoFilterMapping("f0", filters[0]));
90          mappings.add(filters[1]);
91          mappings.add(new IoFilterMapping("f2", filters[2]));
92  
93          chain.addLast("f0", filters[0]);
94          chain.addLast("filter1", filters[1]);
95          chain.addLast("f2", filters[2]);
96  
97          mockChain.replay();
98  
99          DefaultIoFilterChainBuilderFactoryBean factory = new DefaultIoFilterChainBuilderFactoryBean();
100         factory.setFilters(mappings);
101         DefaultIoFilterChainBuilder builder = (DefaultIoFilterChainBuilder) factory
102                 .createInstance();
103         builder.buildFilterChain(chain);
104 
105         mockChain.verify();
106     }
107 }