001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.util;
017:
018: import junit.framework.TestCase;
019:
020: import org.springframework.util.StringUtils;
021:
022: import java.util.Map;
023:
024: /**
025: * Tests {@link org.acegisecurity.util.StringSplitUtils}.
026: *
027: * @author Ben Alex
028: * @version $Id: StringSplitUtilsTests.java 1496 2006-05-23 13:38:33Z benalex $
029: */
030: public class StringSplitUtilsTests extends TestCase {
031: //~ Constructors ===================================================================================================
032:
033: // ===========================================================
034: public StringSplitUtilsTests() {
035: super ();
036: }
037:
038: public StringSplitUtilsTests(String arg0) {
039: super (arg0);
040: }
041:
042: //~ Methods ========================================================================================================
043:
044: // ================================================================
045: public static void main(String[] args) {
046: junit.textui.TestRunner.run(StringSplitUtilsTests.class);
047: }
048:
049: public void testSplitEachArrayElementAndCreateMapNormalOperation() {
050: // note it ignores malformed entries (ie those without an equals sign)
051: 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\"";
052: String[] headerEntries = StringUtils
053: .commaDelimitedListToStringArray(unsplit);
054: Map headerMap = StringSplitUtils
055: .splitEachArrayElementAndCreateMap(headerEntries, "=",
056: "\"");
057:
058: assertEquals("marissa", headerMap.get("username"));
059: assertEquals("Contacts Realm", headerMap.get("realm"));
060: assertEquals(
061: "MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==",
062: headerMap.get("nonce"));
063: assertEquals(
064: "/acegi-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4",
065: headerMap.get("uri"));
066: assertEquals("38644211cf9ac3da63ab639807e2baff", headerMap
067: .get("response"));
068: assertEquals("auth", headerMap.get("qop"));
069: assertEquals("00000004", headerMap.get("nc"));
070: assertEquals("2b8d329a8571b99a", headerMap.get("cnonce"));
071: assertEquals(8, headerMap.size());
072: }
073:
074: public void testSplitEachArrayElementAndCreateMapRespectsInstructionNotToRemoveCharacters() {
075: 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\"";
076: String[] headerEntries = StringUtils
077: .commaDelimitedListToStringArray(unsplit);
078: Map headerMap = StringSplitUtils
079: .splitEachArrayElementAndCreateMap(headerEntries, "=",
080: null);
081:
082: assertEquals("\"marissa\"", headerMap.get("username"));
083: assertEquals("\"Contacts Realm\"", headerMap.get("realm"));
084: assertEquals(
085: "\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\"",
086: headerMap.get("nonce"));
087: assertEquals(
088: "\"/acegi-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\"",
089: headerMap.get("uri"));
090: assertEquals("\"38644211cf9ac3da63ab639807e2baff\"", headerMap
091: .get("response"));
092: assertEquals("auth", headerMap.get("qop"));
093: assertEquals("00000004", headerMap.get("nc"));
094: assertEquals("\"2b8d329a8571b99a\"", headerMap.get("cnonce"));
095: assertEquals(8, headerMap.size());
096: }
097:
098: public void testSplitEachArrayElementAndCreateMapReturnsNullIfArrayEmptyOrNull() {
099: assertNull(StringSplitUtils.splitEachArrayElementAndCreateMap(
100: null, "=", "\""));
101: assertNull(StringSplitUtils.splitEachArrayElementAndCreateMap(
102: new String[] {}, "=", "\""));
103: }
104:
105: public void testSplitNormalOperation() {
106: String unsplit = "username=\"marissa==\"";
107: assertEquals("username",
108: StringSplitUtils.split(unsplit, "=")[0]);
109: assertEquals("\"marissa==\"", StringSplitUtils.split(unsplit,
110: "=")[1]); // should not remove quotes or extra equals
111: }
112:
113: public void testSplitRejectsNullsAndIncorrectLengthStrings() {
114: try {
115: StringSplitUtils.split(null, "="); // null
116: fail("Should have thrown IllegalArgumentException");
117: } catch (IllegalArgumentException expected) {
118: assertTrue(true);
119: }
120:
121: try {
122: StringSplitUtils.split("", "="); // empty string
123: fail("Should have thrown IllegalArgumentException");
124: } catch (IllegalArgumentException expected) {
125: assertTrue(true);
126: }
127:
128: try {
129: StringSplitUtils.split("sdch=dfgf", null); // null
130: fail("Should have thrown IllegalArgumentException");
131: } catch (IllegalArgumentException expected) {
132: assertTrue(true);
133: }
134:
135: try {
136: StringSplitUtils.split("fvfv=dcdc", ""); // empty string
137: fail("Should have thrown IllegalArgumentException");
138: } catch (IllegalArgumentException expected) {
139: assertTrue(true);
140: }
141:
142: try {
143: StringSplitUtils.split("dfdc=dcdc",
144: "BIGGER_THAN_ONE_CHARACTER");
145: fail("Should have thrown IllegalArgumentException");
146: } catch (IllegalArgumentException expected) {
147: assertTrue(true);
148: }
149: }
150:
151: public void testSplitWorksWithDifferentDelimiters() {
152: assertEquals(2,
153: StringSplitUtils.split("18/marissa", "/").length);
154: assertNull(StringSplitUtils.split("18/marissa", "!"));
155:
156: // only guarantees to split at FIRST delimiter, not EACH delimiter
157: assertEquals(2, StringSplitUtils.split("18|marissa|foo|bar",
158: "|").length);
159: }
160: }
|