📄 runtest.java
字号:
// check null's
run.execute(request, response, null, null);
assertEquals("0,\u0000,0.0,0.0,0,0,false,null", TestRunObj.getParamStr());
// check values
Object[] args = {"12", "xyz", "11", "11.58", "2", "1", "true", "abcd"};
run.execute(request, response, args, null);
assertEquals("12,x,11.0,11.58,2,1,true,abcd", TestRunObj.getParamStr());
}
public void testParamsObj() throws Exception {
Run run = new Run();
run.setClassStr(TestRunObj.class.getName());
run.setMethodStr("runWithObjParam(Integer , Character , Double , Float , Short , Byte , Boolean , String )");
run.initialise(servletContext);
assertTrue("Should be initialised " + run.getError(), run.isValid());
assertEquals("runWithObjParam(java.lang.Integer, java.lang.Character, java.lang.Double, java.lang.Float, " +
"java.lang.Short, java.lang.Byte, java.lang.Boolean, java.lang.String)", run.getMethodSignature());
// check null's
run.execute(request, response, null, null);
assertEquals("null,null,null,null,null,null,null,null", TestRunObj.getParamStr());
// check values
Object[] args = {"12", "xyz", "11", "11.58", "2", "1", "true", "abcd"};
run.execute(request, response, args, null);
assertEquals("12,x,11.0,11.58,2,1,true,abcd", TestRunObj.getParamStr());
}
private Throwable doExceptionRun(String methodName) {
Run run = new Run();
run.setClassStr(TestRunObj.class.getName());
run.setMethodStr(methodName);
run.initialise(servletContext);
assertTrue("Should be initialised, but: " + run.getError(), run.isValid());
Throwable throwableViaRun = null;
System.out.println("this...");
try {
run.execute(request, response, null, null);
} catch (Throwable t) {
throwableViaRun = t;
t.printStackTrace(System.out);
}
//noinspection ConstantConditions
return throwableViaRun.getCause();
}
public void testRunExceptionMethod() {
Throwable throwableViaRun = doExceptionRun("runNullPointerException");
Throwable throwableRaw = null;
System.out.println("should look the same as this...");
try {
new TestRunObj().runNullPointerException(null, null);
} catch (Throwable t) {
throwableRaw = t;
t.printStackTrace(System.out);
}
//noinspection ConstantConditions
assertEquals(throwableRaw.toString(), throwableViaRun.toString());
}
public void testRunServletExceptionMethod() {
Throwable throwableViaRun = doExceptionRun("runServletException");
Throwable throwableRaw = null;
System.out.println("should look the same as this...");
TestRunObj testRunObj = new TestRunObj();
try {
testRunObj.runServletException(null, null);
} catch (Throwable t) {
throwableRaw = t;
t.printStackTrace(System.out);
}
//noinspection ConstantConditions
assertEquals(throwableRaw.toString(), throwableViaRun.toString());
}
public void testRunCustomExceptionMethod() {
Throwable throwableViaRun = doExceptionRun("runCustomException");
Throwable throwableRaw = null;
System.out.println("should look the same as this...");
TestRunObj testRunObj = new TestRunObj();
try {
testRunObj.runCustomException(null, null);
} catch (Throwable t) {
throwableRaw = t;
t.printStackTrace(System.out);
}
//noinspection ConstantConditions
assertEquals(throwableRaw.toString(), throwableViaRun.toString());
}
public void testRunIOExceptionMethod() {
Throwable throwableViaRun = doExceptionRun("runIOException");
Throwable throwableRaw = null;
System.out.println("should look the same as this...");
TestRunObj testRunObj = new TestRunObj();
try {
testRunObj.runIOException(null, null);
} catch (Throwable t) {
throwableRaw = t;
t.printStackTrace(System.out);
}
//noinspection ConstantConditions
assertEquals(throwableRaw.toString(), throwableViaRun.toString());
}
public void testRunRuntimeExceptionMethod() {
Throwable throwableViaRun = doExceptionRun("runRuntiumeException");
Throwable throwableRaw = null;
System.out.println("should look the same as this...");
TestRunObj testRunObj = new TestRunObj();
try {
testRunObj.runRuntiumeException(null, null);
} catch (Throwable t) {
throwableRaw = t;
t.printStackTrace(System.out);
}
//noinspection ConstantConditions
assertEquals(throwableRaw.toString(), throwableViaRun.toString());
}
public void testRunPrivateMethod() throws IOException, ServletException, InvocationTargetException {
Run run = new Run();
run.setClassStr(TestRunObj.class.getName());
run.setMethodStr("privateRun");
run.initialise(servletContext);
assertFalse("Should not be initialised " + run.getError(), run.isValid());
run.execute(request, response, null, null);
// Should not error just do nothing, check log msgs
}
public void testRunNewEach() throws IOException, ServletException, InvocationTargetException {
Run run = new Run();
run.setClassStr(TestRunObj.class.getName());
run.setNewEachTime(true);
run.initialise(servletContext);
assertTrue("Should not have been created yet", TestRunObj.getCreatedCount() == 0);
run.execute(request, response, null, null);
assertTrue("Should be created now", TestRunObj.getCreatedCount() == 1);
run.execute(request, response, null, null);
assertTrue("Should be created twice", TestRunObj.getCreatedCount() == 2);
assertTrue("Should be destroyed", TestRunObj.isDestroyCalled());
}
public void testInitParams() {
Run run = new Run();
run.setClassStr(TestRunObj.class.getName());
run.addInitParam("horse", "golden");
run.addInitParam("debs", "nightout");
run.initialise(servletContext);
assertEquals("golden", TestRunObj.getTestServletConfig().getInitParameter("horse"));
assertEquals("nightout", TestRunObj.getTestServletConfig().getInitParameter("debs"));
}
public void testRuleNoToWithRun() throws IOException, ServletException, InvocationTargetException {
Run run = new Run();
run.setClassStr(TestRunObj.class.getName());
run.setMethodStr("run");
NormalRule rule = new NormalRule();
rule.setFrom("from");
rule.addRun(run);
rule.initialise(null);
MockRequest request = new MockRequest("from");
RewrittenUrl rewrittenUrl = rule.matches(request.getRequestURI(), request, response);
assertNull(rewrittenUrl);
assertTrue(TestRunObj.isRunCalled());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -