⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdcinjectionfiltertest.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  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.mina.filter.logging;import java.io.IOException;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.concurrent.CountDownLatch;import org.apache.log4j.AppenderSkeleton;import org.apache.log4j.Level;import org.apache.log4j.spi.LoggingEvent;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;import org.apache.mina.core.filterchain.IoFilterAdapter;import org.apache.mina.core.future.ConnectFuture;import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.core.session.IoSession;import org.apache.mina.filter.codec.ProtocolCodecFactory;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.ProtocolDecoder;import org.apache.mina.filter.codec.ProtocolDecoderAdapter;import org.apache.mina.filter.codec.ProtocolDecoderOutput;import org.apache.mina.filter.codec.ProtocolEncoder;import org.apache.mina.filter.codec.ProtocolEncoderAdapter;import org.apache.mina.filter.codec.ProtocolEncoderOutput;import org.apache.mina.filter.executor.ExecutorFilter;import org.apache.mina.filter.statistic.ProfilerTimerFilter;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;import org.apache.mina.transport.socket.nio.NioSocketConnector;import org.junit.After;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertNull;import static org.junit.Assert.assertEquals;import static org.junit.Assert.fail;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Tests {@link MdcInjectionFilter} in variuos scenarios. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 713957 $, $Date: 2008-11-14 10:27:16 +0100 (Fri, 14 Nov 2008) $ */public class MdcInjectionFilterTest {    private static Logger logger = LoggerFactory.getLogger(MdcInjectionFilterTest.class);    private static final int TIMEOUT = 5000;    private final MyAppender appender = new MyAppender();    private int port;    private NioSocketAcceptor acceptor;    @Before    public void setUp() throws Exception {        // comment out next line if you want to see normal logging        org.apache.log4j.Logger.getRootLogger().removeAllAppenders();        org.apache.log4j.Logger.getRootLogger().setLevel(Level.DEBUG);        org.apache.log4j.Logger.getRootLogger().addAppender(appender);        acceptor = new NioSocketAcceptor();    }    @After    public void tearDown() throws Exception {        acceptor.dispose();    }    @Test    public void testSimpleChain() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        chain.addFirst("mdc-injector", new MdcInjectionFilter());        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        test(chain);    }    @Test    public void testExecutorFilterAtTheEnd() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();        chain.addFirst("mdc-injector1", mdcInjectionFilter);        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        chain.addLast("executor" , new ExecutorFilter());        chain.addLast("mdc-injector2", mdcInjectionFilter);        test(chain);    }    @Test    public void testExecutorFilterAtBeginning() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();        chain.addLast("executor" , new ExecutorFilter());        chain.addLast("mdc-injector", mdcInjectionFilter);        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        test(chain);    }    @Test    public void testExecutorFilterBeforeProtocol() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();        chain.addLast("executor" , new ExecutorFilter());        chain.addLast("mdc-injector", mdcInjectionFilter);        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        test(chain);    }    @Test    public void testMultipleFilters() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();        chain.addLast("executor" , new ExecutorFilter());        chain.addLast("mdc-injector", mdcInjectionFilter);        chain.addLast("profiler", new ProfilerTimerFilter());        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("logger", new LoggingFilter());        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        test(chain);    }    @Test    public void testTwoExecutorFilters() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();        chain.addLast("executor1" , new ExecutorFilter());        chain.addLast("mdc-injector1", mdcInjectionFilter);        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("executor2" , new ExecutorFilter());        // add the MdcInjectionFilter instance after every ExecutorFilter        // it's important to use the same MdcInjectionFilter instance        chain.addLast("mdc-injector2",  mdcInjectionFilter);        test(chain);    }    @Test    public void testOnlyRemoteAddress() throws IOException, InterruptedException {        DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();        chain.addFirst("mdc-injector", new MdcInjectionFilter(            MdcInjectionFilter.MdcKey.remoteAddress));        chain.addLast("dummy", new DummyIoFilter());        chain.addLast("protocol", new ProtocolCodecFilter(new DummyProtocolCodecFactory()));        SimpleIoHandler simpleIoHandler = new SimpleIoHandler();        acceptor.setHandler(simpleIoHandler);        acceptor.bind(new InetSocketAddress(0));        port = acceptor.getLocalAddress().getPort();        acceptor.setFilterChainBuilder(chain);        // create some clients        NioSocketConnector connector = new NioSocketConnector();        connector.setHandler(new IoHandlerAdapter());        connectAndWrite(connector,0);        connectAndWrite(connector,1);        // wait until Iohandler has received all events        simpleIoHandler.messageSentLatch.await();        simpleIoHandler.sessionIdleLatch.await();        simpleIoHandler.sessionClosedLatch.await();        connector.dispose();        // make a copy to prevent ConcurrentModificationException        List<LoggingEvent> events = new ArrayList<LoggingEvent>(appender.events);        // verify that all logging events have correct MDC        for (LoggingEvent event : events) {            for (MdcInjectionFilter.MdcKey mdcKey : MdcInjectionFilter.MdcKey.values()) {              String key = mdcKey.name();              Object value = event.getMDC(key);              if (mdcKey == MdcInjectionFilter.MdcKey.remoteAddress) {                  assertNotNull(                      "MDC[remoteAddress] not set for [" + event.getMessage() + "]", value);              } else {                  assertNull("MDC[" + key + "] set for [" + event.getMessage() + "]", value);              }            }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -