public static void main(String[] args) throws Exception { String name = args.length > 0 ? args[0] : "app"; Mocker mocker = Mocker.init(name); String webXml = mocker.getWebXml(); int port = mocker.getPort(); File fapp = new File(APP_PATH); final Connector conn = new SelectChannelConnector(); conn.setPort(port); conn.setMaxIdleTime(60000); conn.setHeaderBufferSize(20480); // 缺省4K, Chrome下可能会报错: 413 FULL head Listener listerner = new Listener() { @Override public void lifeCycleFailure(LifeCycle arg0, Throwable arg1) { logger.info("YDJT SYSTEM {} FAILED!!!", Application.VERSION); } @Override public void lifeCycleStarted(LifeCycle arg0) { logger.info("YDJT SYSTEM {} STARTED!", Application.VERSION); } @Override public void lifeCycleStarting(LifeCycle arg0) { logger.info("YDJT SYSTEM {} STARTING...", Application.VERSION); } @Override public void lifeCycleStopped(LifeCycle arg0) { logger.info("YDJT SYSTEM {} STOPPED!", Application.VERSION); } @Override public void lifeCycleStopping(LifeCycle arg0) { logger.info("YDJT SYSTEM {} STOPPING...", Application.VERSION); } }; Server server = new Server(); server.addLifeCycleListener(listerner); server.addConnector(conn); WebAppContext wapp = new WebAppContext(server, fapp.getCanonicalPath(), CONTEXT_PATH); HandlerCollection hc = new HandlerCollection(); ContextHandlerCollection chc = new ContextHandlerCollection(); hc.setHandlers(new Handler[] {chc, new DefaultHandler()}); ContextDeployer cd = new ContextDeployer(); cd.setContexts(chc); cd.setConfigurationDir(APP_PATH); cd.setDirectory(APP_PATH); cd.setScanInterval(5); wapp.setClassLoader(ClassLoader.getSystemClassLoader()); wapp.setParentLoaderPriority(false); wapp.setExtractWAR(false); // setDescriptor() -- 使用其他文件作为web.xml // wapp.setDefaultsDescriptor(WEB_XML); // setWelcomeFiles()和web.xml中的welcome-file-list都没有用, // 因为/已定义在test-mvc的servlet-mapping中?? wapp.setDescriptor(APP_PATH + "/WEB-INF/" + webXml); // server.addLifeCycle(wapp); server.addHandler(hc); server.start(); server.join(); wapp.start(); server.addLifeCycle(cd); }
boolean dodata(ServletTest testcase) throws Exception { boolean pass = true; String baseline; // Create request and response objects Mocker mocker = new Mocker("dap4", testcase.makeurl(RequestMode.DAP), this); byte[] byteresult = null; // output // See if the servlet can process this try { byteresult = mocker.execute(); } catch (Throwable t) { System.out.println(testcase.xfail ? "XFail" : "Fail"); t.printStackTrace(); return testcase.xfail; } if (prop_debug || DEBUG) { DapDump.dumpbytestream(byteresult, ByteOrder.nativeOrder(), "TestServlet.dodata"); } if (!testcase.xfail && prop_generate) { // Dump the serialization into a file; this also includes the dmr String target = testcase.generatepath + ".raw"; writefile(target, byteresult); } if (DEBUG) { System.out.println("///////////////////"); ByteBuffer datab = ByteBuffer.wrap(byteresult).order(ByteOrder.nativeOrder()); DapDump.dumpbytes(datab, true); System.out.println("///////////////////"); System.out.flush(); } // Setup a ChunkInputStream ByteArrayInputStream bytestream = new ByteArrayInputStream(byteresult); ChunkInputStream reader = new ChunkInputStream(bytestream, RequestMode.DAP, ByteOrder.nativeOrder()); String sdmr = reader.readDMR(); // Read the DMR if (prop_visual) visual(testcase.title + ".dmr.dap", sdmr); Dump printer = new Dump(); String sdata = printer.dumpdata(reader, testcase.checksumming, reader.getByteOrder(), testcase.template); if (prop_visual) visual(testcase.title + ".dap", sdata); if (!testcase.xfail && prop_baseline) writefile(testcase.baselinepath + ".dap", sdata); if (prop_diff) { // compare with baseline // Read the baseline file System.out.println("Note Comparison:"); String baselinecontent = readfile(testcase.baselinepath + ".dap"); pass = compare(baselinecontent, sdata); System.out.println(pass ? "Pass" : "Fail"); } return pass; }