diff --git a/pom.xml b/pom.xml index 2c77e41..266b6b6 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.chigix netty-router - 3.2.0-beta + 3.2.0-beta1 jar UTF-8 @@ -44,13 +44,13 @@ org.apache.logging.log4j log4j-slf4j-impl - 2.3 + 2.7 test org.apache.logging.log4j log4j-core - 2.3 + 2.7 test diff --git a/src/main/java/io/netty/handler/codec/http/router/RoutingPathMatcher.java b/src/main/java/io/netty/handler/codec/http/router/RoutingPathMatcher.java index 50f06d9..48a16c2 100644 --- a/src/main/java/io/netty/handler/codec/http/router/RoutingPathMatcher.java +++ b/src/main/java/io/netty/handler/codec/http/router/RoutingPathMatcher.java @@ -169,6 +169,7 @@ public RoutingPathMatched match(String path) { * @return Returns {@code null} if there is no {@link Pattern} matched in * this router.In addtion, the pattern with ":*" would also not be matched * in any cases. + * @throws IllegalArgumentException */ public String generatePath(String name, Object... params) { if (params.length == 0) { @@ -181,7 +182,7 @@ public String generatePath(String name, Object... params) { throw new IllegalArgumentException(MessageFormat.format("Missing value for params: {0}", params[params.length - 1])); } final Map map = new HashMap(); - for (int i = 0; i < params.length; i++) { + for (int i = 0; i < params.length; i += 2) { final String key = params[i].toString(); final String value = params[i + 1].toString(); map.put(key, value); diff --git a/src/test/java/io/netty/handler/codec/http/router/RoutingPathMatcherTest.java b/src/test/java/io/netty/handler/codec/http/router/RoutingPathMatcherTest.java index 0f5cd58..a3af994 100644 --- a/src/test/java/io/netty/handler/codec/http/router/RoutingPathMatcherTest.java +++ b/src/test/java/io/netty/handler/codec/http/router/RoutingPathMatcherTest.java @@ -9,6 +9,11 @@ package io.netty.handler.codec.http.router; import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.router.testutil.Log4jUtil; +import io.netty.util.CharsetUtil; +import java.io.ByteArrayOutputStream; +import junit.framework.Assert; +import org.apache.logging.log4j.Level; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -46,14 +51,15 @@ public void tearDown() { */ @Test public void testAdd() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + Log4jUtil.catchLogMessages(result, Level.ALL); System.out.println("add"); - Routing pattern = null; - RoutingPathMatcher instance = new RoutingPathMatcher(); - RoutingPathMatcher expResult = null; - RoutingPathMatcher result = instance.add(pattern); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); + RoutingPathMatcher matcher = new RoutingPathMatcher(); + Routing routing_before_delete = new Routing(new RoutingConfig.SimplePathGet("BEFORE_DELETE", "/before/delete"), HttpMethod.GET); + Routing routing_tobe_delete = new Routing(new RoutingConfig.SimplePathGet("BEFORE_DELETE", "/tobe/delete"), HttpMethod.GET); + Routing routing_after_delete = new Routing(new RoutingConfig.SimplePathGet("AFTER_DELETE", "/after/delete"), HttpMethod.GET); + matcher.add(routing_before_delete).add(routing_tobe_delete).add(routing_after_delete); + Assert.assertEquals("There is Routing Override occured in same name: BEFORE_DELETE", new String(result.toByteArray(), CharsetUtil.UTF_8).trim()); } /** @@ -95,14 +101,24 @@ public void testMatch() { @Test public void testGeneratePath() { System.out.println("generatePath"); - String name = ""; - Object[] params = null; - RoutingPathMatcher instance = new RoutingPathMatcher(); - String expResult = ""; - String result = instance.generatePath(name, params); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); + RoutingPathMatcher matcher = new RoutingPathMatcher(); + Routing plain_path_routing_1 = new Routing(new RoutingConfig.SimplePathGet("plain_path_routing_1", "/tester/plain/get"), HttpMethod.GET); + Routing single_var_routing_1 = new Routing(new RoutingConfig.SimplePathGet("single_var_routing_1", "/tester/var/:var1"), HttpMethod.GET); + Routing dual_var_routing_1 = new Routing(new RoutingConfig.SimplePathGet("dual_var_routing_1", "/tester/var/:var1/var/:var2"), HttpMethod.GET); + matcher.add(plain_path_routing_1).add(single_var_routing_1).add(dual_var_routing_1); + assertEquals("/tester/plain/get", matcher.generatePath("plain_path_routing_1")); + try { + matcher.generatePath("plain_path_routing_1", 123); + fail("Miss IllegalArgumentException thrown."); + } catch (IllegalArgumentException e) { + } + assertEquals("/tester/var/123/var/BANKAI", matcher.generatePath("dual_var_routing_1", "var1", 123, "var2", "BANKAI")); + try { + matcher.generatePath("dual_var_routing_1", "var1", 123, "var2"); + fail("Miss IllegalArgumentException thrown."); + } catch (Exception e) { + } + assertEquals("/tester/var/123", matcher.generatePath("single_var_routing_1", "var1", 123)); } } diff --git a/src/test/java/io/netty/handler/codec/http/router/testutil/Log4jUtil.java b/src/test/java/io/netty/handler/codec/http/router/testutil/Log4jUtil.java new file mode 100644 index 0000000..108f46d --- /dev/null +++ b/src/test/java/io/netty/handler/codec/http/router/testutil/Log4jUtil.java @@ -0,0 +1,48 @@ +/* + * This file is part of the netty-router package. + * + * (c) Richard Lea + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +package io.netty.handler.codec.http.router.testutil; + +import java.io.OutputStream; +import java.io.PrintWriter; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.WriterAppender; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.layout.PatternLayout; + +/** + * + * @author Richard Lea + */ +public class Log4jUtil { + + /** + * Try to get all messages through Log4j via adding a one-time used + * appender. + * http://logging.apache.org/log4j/2.x/manual/customconfig.html#AppendingToWritersAndOutputStreams + * + * @param out + * @param level + */ + public static void catchLogMessages(OutputStream out, Level level) { + LoggerContext ctx = LoggerContext.getContext(false); + Configuration conf = ctx.getConfiguration(); + PatternLayout layout = PatternLayout.createDefaultLayout(); + Appender appender = WriterAppender.createAppender(layout, null, new PrintWriter(out), "LogCatcher", false, true); + appender.start(); + conf.addAppender(appender); + for (LoggerConfig loggerConf : conf.getLoggers().values()) { + loggerConf.addAppender(appender, level, null); + } + conf.getRootLogger().addAppender(appender, level, null); + } + +}