Package org.springside.modules.log

Examples of org.springside.modules.log.MockLog4jAppender


  @Test
  public void gracefulShutdown() throws InterruptedException {

    Logger logger = LoggerFactory.getLogger("test");
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger("test");

    //time enough to shutdown
    ExecutorService pool = Executors.newSingleThreadExecutor();
    Runnable task = new Task(logger, 500, 0);
    pool.execute(task);
    ThreadUtils.gracefulShutdown(pool, 1000, 1000, TimeUnit.MILLISECONDS);
    assertTrue(pool.isTerminated());
    assertNull(appender.getFirstLog());

    //time not enough to shutdown,call shutdownNow
    appender.clearLogs();
    pool = Executors.newSingleThreadExecutor();
    task = new Task(logger, 1000, 0);
    pool.execute(task);
    ThreadUtils.gracefulShutdown(pool, 500, 1000, TimeUnit.MILLISECONDS);
    assertTrue(pool.isTerminated());
    assertEquals("InterruptedException", appender.getFirstLog().getMessage());

    //self thread interrupt while calling gracefulShutdown
    appender.clearLogs();

    final ExecutorService self = Executors.newSingleThreadExecutor();
    task = new Task(logger, 100000, 0);
    self.execute(task);

    final CountDownLatch lock = new CountDownLatch(1);
    Thread thread = new Thread(new Runnable() {

      @Override
      public void run() {
        lock.countDown();
        ThreadUtils.gracefulShutdown(self, 200000, 200000, TimeUnit.MILLISECONDS);
      }
    });
    thread.start();
    lock.await();
    thread.interrupt();
    ThreadUtils.sleep(500);
    assertEquals("InterruptedException", appender.getFirstLog().getMessage());
  }
View Full Code Here


  @Test
  public void normalShutdown() throws InterruptedException {

    Logger logger = LoggerFactory.getLogger("test");
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger("test");

    //time not enough to shutdown,write error log.
    appender.clearLogs();
    ExecutorService pool = Executors.newSingleThreadExecutor();
    Runnable task = new Task(logger, 1000, 0);
    pool.execute(task);
    ThreadUtils.normalShutdown(pool, 500, TimeUnit.MILLISECONDS);
    assertTrue(pool.isTerminated());
    assertEquals("InterruptedException", appender.getFirstLog().getMessage());

    //self thread interrupt while calling shutdown
    appender.clearLogs();
    final ExecutorService selfpool = Executors.newSingleThreadExecutor();
    task = new Task(logger, 100000, 1000);
    selfpool.execute(task);

    final CountDownLatch lock = new CountDownLatch(1);
    Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        lock.countDown();
        ThreadUtils.normalShutdown(selfpool, 200000, TimeUnit.MILLISECONDS);
      }
    });
    thread.start();
    lock.await();
    thread.interrupt();

    ThreadUtils.sleep(1000);

    assertEquals("InterruptedException", appender.getFirstLog().getMessage());
  }
View Full Code Here

  @Test
  public void normal() {
    String testString1 = "Hello";
    String testString2 = "World";
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(MockLog4jAppenderTest.class);

    Logger logger = LoggerFactory.getLogger(MockLog4jAppenderTest.class);
    logger.warn(testString1);
    logger.warn(testString2);

    //getFirstLog/getLastLog
    assertEquals(testString1, appender.getFirstLog().getMessage());
    assertEquals(testString2, appender.getLastLog().getMessage());

    //getAllLogs
    assertEquals(2, appender.getAllLogs().size());
    assertEquals(testString2, appender.getAllLogs().get(1).getMessage());

    //clearLogs
    appender.clearLogs();
    assertNull(appender.getFirstLog());
    assertNull(appender.getLastLog());

  }
View Full Code Here

  @Test
  public void addAndRemoveAppender() {
    String testString = "Hello";
    Logger logger = LoggerFactory.getLogger(MockLog4jAppenderTest.class);
    MockLog4jAppender appender = new MockLog4jAppender();
    //class
    appender.addToLogger(MockLog4jAppenderTest.class);
    logger.warn(testString);
    assertNotNull(appender.getFirstLog());

    appender.clearLogs();
    appender.removeFromLogger(MockLog4jAppenderTest.class);
    logger.warn(testString);
    assertNull(appender.getFirstLog());

    //name
    appender.addToLogger("org.springside.modules.unit.log");
    logger.warn(testString);
    assertNotNull(appender.getFirstLog());

    appender.clearLogs();
    appender.removeFromLogger("org.springside.modules.unit.log");
    logger.warn(testString);
    assertNull(appender.getFirstLog());

  }
View Full Code Here

  @Test
  public void scheduleJob() throws Exception {
    DbUnitUtils.loadData(dataSource, "/data/default-data.xml");

    //加载测试用logger appender
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(QuartzClusterableJob.class);

    //等待任务启动
    ThreadUtils.sleep(3000);

    //验证任务已执行
    assertEquals(1, appender.getAllLogs().size());

    assertEquals("There are 6 user in database, print by default's job.", appender.getFirstLog().getMessage());

    DbUnitUtils.removeData(dataSource, "/data/default-data.xml");
  }
View Full Code Here

  @Test
  public void scheduleJob() throws Exception {
    DbUnitUtils.loadData(dataSource, "/data/default-data.xml");

    //加载测试用logger appender
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(JdkExecutorJob.class);

    //等待任务启动
    ThreadUtils.sleep(3000);

    //验证任务已执行
    assertEquals(1, appender.getAllLogs().size());
    assertEquals("There are 6 user in database.", appender.getFirstLog().getMessage());

    DbUnitUtils.removeData(dataSource, "/data/default-data.xml");

  }
View Full Code Here

  Logger logger = LoggerFactory.getLogger(TraceUtilsTest.class);

  @Test
  public void test() {
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(TraceUtilsTest.class);

    //begin trace
    TraceUtils.beginTrace();
    assertNotNull(MDC.get(TraceUtils.TRACE_ID_KEY));
    assertEquals(TraceUtils.TRACE_ID_LENGTH, ((String) MDC.get(TraceUtils.TRACE_ID_KEY)).length());

    //log message
    logger.info("message");
    assertEquals("message", appender.getAllLogs().get(0).getMessage());
    assertNotNull(MDC.get(TraceUtils.TRACE_ID_KEY));

    //end trace
    TraceUtils.endTrace();
    assertNull(MDC.get(TraceUtils.TRACE_ID_KEY));
View Full Code Here

  private Destination advancedNotifyTopic;

  @Test
  public void queueMessage() {
    ThreadUtils.sleep(1000);
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(AdvancedNotifyMessageListener.class);

    User user = new User();
    user.setName("calvin");
    user.setEmail("calvin@sringside.org.cn");

    notifyMessageProducer.sendQueue(user);
    ThreadUtils.sleep(1000);
    assertNotNull(appender.getFirstLog());
    assertEquals("UserName:calvin, Email:calvin@sringside.org.cn, ObjectType:user", appender.getFirstLog()
        .getMessage());
  }
View Full Code Here

  }

  @Test
  public void topicMessage() {
    ThreadUtils.sleep(1000);
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(AdvancedNotifyMessageListener.class);

    User user = new User();
    user.setName("calvin");
    user.setEmail("calvin@sringside.org.cn");

    notifyMessageProducer.sendTopic(user);
    ThreadUtils.sleep(2000);
    assertNotNull(appender.getFirstLog());
    assertEquals("UserName:calvin, Email:calvin@sringside.org.cn, ObjectType:user", appender.getFirstLog()
        .getMessage());
  }
View Full Code Here

  }

  @Test
  public void topicMessageWithWrongType() {
    ThreadUtils.sleep(1000);
    MockLog4jAppender appender = new MockLog4jAppender();
    appender.addToLogger(AdvancedNotifyMessageListener.class);

    advancedJmsTemplate.send(advancedNotifyTopic, new MessageCreator() {
      public Message createMessage(Session session) throws JMSException {

        MapMessage message = session.createMapMessage();
        message.setStringProperty("objectType", "role");
        return message;
      }
    });

    ThreadUtils.sleep(1000);
    assertTrue(appender.getAllLogs().isEmpty());
  }
View Full Code Here

TOP

Related Classes of org.springside.modules.log.MockLog4jAppender

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.