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.hamcrest.CoreMatchers.is;
    22  import static org.junit.Assert.assertThat;
    23
    24  import java.util.Date;
    25
    26  import org.junit.Rule;
    27  import org.junit.Test;
    28  import org.junit.rules.ExpectedException;
    29
    30  /**
    31   * Tests the class {@link AttributeKey}
    32   *
    33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    34   */
    35  public class AttributeKeyTest {
    36      /** checks Exception parameters */
    37      @Rule
    38      public final ExpectedException exception = ExpectedException.none();
    39
    40      /**
    41       * Tests if the constructor throws an {@link IllegalArgumentException} if
    42       * parameter <code>attributeType</code> is <code>null</code>.
    43       *
    44       * @throws Exception
    45       */
    46      @Test
    47      public void constructorWithoutKeyType() throws Exception {
    48          exception.expect(IllegalArgumentException.class);
    49          exception.expectMessage("Parameter >attributeType< must not be null!");
    50          new AttributeKey<Number>(null, "key");
    51      }
    52
    53      /**
    54       * Tests if the constructor throws an {@link IllegalArgumentException} if
    55       * parameter <code>attributeType</code> is <code>null</code>.
    56       *
    57       * @throws Exception
    58       */
    59      @Test
    60      public void constructorWithoutKeyName() throws Exception {
    61          exception.expect(IllegalArgumentException.class);
    62          exception.expectMessage("Parameter >attributeName< must not be null!");
    63          new AttributeKey<Number>(Number.class, null);
    64      }
    65
    66      /**
    67       * Test if the key-name passed into the constructor will be returned
    68       *
    69       * @throws Exception
    70       */
    71      @Test
    72      public void getName() throws Exception {
    73          String name = new AttributeKey<Number>(Number.class, "keyName").getName();
    74          assertThat(name, is("keyName"));
    75      }
    76
    77      /**
    78       * Test if the key-type passed into the constructor will be returned.
    79       *
    80       * @throws Exception
    81       */
    82      @Test
    83      public void getType() throws Exception {
    84          Class<?> type = new AttributeKey<Number>(Number.class, "keyName").getType();
    85          assertThat(type, is((Object) Number.class));
    86      }
    87
    88      /**
    89       * Test the equals method is symmetric, if two {@link AttributeKey}s  with the same attribute type and attribute-name.
    90       * @throws Exception
    91       */
    92      @Test
    93      public void equalsSymmetric() throws Exception {
    94          AttributeKey<Number> key1 = new AttributeKey<Number>(Number.class, "keyName");
    95          AttributeKey<Number> key2 = new AttributeKey<Number>(Number.class, "keyName");
    96
    97          assertThat(key1.equals(key2), is(true));
    98          assertThat(key2.equals(key1), is(true));
    99      }
    100
    101     /**
    102      * Test if the equals method returns <code>false</code>  if the attribute type is different.
    103      * @throws Exception
    104      */
    105     @Test
    106     public void equalsWithDifferentTypes() throws Exception {
    107         AttributeKey<Number> key1 = new AttributeKey<Number>(Number.class, "keyName");
    108         AttributeKey<Date> key2 = new AttributeKey<Date>(Date.class, "keyName");
    109
    110         assertThat(key1.equals(key2), is(false));
    111         assertThat(key2.equals(key1), is(false));
    112     }
    113
    114     /**
    115      * Test if the equals method returns <code>false</code>  if the attribute name is different.
    116      * @throws Exception
    117      */
    118     @Test
    119     public void equalsWithDifferentName() throws Exception {
    120         AttributeKey<Number> key1 = new AttributeKey<Number>(Number.class, "key1");
    121         AttributeKey<Number> key2 = new AttributeKey<Number>(Number.class, "key2");
    122
    123         assertThat(key1.equals(key2), is(false));
    124         assertThat(key2.equals(key1), is(false));
    125     }
    126
    127     /**
    128      * Test if two {@link AttributeKey}s with the same attribute type and attribute-name have the same hashCode
    129      * @throws Exception
    130      */
    131     @Test
    132     public void hashCodeValue() throws Exception {
    133         AttributeKey<Number> key1 = new AttributeKey<Number>(Number.class, "keyName");
    134         AttributeKey<Number> key2 = new AttributeKey<Number>(Number.class, "keyName");
    135
    136         assertThat(key1.hashCode(), is(key2.hashCode()));
    137     }
    138 }