001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * Steven Grimm <koreth[remove] at midwinter dot com>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id$
008: */
009: package com.uwyn.rife.authentication.credentialsmanagers;
010:
011: import com.uwyn.rife.authentication.CredentialsManager;
012: import com.uwyn.rife.authentication.elements.exceptions.UnknownCredentialsManagerFactoryClassException;
013: import com.uwyn.rife.ioc.HierarchicalProperties;
014: import com.uwyn.rife.ioc.exceptions.MandatoryPropertyMissingException;
015: import com.uwyn.rife.ioc.exceptions.PropertyValueException;
016: import junit.framework.TestCase;
017:
018: public class TestCustomCredentialsManager extends TestCase {
019: private HierarchicalProperties mProperties;
020:
021: /**
022: * Custom credentials manager factory. Returns instances of our custom
023: * credentials manager.
024: */
025: public static class Factory implements CredentialsManagerFactory {
026: public CredentialsManager getCredentialsManager(
027: HierarchicalProperties properties)
028: throws PropertyValueException {
029: String id = properties.getValueTyped("custom_id",
030: String.class);
031: if (null == id || id.length() == 0) {
032: throw new MandatoryPropertyMissingException("custom_id");
033: }
034:
035: return new CustomCredentialsManager(id);
036: }
037: }
038:
039: public TestCustomCredentialsManager(String name) {
040: super (name);
041: }
042:
043: public void setUp() throws Exception {
044: super .setUp();
045: mProperties = new HierarchicalProperties();
046: mProperties
047: .put(
048: CredentialsManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
049: Factory.class.getName());
050: mProperties.put("custom_id", "x");
051: }
052:
053: public void testInstantiation() {
054: CredentialsManagerFactory factory = CredentialsManagerFactoryFactory
055: .getInstance(mProperties);
056:
057: assertNotNull(factory);
058: assertTrue(factory instanceof Factory);
059: }
060:
061: public void testCredentialsManagerInstantiation() {
062: CredentialsManager manager = CredentialsManagerFactoryFactory
063: .getManager(mProperties);
064:
065: assertNotNull(manager);
066: assertTrue(manager instanceof CustomCredentialsManager);
067: assertEquals("x", ((CustomCredentialsManager) manager).getId());
068: }
069:
070: public void testExceptionOnMissingClass() throws Exception {
071: try {
072: CredentialsManagerFactoryFactory
073: .getInstance(new HierarchicalProperties());
074: fail("Didn't get expected exception");
075: } catch (MandatoryPropertyMissingException e) {
076: assertEquals(
077: CredentialsManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
078: e.getPropertyName());
079: }
080: }
081:
082: public void testExceptionOnBogusClass() throws Exception {
083: HierarchicalProperties properties = new HierarchicalProperties();
084: properties
085: .put(
086: CredentialsManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
087: "bad.class.name");
088: try {
089: CredentialsManagerFactoryFactory.getInstance(properties);
090: fail("Didn't get expected exception");
091: } catch (UnknownCredentialsManagerFactoryClassException e) {
092: assertEquals("bad.class.name", e.getManagerClassName());
093: }
094: }
095:
096: public void testExceptionOnMissingFactoryParameter()
097: throws Exception {
098: HierarchicalProperties properties = new HierarchicalProperties();
099: properties
100: .put(
101: CredentialsManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
102: Factory.class.getName());
103: try {
104: CredentialsManagerFactoryFactory.getManager(properties);
105: fail("Didn't get expected exception");
106: } catch (MandatoryPropertyMissingException e) {
107: assertEquals("custom_id", e.getPropertyName());
108: }
109: }
110: }
|