注册MyJavaServer的帐号的方法
注册MyJavaServer的帐号的方法
最近一直在找免费的j2ee空间,发现MyJavaServer很不错,免费并且支持java5.0有趣的是注册必须要先完成一道java测试题,他的在线编译和测试用例运行比较有意思
,题目不难
Signup Challenge
Master a simple Java programming challenge (STATUS: NOT PASSED)As the principal engineer of an HTTP web server, you are responsible for implementing the request processing subsystem of the server.
An incoming request for a specific resource, identified by an URI, must be dispatched to the appropriate handler according to the server configuration which maps URIs to request handlers. 'HandlerFactory.getHandler' must be implemented:
public class HandlerFactory { public String getHandler(String[] config, String requestUri) { } }
The string array 'config' contains URI patterns and handler names. Two consecutive values form a key-value pair comprised of URI pattern and handler. 'requestUri' represents an incoming request, the URI to match against the configured handlers. 'getHandler' must return the correct handler for a given URI as a string value.
An URI pattern never contains wildcards and represents the start of an URI string, a prefix. Matching must be implemented accordingly. The handler with the longest matching URI pattern wins if more than one pattern matches. If no handler can be found, "mKgVEAQ" must be returned.
Example input:
String[] config: { "/", "MainServlet", "/nav", "NavigationServlet" }
String requestUri: "/nav/test"
Correct result: "NavigationServlet"
In this example, the configuration contains a mapping of "/" to "MainServlet" and "/nav" to "NavigationServlet". In the case of an incoming URI "/nav/test.nav", "NavigationServlet" is the correct choice because its pattern is longer than that of "MainServlet".
把代码贴到题目下面的文本编辑框里,后台会自动编译并测试,如果运行通过,则开放注册,否则给出错误提示
--------------------------------
这里给出我的实现:
按照TDD,先写测试用例
import org.junit.Test;
public class HandlerFactoryTest {
private HandlerFactory handlerFactory = new HandlerFactory();
@Test
public void testGetHandler() {
String[] pattern = { "/", "MainServlet", "/nav", "NavServlet",
"/nav/filter", "FilterServlet" };
assertEquals("MainServlet", handlerFactory.getHandler(pattern, "/"));
assertEquals("NavServlet", handlerFactory.getHandler(pattern, "/nav"));
assertEquals("NavServlet", handlerFactory.getHandler(pattern, "/nav/"));
assertEquals("FilterServlet", handlerFactory.getHandler(pattern,
"/nav/filter"));
assertEquals("myqPnW", handlerFactory.getHandler(pattern, "n/filter"));
assertEquals("myqPnW", handlerFactory.getHandler(pattern, "nomapping"));
assertEquals("TestServlet2", handlerFactory.getHandler(new String[] {
"/test2", "TestServlet2", "/test", "TestServlet" }, "/test2/"));
}
}
下面是实现:
public class HandlerFactory {
public static final String NO_MAPPING = "myqPnW";
public String getHandler(String[] config, String requestUri) {
String mappingServlet = NO_MAPPING;
int preMappingLength = 0;
for (int i = 0; i < config.length; i = i + 2) {
String pattern = config[i];
if (requestUri.startsWith(pattern) && pattern.length() > preMappingLength) {
mappingServlet = config[i + 1];
preMappingLength = pattern.length();
}
}
return mappingServlet;
}
}