MyJavaServer的帐号注册Java测试题

最近一直在找免费的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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值