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;
017:
018: import junit.framework.TestCase;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022:
023: /**
024: * Tests {@link ConfigAttributeEditor} and associated {@link ConfigAttributeDefinition}.
025: *
026: * @author Ben Alex
027: * @version $Id: ConfigAttributeEditorTests.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class ConfigAttributeEditorTests extends TestCase {
030: //~ Constructors ===================================================================================================
031:
032: public ConfigAttributeEditorTests() {
033: super ();
034: }
035:
036: public ConfigAttributeEditorTests(String arg0) {
037: super (arg0);
038: }
039:
040: //~ Methods ========================================================================================================
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(ConfigAttributeEditorTests.class);
044: }
045:
046: public final void setUp() throws Exception {
047: super .setUp();
048: }
049:
050: public void testCorrectOperation() {
051: ConfigAttributeEditor editor = new ConfigAttributeEditor();
052: editor.setAsText("HELLO,DOCTOR,NAME,YESTERDAY,TOMORROW");
053:
054: ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
055: .getValue();
056: Iterator iter = result.getConfigAttributes();
057: int position = 0;
058:
059: while (iter.hasNext()) {
060: position++;
061: iter.next();
062: }
063:
064: assertEquals(5, position);
065:
066: assertEquals(5, result.size());
067:
068: assertTrue(result.contains(new SecurityConfig("HELLO")));
069: assertTrue(result.contains(new SecurityConfig("TOMORROW")));
070: assertFalse(result.contains(new SecurityConfig("FOOBAR")));
071: }
072:
073: public void testEmptyStringReturnsNull() {
074: ConfigAttributeEditor editor = new ConfigAttributeEditor();
075: editor.setAsText("");
076:
077: ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
078: .getValue();
079: assertTrue(result == null);
080: }
081:
082: public void testEqualsHandlingWhenDifferentObjectTypes() {
083: ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
084: def1.addConfigAttribute(new SecurityConfig("A"));
085: def1.addConfigAttribute(new SecurityConfig("B"));
086:
087: assertTrue(!def1.equals("A_STRING"));
088: }
089:
090: public void testEqualsHandlingWhenExactlyEqual() {
091: ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
092: def1.addConfigAttribute(new SecurityConfig("A"));
093: def1.addConfigAttribute(new SecurityConfig("B"));
094:
095: ConfigAttributeDefinition def2 = new ConfigAttributeDefinition();
096: def2.addConfigAttribute(new SecurityConfig("A"));
097: def2.addConfigAttribute(new SecurityConfig("B"));
098:
099: assertEquals(def1, def2);
100: }
101:
102: public void testEqualsHandlingWhenOrderingNotEqual() {
103: ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
104: def1.addConfigAttribute(new SecurityConfig("A"));
105: def1.addConfigAttribute(new SecurityConfig("B"));
106:
107: ConfigAttributeDefinition def2 = new ConfigAttributeDefinition();
108: def2.addConfigAttribute(new SecurityConfig("B"));
109: def2.addConfigAttribute(new SecurityConfig("A"));
110:
111: assertTrue(!def1.equals(def2));
112: }
113:
114: public void testEqualsHandlingWhenTestObjectHasNoAttributes() {
115: ConfigAttributeDefinition def1 = new ConfigAttributeDefinition();
116: def1.addConfigAttribute(new SecurityConfig("A"));
117: def1.addConfigAttribute(new SecurityConfig("B"));
118:
119: ConfigAttributeDefinition def2 = new ConfigAttributeDefinition();
120:
121: assertTrue(!def1.equals(def2));
122: assertTrue(!def2.equals(def1));
123: }
124:
125: public void testNullReturnsNull() {
126: ConfigAttributeEditor editor = new ConfigAttributeEditor();
127: editor.setAsText(null);
128:
129: ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
130: .getValue();
131: assertTrue(result == null);
132: }
133:
134: public void testStripsTrailingAndLeadingSpaces() {
135: ConfigAttributeEditor editor = new ConfigAttributeEditor();
136: editor.setAsText(" HELLO, DOCTOR,NAME, YESTERDAY ,TOMORROW ");
137:
138: ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
139: .getValue();
140: Iterator iter = result.getConfigAttributes();
141:
142: ArrayList list = new ArrayList();
143:
144: while (iter.hasNext()) {
145: list.add(iter.next());
146: }
147:
148: assertEquals("HELLO", ((ConfigAttribute) list.get(0))
149: .getAttribute());
150: assertEquals("DOCTOR", ((ConfigAttribute) list.get(1))
151: .getAttribute());
152: assertEquals("NAME", ((ConfigAttribute) list.get(2))
153: .getAttribute());
154: assertEquals("YESTERDAY", ((ConfigAttribute) list.get(3))
155: .getAttribute());
156: assertEquals("TOMORROW", ((ConfigAttribute) list.get(4))
157: .getAttribute());
158: }
159:
160: public void testToString() {
161: ConfigAttributeEditor editor = new ConfigAttributeEditor();
162: editor.setAsText("KOALA,KANGAROO,EMU,WOMBAT");
163:
164: ConfigAttributeDefinition result = (ConfigAttributeDefinition) editor
165: .getValue();
166: assertEquals("[KOALA, KANGAROO, EMU, WOMBAT]", result
167: .toString());
168: }
169: }
|