View Javadoc

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