FileDocCategorySizeDatePackage
ERFATestCase.javaAPI DocApache log4j 1.2.154262Sat Aug 25 00:09:32 BST 2007org.apache.log4j.varia

ERFATestCase

public class ERFATestCase extends TestCase
Test of ExternallyRolledFileAppender.
author
Curt Arnold

Fields Summary
Constructors Summary
public ERFATestCase(String name)
Create new instance of test.

param
name test name.

    super(name);
  
Methods Summary
voidsendMessage(int port, java.lang.String msg, java.lang.String expectedResponse)
Send a message to the ERFA.

param
port port number.
param
msg message, may not be null.
param
expectedResponse expected response, may not be null.
throws
IOException thrown on IO error.

      Socket socket = new Socket((String) null, port);
      DataInputStream reader = new DataInputStream(socket.getInputStream());
      DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
      writer.writeUTF(msg);
      String response = reader.readUTF();
      assertEquals(expectedResponse, response);
      reader.close();
      writer.close();
      socket.close();
  
public voidtearDown()
Reset configuration after test.

      LogManager.resetConfiguration();
  
public voidtestConstructor()
Test ExternallyRolledFileAppender constructor.

      ExternallyRolledFileAppender appender =
              new ExternallyRolledFileAppender();
      assertEquals(0, appender.getPort());
  
public voidtestRollover()
Test externally triggered rollover.

throws
IOException thrown on IO error.

      ExternallyRolledFileAppender erfa =
              new ExternallyRolledFileAppender();

      int port = 5500;

      Logger logger = Logger.getLogger(RFATestCase.class);
      Logger root = Logger.getRootLogger();
      PatternLayout layout = new PatternLayout("%m\n");
      erfa.setLayout(layout);
      erfa.setAppend(false);
      erfa.setMaxBackupIndex(2);
      erfa.setPort(port);
      erfa.setFile("output/ERFA-test2.log");
      try {
        erfa.activateOptions();
      } catch(SecurityException ex) {
          return;
      }
      try {
         Thread.sleep(100);
      } catch(InterruptedException ex) {
      }
      root.addAppender(erfa);


      // Write exactly 10 bytes with each log
      for (int i = 0; i < 55; i++) {
        if (i < 10) {
          logger.debug("Hello---" + i);
        } else if (i < 100) {
          logger.debug("Hello--" + i);
        }
        if ((i % 10) == 9) {
            try {
                sendMessage(port, "RollOver", "OK");
            } catch(SecurityException ex) {
                return;
            }
        }
      }

      try {
        sendMessage(port,
              "That's all folks.",
              "Expecting [RollOver] string.");
      } catch(SecurityException ex) {
          return;
      }


      assertTrue(new File("output/ERFA-test2.log").exists());
      assertTrue(new File("output/ERFA-test2.log.1").exists());
      assertTrue(new File("output/ERFA-test2.log.2").exists());
      assertFalse(new File("output/ERFA-test2.log.3").exists());