    1   /* Copyright 2004, 2005 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.util;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.springframework.util.StringUtils;
    21
    22  import java.util.Map;
    23
    24
    25  /***
    26   * Tests {@link org.acegisecurity.util.StringSplitUtils}.
    27   *
    28   * @author Ben Alex
    29   * @version $Id: StringSplitUtilsTests.java,v 1.3 2005/11/17 00:56:08 benalex Exp $
    30   */
    31  public class StringSplitUtilsTests extends TestCase {
    32      //~ Constructors ===========================================================
    33
    34      // ===========================================================
    35      public StringSplitUtilsTests() {
    36          super();
    37      }
    38
    39      public StringSplitUtilsTests(String arg0) {
    40          super(arg0);
    41      }
    42
    43      //~ Methods ================================================================
    44
    45      // ================================================================
    46      public static void main(String[] args) {
    47          junit.textui.TestRunner.run(StringSplitUtilsTests.class);
    48      }
    49
    50      public void testSplitEachArrayElementAndCreateMapNormalOperation() {
    51          // note it ignores malformed entries (ie those without an equals sign)
    52          String unsplit = "username=\"marissa\", invalidEntryThatHasNoEqualsSign, realm=\"Contacts Realm\", nonce=\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\", uri=\"/acegi-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\", response=\"38644211cf9ac3da63ab639807e2baff\", qop=auth, nc=00000004, cnonce=\"2b8d329a8571b99a\"";
    53          String[] headerEntries = StringUtils.commaDelimitedListToStringArray(unsplit);
    54          Map headerMap = StringSplitUtils.splitEachArrayElementAndCreateMap(headerEntries,
    55                  "=", "\"");
    56
    57          assertEquals("marissa", headerMap.get("username"));
    58          assertEquals("Contacts Realm", headerMap.get("realm"));
    59          assertEquals("MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==",
    60              headerMap.get("nonce"));
    61          assertEquals("/acegi-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4",
    62              headerMap.get("uri"));
    63          assertEquals("38644211cf9ac3da63ab639807e2baff",
    64              headerMap.get("response"));
    65          assertEquals("auth", headerMap.get("qop"));
    66          assertEquals("00000004", headerMap.get("nc"));
    67          assertEquals("2b8d329a8571b99a", headerMap.get("cnonce"));
    68          assertEquals(8, headerMap.size());
    69      }
    70
    71      public void testSplitEachArrayElementAndCreateMapRespectsInstructionNotToRemoveCharacters() {
    72          String unsplit = "username=\"marissa\", realm=\"Contacts Realm\", nonce=\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\", uri=\"/acegi-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\", response=\"38644211cf9ac3da63ab639807e2baff\", qop=auth, nc=00000004, cnonce=\"2b8d329a8571b99a\"";
    73          String[] headerEntries = StringUtils.commaDelimitedListToStringArray(unsplit);
    74          Map headerMap = StringSplitUtils.splitEachArrayElementAndCreateMap(headerEntries,
    75                  "=", null);
    76
    77          assertEquals("\"marissa\"", headerMap.get("username"));
    78          assertEquals("\"Contacts Realm\"", headerMap.get("realm"));
    79          assertEquals("\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\"",
    80              headerMap.get("nonce"));
    81          assertEquals("\"/acegi-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\"",
    82              headerMap.get("uri"));
    83          assertEquals("\"38644211cf9ac3da63ab639807e2baff\"",
    84              headerMap.get("response"));
    85          assertEquals("auth", headerMap.get("qop"));
    86          assertEquals("00000004", headerMap.get("nc"));
    87          assertEquals("\"2b8d329a8571b99a\"", headerMap.get("cnonce"));
    88          assertEquals(8, headerMap.size());
    89      }
    90
    91      public void testSplitEachArrayElementAndCreateMapReturnsNullIfArrayEmptyOrNull() {
    92          assertNull(StringSplitUtils.splitEachArrayElementAndCreateMap(null,
    93                  "=", "\""));
    94          assertNull(StringSplitUtils.splitEachArrayElementAndCreateMap(
    95                  new String[] {}, "=", "\""));
    96      }
    97
    98      public void testSplitNormalOperation() {
    99          String unsplit = "username=\"marissa==\"";
    100         assertEquals("username", StringSplitUtils.split(unsplit, "=")[0]);
    101         assertEquals("\"marissa==\"", StringSplitUtils.split(unsplit, "=")[1]); // should not remove quotes or extra equals
    102     }
    103
    104     public void testSplitRejectsNullsAndIncorrectLengthStrings() {
    105         try {
    106             StringSplitUtils.split(null, "="); // null
    107             fail("Should have thrown IllegalArgumentException");
    108         } catch (IllegalArgumentException expected) {
    109             assertTrue(true);
    110         }
    111
    112         try {
    113             StringSplitUtils.split("", "="); // empty string
    114             fail("Should have thrown IllegalArgumentException");
    115         } catch (IllegalArgumentException expected) {
    116             assertTrue(true);
    117         }
    118
    119         try {
    120             StringSplitUtils.split("sdch=dfgf", null); // null
    121             fail("Should have thrown IllegalArgumentException");
    122         } catch (IllegalArgumentException expected) {
    123             assertTrue(true);
    124         }
    125
    126         try {
    127             StringSplitUtils.split("fvfv=dcdc", ""); // empty string
    128             fail("Should have thrown IllegalArgumentException");
    129         } catch (IllegalArgumentException expected) {
    130             assertTrue(true);
    131         }
    132
    133         try {
    134             StringSplitUtils.split("dfdc=dcdc", "BIGGER_THAN_ONE_CHARACTER");
    135             fail("Should have thrown IllegalArgumentException");
    136         } catch (IllegalArgumentException expected) {
    137             assertTrue(true);
    138         }
    139     }
    140
    141     public void testSplitWorksWithDifferentDelimiters() {
    142         assertEquals(2, StringSplitUtils.split("18/marissa", "/").length);
    143         assertNull(StringSplitUtils.split("18/marissa", "!"));
    144
    145         // only guarantees to split at FIRST delimiter, not EACH delimiter
    146         assertEquals(2, StringSplitUtils.split("18|marissa|foo|bar", "|").length);
    147     }
    148 }