Author: norman
Date: Tue Dec 19 08:33:24 2006
New Revision: 488724

URL: http://svn.apache.org/viewvc?view=rev&rev=488724
Log:
Modify VUT implementations to recursive map addresses. See JAMES-741
Modify junit tests for VUT to use one abstract test class
Fix a bug in domainList implementation. Domains were sometimes added even if 
they allready exists
Throw an exception on invalid regex in storage

Modified:
    james/server/trunk/src/java/org/apache/james/util/DomainListUtil.java
    james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java
    
james/server/trunk/src/java/org/apache/james/vut/AbstractVirtualUserTable.java
    james/server/trunk/src/java/org/apache/james/vut/JDBCVirtualUserTable.java
    james/server/trunk/src/java/org/apache/james/vut/XMLVirtualUserTable.java
    
james/server/trunk/src/test/org/apache/james/vut/AbstractVirtualUserTableTest.java
    
james/server/trunk/src/test/org/apache/james/vut/JDBCVirtualUserTableTest.java
    
james/server/trunk/src/test/org/apache/james/vut/XMLVirtualUserTableTest.java

Modified: james/server/trunk/src/java/org/apache/james/util/DomainListUtil.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/DomainListUtil.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/DomainListUtil.java 
(original)
+++ james/server/trunk/src/java/org/apache/james/util/DomainListUtil.java Tue 
Dec 19 08:33:24 2006
@@ -47,7 +47,7 @@
         if (domains.size() > 0 ) {
             for (int i = 0; i < domains.size(); i++) {
                 List domList = getDomainIP(domains.get(i).toString(),dns,log);
-            
+                
                 for(int i2 = 0; i2 < domList.size();i2++) {
                     if(domainIP.contains(domList.get(i2)) == false) {
                         domainIP.add(domList.get(i2));
@@ -66,7 +66,10 @@
         try {
             InetAddress[]  addrs = dns.getAllByName(domain);
             for (int j = 0; j < addrs.length ; j++) {
-                domainIP.add(addrs[j].getHostAddress());
+                String ip = addrs[j].getHostAddress();
+                if (domainIP.contains(ip) == false) {
+                    domainIP.add(ip);
+                }
             }
         } catch (UnknownHostException e) {
             log.error("Cannot get IP address(es) for " + domain);

Modified: 
james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java 
(original)
+++ james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java 
Tue Dec 19 08:33:24 2006
@@ -28,6 +28,7 @@
 import java.util.Map;
 import java.util.StringTokenizer;
 
+import org.apache.james.services.VirtualUserTable;
 import org.apache.mailet.MailAddress;
 import org.apache.oro.text.regex.MalformedPatternException;
 import org.apache.oro.text.regex.MatchResult;
@@ -44,6 +45,7 @@
     
 
     public static String QUERY = "select VirtualUserTable.target_address from 
VirtualUserTable, VirtualUserTable as VUTDomains where (VirtualUserTable.user 
like ? or VirtualUserTable.user like '\\%') and (VirtualUserTable.domain like ? 
or (VirtualUserTable.domain like '\\%' and VUTDomains.domain like ?)) order by 
concat(VirtualUserTable.user,'@',VirtualUserTable.domain) desc limit 1";
+    
     /**
      * Processes regex virtual user mapping
      *
@@ -57,18 +59,20 @@
      */
      public static String regexMap( MailAddress address, String targetString) 
throws MalformedPatternException {
         String result = null;
+        int identifierLength = VirtualUserTable.REGEX_PREFIX.length();
 
+        int msgPos = targetString.indexOf(':', identifierLength + 1);
 
-        //TODO: Throw exception on invalid syntax ?
-        int msgPos = targetString.indexOf(':', "regex:".length() + 1);
-
+        // Throw exception on invalid format
+        if (msgPos < identifierLength + 1) throw new 
MalformedPatternException("Regex should be formatted as 
regex:<regular-expression>:<parameterized-string>");
+        
         // log("regex: targetString = " + targetString);
         // log("regex: msgPos = " + msgPos);
         // log("regex: compile " + targetString.substring("regex:".length(), 
msgPos));
         // log("regex: address = " + address.toString());
         // log("regex: replace = " + targetString.substring(msgPos + 1));
 
-        Pattern pattern = new 
Perl5Compiler().compile(targetString.substring("regex:".length(), msgPos));
+        Pattern pattern = new 
Perl5Compiler().compile(targetString.substring(identifierLength, msgPos));
         Perl5Matcher matcher = new Perl5Matcher();
 
         if (matcher.matches(address.toString(), pattern)) {
@@ -125,7 +129,7 @@
       */
      public static String getSeparator(String targetString) {
         return (targetString.indexOf(',') > -1 ? "," : (targetString
-        .indexOf(';') > -1 ? ";" : ((targetString.indexOf("regex:") > -1 || 
targetString.indexOf("error:") > -1)? "" : ":")));
+        .indexOf(';') > -1 ? ";" : 
((targetString.indexOf(VirtualUserTable.ERROR_PREFIX) > -1 || 
targetString.indexOf(VirtualUserTable.REGEX_PREFIX) > -1)? "" : ":")));
      }
      
      /**

Modified: 
james/server/trunk/src/java/org/apache/james/vut/AbstractVirtualUserTable.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/vut/AbstractVirtualUserTable.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/vut/AbstractVirtualUserTable.java 
(original)
+++ 
james/server/trunk/src/java/org/apache/james/vut/AbstractVirtualUserTable.java 
Tue Dec 19 08:33:24 2006
@@ -31,6 +31,9 @@
 
 import javax.mail.internet.ParseException;
 
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
@@ -49,11 +52,17 @@
  * 
  */
 public abstract class AbstractVirtualUserTable extends AbstractLogEnabled
-    implements VirtualUserTable, VirtualUserTableManagement, DomainList, 
Serviceable {
+    implements VirtualUserTable, VirtualUserTableManagement, DomainList, 
Serviceable, Configurable {
     
     private boolean autoDetect = true;
     private boolean autoDetectIP = true;
     private DNSServer dns;
+    
+    // The maximum mappings which will process before throwing exception
+    private int mappingLimit = 10;
+       
+    // TODO: Should we use true or false as default ?
+    private boolean recursive = true;
 
     /**
      * @see 
org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
@@ -64,9 +73,42 @@
     
     
     /**
+     * @see 
org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
+    public void configure(Configuration arg0) throws ConfigurationException {
+        Configuration recursiveConf = arg0.getChild("recursiveMapping", false);
+
+        if (recursiveConf != null) {
+            setRecursiveMapping(recursiveConf.getValueAsBoolean(true));
+        }
+        
+        Configuration mappingLimitConf = arg0.getChild("mappingLimit", false);
+        
+        if (mappingLimitConf != null )  {
+            setMappingLimit(mappingLimitConf.getValueAsInteger(10));
+        }
+    }
+    
+    public void setRecursiveMapping(boolean recursive) {
+        this.recursive = recursive;
+    }
+    
+    public void setMappingLimit(int mappingLimit) {
+        this.mappingLimit = mappingLimit;
+    }
+    
+    /**
      * @see org.apache.james.services.VirtualUserTable#getMappings(String, 
String)
      */
     public Collection getMappings(String user,String domain) throws 
ErrorMappingException {
+        return getMappings(user,domain,mappingLimit);
+    }
+    
+
+    public Collection getMappings(String user,String domain,int mappingLimit) 
throws ErrorMappingException {
+
+        // We have to much mappings throw ErrorMappingException to avoid 
infinity loop
+        if (mappingLimit == 0) throw new ErrorMappingException("554 Too many 
mappings to process");
 
         String targetString = mapAddress(user, domain);
         
@@ -77,15 +119,14 @@
                 throw new 
ErrorMappingException(targetString.substring(VirtualUserTable.ERROR_PREFIX.length()));
 
             } else {
-                Iterator map= 
VirtualUserTableUtil.getMappings(targetString).iterator();
+                Iterator map = 
VirtualUserTableUtil.getMappings(targetString).iterator();
 
                 while (map.hasNext()) {
-                    String target;
-                    String targetAddress = map.next().toString();
+                    String target = map.next().toString();
 
-                    if 
(targetAddress.startsWith(VirtualUserTable.REGEX_PREFIX)) {
+                    if (target.startsWith(VirtualUserTable.REGEX_PREFIX)) {
                         try {
-                            targetAddress = VirtualUserTableUtil.regexMap(new 
MailAddress(user,domain), targetAddress);
+                            target = VirtualUserTableUtil.regexMap(new 
MailAddress(user,domain), target);
                         } catch (MalformedPatternException e) {
                             getLogger().error("Exception during regexMap 
processing: ", e);
                         } catch (ParseException e) {
@@ -93,30 +134,51 @@
                             getLogger().error("Exception during regexMap 
processing: ", e);
                         } 
 
-                        if (targetAddress == null) continue;
-                    }
-                    
-                    /* The VirtualUserTable not know anything about the 
defaultDomain. The defaultDomain should be added by the service which use 
-                     * the VirtualUserTable
-                     * 
-                    if (targetAddress.indexOf('@') < 0) {
-                         target = targetAddress + "@localhost";
-                    } else {
-                        target = targetAddress;
+                        if (target == null) continue;
                     }
-                    */
-                    
-                    target = targetAddress;
-            
-                    // add mapping
-                    mappings.add(target);
 
                     StringBuffer buf = new StringBuffer().append("Valid 
virtual user mapping ")
                                                          
.append(user).append("@").append(domain)
-                                                         .append(" to 
").append(targetAddress);
+                                                         .append(" to 
").append(target);
                     getLogger().debug(buf.toString());
-
-                 }
+                   
+                 
+                    if (recursive) {
+                    
+                        String userName = null;
+                        String domainName = null;
+                        String args[] = target.split("@");
+                                        
+                        if (args != null && args.length > 0) {
+                    
+                            userName = args[0];
+                            domainName = args[1];
+                        } else {
+                            // TODO Is that the right todo here?
+                            userName = target;
+                            domainName = domain;
+                        }
+                                        
+                        // Check if the returned mapping is the same as the 
input. If so return null to avoid loops
+                        if (userName.equalsIgnoreCase(user) && 
domainName.equalsIgnoreCase(domain)) {
+                            return null;
+                        }
+                                        
+                        Collection childMappings = getMappings(userName, 
domainName, mappingLimit -1);
+                    
+                        if (childMappings == null) {
+                             // add mapping
+                            mappings.add(target);
+                        } else {
+                            mappings.addAll(childMappings);         
+                        }
+                                        
+                    } else {
+                        mappings.add(target);
+                    }
+               
+                }
+                
             }
             return mappings;
         }
@@ -143,7 +205,6 @@
 
     
     /**
-     * @throws InvalidMappingException 
      * @see 
org.apache.james.services.VirtualUserTableManagement#removeRegexMapping(java.lang.String,
 java.lang.String, java.lang.String)
      */
     public synchronized boolean removeRegexMapping(String user, String domain, 
String regex) throws InvalidMappingException {
@@ -274,13 +335,20 @@
             
             getLogger().info("Local host is: " + hostName);
             
-            if (autoDetect == true && (!hostName.equals("localhost"))) {
-                domains.add(hostName.toLowerCase(Locale.US));
+            hostName = hostName.toLowerCase(Locale.US);
+            
+            if (autoDetect == true && hostName.equals("localhost") == false && 
domains.contains(hostName) == false) {
+                domains.add(hostName);
             }
 
             
             if (autoDetectIP == true) {
-                
domains.addAll(DomainListUtil.getDomainsIP(domains,dns,getLogger()));
+                List ipList = 
DomainListUtil.getDomainsIP(domains,dns,getLogger());
+                for(int i = 0; i < ipList.size(); i++) {
+                    if (domains.contains(ipList.get(i)) == false) {
+                        domains.add(ipList.get(i));
+                    }
+                }
             }
        
             if (getLogger().isInfoEnabled()) {

Modified: 
james/server/trunk/src/java/org/apache/james/vut/JDBCVirtualUserTable.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/vut/JDBCVirtualUserTable.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- james/server/trunk/src/java/org/apache/james/vut/JDBCVirtualUserTable.java 
(original)
+++ james/server/trunk/src/java/org/apache/james/vut/JDBCVirtualUserTable.java 
Tue Dec 19 08:33:24 2006
@@ -89,6 +89,7 @@
      * @see 
org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
      */
     public void configure(Configuration arg0) throws ConfigurationException {
+        super.configure(arg0);
         String destination = arg0.getAttribute("destinationURL",null);
     
         if (destination == null) {

Modified: 
james/server/trunk/src/java/org/apache/james/vut/XMLVirtualUserTable.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/vut/XMLVirtualUserTable.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- james/server/trunk/src/java/org/apache/james/vut/XMLVirtualUserTable.java 
(original)
+++ james/server/trunk/src/java/org/apache/james/vut/XMLVirtualUserTable.java 
Tue Dec 19 08:33:24 2006
@@ -37,15 +37,22 @@
     /**
      * Holds the configured mappings
      */
-    private Map mappings = new HashMap();
+    private Map mappings;
     
-    private List domains = new ArrayList(); 
+    private List domains;
+    
+    private final static String WILDCARD = "*";
     
     /**
      * @see 
org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
      */
     public void configure(Configuration arg0) throws ConfigurationException {
+        super.configure(arg0);
         Configuration[] mapConf = arg0.getChildren("mapping");
+    
+        mappings = new HashMap();
+        domains = new ArrayList();
+        
         if (mapConf != null) {
             for (int i = 0; i < mapConf.length; i ++) {       
                 
mappings.putAll(VirtualUserTableUtil.getXMLMappings(mapConf[i].getValue()));
@@ -59,12 +66,11 @@
         
         while (keys.hasNext()) {
             String key = keys.next().toString();
-            Collection values = 
VirtualUserTableUtil.mappingToCollection(mappings.get(key).toString());
-            
+
             String[] args1 = key.split("@");
-            if (args1 != null && args1.length == 2) {
+            if (args1 != null && args1.length > 1) {
                 String domain = args1[1].toLowerCase();
-                if (domains.contains(domain) == false) {
+                if (domains.contains(domain) == false && 
domain.equals(WILDCARD) == false) {
                     domains.add(domain);
                 }
             }

Modified: 
james/server/trunk/src/test/org/apache/james/vut/AbstractVirtualUserTableTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/vut/AbstractVirtualUserTableTest.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/vut/AbstractVirtualUserTableTest.java
 (original)
+++ 
james/server/trunk/src/test/org/apache/james/vut/AbstractVirtualUserTableTest.java
 Tue Dec 19 08:33:24 2006
@@ -21,15 +21,23 @@
 
 package org.apache.james.vut;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.service.ServiceException;
+import org.apache.james.services.AbstractDNSServer;
+import org.apache.james.services.DNSServer;
 
 import junit.framework.TestCase;
 
 public abstract class AbstractVirtualUserTableTest extends TestCase {
 
     protected AbstractVirtualUserTable virtualUserTable;
+    protected final static int REGEX_TYPE = 0;
+    protected final static int ERROR_TYPE = 1;
+    protected final static int ADDRESS_TYPE = 2;
     
     protected void setUp() throws Exception {
         virtualUserTable = getVirtalUserTable();
@@ -42,9 +50,29 @@
     
     protected abstract AbstractVirtualUserTable getVirtalUserTable() throws 
ServiceException, ConfigurationException, Exception;
     
-
-    public void testStoreAndRetrieveRegexMapping() throws 
ErrorMappingException {
+    protected abstract boolean addMapping(String user , String domain, String 
mapping,int type)throws InvalidMappingException;
+    
+    protected abstract boolean removeMapping(String user, String domain, 
String mapping, int type) throws InvalidMappingException;
+    
+ 
+    protected DNSServer setUpDNSServer() {
+        DNSServer dns = new AbstractDNSServer() {
+            public String getHostName(InetAddress inet) {
+                return "test";
+            }
+            
+            public InetAddress[] getAllByName(String name) throws 
UnknownHostException {
+                return new InetAddress[] { 
InetAddress.getByName("127.0.0.1")};        
+            }
+            
+            public InetAddress getLocalHost() throws UnknownHostException {
+                return InetAddress.getLocalHost();
+            }
+        };
+        return dns;
+    }
     
+    public void testStoreAndRetrieveRegexMapping() throws 
ErrorMappingException {
         String user = "test";
         String domain = "localhost";
         String regex = "(.*):[EMAIL PROTECTED]";
@@ -55,17 +83,19 @@
         
             assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
         
-            assertTrue("Added virtual mapping", 
virtualUserTable.addRegexMapping(user, domain, regex));
-            assertTrue("Added virtual mapping", 
virtualUserTable.addRegexMapping(user, domain, regex2));
+            assertTrue("Added virtual mapping", addMapping(user, domain, 
regex, REGEX_TYPE));
+            assertTrue("Added virtual mapping", addMapping(user, domain, 
regex2, REGEX_TYPE));
 
             assertEquals("Two mappings",virtualUserTable.getMappings(user, 
domain).size(), 2);           
             assertEquals("One 
mappingline",virtualUserTable.getAllMappings().size(),1);
             
+            System.err.println("MAPPINGS:" + virtualUserTable.getAllMappings() 
+ " domains:" + virtualUserTable.getDomains().size()); 
+           
             // Test DomainList implementations!
-            assertEquals("Four domains",virtualUserTable.getDomains().size(), 
3);
+            assertEquals("Three domains",virtualUserTable.getDomains().size(), 
3);
             assertTrue("Contains 
Domain",virtualUserTable.containsDomain(domain));
             
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeRegexMapping(user, domain, regex));
+            assertTrue("remove virtual mapping", removeMapping(user, domain, 
regex, REGEX_TYPE));
         
             try {
                 assertTrue("Added virtual mapping", 
virtualUserTable.addRegexMapping(user, domain, invalidRegex));
@@ -74,8 +104,9 @@
             }
             assertTrue("Invalid Mapping throw exception" , catched);
 
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeRegexMapping(user, domain, regex2));
+            assertTrue("remove virtual mapping", removeMapping(user, domain, 
regex2, REGEX_TYPE));
             assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
+            
             assertNull("No mappings",virtualUserTable.getAllMappings());
             
         } catch (InvalidMappingException e) {
@@ -83,55 +114,60 @@
         }
     
     }
+    
 
     public void testStoreAndRetrieveAddressMapping() throws 
ErrorMappingException {
-    
         String user = "test";
         String domain = "localhost";
         String address = "[EMAIL PROTECTED]";
         String address2 = "[EMAIL PROTECTED]";
         String invalidAddress= "[EMAIL PROTECTED]:";
         boolean catched = false;
+        
         try {
         
             assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
         
-            assertTrue("Added virtual mapping", 
virtualUserTable.addAddressMapping(user, domain, address));
-            assertTrue("Added virtual mapping", 
virtualUserTable.addAddressMapping(user, domain, address2));
+            assertTrue("Added virtual mapping", addMapping(user, domain, 
address, ADDRESS_TYPE));
+            assertTrue("Added virtual mapping", addMapping(user, domain, 
address2, ADDRESS_TYPE));
 
             assertEquals("Two mappings",virtualUserTable.getMappings(user, 
domain).size(),2);
             assertEquals("One 
mappingline",virtualUserTable.getAllMappings().size(),1);
         
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeAddressMapping(user, domain, address));
+            assertTrue("remove virtual mapping", removeMapping(user, domain, 
address, ADDRESS_TYPE));
         
-            try {
-                assertTrue("Added virtual mapping", 
virtualUserTable.addAddressMapping(user, domain, invalidAddress));
-            } catch (InvalidMappingException e) {
-                catched = true;
-            }
-            assertTrue("Invalid Mapping throw exception" , catched);
-
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeAddressMapping(user, domain, address2));
+            if (virtualUserTable instanceof JDBCVirtualUserTable) {
+                try {
+                    assertTrue("Added virtual mapping", addMapping(user, 
domain, invalidAddress, ADDRESS_TYPE));
+                } catch (InvalidMappingException e) {
+                    catched = true;
+                } 
+                assertTrue("Invalid Mapping throw exception" , catched);
+            }         
+            
+            assertTrue("remove virtual mapping", removeMapping(user, domain, 
address2, ADDRESS_TYPE));
+            
             assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
             assertNull("No mappings",virtualUserTable.getAllMappings());
             
         } catch (InvalidMappingException e) {
             fail("Storing failed");
         }
+        
     
     }
 
-    public void testStoreAndRetrieveErrorMapping() throws 
ErrorMappingException {
-    
+    public void testStoreAndRetrieveErrorMapping() throws 
ErrorMappingException { 
         String user = "test";
         String domain = "localhost";
-        String error = "Bounce!";
+        String error = "bounce!";
         boolean catched = false;
+        
         try {
         
             assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
         
-            assertTrue("Added virtual mapping", 
virtualUserTable.addErrorMapping(user, domain, error));
+            assertTrue("Added virtual mapping", addMapping(user, domain, 
error, ERROR_TYPE));
             assertEquals("One 
mappingline",virtualUserTable.getAllMappings().size(),1);
 
             try {
@@ -141,7 +177,7 @@
             }
             assertTrue("Error Mapping throw exception" , catched);
 
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeErrorMapping(user, domain, error));
+            assertTrue("remove virtual mapping", removeMapping(user, domain, 
error, ERROR_TYPE));
             assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
             assertNull("No mappings",virtualUserTable.getAllMappings());
             
@@ -150,5 +186,69 @@
         }
 
     }
+    
+    public void testStoreAndRetrieveWildCardAddressMapping() throws 
ErrorMappingException {        
+        String user = "test";
+        String user2 = "test2";
+        String domain = "localhost";
+        String address = "[EMAIL PROTECTED]";
+        String address2 = "[EMAIL PROTECTED]";
+
+
+       try {
+                 
+            assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
+        
+            assertTrue("Added virtual mapping", addMapping(null, domain, 
address, ADDRESS_TYPE));
+            assertTrue("Added virtual mapping", addMapping(user, domain, 
address2, ADDRESS_TYPE));
 
+          
+            assertTrue("One mappings",virtualUserTable.getMappings(user, 
domain).size() == 1);
+            assertTrue("One mappings",virtualUserTable.getMappings(user2, 
domain).size() == 1);
+           
+            assertTrue("remove virtual mapping", removeMapping(user, domain, 
address2, ADDRESS_TYPE));
+            assertTrue("remove virtual mapping", removeMapping(null, domain, 
address, ADDRESS_TYPE));
+            assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
+            assertNull("No mapping",virtualUserTable.getMappings(user2, 
domain));
+      
+        } catch (InvalidMappingException e) {
+           fail("Storing failed");
+        }
+    
+    }
+    
+    public void testRecursiveMapping() throws ErrorMappingException {
+        String user1 = "user1";
+        String user2 = "user2";
+        String user3 = "user3";
+        String domain1 = "domain1";
+        String domain2 = "domain2";
+        String domain3 = "domain3";
+        boolean exception1 = false;
+    
+        virtualUserTable.setRecursiveMapping(true);
+       
+        try {
+            assertNull("No mappings",virtualUserTable.getAllMappings());
+     
+            assertTrue("Add mapping", addMapping(user1, domain1, user2 + "@" + 
domain2, ADDRESS_TYPE));
+            assertTrue("Add mapping", addMapping(user2, domain2, user3 + "@" + 
domain3, ADDRESS_TYPE));
+            assertEquals("Recursive mapped", 
virtualUserTable.getMappings(user1, domain1).iterator().next(),user3 + "@" + 
domain3);
+
+            assertTrue("Add mapping", addMapping(user3, domain3, user1 + "@" + 
domain1, ADDRESS_TYPE));
+            try {
+                virtualUserTable.getMappings(user1, domain1);
+            } catch (ErrorMappingException e) {
+                exception1 = true;
+            }
+            assertTrue("Exception thrown on to many mappings", exception1);
+            
+            // disable recursive mapping
+            virtualUserTable.setRecursiveMapping(false);
+            assertEquals("Not recursive mapped", 
virtualUserTable.getMappings(user1, domain1).iterator().next(),user2 + "@" + 
domain2);
+            
+        } catch (InvalidMappingException e) {
+            fail("Storing failed");
+        }
+    }
 }

Modified: 
james/server/trunk/src/test/org/apache/james/vut/JDBCVirtualUserTableTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/vut/JDBCVirtualUserTableTest.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/vut/JDBCVirtualUserTableTest.java 
(original)
+++ 
james/server/trunk/src/test/org/apache/james/vut/JDBCVirtualUserTableTest.java 
Tue Dec 19 08:33:24 2006
@@ -21,8 +21,6 @@
 
 package org.apache.james.vut;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Map;
@@ -33,7 +31,6 @@
 import org.apache.avalon.framework.service.DefaultServiceManager;
 import org.apache.avalon.framework.service.ServiceException;
 
-import org.apache.james.services.AbstractDNSServer;
 import org.apache.james.services.DNSServer;
 import org.apache.james.services.FileSystem;
 
@@ -45,7 +42,7 @@
 
 public class JDBCVirtualUserTableTest extends AbstractVirtualUserTableTest {
     
-    public void tearDown() {
+    public void tearDown() throws Exception {
         Map mappings = virtualUserTable.getAllMappings();
     
         if (mappings != null) {
@@ -69,8 +66,12 @@
                 }
             }
         }
+        super.tearDown();
     }
     
+    /**
+     * @see 
org.apache.james.vut.AbstractVirtualUserTableTest#getVirtalUserTable()
+     */
     protected AbstractVirtualUserTable getVirtalUserTable() throws 
ServiceException, ConfigurationException, Exception {
         DefaultServiceManager serviceManager = new DefaultServiceManager();
         serviceManager.put(FileSystem.ROLE, new MockFileSystem());
@@ -87,54 +88,36 @@
         mr.configure(defaultConfiguration);
         mr.initialize();
         return mr;
-    }
+    }    
     
-    private DNSServer setUpDNSServer() {
-        DNSServer dns = new AbstractDNSServer() {
-            public String getHostName(InetAddress inet) {
-                return "test";
-            }
-            
-            public InetAddress[] getAllByName(String name) throws 
UnknownHostException {
-                return new InetAddress[] { 
InetAddress.getByName("127.0.0.1")};        
-            }
-            
-            public InetAddress getLocalHost() throws UnknownHostException {
-                return InetAddress.getLocalHost();
-            }
-        };
-        return dns;
+    /**
+     * @see 
org.apache.james.vut.AbstractVirtualUserTableTest#addMapping(java.lang.String, 
java.lang.String, java.lang.String, int)
+     */
+    protected boolean addMapping(String user, String domain, String mapping, 
int type) throws InvalidMappingException {
+        if (type == ERROR_TYPE) {
+            return virtualUserTable.addErrorMapping(user, domain, mapping);
+        } else if (type == REGEX_TYPE) {
+            return virtualUserTable.addRegexMapping(user, domain, mapping);
+        } else if (type == ADDRESS_TYPE) {
+            return virtualUserTable.addAddressMapping(user, domain, mapping);
+        } else {
+            return false;
+        }
     }
-    
-    public void testStoreAndRetrieveWildCardAddressMapping() throws 
ErrorMappingException {
-        
-        String user = "test";
-        String user2 = "test2";
-        String domain = "localhost";
-        String address = "[EMAIL PROTECTED]";
-        String address2 = "[EMAIL PROTECTED]";
-
 
-       try {
-                 
-            assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
-        
-            assertTrue("Added virtual mapping", 
virtualUserTable.addAddressMapping(null, domain, address));
-            assertTrue("Added virtual mapping", 
virtualUserTable.addAddressMapping(user, domain, address2));
-
-          
-            assertTrue("One mappings",virtualUserTable.getMappings(user, 
domain).size() == 1);
-            assertTrue("One mappings",virtualUserTable.getMappings(user2, 
domain).size() == 1);
-           
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeAddressMapping(user, domain, address2));
-            assertTrue("remove virtual mapping", 
virtualUserTable.removeAddressMapping(null, domain, address));
-            assertNull("No mapping",virtualUserTable.getMappings(user, 
domain));
-            assertNull("No mapping",virtualUserTable.getMappings(user2, 
domain));
-      
-        } catch (InvalidMappingException e) {
-           fail("Storing failed");
+    /**
+     * @see 
org.apache.james.vut.AbstractVirtualUserTableTest#removeMapping(java.lang.String,
 java.lang.String, java.lang.String, int)
+     */
+    protected boolean removeMapping(String user, String domain, String 
mapping, int type) throws InvalidMappingException {
+        if (type == ERROR_TYPE) {
+            return virtualUserTable.removeErrorMapping(user, domain, mapping);
+        } else if (type == REGEX_TYPE) {
+            return virtualUserTable.removeRegexMapping(user, domain, mapping);
+        } else if (type == ADDRESS_TYPE) {
+            return virtualUserTable.removeAddressMapping(user, domain, 
mapping);
+        } else {
+            return false;
         }
-    
     }
     
 }

Modified: 
james/server/trunk/src/test/org/apache/james/vut/XMLVirtualUserTableTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/vut/XMLVirtualUserTableTest.java?view=diff&rev=488724&r1=488723&r2=488724
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/vut/XMLVirtualUserTableTest.java 
(original)
+++ 
james/server/trunk/src/test/org/apache/james/vut/XMLVirtualUserTableTest.java 
Tue Dec 19 08:33:24 2006
@@ -20,21 +20,21 @@
 
 package org.apache.james.vut;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
 
-import junit.framework.TestCase;
 
 import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector;
+import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.configuration.DefaultConfiguration;
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.service.DefaultServiceManager;
 import org.apache.avalon.framework.service.ServiceException;
 
-import org.apache.james.services.AbstractDNSServer;
 import org.apache.james.services.DNSServer;
 import org.apache.james.services.FileSystem;
+import org.apache.james.services.VirtualUserTable;
 
 import org.apache.james.test.mock.avalon.MockLogger;
 
@@ -42,58 +42,114 @@
 import org.apache.james.test.mock.util.AttrValConfiguration;
 
 import org.apache.james.test.util.Util;
+import org.apache.james.util.VirtualUserTableUtil;
 
-public class XMLVirtualUserTableTest extends TestCase {
-    private String user = "user1";
-    private String domain = "anydomain";
-    
-    AbstractVirtualUserTable table;
+public class XMLVirtualUserTableTest extends AbstractVirtualUserTableTest {
+    DefaultConfiguration defaultConfiguration = new 
DefaultConfiguration("conf");
     
     protected AbstractVirtualUserTable getVirtalUserTable() throws 
ServiceException, ConfigurationException, Exception {
         DefaultServiceManager serviceManager = new DefaultServiceManager();
         serviceManager.put(FileSystem.ROLE, new MockFileSystem());
         serviceManager.put(DataSourceSelector.ROLE, 
Util.getDataSourceSelector());
-        serviceManager.put(DNSServer.ROLE, new AbstractDNSServer() {
-            public InetAddress getLocalHost() throws UnknownHostException {
-                return InetAddress.getLocalHost();
-            }
-            
-            public InetAddress[] getAllByName(String domain) throws 
UnknownHostException{
-                throw new UnknownHostException();
-            }
-            
-            public String getHostName(InetAddress in) {
-                return "localHost";      
-            }
-        });
+        serviceManager.put(DNSServer.ROLE, setUpDNSServer());
         XMLVirtualUserTable mr = new XMLVirtualUserTable();
-        
+        ContainerUtil.enableLogging(mr, new MockLogger());
 
-        mr.enableLogging(new MockLogger());
-        DefaultConfiguration defaultConfiguration = new 
DefaultConfiguration("conf");
-        defaultConfiguration.addChild(new AttrValConfiguration("mapping",user 
+ "@" + domain +"[EMAIL PROTECTED];[EMAIL PROTECTED]"));
-        defaultConfiguration.addChild(new AttrValConfiguration("mapping","*" + 
"@" + domain +"[EMAIL PROTECTED];[EMAIL PROTECTED]"));
-        mr.configure(defaultConfiguration);
-        mr.service(serviceManager);
+        ContainerUtil.service(mr, serviceManager);
+        ContainerUtil.configure(mr, defaultConfiguration);
         return mr;
     }
     
-    public void setUp() throws Exception {
-        table = getVirtalUserTable();
-    }
     
-    public void tearDown() {
-        ContainerUtil.dispose(table);
+
+    /**
+     * @see 
org.apache.james.vut.AbstractVirtualUserTableTest#addMapping(java.lang.String, 
java.lang.String, java.lang.String, int)
+     */
+    protected boolean addMapping(String user, String domain, String mapping, 
int type) throws InvalidMappingException {
+        if (user == null) user = "*";
+        if (domain == null) domain = "*";
+        
+        Collection mappings = virtualUserTable.getUserDomainMappings(user, 
domain);
+
+        if (mappings == null) {
+            mappings = new ArrayList();
+        } else {
+           removeMappings(user,domain,mappings);
+        }
+    
+        if (type == ERROR_TYPE) {
+            mappings.add(VirtualUserTable.ERROR_PREFIX + mapping);
+        } else if (type == REGEX_TYPE) {
+            mappings.add(VirtualUserTable.REGEX_PREFIX + mapping);
+        } else if (type == ADDRESS_TYPE) {
+            mappings.add(mapping);
+        }
+        if (mappings.size() > 0) { 
+            defaultConfiguration.addChild(new 
AttrValConfiguration("mapping",user + "@" + domain +"=" + 
VirtualUserTableUtil.CollectionToMapping(mappings)));
+        }
+    
+        try {
+            ContainerUtil.configure(((XMLVirtualUserTable) 
virtualUserTable),defaultConfiguration);
+        } catch (ConfigurationException e) {
+            e.printStackTrace();
+            return false;
+        }
+        return true;
     }
-    
-    public void testGetMappings() throws ErrorMappingException {
-        assertEquals("Found 2 mappings", table.getMappings(user, 
domain).size(), 2);
-        assertEquals("Found 2 domains", table.getDomains().size(),2);
-        assertEquals("Found 2 mapping line", table.getAllMappings().size(),2);
+
+    /**
+     * @see 
org.apache.james.vut.AbstractVirtualUserTableTest#removeMapping(java.lang.String,
 java.lang.String, java.lang.String, int)
+     */
+    protected boolean removeMapping(String user, String domain, String 
mapping, int type) throws InvalidMappingException {       
+        if (user == null) user = "*";
+        if (domain == null) domain = "*";
+        
+        Collection mappings = virtualUserTable.getUserDomainMappings(user, 
domain);
+        
+        if (mappings == null) {
+            return false;
+        }  
+    
+        removeMappings(user,domain, mappings);
+    
+        if (type == ERROR_TYPE) {
+            mappings.remove(VirtualUserTable.ERROR_PREFIX + mapping);
+        } else if (type == REGEX_TYPE) {
+            mappings.remove(VirtualUserTable.REGEX_PREFIX + mapping);
+        } else if (type == ADDRESS_TYPE) {
+            mappings.remove(mapping);    
+        } 
+
+        if (mappings.size() > 0) {
+            defaultConfiguration.addChild(new 
AttrValConfiguration("mapping",user + "@" + domain +"=" + 
VirtualUserTableUtil.CollectionToMapping(mappings)));
+        } 
+    
+        try {
+            ContainerUtil.configure(((XMLVirtualUserTable) 
virtualUserTable),defaultConfiguration);
+        } catch (ConfigurationException e) {
+            e.printStackTrace();
+           return false;
+        }
+        return true;
     }
     
-    public void testGetUserMappings() throws ErrorMappingException, 
InvalidMappingException {
-        assertTrue("Found 1 mappings", table.getMappings("any", domain).size() 
== 2);
-        assertNull("Found 0 mappings", table.getUserDomainMappings("any", 
domain));
+    
+    private void removeMappings(String user, String domain, Collection 
mappings) {
+        Configuration [] conf = defaultConfiguration.getChildren("mapping");
+        
+        for (int i = 0; i < conf.length; i++ ) {
+            DefaultConfiguration c = (DefaultConfiguration) conf[i];
+            try {
+                String mapping = user + "@" + domain + "=" + 
VirtualUserTableUtil.CollectionToMapping(mappings);
+            
+            
+                if (c.getValue().equalsIgnoreCase(mapping)){
+                    defaultConfiguration.removeChild(c);
+                }
+            } catch (ConfigurationException e) {
+                e.printStackTrace();
+            }
+        }
     }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to