Skip to content

Commit 1db3ab0

Browse files
committed
Merge branch '3.1' into 3.2
# Conflicts: # pom.xml Signed-off-by: Richard Lea <chigix@zoho.com>
2 parents f3d20ba + a051a62 commit 1db3ab0

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.chigix</groupId>
55
<artifactId>netty-router</artifactId>
6-
<version>3.2.0-beta</version>
6+
<version>3.2.0-beta1</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -44,13 +44,13 @@
4444
<dependency>
4545
<groupId>org.apache.logging.log4j</groupId>
4646
<artifactId>log4j-slf4j-impl</artifactId>
47-
<version>2.3</version>
47+
<version>2.7</version>
4848
<scope>test</scope>
4949
</dependency>
5050
<dependency>
5151
<groupId>org.apache.logging.log4j</groupId>
5252
<artifactId>log4j-core</artifactId>
53-
<version>2.3</version>
53+
<version>2.7</version>
5454
<scope>test</scope>
5555
</dependency>
5656
</dependencies>

src/main/java/io/netty/handler/codec/http/router/RoutingPathMatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public RoutingPathMatched match(String path) {
169169
* @return Returns {@code null} if there is no {@link Pattern} matched in
170170
* this router.In addtion, the pattern with ":*" would also not be matched
171171
* in any cases.
172+
* @throws IllegalArgumentException
172173
*/
173174
public String generatePath(String name, Object... params) {
174175
if (params.length == 0) {
@@ -181,7 +182,7 @@ public String generatePath(String name, Object... params) {
181182
throw new IllegalArgumentException(MessageFormat.format("Missing value for params: {0}", params[params.length - 1]));
182183
}
183184
final Map map = new HashMap();
184-
for (int i = 0; i < params.length; i++) {
185+
for (int i = 0; i < params.length; i += 2) {
185186
final String key = params[i].toString();
186187
final String value = params[i + 1].toString();
187188
map.put(key, value);

src/test/java/io/netty/handler/codec/http/router/RoutingPathMatcherTest.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
package io.netty.handler.codec.http.router;
1010

1111
import io.netty.handler.codec.http.HttpMethod;
12+
import io.netty.handler.codec.http.router.testutil.Log4jUtil;
13+
import io.netty.util.CharsetUtil;
14+
import java.io.ByteArrayOutputStream;
15+
import junit.framework.Assert;
16+
import org.apache.logging.log4j.Level;
1217
import org.junit.After;
1318
import org.junit.AfterClass;
1419
import org.junit.Before;
@@ -46,14 +51,15 @@ public void tearDown() {
4651
*/
4752
@Test
4853
public void testAdd() {
54+
ByteArrayOutputStream result = new ByteArrayOutputStream();
55+
Log4jUtil.catchLogMessages(result, Level.ALL);
4956
System.out.println("add");
50-
Routing pattern = null;
51-
RoutingPathMatcher instance = new RoutingPathMatcher();
52-
RoutingPathMatcher expResult = null;
53-
RoutingPathMatcher result = instance.add(pattern);
54-
assertEquals(expResult, result);
55-
// TODO review the generated test code and remove the default call to fail.
56-
fail("The test case is a prototype.");
57+
RoutingPathMatcher matcher = new RoutingPathMatcher();
58+
Routing routing_before_delete = new Routing(new RoutingConfig.SimplePathGet("BEFORE_DELETE", "/before/delete"), HttpMethod.GET);
59+
Routing routing_tobe_delete = new Routing(new RoutingConfig.SimplePathGet("BEFORE_DELETE", "/tobe/delete"), HttpMethod.GET);
60+
Routing routing_after_delete = new Routing(new RoutingConfig.SimplePathGet("AFTER_DELETE", "/after/delete"), HttpMethod.GET);
61+
matcher.add(routing_before_delete).add(routing_tobe_delete).add(routing_after_delete);
62+
Assert.assertEquals("There is Routing Override occured in same name: BEFORE_DELETE", new String(result.toByteArray(), CharsetUtil.UTF_8).trim());
5763
}
5864

5965
/**
@@ -95,14 +101,24 @@ public void testMatch() {
95101
@Test
96102
public void testGeneratePath() {
97103
System.out.println("generatePath");
98-
String name = "";
99-
Object[] params = null;
100-
RoutingPathMatcher instance = new RoutingPathMatcher();
101-
String expResult = "";
102-
String result = instance.generatePath(name, params);
103-
assertEquals(expResult, result);
104-
// TODO review the generated test code and remove the default call to fail.
105-
fail("The test case is a prototype.");
104+
RoutingPathMatcher matcher = new RoutingPathMatcher();
105+
Routing plain_path_routing_1 = new Routing(new RoutingConfig.SimplePathGet("plain_path_routing_1", "/tester/plain/get"), HttpMethod.GET);
106+
Routing single_var_routing_1 = new Routing(new RoutingConfig.SimplePathGet("single_var_routing_1", "/tester/var/:var1"), HttpMethod.GET);
107+
Routing dual_var_routing_1 = new Routing(new RoutingConfig.SimplePathGet("dual_var_routing_1", "/tester/var/:var1/var/:var2"), HttpMethod.GET);
108+
matcher.add(plain_path_routing_1).add(single_var_routing_1).add(dual_var_routing_1);
109+
assertEquals("/tester/plain/get", matcher.generatePath("plain_path_routing_1"));
110+
try {
111+
matcher.generatePath("plain_path_routing_1", 123);
112+
fail("Miss IllegalArgumentException thrown.");
113+
} catch (IllegalArgumentException e) {
114+
}
115+
assertEquals("/tester/var/123/var/BANKAI", matcher.generatePath("dual_var_routing_1", "var1", 123, "var2", "BANKAI"));
116+
try {
117+
matcher.generatePath("dual_var_routing_1", "var1", 123, "var2");
118+
fail("Miss IllegalArgumentException thrown.");
119+
} catch (Exception e) {
120+
}
121+
assertEquals("/tester/var/123", matcher.generatePath("single_var_routing_1", "var1", 123));
106122
}
107123

108124
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of the netty-router package.
3+
*
4+
* (c) Richard Lea <chigix@zoho.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
package io.netty.handler.codec.http.router.testutil;
10+
11+
import java.io.OutputStream;
12+
import java.io.PrintWriter;
13+
import org.apache.logging.log4j.Level;
14+
import org.apache.logging.log4j.core.Appender;
15+
import org.apache.logging.log4j.core.LoggerContext;
16+
import org.apache.logging.log4j.core.appender.WriterAppender;
17+
import org.apache.logging.log4j.core.config.Configuration;
18+
import org.apache.logging.log4j.core.config.LoggerConfig;
19+
import org.apache.logging.log4j.core.layout.PatternLayout;
20+
21+
/**
22+
*
23+
* @author Richard Lea <chigix@zoho.com>
24+
*/
25+
public class Log4jUtil {
26+
27+
/**
28+
* Try to get all messages through Log4j via adding a one-time used
29+
* appender.
30+
* http://logging.apache.org/log4j/2.x/manual/customconfig.html#AppendingToWritersAndOutputStreams
31+
*
32+
* @param out
33+
* @param level
34+
*/
35+
public static void catchLogMessages(OutputStream out, Level level) {
36+
LoggerContext ctx = LoggerContext.getContext(false);
37+
Configuration conf = ctx.getConfiguration();
38+
PatternLayout layout = PatternLayout.createDefaultLayout();
39+
Appender appender = WriterAppender.createAppender(layout, null, new PrintWriter(out), "LogCatcher", false, true);
40+
appender.start();
41+
conf.addAppender(appender);
42+
for (LoggerConfig loggerConf : conf.getLoggers().values()) {
43+
loggerConf.addAppender(appender, level, null);
44+
}
45+
conf.getRootLogger().addAppender(appender, level, null);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)