    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity;
    17
    18  import junit.framework.TestCase;
    19
    20  import java.util.ArrayList;
    21  import java.util.Iterator;
    22
    23
    24  /***
    25   * Tests {@link ConfigAttributeEditor} and associated {@link
    26   * ConfigAttributeDefinition}.
    27   *
    28   * @author Ben Alex
    29   * @version $Id: ConfigAttributeEditorTests.java,v 1.4 2005/11/17 00:55:47 benalex Exp $
    30   */
    31  public class ConfigAttributeEditorTests extends TestCase {
    32      //~ Constructors ===========================================================
    33
    34      public ConfigAttributeEditorTests() {
    35          super();
    36      }
    37
    38      public ConfigAttributeEditorTests(String arg0) {
    39          super(arg0);
    40      }
    41
    42      //~ Methods ================================================================
    43
    44      public final void setUp() throws Exception {
    45          super.setUp();
    46      }
    47
    48      public static void main(String[] args) {
    49          junit.textui.TestRunner.run(ConfigAttributeEditorTests.class);
    50      }
    51
    52      public void testCorrectOperation() {
    53          ConfigAttributeEditor editor = new ConfigAttributeEditor();
    54          editor.setAsText("HELLO,DOCTOR,NAME,YESTERDAY,TOMORROW");
    55
    56          ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
    57              .getValue();
    58          Iterator iter = result.getConfigAttributes();
    59          int position = 0;
    60
    61          while (iter.hasNext()) {
    62              position++;
    63              iter.next();
    64          }
    65
    66          assertEquals(5, position);
    67
    68          assertEquals(5, result.size());
    69
    70          assertTrue(result.contains(new SecurityConfig("HELLO")));
    71          assertTrue(result.contains(new SecurityConfig("TOMORROW")));
    72          assertFalse(result.contains(new SecurityConfig("FOOBAR")));
    73      }
    74
    75      public void testEmptyStringReturnsNull() {
    76          ConfigAttributeEditor editor = new ConfigAttributeEditor();
    77          editor.setAsText("");
    78
    79          ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
    80              .getValue();
    81          assertTrue(result == null);
    82      }
    83
    84      public void testEqualsHandlingWhenDifferentObjectTypes() {
    85          ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
    86          def1.addConfigAttribute(new SecurityConfig("A"));
    87          def1.addConfigAttribute(new SecurityConfig("B"));
    88
    89          assertTrue(!def1.equals("A_STRING"));
    90      }
    91
    92      public void testEqualsHandlingWhenExactlyEqual() {
    93          ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
    94          def1.addConfigAttribute(new SecurityConfig("A"));
    95          def1.addConfigAttribute(new SecurityConfig("B"));
    96
    97          ConfigAttributeDefinition def2 = new ConfigAttributeDefinition();
    98          def2.addConfigAttribute(new SecurityConfig("A"));
    99          def2.addConfigAttribute(new SecurityConfig("B"));
    100
    101         assertEquals(def1, def2);
    102     }
    103
    104     public void testEqualsHandlingWhenOrderingNotEqual() {
    105         ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
    106         def1.addConfigAttribute(new SecurityConfig("A"));
    107         def1.addConfigAttribute(new SecurityConfig("B"));
    108
    109         ConfigAttributeDefinition def2 = new ConfigAttributeDefinition();
    110         def2.addConfigAttribute(new SecurityConfig("B"));
    111         def2.addConfigAttribute(new SecurityConfig("A"));
    112
    113         assertTrue(!def1.equals(def2));
    114     }
    115
    116     public void testEqualsHandlingWhenTestObjectHasNoAttributes() {
    117         ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
    118         def1.addConfigAttribute(new SecurityConfig("A"));
    119         def1.addConfigAttribute(new SecurityConfig("B"));
    120
    121         ConfigAttributeDefinition def2 = new ConfigAttributeDefinition();
    122
    123         assertTrue(!def1.equals(def2));
    124         assertTrue(!def2.equals(def1));
    125     }
    126
    127     public void testNullReturnsNull() {
    128         ConfigAttributeEditor editor = new ConfigAttributeEditor();
    129         editor.setAsText(null);
    130
    131         ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
    132             .getValue();
    133         assertTrue(result == null);
    134     }
    135
    136     public void testStripsTrailingAndLeadingSpaces() {
    137         ConfigAttributeEditor editor = new ConfigAttributeEditor();
    138         editor.setAsText("  HELLO, DOCTOR,NAME,  YESTERDAY ,TOMORROW ");
    139
    140         ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
    141             .getValue();
    142         Iterator iter = result.getConfigAttributes();
    143
    144         ArrayList list = new ArrayList();
    145
    146         while (iter.hasNext()) {
    147             list.add(iter.next());
    148         }
    149
    150         assertEquals("HELLO", ((ConfigAttribute) list.get(0)).getAttribute());
    151         assertEquals("DOCTOR", ((ConfigAttribute) list.get(1)).getAttribute());
    152         assertEquals("NAME", ((ConfigAttribute) list.get(2)).getAttribute());
    153         assertEquals("YESTERDAY", ((ConfigAttribute) list.get(3)).getAttribute());
    154         assertEquals("TOMORROW", ((ConfigAttribute) list.get(4)).getAttribute());
    155     }
    156
    157     public void testToString() {
    158         ConfigAttributeEditor editor = new ConfigAttributeEditor();
    159         editor.setAsText("KOALA,KANGAROO,EMU,WOMBAT");
    160
    161         ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
    162             .getValue();
    163         assertEquals("[KOALA, KANGAROO, EMU, WOMBAT]", result.toString());
    164     }
    165 }