Author: bago
Date: Fri Dec 22 15:56:15 2006
New Revision: 489805

URL: http://svn.apache.org/viewvc?view=rev&rev=489805
Log:
Added support for childloggers to ConsoleLogger (and write the logger name to 
output)
Make use of ChildLoggers also in tests.
Added a main to org.apache.james.jspf.RFC4408YamlTest that use log4j (to help 
spf spec people testing our tests)

Modified:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Directive.java
    
james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/DefaultSPF1Parser.java
    
james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/TrustedForwarderPolicy.java
    james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java
    james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java
    
james/jspf/trunk/src/test/java/org/apache/james/jspf/DNSServiceXBillImplTest.java
    james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java
    james/jspf/trunk/src/test/java/org/apache/james/jspf/RFC4408YamlTest.java
    james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java Fri Dec 22 
15:56:15 2006
@@ -91,7 +91,7 @@
         WiringServiceTable wiringService = new WiringServiceTable();
         wiringService.put(LogEnabled.class, this.log);
         wiringService.put(DNSServiceEnabled.class, this.dnsProbe);
-        this.parser = new DefaultSPF1Parser(logger, new 
DefaultTermsFactory(this.log, wiringService));
+        this.parser = new DefaultSPF1Parser(logger.getChildLogger("parser"), 
new DefaultTermsFactory(logger.getChildLogger("termsfactory"), wiringService));
         // We add this after the parser creation because services cannot be 
null
         wiringService.put(SPFCheckEnabled.class, this.parser);
     }
@@ -270,7 +270,7 @@
     public synchronized FallbackPolicy getFallbackPolicy() {
         // Initialize fallback policy
         if (fallBack == null) {
-            this.fallBack =  new FallbackPolicy(this.log, parser);
+            this.fallBack =  new 
FallbackPolicy(log.getChildLogger("fallbackpolicy"), parser);
         }
         return fallBack;
     }
@@ -295,7 +295,7 @@
      */
     public synchronized OverridePolicy getOverridePolicy() {
         if (override == null) {
-            override = new OverridePolicy(this.log, parser);
+            override = new 
OverridePolicy(log.getChildLogger("overridepolicy"), parser);
         }
         return override;
     }

Modified: 
james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Directive.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Directive.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Directive.java 
(original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Directive.java 
Fri Dec 22 15:56:15 2006
@@ -68,7 +68,6 @@
             TempErrorException, NoneException {
         // if already have a current result we don't run this
         if (spfData.getCurrentResult() == null) {
-            log.debug("Processing directive: " + this);
 
             if (mechanism.run(spfData)) {
                 if (qualifier != null) {
@@ -79,7 +78,9 @@
                     }
                 }
                 
-                log.debug("Processed directive matched: " + this + " returned 
" + spfData.getCurrentResult());
+                log.info("Processed directive matched: " + this + " returned " 
+ spfData.getCurrentResult());
+            } else {
+                log.debug("Processed directive NOT matched: " + this);
             }
 
         }

Modified: 
james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/DefaultSPF1Parser.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/DefaultSPF1Parser.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- 
james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/DefaultSPF1Parser.java
 (original)
+++ 
james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/DefaultSPF1Parser.java
 Fri Dec 22 15:56:15 2006
@@ -310,7 +310,7 @@
                             TERM_STEP_REGEX_MECHANISM_POS);
 
                     result.getDirectives().add(
-                            new Directive(qualifier, (Mechanism) mech, log));
+                            new Directive(qualifier, (Mechanism) mech, 
log.getChildLogger(qualifier+"directive")));
 
                 }
 

Modified: 
james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/TrustedForwarderPolicy.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/TrustedForwarderPolicy.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- 
james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/TrustedForwarderPolicy.java
 (original)
+++ 
james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/TrustedForwarderPolicy.java
 Fri Dec 22 15:56:15 2006
@@ -62,7 +62,7 @@
                         return this;
                     }
                 }.setHost(TRUSTED_FORWARDER_HOST);
-                
spfRecord.getDirectives().add(spfRecord.getDirectives().size()-1, new 
Directive(null, trusted, log));
+                
spfRecord.getDirectives().add(spfRecord.getDirectives().size()-1, new 
Directive(null, trusted, log.getChildLogger("trustedforwarder")));
             } catch (PermErrorException e) {
                 // will never happen
             }

Modified: 
james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java 
(original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java 
Fri Dec 22 15:56:15 2006
@@ -50,6 +50,7 @@
 
     SPFYamlTestSuite data;
     String test;
+    protected Logger log;
     protected static SPF spf;
     protected static SPFRecordParser parser;
     private static DNSService dns;
@@ -95,7 +96,6 @@
     
             public int read(char[] arg0) throws IOException {
                 int rl = super.read(arg0);
-                // System.out.println("<<< "+new String(arg0));
                 return rl;
             }
             
@@ -121,20 +121,25 @@
     protected void runTest() throws Throwable {
         String next = test;
         HashMap currentTest = (HashMap) data.getTests().get(next);
-        
-        System.out.println("testing "+next+": 
"+currentTest.get("description"));
+
+        if (log == null) {
+                log = new ConsoleLogger();
+        }
+
+        Logger testLogger = log.getChildLogger("test");
+        testLogger.info("TESTING "+next+": "+currentTest.get("description"));
     
         if (parser == null) {
-            Logger log = new ConsoleLogger();
             /* PREVIOUS SLOW WAY 
             enabledServices = new WiringServiceTable();
             enabledServices.put(LogEnabled.class, log);
             */
-            parser = new DefaultSPF1Parser(log, new DefaultTermsFactory(log, 
new WiringService() {
+            parser = new DefaultSPF1Parser(log.getChildLogger("parser"), new 
DefaultTermsFactory(log.getChildLogger("termsfactory"), new WiringService() {
 
                 public void wire(Object component) throws 
WiringServiceException {
                     if (component instanceof LogEnabled) {
-                        ((LogEnabled) component).enableLogging(new 
ConsoleLogger());
+                        String[] path = 
component.getClass().toString().split("\\.");
+                        ((LogEnabled) 
component).enableLogging(log.getChildLogger("dep").getChildLogger(path[path.length-1].toLowerCase()));
                     }
                     if (component instanceof DNSServiceEnabled) {
                         ((DNSServiceEnabled) component).enableDNSService(dns);
@@ -146,8 +151,8 @@
                 
             }));
         }
-        dns = new LoggingDNSService(getDNSService());
-        spf = new SPF(dns, parser, new ConsoleLogger());
+        dns = new LoggingDNSService(getDNSService(), 
log.getChildLogger("dns"));
+        spf = new SPF(dns, parser, log.getChildLogger("spf"));
         /* PREVIOUS SLOW WAY 
         // we add this after the creation because it is a loop reference
         enabledServices.remove(DNSServiceEnabled.class);
@@ -182,7 +187,7 @@
             boolean match = false;
             for (int i = 0; i < results.size(); i++) {
                 if (results.get(i).equals(resultSPF)) match = true;
-                System.err.println("checking "+results.get(i)+" => 
"+resultSPF);
+                // testLogger.debug("checking "+resultSPF+" against allowed 
result "+results.get(i));
             }
             assertTrue(match);
         }
@@ -203,6 +208,8 @@
     
         }
     
+        testLogger.info("PASSED. Result="+res.getResult()+" 
Explanation="+res.getExplanation()+" Header="+res.getHeaderText());
+        
     }
 
     /**

Modified: 
james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java 
(original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java Fri 
Dec 22 15:56:15 2006
@@ -50,20 +50,26 @@
     private final int m_logLevel;
 
     /**
+     * Current logger path.
+     */
+    private String m_path;
+
+    /**
      * Creates a new ConsoleLogger with the priority set to DEBUG.
      */
     public ConsoleLogger()
     {
-        this( LEVEL_DEBUG );
+        this( LEVEL_DEBUG , "ROOT");
     }
 
     /**
      * Creates a new ConsoleLogger.
      * @param logLevel log level typecode
      */
-    public ConsoleLogger( final int logLevel )
+    public ConsoleLogger( final int logLevel, final String path )
     {
         m_logLevel = logLevel;
+        m_path = path;
     }
 
     /**
@@ -87,6 +93,7 @@
         if( m_logLevel <= LEVEL_DEBUG )
         {
             System.out.print( "[DEBUG] " );
+            System.out.print( m_path+" " );
             System.out.println( message );
 
             if( null != throwable )
@@ -127,6 +134,7 @@
         if( m_logLevel <= LEVEL_INFO )
         {
             System.out.print( "[INFO] " );
+            System.out.print( m_path+" " );
             System.out.println( message );
 
             if( null != throwable )
@@ -167,6 +175,7 @@
         if( m_logLevel <= LEVEL_WARN )
         {
             System.out.print( "[WARNING] " );
+            System.out.print( m_path+" " );
             System.out.println( message );
 
             if( null != throwable )
@@ -207,6 +216,7 @@
         if( m_logLevel <= LEVEL_ERROR )
         {
             System.out.print( "[ERROR] " );
+            System.out.print( m_path+" " );
             System.out.println( message );
 
             if( null != throwable )
@@ -247,6 +257,7 @@
         if( m_logLevel <= LEVEL_FATAL )
         {
             System.out.print( "[FATAL ERROR] " );
+            System.out.print( m_path+" " );
             System.out.println( message );
 
             if( null != throwable )
@@ -274,6 +285,6 @@
      */
     public Logger getChildLogger( final String name )
     {
-        return this;
+        return new ConsoleLogger(m_logLevel, m_path+"."+name);
     }
 }

Modified: 
james/jspf/trunk/src/test/java/org/apache/james/jspf/DNSServiceXBillImplTest.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/DNSServiceXBillImplTest.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- 
james/jspf/trunk/src/test/java/org/apache/james/jspf/DNSServiceXBillImplTest.java
 (original)
+++ 
james/jspf/trunk/src/test/java/org/apache/james/jspf/DNSServiceXBillImplTest.java
 Fri Dec 22 15:56:15 2006
@@ -47,9 +47,13 @@
      */
     public void testGetLocalDomainNames() throws UnknownHostException,
             TextParseException {
+        // This write MACHINE-NAME/MACHINE-ADDRESS
         System.out.println(InetAddress.getLocalHost());
+        // THIS WRITE localhost/127.0.0.1
         System.out.println(InetAddress.getAllByName(null)[0]);
+        // THIS WRITE a fully qualified MACHINE-NAME.MACHINE-DOMAIN
         System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
+        // THIS WRITE localhost
         System.out.println(InetAddress.getAllByName(null)[0]
                 .getCanonicalHostName());
         Record[] record = new Lookup(Name.root, Type.ANY).run();

Modified: 
james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java 
(original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java 
Fri Dec 22 15:56:15 2006
@@ -20,20 +20,18 @@
 package org.apache.james.jspf;
 
 import org.apache.james.jspf.core.DNSService;
-import org.apache.james.jspf.impl.DNSServiceXBillImpl;
+import org.apache.james.jspf.core.Logger;
 
 import java.util.List;
 
 public class LoggingDNSService implements DNSService {
 
     private DNSService dnsService;
+    private Logger logger;
 
-    public LoggingDNSService(DNSService service) {
+    public LoggingDNSService(DNSService service, Logger logger) {
         this.dnsService = service;
-    }
-
-    public LoggingDNSService() {
-        this.dnsService = new DNSServiceXBillImpl(new ConsoleLogger());
+        this.logger = logger;
     }
     
     public int getRecordLimit() {
@@ -46,19 +44,21 @@
 
     public List getLocalDomainNames() {
         List res = dnsService.getLocalDomainNames();
-        System.out.print("getLocalDomainNames() = ");
+        StringBuffer logBuff = new StringBuffer();
+        logBuff.append("getLocalDomainNames() = ");
         if (res != null) {
             for (int i = 0; i < res.size(); i++) {
-                System.out.print(res.get(i));
+                logBuff.append(res.get(i));
                 if (i == res.size() - 1) {
-                    System.out.println("");
+                    logBuff.append("");
                 } else {
-                    System.out.print(",");
+                    logBuff.append(",");
                 }
             }
         } else {
-            System.out.println("getLocalDomainNames-ret: null");
+            logBuff.append("getLocalDomainNames-ret: null");
         }
+        logger.debug(logBuff.toString());
         return res;
 
     }
@@ -70,22 +70,24 @@
     public List getRecords(String hostname, int recordType) throws 
TimeoutException {
         try {
             List result = dnsService.getRecords(hostname, recordType);
-            System.out.print("getRecords(" + hostname + "," + recordType + ") 
= ");
+            StringBuffer logBuff = new StringBuffer();
+            logBuff.append("getRecords(" + hostname + "," + recordType + ") = 
");
             if (result != null) {
                 for (int i = 0; i < result.size(); i++) {
-                    System.out.print(result.get(i));
+                    logBuff.append(result.get(i));
                     if (i == result.size() - 1) {
-                        System.out.println("");
+                        logBuff.append("");
                     } else {
-                        System.out.print(",");
+                        logBuff.append(",");
                     }
                 }
             } else {
-                System.out.println("getRecords-ret: null");
+                logBuff.append("getRecords-ret: null");
             }
+            logger.debug(logBuff.toString());
             return result;
         } catch (TimeoutException e) {
-            System.out.println("getRecords(" + hostname
+            logger.debug("getRecords(" + hostname
                     + ") = TempErrorException[" + e.getMessage() + "]");
             throw e;
         }

Modified: 
james/jspf/trunk/src/test/java/org/apache/james/jspf/RFC4408YamlTest.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/RFC4408YamlTest.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/RFC4408YamlTest.java 
(original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/RFC4408YamlTest.java 
Fri Dec 22 15:56:15 2006
@@ -19,12 +19,16 @@
 
 package org.apache.james.jspf;
 
+import org.apache.james.jspf.core.Logger;
+import org.apache.james.jspf.impl.Log4JLogger;
+
 import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
+import junit.textui.TestRunner;
 
 public class RFC4408YamlTest extends AbstractYamlTest {
 
@@ -69,6 +73,33 @@
             }
         }
 
+    }
+    
+    protected void setLogger(Logger logger) {
+        this.log = logger;
+    }
+    
+    /**
+     * This method has been created for spf spec people to let them better 
read the
+     * output of our tests against their yaml file
+     * 
+     * @param args
+     * @throws Throwable 
+     */
+    public static void main(String[] args) throws Throwable {
+        Logger l = new Log4JLogger(org.apache.log4j.Logger.getLogger("ROOT"));
+
+        List tests = loadTests(YAMLFILE2);
+        Iterator i = tests.iterator();
+        while (i.hasNext()) {
+            SPFYamlTestSuite o = (SPFYamlTestSuite) i.next();
+            Iterator ttt = o.getTests().keySet().iterator();
+            while (ttt.hasNext()) {
+                RFC4408YamlTest t = new RFC4408YamlTest(o,(String) ttt.next());
+                t.setLogger(l);
+                TestRunner.run(t);
+            }
+        }
     }
 
 }

Modified: 
james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java
URL: 
http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java?view=diff&rev=489805&r1=489804&r2=489805
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java 
(original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java 
Fri Dec 22 15:56:15 2006
@@ -146,14 +146,14 @@
                                 if (def.recOutAuto == null)
                                     def.recOutAuto = "";
                             }
-                        } else {
-                            System.err.println("Unknown token: " + tokens[0]);
+//                        } else {
+//                            // System.err.println("Unknown token: " + 
tokens[0]);
                         }
 
-                    } else {
-                        System.err
-                                .println("Ignored line for different 
implementation: "
-                                        + tokens[1]);
+//                    } else {
+//                        System.err
+//                                .println("Ignored line for different 
implementation: "
+//                                        + tokens[1]);
                     }
 
                 } else {



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

Reply via email to