Revision: 1330
Author:   mathiasbr
Date:     2006-08-22 00:15:22 -0700 (Tue, 22 Aug 2006)
ViewCVS:  http://svn.sourceforge.net/spring-rich-c/?rev=1330&view=rev

Log Message:
-----------
support rules for abstract types

Modified Paths:
--------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/support/DefaultRulesSource.java

Added Paths:
-----------
    
trunk/spring-richclient/support/src/test/java/org/springframework/rules/support/
    
trunk/spring-richclient/support/src/test/java/org/springframework/rules/support/DefaultRulesSourceTests.java
Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/support/DefaultRulesSource.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/support/DefaultRulesSource.java
     2006-08-21 19:49:55 UTC (rev 1329)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/support/DefaultRulesSource.java
     2006-08-22 07:15:22 UTC (rev 1330)
@@ -23,99 +23,103 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.core.style.ToStringCreator;
+import org.springframework.richclient.util.ClassUtils;
 import org.springframework.rules.Rules;
 import org.springframework.rules.RulesSource;
 import org.springframework.rules.constraint.ConstraintsAccessor;
 import org.springframework.rules.constraint.property.PropertyConstraint;
+import org.springframework.util.Assert;
+import org.springframework.util.CachingMapDecorator;
 import org.springframework.util.StringUtils;
 
 /**
- * A default rules source implementation which is simply a in-memory registry
- * for bean validation rules backed by a map.
+ * A default rules source implementation which is simply a in-memory registry 
for bean validation rules backed by a map.
  * 
  * @author Keith Donald
  */
 public class DefaultRulesSource extends ConstraintsAccessor implements 
RulesSource {
-       protected final Log logger = LogFactory.getLog(getClass());
+    protected final Log logger = LogFactory.getLog(getClass());
 
-       private static final String DEFAULT_CONTEXT_ID = "default";
+    private static final String DEFAULT_CONTEXT_ID = "default";
 
-       private Map ruleContexts = new HashMap();
+    private Map ruleContexts = new CachingMapDecorator() {
+        protected Object create(Object key) {
+            return new HashMap();
+        }
+    };
 
-       /**
-        * Add or update the rules for a single bean class.
-        * 
-        * @param rules
-        *            The rules.
-        */
-       public void addRules(Rules rules) {
-               if (logger.isDebugEnabled()) {
-                       logger.debug("Adding rules -> " + rules);
-               }
-               addRules(DEFAULT_CONTEXT_ID, rules);
-       }
+    /**
+     * Add or update the rules for a single bean class.
+     * 
+     * @param rules
+     *            The rules.
+     */
+    public void addRules(Rules rules) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Adding rules -> " + rules);
+        }
+        addRules(DEFAULT_CONTEXT_ID, rules);
+    }
 
-       public void addRules(String contextId, Rules rules) {
-               Map context = getOrCreateRuleContext(contextId);
-               context.put(rules.getDomainObjectType(), rules);
-       }
+    public void addRules(String contextId, Rules rules) {
+        Assert.notNull(contextId);
+        Assert.notNull(rules);
+        Map context = getRuleContext(contextId);
+        context.put(rules.getDomainObjectType(), rules);
+    }
 
-       private Map getOrCreateRuleContext(String contextId) {
-               Map context = (Map)this.ruleContexts.get(contextId);
-               if (context == null) {
-                       context = new HashMap();
-                       this.ruleContexts.put(contextId, context);
-               }
-               return context;
-       }
+    private Map getRuleContext(String contextId) {
+        return (Map) ruleContexts.get(contextId);
+    }
 
-       /**
-        * Set the list of rules retrievable by this source, where each item in 
the
-        * list is a <code>Rules</code> object which maintains validation rules
-        * for a bean class.
-        * 
-        * @param rules
-        *            The list of rules.
-        */
-       public void setRules(List rules) {
-               if (logger.isDebugEnabled()) {
-                       logger.debug("Configuring rules in source...");
-               }
-               getOrCreateRuleContext(DEFAULT_CONTEXT_ID).clear();
-               for (Iterator i = rules.iterator(); i.hasNext();) {
-                       addRules((Rules)i.next());
-               }
-       }
+    /**
+     * Set the list of rules retrievable by this source, where each item in 
the list is a <code>Rules</code> object
+     * which maintains validation rules for a bean class.
+     * 
+     * @param rules
+     *            The list of rules.
+     */
+    public void setRules(List rules) {
+        Assert.notNull(rules);
+        if (logger.isDebugEnabled()) {
+            logger.debug("Configuring rules in source...");
+        }
+        getRuleContext(DEFAULT_CONTEXT_ID).clear();
+        for (Iterator i = rules.iterator(); i.hasNext();) {
+            addRules((Rules) i.next());
+        }
+    }
 
-       public Rules getRules(Class bean) {
-               return getRules(bean, DEFAULT_CONTEXT_ID);
-       }
+    public Rules getRules(Class bean) {
+        return getRules(bean, DEFAULT_CONTEXT_ID);
+    }
 
-       public Rules getRules(Class bean, String contextId) {
-               if (!StringUtils.hasText(contextId)) {
-                       contextId = DEFAULT_CONTEXT_ID;
-               }
-               return (Rules)getOrCreateRuleContext(contextId).get(bean);
-       }
+    public Rules getRules(Class beanType, String contextId) {
+        Assert.notNull(beanType);
+        if (!StringUtils.hasText(contextId)) {
+            contextId = DEFAULT_CONTEXT_ID;
+        }
+        return (Rules) ClassUtils.getValueFromMapForClass(beanType, 
getRuleContext(contextId));
+    }
 
-       public PropertyConstraint getPropertyConstraint(Class bean, String 
propertyName) {
-               return getPropertyConstraint(bean, propertyName, 
DEFAULT_CONTEXT_ID);
-       }
+    public PropertyConstraint getPropertyConstraint(Class bean, String 
propertyName) {
+        return getPropertyConstraint(bean, propertyName, DEFAULT_CONTEXT_ID);
+    }
 
-       public PropertyConstraint getPropertyConstraint(Class bean, String 
propertyName, String contextId) {
-               if (logger.isDebugEnabled()) {
-                       logger.debug("Retrieving rules for bean '" + bean + "', 
context = " + contextId + ", property '" + propertyName
-                                       + "'");
-               }
-               Rules rules = getRules(bean, contextId);
-               if (rules != null)
-                       return rules.getPropertyConstraint(propertyName);
+    public PropertyConstraint getPropertyConstraint(Class bean, String 
propertyName, String contextId) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Retrieving rules for bean '" + bean + "', context = 
" + contextId + ", property '"
+                    + propertyName + "'");
+        }
+        Rules rules = getRules(bean, contextId);
+        if (rules != null)
+            return rules.getPropertyConstraint(propertyName);
 
         return null;
-       }
+    }
 
-       public String toString() {
-               return new ToStringCreator(this).append("rules", 
ruleContexts).toString();
-       }
+    public String toString() {
+        return new ToStringCreator(this).append("rules", 
ruleContexts).toString();
+    }
 
 }
\ No newline at end of file

Added: 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/support/DefaultRulesSourceTests.java
===================================================================
--- 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/support/DefaultRulesSourceTests.java
                                (rev 0)
+++ 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/support/DefaultRulesSourceTests.java
        2006-08-22 07:15:22 UTC (rev 1330)
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2002-2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy
+ * of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package org.springframework.rules.support;
+
+import junit.framework.TestCase;
+
+import org.springframework.rules.Rules;
+
+/**
+ * @author Mathias Broekelmann
+ * 
+ */
+public class DefaultRulesSourceTests extends TestCase {
+
+    private DefaultRulesSource source;
+
+    private Rules interfaceRules;
+
+    protected void setUp() throws Exception {
+        source = new DefaultRulesSource();
+        interfaceRules = new Rules(TestInterface.class);
+    }
+
+    protected void tearDown() throws Exception {
+        source = null;
+        interfaceRules = null;
+    }
+
+    public void testInterfaceRules() {
+        source.addRules(interfaceRules);
+        assertEquals(interfaceRules, source.getRules(TestInterfaceImpl.class));
+    }
+
+    private static interface TestInterface {
+    }
+
+    private static class TestInterfaceImpl implements TestInterface {
+    }
+
+}


Property changes on: 
trunk/spring-richclient/support/src/test/java/org/springframework/rules/support/DefaultRulesSourceTests.java
___________________________________________________________________
Name: svn:keywords
   + URL Author Revision Date
Name: svn:eol-style
   + native


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to