* Added MapUtilTest
* Added SilentLoggerFactory for a testing purpose
This commit is contained in:
parent
d2c157d32d
commit
c84b1bc139
@ -29,8 +29,6 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import org.jboss.netty.channel.ChannelHandler;
|
|
||||||
import org.jboss.netty.channel.SimpleChannelHandler;
|
|
||||||
import org.jboss.netty.logging.InternalLogger;
|
import org.jboss.netty.logging.InternalLogger;
|
||||||
import org.jboss.netty.logging.InternalLoggerFactory;
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
@ -45,8 +43,9 @@ public class MapUtil {
|
|||||||
private static final InternalLogger logger =
|
private static final InternalLogger logger =
|
||||||
InternalLoggerFactory.getInstance(MapUtil.class);
|
InternalLoggerFactory.getInstance(MapUtil.class);
|
||||||
|
|
||||||
public static boolean isOrderedMap(Map<String, ChannelHandler> map) {
|
@SuppressWarnings("unchecked")
|
||||||
Class<Map<String, ChannelHandler>> mapType = getMapClass(map);
|
public static boolean isOrderedMap(Map<?, ?> map) {
|
||||||
|
Class<?> mapType = map.getClass();
|
||||||
if (LinkedHashMap.class.isAssignableFrom(mapType)) {
|
if (LinkedHashMap.class.isAssignableFrom(mapType)) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(mapType.getSimpleName() + " is an ordered map.");
|
logger.debug(mapType.getSimpleName() + " is an ordered map.");
|
||||||
@ -90,9 +89,9 @@ public class MapUtil {
|
|||||||
"default constructor and test if insertion order is " +
|
"default constructor and test if insertion order is " +
|
||||||
"maintained.");
|
"maintained.");
|
||||||
|
|
||||||
Map<String, ChannelHandler> newMap;
|
Map newMap;
|
||||||
try {
|
try {
|
||||||
newMap = mapType.newInstance();
|
newMap = (Map) mapType.newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
@ -103,20 +102,20 @@ public class MapUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
List<String> expectedNames = new ArrayList<String>();
|
List<String> expectedKeys = new ArrayList<String>();
|
||||||
ChannelHandler dummyHandler = new SimpleChannelHandler();
|
String dummyValue = "dummyValue";
|
||||||
for (int i = 0; i < 65536; i ++) {
|
for (int i = 0; i < 10000; i ++) {
|
||||||
String filterName;
|
String key;
|
||||||
do {
|
do {
|
||||||
filterName = String.valueOf(rand.nextInt());
|
key = String.valueOf(rand.nextInt());
|
||||||
} while (newMap.containsKey(filterName));
|
} while (newMap.containsKey(key));
|
||||||
|
|
||||||
newMap.put(filterName, dummyHandler);
|
newMap.put(key, dummyValue);
|
||||||
expectedNames.add(filterName);
|
expectedKeys.add(key);
|
||||||
|
|
||||||
Iterator<String> it = expectedNames.iterator();
|
Iterator<String> it = expectedKeys.iterator();
|
||||||
for (Object key: newMap.keySet()) {
|
for (Object actualKey: newMap.keySet()) {
|
||||||
if (!it.next().equals(key)) {
|
if (!it.next().equals(actualKey)) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"The specified map didn't pass the insertion " +
|
"The specified map didn't pass the insertion " +
|
||||||
@ -131,12 +130,6 @@ public class MapUtil {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static Class<Map<String, ChannelHandler>> getMapClass(
|
|
||||||
Map<String, ChannelHandler> map) {
|
|
||||||
return (Class<Map<String, ChannelHandler>>) map.getClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
private MapUtil() {
|
private MapUtil() {
|
||||||
// Unused
|
// Unused
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,8 @@ import org.jboss.netty.channel.ChannelFactory;
|
|||||||
import org.jboss.netty.channel.ChannelHandler;
|
import org.jboss.netty.channel.ChannelHandler;
|
||||||
import org.jboss.netty.channel.ChannelPipeline;
|
import org.jboss.netty.channel.ChannelPipeline;
|
||||||
import org.jboss.netty.channel.ChannelPipelineFactory;
|
import org.jboss.netty.channel.ChannelPipelineFactory;
|
||||||
import org.jboss.netty.channel.DefaultChannelPipeline;
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||||
import org.junit.AfterClass;
|
import org.jboss.netty.util.SilentLoggerFactory;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -277,14 +277,6 @@ public class BootstrapTest {
|
|||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
// DefaultChannelPipeline will generate expected warning messages.
|
InternalLoggerFactory.setDefaultFactory(new SilentLoggerFactory());
|
||||||
// Suppress them.
|
|
||||||
Logger.getLogger(DefaultChannelPipeline.class.getName()).setLevel(Level.SEVERE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void tearDown() {
|
|
||||||
// Revert the logger settings back.
|
|
||||||
Logger.getLogger(DefaultChannelPipeline.class.getName()).setLevel(Level.INFO);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
135
src/test/java/org/jboss/netty/util/MapUtilTest.java
Normal file
135
src/test/java/org/jboss/netty/util/MapUtilTest.java
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* JBoss, Home of Professional Open Source
|
||||||
|
*
|
||||||
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
||||||
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
||||||
|
* full listing of individual contributors.
|
||||||
|
*
|
||||||
|
* This is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Lesser General Public License as
|
||||||
|
* published by the Free Software Foundation; either version 2.1 of
|
||||||
|
* the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This software is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this software; if not, write to the Free
|
||||||
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
||||||
|
*/
|
||||||
|
package org.jboss.netty.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
|
*
|
||||||
|
* @version $Rev$, $Date$
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MapUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnTrueIfLinkedHashMap() {
|
||||||
|
assertTrue(MapUtil.isOrderedMap(new LinkedHashMap<String, String>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnTrueIfMapImplementsOrderedMap() {
|
||||||
|
assertTrue(MapUtil.isOrderedMap(new DummyOrderedMap<String, String>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfMapHasNoDefaultConstructor() {
|
||||||
|
assertFalse(MapUtil.isOrderedMap(
|
||||||
|
new MapWithoutDefaultConstructor<String, String>(
|
||||||
|
new HashMap<String, String>())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnFalseIfMapIsNotOrdered() {
|
||||||
|
assertFalse(MapUtil.isOrderedMap(new HashMap<String, String>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnTrueIfMapIsOrdered() {
|
||||||
|
assertTrue(MapUtil.isOrderedMap(new UnknownOrderedMap<String, String>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() {
|
||||||
|
InternalLoggerFactory.setDefaultFactory(new SilentLoggerFactory());
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OrderedMap {
|
||||||
|
// A tag interface
|
||||||
|
}
|
||||||
|
|
||||||
|
static class DummyOrderedMap<K,V> extends AbstractMap<K, V> implements OrderedMap {
|
||||||
|
|
||||||
|
private final Map<K, V> map = new HashMap<K, V>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Entry<K, V>> entrySet() {
|
||||||
|
return map.entrySet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class MapWithoutDefaultConstructor<K, V> extends AbstractMap<K, V> {
|
||||||
|
private final Map<K, V> map;
|
||||||
|
|
||||||
|
MapWithoutDefaultConstructor(Map<K, V> map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Entry<K, V>> entrySet() {
|
||||||
|
return map.entrySet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class UnknownOrderedMap<K,V> extends AbstractMap<K, V> {
|
||||||
|
|
||||||
|
private final Map<K, V> map = new LinkedHashMap<K, V>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsKey(Object key) {
|
||||||
|
return map.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return map.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V put(K key, V value) {
|
||||||
|
return map.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<K> keySet() {
|
||||||
|
return map.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Entry<K, V>> entrySet() {
|
||||||
|
return map.entrySet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
97
src/test/java/org/jboss/netty/util/SilentLoggerFactory.java
Normal file
97
src/test/java/org/jboss/netty/util/SilentLoggerFactory.java
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* JBoss, Home of Professional Open Source
|
||||||
|
*
|
||||||
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
||||||
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
||||||
|
* full listing of individual contributors.
|
||||||
|
*
|
||||||
|
* This is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU Lesser General Public License as
|
||||||
|
* published by the Free Software Foundation; either version 2.1 of
|
||||||
|
* the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This software is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this software; if not, write to the Free
|
||||||
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
||||||
|
*/
|
||||||
|
package org.jboss.netty.util;
|
||||||
|
|
||||||
|
import org.jboss.netty.logging.InternalLogger;
|
||||||
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger factory which suppresses all log messages.
|
||||||
|
*
|
||||||
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
|
*
|
||||||
|
* @version $Rev$, $Date$
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SilentLoggerFactory extends InternalLoggerFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InternalLogger newInstance(final String name) {
|
||||||
|
return new InternalLogger() {
|
||||||
|
public void debug(String msg) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void debug(String msg, Throwable cause) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error(String msg) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error(String msg, Throwable cause) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void info(String msg) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void info(String msg, Throwable cause) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDebugEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isErrorEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isInfoEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWarnEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void warn(String msg) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
public void warn(String msg, Throwable cause) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user