1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35 public StringSplitUtilsTests() {
36 super();
37 }
38
39 public StringSplitUtilsTests(String arg0) {
40 super(arg0);
41 }
42
43
44
45
46 public static void main(String[] args) {
47 junit.textui.TestRunner.run(StringSplitUtilsTests.class);
48 }
49
50 public void testSplitEachArrayElementAndCreateMapNormalOperation() {
51
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]);
102 }
103
104 public void testSplitRejectsNullsAndIncorrectLengthStrings() {
105 try {
106 StringSplitUtils.split(null, "=");
107 fail("Should have thrown IllegalArgumentException");
108 } catch (IllegalArgumentException expected) {
109 assertTrue(true);
110 }
111
112 try {
113 StringSplitUtils.split("", "=");
114 fail("Should have thrown IllegalArgumentException");
115 } catch (IllegalArgumentException expected) {
116 assertTrue(true);
117 }
118
119 try {
120 StringSplitUtils.split("sdch=dfgf", null);
121 fail("Should have thrown IllegalArgumentException");
122 } catch (IllegalArgumentException expected) {
123 assertTrue(true);
124 }
125
126 try {
127 StringSplitUtils.split("fvfv=dcdc", "");
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
146 assertEquals(2, StringSplitUtils.split("18|marissa|foo|bar", "|").length);
147 }
148 }