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: TestMemorySessions.java 3308 2006-06-15 18:54:14Z gbevin $
008: */
009: package com.uwyn.rife.authentication.sessionvalidators;
010:
011: import com.uwyn.rife.authentication.SessionAttributes;
012: import com.uwyn.rife.authentication.SessionValidator;
013: import com.uwyn.rife.authentication.elements.exceptions.UnknownSessionValidatorFactoryClassException;
014: import com.uwyn.rife.ioc.HierarchicalProperties;
015: import com.uwyn.rife.ioc.exceptions.MandatoryPropertyMissingException;
016: import com.uwyn.rife.ioc.exceptions.PropertyValueException;
017: import junit.framework.TestCase;
018:
019: public class TestCustomSessionValidator extends TestCase {
020: private HierarchicalProperties mProperties = null;
021:
022: /**
023: * Dummy session validator class; this is just a mock we can use to test
024: * that our factory is being used.
025: */
026: public static class CustomSessionValidator extends
027: AbstractSessionValidator {
028: private String mId;
029:
030: public CustomSessionValidator(String id) {
031: mId = id;
032: }
033:
034: public String getId() {
035: return mId;
036: }
037:
038: public boolean isAccessAuthorized(int id) {
039: return false;
040: }
041:
042: public int validateSession(String authId, String hostIp,
043: SessionAttributes attributes) {
044: return 0;
045: }
046: }
047:
048: /**
049: * Custom session validator factory class. Returns instances of our custom
050: * session validator, above.
051: */
052: public static class CustomValidatorFactory implements
053: SessionValidatorFactory {
054: public SessionValidator getValidator(
055: HierarchicalProperties properties)
056: throws PropertyValueException {
057: String id = properties.getValueTyped("custom_id",
058: String.class);
059:
060: if (null == id || id.length() == 0) {
061: throw new MandatoryPropertyMissingException("custom_id");
062: }
063:
064: return new CustomSessionValidator(id);
065: }
066: }
067:
068: public TestCustomSessionValidator(String name) {
069: super (name);
070: }
071:
072: public void setUp() throws Exception {
073: super .setUp();
074: mProperties = new HierarchicalProperties();
075: mProperties
076: .put(
077: SessionValidatorFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
078: CustomValidatorFactory.class.getName());
079: mProperties.put("custom_id", "x");
080: }
081:
082: public void testInstantiation() {
083: SessionValidatorFactory factory = SessionValidatorFactoryFactory
084: .getInstance(mProperties);
085:
086: assertNotNull(factory);
087: assertTrue(factory instanceof CustomValidatorFactory);
088: }
089:
090: public void testSessionValidatorInstantiation() {
091: SessionValidator validator = SessionValidatorFactoryFactory
092: .getValidator(mProperties);
093:
094: assertNotNull(validator);
095: assertTrue(validator instanceof CustomSessionValidator);
096: assertEquals("x", ((CustomSessionValidator) validator).getId());
097: }
098:
099: public void testExceptionOnMissingClass() throws Exception {
100: try {
101: SessionValidatorFactoryFactory
102: .getInstance(new HierarchicalProperties());
103: fail("Didn't get expected exception");
104: } catch (MandatoryPropertyMissingException e) {
105: assertEquals(
106: SessionValidatorFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
107: e.getPropertyName());
108: }
109: }
110:
111: public void testExceptionOnBogusClass() throws Exception {
112: HierarchicalProperties properties = new HierarchicalProperties();
113: properties
114: .put(
115: SessionValidatorFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
116: "bad.class.name");
117: try {
118: SessionValidatorFactoryFactory.getInstance(properties);
119: fail("Didn't get expected exception");
120: } catch (UnknownSessionValidatorFactoryClassException e) {
121: assertEquals(e.getValidatorClassName(), "bad.class.name");
122: }
123: }
124:
125: public void testExceptionOnMissingFactoryParameter()
126: throws Exception {
127: HierarchicalProperties properties = new HierarchicalProperties();
128: properties
129: .put(
130: SessionValidatorFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
131: CustomValidatorFactory.class.getName());
132: try {
133: SessionValidatorFactoryFactory.getValidator(properties);
134: fail("Didn't get expected exception");
135: } catch (MandatoryPropertyMissingException e) {
136: assertEquals("custom_id", e.getPropertyName());
137: }
138: }
139: }
|