MyJavaServer的帐号注册Java测试题
2006-10-02 23:41
344 查看
最近一直在找免费的j2ee空间,发现MyJavaServer很不错,免费并且支持java5.0
有趣的是注册必须要先完成一道java测试题,他的在线编译和测试用例运行比较有意思
,题目不难
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:
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;
}
}
有趣的是注册必须要先完成一道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;
}
}
相关文章推荐
- .NET/ASP.NET/C#/WCF/SQL Server/My SQL/Java/JSP/JDBC/Spring/Spring MVC/PHP/Python/Ruby/Shell/Agile/CSS/HTML/HTTP/Unix/Linux大量PDF书籍/电子书籍下载, Effective Java 下载
- 用JAVA实现自己的MyWebServer
- 立即注册Oracle虚拟开发者日:Oracle WebLogic Server & Java EE (Virtual Developer Day: A New Breed of Events for Java developers )
- Java注册帐号邮箱激活验证实现
- www.myjavaserver.com免费空间中Hypersonic 数据库的连接
- java web开发一个帐号同一时间只能一个人登录
- Using Sockets in Java - Server
- java.io.IOException: Unable to parse response from server
- JAVA测试题二
- Java Server Faces (JSF) Tutorial
- How do I reboot my AIX server?
- eclipse 启动 tomcat 报错:Server mylocalhost was unable to start within 45 seconds
- java server 多client异步socket通信demo
- 用JavaServer Faces开发Web应用(1)
- Java中ServerSocket源码
- 关闭 mapxtreme java server 时的疑问
- tomcat:Could not publish to the server. java.lang.IndexOutOfBoundsException
- 批判性地看待一种可行的表示技术:JavaServerPages se
- 从0开始学Java——eclipse下运行servlet程序警告:Setting property 'source' to 'org.eclipse.jst.jee.server:类名' did not find a matching property.
- Could not publish to the server. Java.lang.NullPointerException