james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r572449 - in /james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver: DNSTestingServer.java DNSTestingServerLauncher.java
Date Mon, 03 Sep 2007 23:37:23 GMT
Author: bago
Date: Mon Sep  3 16:37:22 2007
New Revision: 572449

URL: http://svn.apache.org/viewvc?rev=572449&view=rev
Log:
Created a fake dnsserver launcher class.
This class allow running a fake dnsserver on the given interface/port reading the given yaml
file and the given test suite description.
If the parameters are correct the server will start listening/answering until killed.

Added:
    james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServerLauncher.java
  (with props)
Modified:
    james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServer.java

Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServer.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServer.java?rev=572449&r1=572448&r2=572449&view=diff
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServer.java (original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServer.java Mon
Sep  3 16:37:22 2007
@@ -433,9 +433,7 @@
         return response.toWire();
     }
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
      * @see org.apache.james.jspf.MessageGenerator#formerrMessage(byte[])
      */
     public byte[] formerrMessage(byte[] in) {

Added: james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServerLauncher.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServerLauncher.java?rev=572449&view=auto
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServerLauncher.java
(added)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServerLauncher.java
Mon Sep  3 16:37:22 2007
@@ -0,0 +1,208 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.jspf.dnsserver;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.jvyaml.Constructor;
+import org.jvyaml.DefaultYAMLFactory;
+import org.jvyaml.YAMLFactory;
+import org.xbill.DNS.TextParseException;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Set;
+
+public class DNSTestingServerLauncher {
+
+    private static final char CHAR_TESTNAME = 't';
+
+    private static final char CHAR_FILE = 'f';
+
+    private static final char CHAR_PORT = 'p';
+
+    private static final char CHAR_IP = 'i';
+
+    private final static String CMD_IP = "ip";
+
+    private final static String CMD_PORT = "port";
+
+    private final static String CMD_FILE = "file";
+
+    private final static String CMD_TESTNAME = "test";
+
+    /**
+     * @param args
+     */
+    public static void main(String[] args) {
+        String ip = null;
+        String port = null;
+        String file = null;
+        String test = null;
+        
+        Options options = generateOptions();
+        CommandLineParser parser = new PosixParser();
+
+        try {
+            CommandLine line = parser.parse(options, args);
+            
+            ip = line.getOptionValue(CHAR_IP);
+            port = line.getOptionValue(CHAR_PORT);
+            file = line.getOptionValue(CHAR_FILE);
+            test = line.getOptionValue(CHAR_TESTNAME);
+            
+            if (ip == null) ip = "0.0.0.0";
+            if (port == null) port = "53";
+            
+            if (file != null && test != null) {
+                
+                InputStream is = new FileInputStream(file);
+                
+                if (is != null) {
+                    Reader br = new BufferedReader(new InputStreamReader(is));
+                    YAMLFactory fact = new DefaultYAMLFactory();
+                    
+                    Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(br)),fact.createResolver()));
+                    boolean found = false;
+                    HashMap testMap = null;
+                    while(ctor.checkData() && !found) {
+                        Object o = ctor.getData();
+                        if (o instanceof HashMap) {
+                          testMap = (HashMap) o;
+                          if (test.equals(testMap.get("description"))) {
+                              found = true;
+                          }
+                        }
+                    }
+                    if (found) {
+                        HashMap loadedZoneData = (HashMap) testMap.get("zonedata");
+                        HashMap zonedata = new HashMap();
+                        Set keys = loadedZoneData.keySet();
+                        for (Iterator i = keys.iterator(); i.hasNext(); ) {
+                            String hostname = (String) i.next();
+                            String lowercase = hostname.toLowerCase(Locale.US);
+                            zonedata.put(lowercase, loadedZoneData.get(hostname));
+                        }
+                        
+                        DNSTestingServer testingServer = new DNSTestingServer(ip, port);
+                        testingServer.setData(zonedata);
+                        
+                        System.out.println("Listening on "+ip+":"+port);
+                        
+                        while (true) {
+                            try {
+                                Thread.sleep(1000);
+                            } catch (InterruptedException e) {
+                                // TODO Auto-generated catch block
+                            }
+                        }
+                        
+                    } else {
+                        throw new RuntimeException("Unable to find a <"+test+"> section
in the passed file.");
+                    }
+                } else {
+                    throw new RuntimeException("Unable to load the file: "+file);
+                }
+
+                
+            } else {
+                System.out.println("Missing required parameter.");
+                usage();
+            }
+        } catch (ParseException e) {
+            usage();
+        } catch (RuntimeException e) {
+            System.out.println("Error: "+e.getMessage());
+            e.printStackTrace();
+            usage();
+        } catch (TextParseException e) {
+            System.out.println("Parsing Error: "+e.getMessage());
+            e.printStackTrace();
+            usage();
+        } catch (IOException e) {
+            System.out.println("IO Error: "+e.getMessage());
+            e.printStackTrace();
+            usage();
+        }
+
+    }
+
+    /**
+     * Print out the usage
+     */
+    private static void usage() {
+        HelpFormatter hf = new HelpFormatter();
+        hf.printHelp("DNSTestingServerLauncher", generateOptions(), true);
+        System.exit(0);
+    }
+
+    /**
+     * Return the generated Options
+     * 
+     * @return options
+     */
+    private static Options generateOptions() {
+        Options options = new Options();
+        options.addOption(OptionBuilder
+                .withLongOpt(CMD_IP)
+                .withValueSeparator('=')
+                .hasArg()
+                .withArgName("ip")
+                .withDescription("Listening IP (default: 0.0.0.0 for every IP)")
+                .create(CHAR_IP));
+        options.addOption(OptionBuilder
+                .withLongOpt(CMD_PORT)
+                .withValueSeparator('=')
+                .hasArg()
+                .withArgName("port")
+                .withDescription("Listening port (default: 53)")
+                .create(CHAR_PORT));
+        options.addOption(OptionBuilder
+                .withLongOpt(CMD_FILE)
+                .withValueSeparator('=')
+                .withDescription("YML file name")
+                .withArgName("file")
+                .isRequired()
+                .hasArg()
+                .create(CHAR_FILE));
+        options.addOption(OptionBuilder
+                .withLongOpt(CMD_TESTNAME)
+                .withValueSeparator('=')
+                .hasArg()
+                .withDescription("Test name")
+                .withArgName("test")
+                .isRequired()
+                .create(CHAR_TESTNAME));
+        return options;
+    }
+
+}

Propchange: james/jspf/trunk/src/test/java/org/apache/james/jspf/dnsserver/DNSTestingServerLauncher.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message