hbedi       2002/10/12 12:35:18

  Added:       src/java/org/apache/james/testing BaseTest.java Main.java
                        POP3Test.java TestMethod.java runtest.bat
                        runtest.sh testconf.xml
  Log:
  test suite and pop3 test.
  
  Revision  Changes    Path
  1.1                  jakarta-james/src/java/org/apache/james/testing/BaseTest.java
  
  Index: BaseTest.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.james.testing;
  
  import junit.framework.TestCase;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  
  /**
   * Base Test class. Can run a set of tests.
   */
  public class BaseTest extends TestCase implements Configurable {
  
      private TestMethod[] testMethod;
      private boolean showStat;
  
      public BaseTest(String name) {
          super(name);
      }
  
      public void configure(Configuration configuration) 
          throws ConfigurationException 
      {
          // list of test methods
          Configuration testseq = configuration.getChild("testsequence");
          showStat = testseq.getAttributeAsBoolean("showStat");
          Configuration[] testconf = testseq.getChildren("testmethod");
          testMethod = new TestMethod[testconf.length];
          for ( int i = 0 ; i < testMethod.length ; i++ )
              testMethod[i] = new TestMethod(testconf[i].getValue());
      }
  
      public final int countTestCases() {
          return testMethod.length;
      }
  
      protected final void runTest() throws Throwable {
          for ( int i = 0 ; i < testMethod.length ; i++ ) {
              testMethod[i].invoke(this);
              if ( showStat )
                  System.out.println("stat: "+getName()+", "+testMethod[i]);
          }
      }
  }
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/testing/Main.java
  
  Index: Main.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.james.testing;
  
  import junit.framework.*;
  import junit.extensions.*;
  import junit.textui.*;
  import java.io.*;
  import org.apache.avalon.framework.configuration.*;
  import java.lang.reflect.*;
  
  /**
   * Run tests. 
   * - Reads test configuration file, constructs test suite
   * - initiates tests, reports result.
   */
  public class Main {
      public static void main(String[] args) throws Exception {
          System.out.println("Usage java org.apache.james.testing.Main 
<testconfigfile>");
  
          File testconfFile = new File(args[0]);
          DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
          Configuration alltestconf = builder.buildFromFile(testconfFile);
          Configuration[] testconf = alltestconf.getChildren("test");
          TestSuite alltests = new TestSuite();
          for ( int i = 0 ; i < testconf.length ; i++ ) {
              Configuration conf = testconf[i];
              String clsname = conf.getAttribute("class");
              String name = conf.getAttribute("name");
              int repetition = conf.getAttributeAsInteger("repetition");
              boolean async = conf.getAttributeAsBoolean("async");
  
              Class clazz = Class.forName(clsname);
              Constructor cstr = clazz.getConstructor(new Class[] { String.class });
              Test test = (Test)cstr.newInstance(new Object[] {name});
              if ( test instanceof Configurable )
                  ((Configurable)test).configure(conf);
  
              if (repetition > 1)
                  test = new RepeatedTest(test,repetition);
  
              if ( async ) {
                  TestSuite ts = new ActiveTestSuite();
                  ts.addTest(test);
                  test = ts;
              }
              alltests.addTest(test);
          }
          TestRunner.run(alltests);
      }
  }
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/testing/POP3Test.java
  
  Index: POP3Test.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.james.testing;
  
  import java.io.InputStream;
  import java.io.FileOutputStream;
  import java.io.OutputStream;
  import javax.mail.internet.MimeMessage;
  import org.apache.avalon.excalibur.io.IOUtil;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.commons.net.SocketClient;
  import org.apache.commons.net.pop3.POP3;
  import org.apache.commons.net.pop3.POP3Client;
  import org.apache.commons.net.pop3.POP3MessageInfo;
  import org.apache.james.fetchpop.ReaderInputStream;
  
  /**
   * Fetch mail. Can be configured and extended to test specific pop3
   * operations.
   */
  public class POP3Test extends BaseTest {
      private String host, username, password;
  
      public POP3Test(String name) {
          super(name);
      }
  
      public void configure(Configuration configuration) 
          throws ConfigurationException 
      {
          this.host = configuration.getChild("host").getValue();
          this.username = configuration.getChild("username").getValue();
          this.password = configuration.getChild("password").getValue();
          super.configure(configuration);
      }
  
      private POP3Client client;
  
      protected void setUp() throws Exception {
          client = new POP3Client();
          client.connect(host);
      }
  
      protected void tearDown() throws Exception {
          client.disconnect();
      }
  
      // ----------- pop3 operations ------------
  
      private POP3MessageInfo[] msg;
      private static int saveMsgCounter;
  
      public void login() throws Exception {
          client.login(username, password);
      }
      public void logout() throws Exception {
          client.logout();
      }
      public void fetchMsgsInfo() throws Exception {
          msg = client.listMessages();
      }
  
      public void fetchMsgs() throws Exception {
          for ( int i = 0 ; i < msg.length ; i++ )
              fetchMsg(msg[i],false);
      }
  
      public void fetchAndSaveMsgs() throws Exception {
          for ( int i = 0 ; i < msg.length ; i++ )
              fetchMsg(msg[i],true);
      }
  
      public void deleteMsgs() throws Exception {
          for ( int i = 0 ; i < msg.length ; i++ )
              client.deleteMessage(msg[i].number);
      }
  
      private void fetchMsg(POP3MessageInfo msg,boolean save) throws Exception {
          InputStream in = new ReaderInputStream(client.retrieveMessage(msg.number));
          try {
              MimeMessage message = new MimeMessage(null, in);
              if ( save ) {
                  OutputStream out = new FileOutputStream
                      ("pop3test-"+host+"-"+username+"."+(saveMsgCounter++)+".eml");
                  try {
                      message.writeTo(out);
                  } finally {
                      IOUtil.shutdownStream(out);
                  }
              }
          } finally {
              IOUtil.shutdownStream(in);
          }
      }
  }
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/testing/TestMethod.java
  
  Index: TestMethod.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.james.testing;
  
  import java.lang.reflect.Method;
  
  class TestMethod {
      // stats
      private int timeTaken;
      private int attempts;
      private int success;
      
      final String name;
      TestMethod(String name) {
          this.name = name;
      }
      
      // invoke test
      void invoke(Object obj) throws Exception {
          Method m = obj.getClass().getMethod(name,new Class[0]);
          attempts++;
          long st = System.currentTimeMillis();
          try {
              m.invoke(obj,new Object[0]);
              success++;
          } finally {
              timeTaken = (int)(System.currentTimeMillis() - st);
          }
      }
      // print report.
      public String toString() {
          return name+", "+timeTaken+", "+success+", "+attempts;
      }
  }
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/testing/runtest.bat
  
  Index: runtest.bat
  ===================================================================
  echo 'runs james test suite and outputs result in csv file format'
  echo 'test, method, time taken, successful invocations, invocation attempt'
  java org.apache.james.testing.Main testconf.xml | grep "stat: " | sed "s/.*stat: //g"
  
  
  1.1                  jakarta-james/src/java/org/apache/james/testing/runtest.sh
  
  Index: runtest.sh
  ===================================================================
  echo 'runs james test suite and outputs result in csv file format'
  echo 'test, method, time taken, successful invocations, invocation attempt'
  java org.apache.james.testing.Main testconf.xml | grep "stat: " | sed "s/.*stat: //g"
  
  
  
  1.1                  jakarta-james/src/java/org/apache/james/testing/testconf.xml
  
  Index: testconf.xml
  ===================================================================
  <!-- test configuration file -->
  <testsuite>
  <test name="pop3test" async="true" repetition="5" 
class="org.apache.james.testing.POP3Test">
    <host>localhost</host>
    <username>harmeet</username>
    <password>harmeet</password>
  
    <!-- call these test methods -->
    <testsequence showStat="true">
      <testmethod>login</testmethod>
      <testmethod>fetchMsgsInfo</testmethod>
      <testmethod>fetchMsgs</testmethod>
      <testmethod>logout</testmethod>
    </testsequence>
  </test>
  <test name="pop3test" async="true" repetition="5" 
class="org.apache.james.testing.POP3Test">
    <host>localhost</host>
    <username>user1</username>
    <password>user1</password>
  
    <!-- call these test methods -->
    <testsequence showStat="true">
      <testmethod>login</testmethod>
      <testmethod>fetchMsgsInfo</testmethod>
      <testmethod>fetchMsgs</testmethod>
      <testmethod>logout</testmethod>
    </testsequence>
  </test>
  <test name="pop3test" async="true" repetition="5" 
class="org.apache.james.testing.POP3Test">
    <host>localhost</host>
    <username>user2</username>
    <password>user2</password>
  
    <!-- call these test methods -->
    <testsequence showStat="true">
      <testmethod>login</testmethod>
      <testmethod>fetchMsgsInfo</testmethod>
      <testmethod>fetchMsgs</testmethod>
      <testmethod>logout</testmethod>
    </testsequence>
  </test>
  <test name="pop3test" async="true" repetition="5" 
class="org.apache.james.testing.POP3Test">
    <host>localhost</host>
    <username>user3</username>
    <password>user3</password>
  
    <!-- call these test methods -->
    <testsequence showStat="true">
      <testmethod>login</testmethod>
      <testmethod>fetchMsgsInfo</testmethod>
      <testmethod>fetchMsgs</testmethod>
      <testmethod>logout</testmethod>
    </testsequence>
  </test>
  <test name="pop3test" async="true" repetition="5" 
class="org.apache.james.testing.POP3Test">
    <host>localhost</host>
    <username>user4</username>
    <password>user4</password>
  
    <!-- call these test methods -->
    <testsequence showStat="true">
      <testmethod>login</testmethod>
      <testmethod>fetchMsgsInfo</testmethod>
      <testmethod>fetchMsgs</testmethod>
      <testmethod>logout</testmethod>
    </testsequence>
  </test>
  </testsuite>
  
  
  

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

Reply via email to