KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > authentication > configuration > JGuardConfigurationTest


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.core.authentication.configuration;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
36 import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
37
38 import junit.framework.TestCase;
39 import net.sf.jguard.core.CoreConstants;
40
41
42 public class JGuardConfigurationTest extends TestCase {
43
44     /*
45      * Generates a mock list of Login Modules
46      */

47     private List JavaDoc generateMockLoginModuleList() {
48         List JavaDoc loginModules = new ArrayList JavaDoc();
49
50         Map JavaDoc first = new HashMap JavaDoc();
51         first.put(CoreConstants.NAME,"net.sf.jguard.authentication.loginmodules.XmlLoginModule");
52         first.put(CoreConstants.FLAG, "REQUIRED");
53             Map JavaDoc firstOptions = new HashMap JavaDoc();
54             firstOptions.put("debug", "false");
55             firstOptions.put("fileLocation","WEB-INF/conf/jGuard/jGuardUsersPrincipals.xml");
56             firstOptions.put("authenticationManager","net.sf.jguard.authentication.XmlAuthenticationManager");
57         first.put(CoreConstants.LOGIN_MODULE_OPTIONS, firstOptions);
58         loginModules.add(first);
59
60         Map JavaDoc second = new HashMap JavaDoc();
61         second.put(CoreConstants.NAME,"net.sf.jguard.authentication.loginmodules.PostgreSQLLoginModule");
62         second.put(CoreConstants.FLAG, "OPTIONAL");
63             Map JavaDoc secondOptions = new HashMap JavaDoc();
64             secondOptions.put("authenticationUrl", "jdbc:postgresql://127.0.0.1:5434/test");
65             secondOptions.put("authenticationLogin","testLogin");
66             secondOptions.put("authenticationPassword","testPassword");
67             secondOptions.put("authenticationDriver","org.postgresql.Driver");
68         second.put(CoreConstants.LOGIN_MODULE_OPTIONS, secondOptions);
69         loginModules.add(second);
70
71         return loginModules;
72     }
73
74     /*
75      * Tests interaction between ConfigurationHelper and JGuardConfiguration
76      */

77     public void testGetApplicationEntry() {
78         Map JavaDoc authenticationMap = new HashMap JavaDoc();
79         authenticationMap.put(CoreConstants.DEBUG, "true");
80
81         authenticationMap.put(CoreConstants.INCLUDE_OLD_CONFIG, "false");
82
83         List JavaDoc loginModules = generateMockLoginModuleList();
84         authenticationMap.put(CoreConstants.LOGIN_MODULES, loginModules);
85
86         // Application name
87
String JavaDoc appName = "test";
88         JGuardConfiguration jGuardConf = new JGuardConfiguration();
89         ConfigurationHelper.addConfigurationEntryForWebapp(jGuardConf,appName,authenticationMap,false);
90         //JGuardConfiguration config = (JGuardConfiguration) Configuration.getConfiguration();
91
AppConfigurationEntry JavaDoc[] entries = jGuardConf.getAppConfigurationEntry("test");
92
93         // check that both loginModules have been inserted in the configuration corresponding
94
// to the application called 'test'
95
assertEquals(entries.length, 2);
96
97         // XML Mock Module
98
AppConfigurationEntry JavaDoc xmlEntry = entries[0];
99
100         // Values for login module settings
101
assertEquals(xmlEntry.getLoginModuleName(), "net.sf.jguard.authentication.loginmodules.XmlLoginModule");
102         assertEquals(xmlEntry.getControlFlag(), LoginModuleControlFlag.REQUIRED);
103         Map JavaDoc xmlEntryOptions = xmlEntry.getOptions();
104         assertEquals( (String JavaDoc) xmlEntryOptions.get("debug"), "false");
105         assertEquals( (String JavaDoc) xmlEntryOptions.get("fileLocation"), "WEB-INF/conf/jGuard/jGuardUsersPrincipals.xml");
106         assertEquals( (String JavaDoc) xmlEntryOptions.get("authenticationManager"), "net.sf.jguard.authentication.XmlAuthenticationManager");
107
108
109         // PostgreSQL Mock Module
110
AppConfigurationEntry JavaDoc pgEntry = entries[1];
111
112         // Values for login module settings
113
assertEquals(pgEntry.getLoginModuleName(), "net.sf.jguard.authentication.loginmodules.PostgreSQLLoginModule");
114         assertEquals(pgEntry.getControlFlag(), LoginModuleControlFlag.OPTIONAL);
115         Map JavaDoc pgEntryOptions = pgEntry.getOptions();
116         assertEquals( (String JavaDoc) pgEntryOptions.get("authenticationUrl"), "jdbc:postgresql://127.0.0.1:5434/test");
117         assertEquals( (String JavaDoc) pgEntryOptions.get("authenticationLogin"), "testLogin");
118         assertEquals( (String JavaDoc) pgEntryOptions.get("authenticationPassword"), "testPassword");
119         assertEquals( (String JavaDoc) pgEntryOptions.get("authenticationDriver"), "org.postgresql.Driver");
120
121     }
122
123 }
124
Popular Tags