01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: TestXml2MemoryUsers.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.authentication.credentialsmanagers;
09:
10: import com.uwyn.rife.authentication.credentials.RoleUser;
11: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
12: import com.uwyn.rife.resources.ResourceFinderClasspath;
13: import com.uwyn.rife.tools.ExceptionUtils;
14: import junit.framework.TestCase;
15:
16: public class TestXml2MemoryUsers extends TestCase {
17: public TestXml2MemoryUsers(String name) {
18: super (name);
19: }
20:
21: public void testInstantiation() {
22: Xml2MemoryUsers xml2users = new Xml2MemoryUsers();
23:
24: assertNotNull(xml2users);
25: }
26:
27: public void testParse() {
28: MemoryUsers users = null;
29:
30: try {
31: users = new MemoryUsers("xml/test_xml2users.xml",
32: ResourceFinderClasspath.getInstance());
33:
34: assertEquals(4, users.countUsers());
35:
36: assertTrue(!users.containsUser("login"));
37: assertTrue(users.containsUser("the_first_login"));
38: assertTrue(users.containsUser("the_second_login"));
39: assertTrue(users.containsUser("the_third_login"));
40: assertTrue(users.containsUser("the_fourth_login"));
41:
42: assertTrue(-1 == users.verifyCredentials(new RoleUser(
43: "login", "thepassword")));
44:
45: assertTrue(0 == users.verifyCredentials(new RoleUser(
46: "the_first_login", "a password")));
47:
48: assertTrue(17 == users.verifyCredentials(new RoleUser(
49: "the_second_login", "another password")));
50: assertTrue(17 == users.verifyCredentials(new RoleUser(
51: "the_second_login", "another password", "role1")));
52: assertTrue(17 == users.verifyCredentials(new RoleUser(
53: "the_second_login", "another password", "role2")));
54:
55: assertTrue(98 == users.verifyCredentials(new RoleUser(
56: "the_third_login", "a third password")));
57:
58: assertTrue(1 == users.verifyCredentials(new RoleUser(
59: "the_fourth_login", "the last password")));
60: assertTrue(1 == users.verifyCredentials(new RoleUser(
61: "the_fourth_login", "the last password", "role3")));
62: assertTrue(1 == users.verifyCredentials(new RoleUser(
63: "the_fourth_login", "the last password", "role2")));
64: assertTrue(1 == users.verifyCredentials(new RoleUser(
65: "the_fourth_login", "the last password", "role4")));
66:
67: assertEquals("the_first_login", users.getLogin(0));
68: assertEquals("the_second_login", users.getLogin(17));
69: assertEquals("the_third_login", users.getLogin(98));
70: assertEquals("the_fourth_login", users.getLogin(1));
71: } catch (CredentialsManagerException e) {
72: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
73: }
74: }
75:
76: public void testUnavailableXmlFile() {
77: MemoryUsers users = null;
78:
79: try {
80: users = new MemoryUsers("xml/this_file_is_not_there.xml",
81: ResourceFinderClasspath.getInstance());
82: fail();
83: assertNotNull(users);
84: } catch (IllegalArgumentException e) {
85: assertTrue(true);
86: } catch (CredentialsManagerException e) {
87: assertTrue(true);
88: }
89: }
90: }
|