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.remembermanagers;
010:
011: import com.uwyn.rife.authentication.RememberManager;
012: import com.uwyn.rife.authentication.elements.exceptions.UnknownRememberManagerFactoryClassException;
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 TestCustomRememberManager extends TestCase {
019: private HierarchicalProperties mProperties = null;
020:
021: /**
022: * Custom remember manager factory. Returns instances of our custom remember
023: * manager.
024: */
025: public static class Factory implements RememberManagerFactory {
026: public RememberManager getRememberManager(
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 CustomRememberManager(id);
036: }
037: }
038:
039: public TestCustomRememberManager(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: RememberManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
049: Factory.class.getName());
050: mProperties.put("custom_id", "x");
051: }
052:
053: public void testInstantiation() {
054: RememberManagerFactory factory = RememberManagerFactoryFactory
055: .getInstance(mProperties);
056:
057: assertNotNull(factory);
058: assertTrue(factory instanceof Factory);
059: }
060:
061: public void testRememberManagerInstantiation() {
062: RememberManager manager = RememberManagerFactoryFactory
063: .getManager(mProperties);
064:
065: assertNotNull(manager);
066: assertTrue(manager instanceof CustomRememberManager);
067: assertEquals("x", ((CustomRememberManager) manager).getId());
068: }
069:
070: public void testExceptionOnMissingClass() throws Exception {
071: try {
072: RememberManagerFactoryFactory
073: .getInstance(new HierarchicalProperties());
074: fail("Didn't get expected exception");
075: } catch (MandatoryPropertyMissingException e) {
076: assertEquals(
077: RememberManagerFactoryFactory.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: RememberManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
087: "bad.class.name");
088: try {
089: RememberManagerFactoryFactory.getInstance(properties);
090: fail("Didn't get expected exception");
091: } catch (UnknownRememberManagerFactoryClassException e) {
092: assertEquals("bad.class.name", e.getFactoryClassName());
093: }
094: }
095:
096: public void testExceptionOnMissingFactoryParameter()
097: throws Exception {
098: HierarchicalProperties properties = new HierarchicalProperties();
099: properties
100: .put(
101: RememberManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
102: Factory.class.getName());
103: try {
104: RememberManagerFactoryFactory.getManager(properties);
105: fail("Didn't get expected exception");
106: } catch (MandatoryPropertyMissingException e) {
107: assertEquals("custom_id", e.getPropertyName());
108: }
109: }
110: }
|