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   */package org.apache.mina.session;
    20
    21  import static org.apache.mina.session.AttributeKey.createKey;
    22  import static org.hamcrest.CoreMatchers.is;
    23  import static org.hamcrest.CoreMatchers.nullValue;
    24  import static org.junit.Assert.assertThat;
    25
    26  import java.util.Set;
    27
    28  import org.junit.Before;
    29  import org.junit.Rule;
    30  import org.junit.Test;
    31  import org.junit.rules.ExpectedException;
    32
    33  /**
    34   * Tests the class {@link AttributeContainer}
    35   *
    36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    37   */
    38  public class AttributeContainerTest {
    39
    40      /** the default value that is used for every test that calls {@link AttributeContainer#getAttribute(AttributeKey, Object)}*/
    41      private static final int DEFAULT_VALUE = 0;
    42
    43      /** the class under test */
    44      private AttributeContainer container;
    45
    46      @Rule
    47      public ExpectedException exception = ExpectedException.none();
    48
    49      private static final AttributeKey<Integer> ATTRIBUTE_KEY = createKey(Integer.class, "myKey");
    50
    51      @Before
    52      public void setUp() {
    53          container = new DefaultAttributeContainer();
    54      }
    55
    56      /**
    57       * Test if a {@link IllegalArgumentException} is thrown when the key is
    58       * <code>null</code>.
    59       *
    60       * @throws Exception
    61       */
    62      @Test
    63      public void setAttributeWithoutKey() throws Exception {
    64          exception.expect(IllegalArgumentException.class);
    65          exception.expectMessage("Parameter >key< must not be null!");
    66          container.setAttribute(null, DEFAULT_VALUE);
    67      }
    68
    69      /**
    70       * Test if a {@link IllegalArgumentException} is thrown if illegal value is
    71       * set using an unsafe key.<br>
    72       * Expected is a value of type Integer, but passed is a Double-value using
    73       * an covariant key.
    74       *
    75       *
    76       * @throws Exception
    77       */
    78      @Test
    79      public void setAttributeWithUnsafeKey() throws Exception {
    80          exception.expect(IllegalArgumentException.class);
    81          exception.expectMessage("Invalid attribute value\r\n" + "  expected type: java.lang.Integer\r\n"
    82                  + "  actual type  : java.lang.Double\r\n" + "  actual value : 12.3");
    83
    84          AttributeKey<? extends Number> key = ATTRIBUTE_KEY;
    85          Double value = 12.3;
    86          container.setAttribute(key, value);
    87      }
    88
    89      /**
    90       * Test if <code>null</code> is returned when an Attribute is set for the
    91       * first time.
    92       *
    93       * @throws Exception
    94       */
    95      @Test
    96      public void setAttributeForTheFirstTime() throws Exception {
    97          Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, 123);
    98          assertThat(oldValue, is(nullValue()));
    99      }
    100
    101     /**
    102      * Test if the old value is returned, if the attribute was set before.
    103      *
    104      * @throws Exception
    105      */
    106     @Test
    107     public void setAttributeForTheSecondTime() throws Exception {
    108         container.setAttribute(ATTRIBUTE_KEY, 123);
    109         Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, 456);
    110         assertThat(oldValue, is(123));
    111     }
    112
    113     /**
    114      * Test if the <code>null</code> value is returned, if the attribute has no
    115      * previous value.
    116      *
    117      * @throws Exception
    118      */
    119     @Test
    120     public void setAttributeValueToNull() throws Exception {
    121         Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, 456);
    122
    123         assertThat(oldValue, is(nullValue()));
    124     }
    125
    126     /**
    127      * Test if the old value is returned, if the attribute has a previous value.
    128      *
    129      * @throws Exception
    130      */
    131     @Test
    132     public void setAttributeValueToNullIfPreviousValueIsAvailable() throws Exception {
    133         container.setAttribute(ATTRIBUTE_KEY, 123);
    134         Integer oldValue = container.setAttribute(ATTRIBUTE_KEY, null);
    135
    136         assertThat(oldValue, is(123));
    137     }
    138
    139     /**
    140      * Test if {@link IllegalArgumentException} is thrown when <code>null</code>
    141      * is passed.
    142      *
    143      * @throws Exception
    144      */
    145     @Test
    146     public void getAttributeWithoutKey() throws Exception {
    147         exception.expect(IllegalArgumentException.class);
    148         exception.expectMessage("Parameter >key< must not be null!");
    149         container.getAttribute(null, DEFAULT_VALUE);
    150     }
    151
    152     /**
    153      * Test if the default is returned if the value is not present.
    154      *
    155      * @throws Exception
    156      */
    157     @Test
    158     public void getAttributeThatIsNotPresent() throws Exception {
    159         Integer value = container.getAttribute(ATTRIBUTE_KEY, DEFAULT_VALUE);
    160         assertThat(value, is(DEFAULT_VALUE));
    161     }
    162
    163     /**
    164      * Test if {@link IllegalArgumentException} is thrown when <code>null</code>
    165      * is passed.
    166      *
    167      * @throws Exception
    168      */
    169     @Test
    170     public void getAttributeThatIsPresent() throws Exception {
    171         container.setAttribute(ATTRIBUTE_KEY, 123);
    172         Integer value = container.getAttribute(ATTRIBUTE_KEY, DEFAULT_VALUE);
    173         assertThat(value, is(123));
    174     }
    175
    176     /**
    177      * Test if write-operations on the Key-Set of
    178      * {@link AttributeContainer#getAttributeKeys()}, doesn't affect the
    179      * container it self.
    180      *
    181      * @throws Exception
    182      */
    183     @Test
    184     public void getAttributeKeysAndRemoveKey() throws Exception {
    185         container.setAttribute(ATTRIBUTE_KEY, 123);
    186         Set<AttributeKey<?>> set = container.getAttributeKeys();
    187
    188         try {
    189             set.remove(ATTRIBUTE_KEY);
    190         } catch (UnsupportedOperationException e) {
    191         }
    192
    193         Integer value = container.getAttribute(ATTRIBUTE_KEY, DEFAULT_VALUE);
    194         assertThat(value, is(123));
    195     }
    196
    197     /**
    198      * Test if a present attribute is returned after remove.
    199      * @throws Exception
    200      */
    201     @Test
    202     public void removeAPresentAttribute() throws Exception {
    203         container.setAttribute(ATTRIBUTE_KEY, 123);
    204         Integer oldValue = container.removeAttribute(ATTRIBUTE_KEY);
    205         assertThat(oldValue, is(123));
    206     }
    207
    208     /**
    209      * Test if a <code>null</code> is returned if no attribute is present.
    210      * @throws Exception
    211      */
    212     @Test
    213     public void removeNonPresentAttribute() throws Exception {
    214         Integer oldValue = container.removeAttribute(ATTRIBUTE_KEY);
    215         assertThat(oldValue, is(nullValue()));
    216     }
    217
    218     /**
    219      * Test if a {@link IllegalArgumentException} is thrown if a<code>null</code> key is passed.
    220      * @throws Exception
    221      */
    222     @Test
    223     public void removeWithNullKey() throws Exception {
    224         exception.expect(IllegalArgumentException.class);
    225         exception.expectMessage("Parameter >key< must not be null!");
    226         container.removeAttribute(null);
    227     }
    228 }