001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestRoleUserAttributes.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.credentialsmanagers;
009:
010: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
011: import java.util.ArrayList;
012: import junit.framework.TestCase;
013:
014: public class TestRoleUserAttributes extends TestCase {
015: public TestRoleUserAttributes(String name) {
016: super (name);
017: }
018:
019: public void testInstantiation() {
020: RoleUserAttributes user_attributes = null;
021:
022: user_attributes = new RoleUserAttributes("thepassword");
023: assertNotNull(user_attributes);
024: assertEquals("thepassword", user_attributes.getPassword());
025:
026: ArrayList<String> roles = new ArrayList<String>();
027: roles.add("firstrole");
028: roles.add("secondrole");
029: user_attributes = new RoleUserAttributes("thepassword", roles);
030: assertNotNull(user_attributes);
031: assertEquals("thepassword", user_attributes.getPassword());
032:
033: assertEquals(2, user_attributes.getRoles().size());
034: boolean firstrole = false;
035: boolean secondrole = false;
036: for (String role : user_attributes.getRoles()) {
037: if (role.equals("firstrole")) {
038: firstrole = true;
039: } else if (role.equals("secondrole")) {
040: secondrole = true;
041: }
042: }
043: assertTrue(firstrole && secondrole);
044:
045: user_attributes = new RoleUserAttributes("thepassword",
046: new String[] { "firstrole", "secondrole" });
047: assertNotNull(user_attributes);
048: assertEquals("thepassword", user_attributes.getPassword());
049:
050: assertEquals(2, user_attributes.getRoles().size());
051: firstrole = false;
052: secondrole = false;
053: for (String role : user_attributes.getRoles()) {
054: if (role.equals("firstrole")) {
055: firstrole = true;
056: } else if (role.equals("secondrole")) {
057: secondrole = true;
058: }
059: }
060: assertTrue(firstrole && secondrole);
061: }
062:
063: public void testEquals() {
064: RoleUserAttributes user_attributes1 = new RoleUserAttributes(
065: "thepassword");
066: RoleUserAttributes user_attributes2 = new RoleUserAttributes(
067: "thepassword");
068: RoleUserAttributes user_attributes3 = new RoleUserAttributes(
069: "thepassword2");
070: RoleUserAttributes user_attributes4 = new RoleUserAttributes(
071: 12, "thepassword2");
072: RoleUserAttributes user_attributes5 = new RoleUserAttributes(
073: "thepassword",
074: new String[] { "firstrole", "secondrole" });
075: RoleUserAttributes user_attributes6 = new RoleUserAttributes(
076: "thepassword",
077: new String[] { "firstrole", "secondrole" });
078: RoleUserAttributes user_attributes7 = new RoleUserAttributes(
079: "thepassword", new String[] { "firstrole" });
080: RoleUserAttributes user_attributes8 = new RoleUserAttributes(
081: "thepassword",
082: new String[] { "firstrole", "thirdrole" });
083: RoleUserAttributes user_attributes9 = new RoleUserAttributes(
084: 13, "thepassword", new String[] { "firstrole",
085: "secondrole" });
086: RoleUserAttributes user_attributes10 = new RoleUserAttributes(
087: 13, "thepassword", new String[] { "firstrole",
088: "secondrole" });
089:
090: assertTrue(user_attributes1.equals(user_attributes1));
091: assertTrue(user_attributes1.equals(user_attributes2));
092: assertFalse(user_attributes1.equals(user_attributes3));
093: assertFalse(user_attributes1.equals(user_attributes4));
094: assertFalse(user_attributes1.equals(user_attributes5));
095: assertFalse(user_attributes1.equals(user_attributes6));
096: assertFalse(user_attributes1.equals(user_attributes7));
097: assertFalse(user_attributes1.equals(user_attributes8));
098: assertFalse(user_attributes1.equals(user_attributes9));
099: assertFalse(user_attributes1.equals(user_attributes10));
100:
101: assertTrue(user_attributes2.equals(user_attributes2));
102: assertFalse(user_attributes2.equals(user_attributes3));
103: assertFalse(user_attributes2.equals(user_attributes4));
104: assertFalse(user_attributes2.equals(user_attributes5));
105: assertFalse(user_attributes2.equals(user_attributes6));
106: assertFalse(user_attributes2.equals(user_attributes7));
107: assertFalse(user_attributes2.equals(user_attributes8));
108: assertFalse(user_attributes2.equals(user_attributes9));
109: assertFalse(user_attributes2.equals(user_attributes10));
110:
111: assertTrue(user_attributes3.equals(user_attributes3));
112: assertFalse(user_attributes3.equals(user_attributes4));
113: assertFalse(user_attributes3.equals(user_attributes5));
114: assertFalse(user_attributes3.equals(user_attributes6));
115: assertFalse(user_attributes3.equals(user_attributes7));
116: assertFalse(user_attributes3.equals(user_attributes8));
117: assertFalse(user_attributes3.equals(user_attributes9));
118: assertFalse(user_attributes3.equals(user_attributes10));
119:
120: assertTrue(user_attributes4.equals(user_attributes4));
121: assertFalse(user_attributes4.equals(user_attributes5));
122: assertFalse(user_attributes4.equals(user_attributes6));
123: assertFalse(user_attributes4.equals(user_attributes7));
124: assertFalse(user_attributes4.equals(user_attributes8));
125: assertFalse(user_attributes4.equals(user_attributes9));
126: assertFalse(user_attributes4.equals(user_attributes10));
127:
128: assertTrue(user_attributes5.equals(user_attributes5));
129: assertTrue(user_attributes5.equals(user_attributes6));
130: assertFalse(user_attributes5.equals(user_attributes7));
131: assertFalse(user_attributes5.equals(user_attributes8));
132: assertFalse(user_attributes5.equals(user_attributes9));
133: assertFalse(user_attributes5.equals(user_attributes10));
134:
135: assertTrue(user_attributes6.equals(user_attributes6));
136: assertFalse(user_attributes6.equals(user_attributes7));
137: assertFalse(user_attributes6.equals(user_attributes8));
138: assertFalse(user_attributes6.equals(user_attributes9));
139: assertFalse(user_attributes6.equals(user_attributes10));
140:
141: assertTrue(user_attributes7.equals(user_attributes7));
142: assertFalse(user_attributes7.equals(user_attributes8));
143: assertFalse(user_attributes7.equals(user_attributes9));
144: assertFalse(user_attributes7.equals(user_attributes10));
145:
146: assertTrue(user_attributes8.equals(user_attributes8));
147: assertFalse(user_attributes8.equals(user_attributes9));
148: assertFalse(user_attributes8.equals(user_attributes10));
149:
150: assertTrue(user_attributes9.equals(user_attributes9));
151: assertTrue(user_attributes9.equals(user_attributes10));
152:
153: assertTrue(user_attributes10.equals(user_attributes10));
154: }
155:
156: public void testEmptyInitialRoles() {
157: RoleUserAttributes user_attributes = new RoleUserAttributes(
158: "thepassword");
159:
160: assertEquals(0, user_attributes.getRoles().size());
161: }
162:
163: public void testPopulate() {
164: RoleUserAttributes user_attributes = new RoleUserAttributes(
165: "thepassword");
166:
167: ArrayList<String> roles = new ArrayList<String>();
168: roles.add("firstrole");
169: roles.add("secondrole");
170: user_attributes.setRoles(roles);
171:
172: assertEquals("thepassword", user_attributes.getPassword());
173:
174: assertEquals(2, user_attributes.getRoles().size());
175: boolean firstrole = false;
176: boolean secondrole = false;
177: for (String role : user_attributes.getRoles()) {
178: if (role.equals("firstrole")) {
179: firstrole = true;
180: } else if (role.equals("secondrole")) {
181: secondrole = true;
182: }
183: }
184:
185: assertTrue(firstrole && secondrole);
186: }
187:
188: public void testIsInRole() {
189: RoleUserAttributes user_attributes = new RoleUserAttributes(
190: "thepassword");
191:
192: ArrayList<String> roles = new ArrayList<String>();
193: roles.add("firstrole");
194: roles.add("secondrole");
195: user_attributes.setRoles(roles);
196:
197: assertTrue(user_attributes.isInRole("firstrole"));
198: assertTrue(user_attributes.isInRole("secondrole"));
199: assertTrue(!user_attributes.isInRole("thirdrole"));
200: }
201:
202: public void testIsValid() {
203: RoleUserAttributes user_attributes = new RoleUserAttributes(
204: "thepassword");
205:
206: ArrayList<String> roles = new ArrayList<String>();
207: roles.add("firstrole");
208: roles.add("secondrole");
209: user_attributes.setRoles(roles);
210:
211: assertTrue(user_attributes.isValid("thepassword"));
212: assertTrue(!user_attributes.isValid("anotherpassword"));
213: assertTrue(user_attributes.isValid("thepassword", "firstrole"));
214: assertTrue(user_attributes.isValid("thepassword", "secondrole"));
215: assertTrue(!user_attributes.isValid("anotherpassword",
216: "firstrole"));
217: assertTrue(!user_attributes.isValid("anotherpassword",
218: "secondrole"));
219: assertTrue(!user_attributes.isValid("thepassword", "thirdrole"));
220: assertTrue(!user_attributes.isValid("anotherpassword",
221: "thirdrole"));
222: }
223: }
|