    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.common;
    21
    22  import java.util.Iterator;
    23
    24  import junit.framework.Assert;
    25  import junit.framework.TestCase;
    26
    27  import org.apache.mina.common.IoFilterChain.Entry;
    28
    29  /**
    30   * Tests {@link DefaultIoFilterChainBuilder}.
    31   *
    32   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    33   * @version $Rev$, $Date$
    34   */
    35  public class DefaultIoFilterChainBuilderTest extends TestCase {
    36      public static void main(String[] args) {
    37          junit.textui.TestRunner.run(DefaultIoFilterChainBuilderTest.class);
    38      }
    39
    40      protected void setUp() throws Exception {
    41      }
    42
    43      protected void tearDown() throws Exception {
    44      }
    45
    46      public void testAdd() throws Exception {
    47          DefaultIoFilterChainBuilder builder = new DefaultIoFilterChainBuilder();
    48
    49          builder.addFirst("A", new IoFilterAdapter());
    50          builder.addLast("B", new IoFilterAdapter());
    51          builder.addFirst("C", new IoFilterAdapter());
    52          builder.addLast("D", new IoFilterAdapter());
    53          builder.addBefore("B", "E", new IoFilterAdapter());
    54          builder.addBefore("C", "F", new IoFilterAdapter());
    55          builder.addAfter("B", "G", new IoFilterAdapter());
    56          builder.addAfter("D", "H", new IoFilterAdapter());
    57
    58          String actual = "";
    59          for (Iterator i = builder.getAll().iterator(); i.hasNext();) {
    60              Entry e = (Entry) i.next();
    61              actual += e.getName();
    62          }
    63
    64          Assert.assertEquals("FCAEBGDH", actual);
    65      }
    66
    67      public void testGet() throws Exception {
    68          DefaultIoFilterChainBuilder builder = new DefaultIoFilterChainBuilder();
    69
    70          IoFilter filterA = new IoFilterAdapter();
    71          IoFilter filterB = new IoFilterAdapter();
    72          IoFilter filterC = new IoFilterAdapter();
    73          IoFilter filterD = new IoFilterAdapter();
    74
    75          builder.addFirst("A", filterA);
    76          builder.addLast("B", filterB);
    77          builder.addBefore("B", "C", filterC);
    78          builder.addAfter("A", "D", filterD);
    79
    80          Assert.assertSame(filterA, builder.get("A"));
    81          Assert.assertSame(filterB, builder.get("B"));
    82          Assert.assertSame(filterC, builder.get("C"));
    83          Assert.assertSame(filterD, builder.get("D"));
    84      }
    85
    86      public void testRemove() throws Exception {
    87          DefaultIoFilterChainBuilder builder = new DefaultIoFilterChainBuilder();
    88
    89          builder.addLast("A", new IoFilterAdapter());
    90          builder.addLast("B", new IoFilterAdapter());
    91          builder.addLast("C", new IoFilterAdapter());
    92          builder.addLast("D", new IoFilterAdapter());
    93          builder.addLast("E", new IoFilterAdapter());
    94
    95          builder.remove("A");
    96          builder.remove("E");
    97          builder.remove("C");
    98          builder.remove("B");
    99          builder.remove("D");
    100
    101         Assert.assertEquals(0, builder.getAll().size());
    102     }
    103
    104     public void testClear() throws Exception {
    105         DefaultIoFilterChainBuilder builder = new DefaultIoFilterChainBuilder();
    106
    107         builder.addLast("A", new IoFilterAdapter());
    108         builder.addLast("B", new IoFilterAdapter());
    109         builder.addLast("C", new IoFilterAdapter());
    110         builder.addLast("D", new IoFilterAdapter());
    111         builder.addLast("E", new IoFilterAdapter());
    112
    113         builder.clear();
    114
    115         Assert.assertEquals(0, builder.getAll().size());
    116     }
    117
    118     public void testToString() {
    119         DefaultIoFilterChainBuilder builder = new DefaultIoFilterChainBuilder();
    120
    121         // When the chain is empty
    122         Assert.assertEquals("{ empty }", builder.toString());
    123
    124         // When there's one filter
    125         builder.addLast("A", new IoFilterAdapter() {
    126             public String toString() {
    127                 return "B";
    128             }
    129         });
    130         Assert.assertEquals("{ (A:B) }", builder.toString());
    131
    132         // When there are two
    133         builder.addLast("C", new IoFilterAdapter() {
    134             public String toString() {
    135                 return "D";
    136             }
    137         });
    138         Assert.assertEquals("{ (A:B), (C:D) }", builder.toString());
    139     }
    140 }