@Test public void structuralIntegration_WcCat_GetStatusCode0() { String[] wcArgs = new String[] {"-m", "-"}; ITool wcTool = new WcTool(wcArgs); String[] catArgs = new String[] {"-"}; ITool catTool = new CatTool(catArgs); String result = wcTool.execute(new File(userDir), "this is a standard input"); result = catTool.execute(new File(userDir), result); assertEquals(0, wcTool.getStatusCode()); assertEquals(0, catTool.getStatusCode()); assertEquals("\t24", result); }
@Test public void structuralIntegration_EchoWc_GetStatusCode0() { String[] echoArgs = new String[] {"testing wc and echo together"}; ITool echoTool = new EchoTool(echoArgs); String[] wcArgs = new String[] {"-w", "-"}; ITool wcTool = new WcTool(wcArgs); String result = echoTool.execute(new File(userDir), "no stdin"); result = wcTool.execute(new File(userDir), result); assertEquals(0, echoTool.getStatusCode()); assertEquals(0, wcTool.getStatusCode()); assertEquals("\t5", result); }
@Test public void structuralIntegration_GrepWc_GetStatusCode0() { String[] grepArgs = new String[] {"-A", "2", "testing", "file1"}; ITool grepTool = new GrepTool(grepArgs); String[] wcArgs = new String[] {"-l", "-"}; ITool wcTool = new WcTool(wcArgs); String result = grepTool.execute(new File(userDir), null); result = wcTool.execute(new File(userDir), result); assertEquals(0, grepTool.getStatusCode()); assertEquals(0, wcTool.getStatusCode()); assertEquals("\t3", result); }
@Test public void structuralIntegration_WcEchoError_GetStatusCodeNot0() { // wc no standard input when it is expected String[] wcArgs = new String[] {"-m", "-"}; ITool wcTool = new WcTool(wcArgs); String[] echoArgs = null; ITool echoTool = new EchoTool(echoArgs); String result = wcTool.execute(new File(userDir), null); assertEquals(WcTool.STATUS_CODE_STDIN_EXPECTED, wcTool.getStatusCode()); result = echoTool.execute(new File(userDir), result); assertEquals(0, echoTool.getStatusCode()); }
@Test public void structuralIntegration_CatWcError_GetStatusCodeNot0() { // cat file does not exist String[] catArgs = new String[] {"fileNotExist"}; ITool catTool = new CatTool(catArgs); String[] wcArgs = new String[] {"-l", "-"}; ITool wcTool = new WcTool(wcArgs); String result = catTool.execute(new File(userDir), null); assertEquals(CatTool.ERR_CODE_FILE_NOT_FOUND, catTool.getStatusCode()); // error message from cat passed to wc result = wcTool.execute(new File(userDir), result); assertEquals(0, wcTool.getStatusCode()); }
@Test public void testRunningPWD() { ITool itool = shell.parse("pwd"); Future<?> future = shell.executeTest(itool); while (!(future.isDone() || future.isCancelled())) { // just wait. } shell.stopTest(future, itool); assertEquals(0, itool.getStatusCode()); }
/** * Ctrl-Z testing * * <p>Step 1. Create a mock up iTool than can run for a long time * * <p>Step 2. Then use the decoupled method runItool to add the new tool to the thread pool and * executing. * * <p>Step 3. Generally wait for a little while to let itool start run * * <p>Step 4. As soon as we detect it running, we can call stopItool to stop it. * * <p>Step 5. We assert the error code to make sure that the itool thread did not reach the final * state where the status code is changes. We also assert the thread state to make sure it is * really stopped. */ @Test public void testTerminatingSleepThread() { // // // This test focused on the waiting thread // which is generally easy to interrupt // // ITool itool = new ITool() { private int statusCode = 1; @Override public String execute(File workingDir, String stdin) { for (int i = 0; i < 1000; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { return null; } } statusCode = 0; return "finished!"; } @Override public int getStatusCode() { return statusCode; } }; Future<?> future = shell.executeTest(itool); boolean result = false; // generally wait for a while try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } while (!(future.isDone() || future.isCancelled())) { result = shell.stopTest(future, itool); } assertTrue(result); assertNotEquals(0, itool.getStatusCode()); }
@Test public void testTerminatingNonStopThread() { // // // This test focused on the forever thread // which is not able to interrupt // // ITool itool = new ITool() { private int statusCode = 1; @Override public String execute(File workingDir, String stdin) { @SuppressWarnings("unused") double a = 0; for (double i = 1; i < 1000000000; i += 0.000000001) { a += i; } statusCode = 0; return "finished!"; } @Override public int getStatusCode() { return statusCode; } }; Future<?> future = shell.executeTest(itool); boolean result = false; // generally wait for a while try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } while (!(future.isDone() || future.isCancelled())) { result = shell.stopTest(future, itool); } assertTrue(result); assertNotEquals(0, itool.getStatusCode()); }
/** * This method is only used for testing * * @param commandlines commands to be executed sequentially * @return The 1st element is the result string. The 2nd element onwards are the resulting status * code from the execution of each of the commands given If the commandline is invalid, the * result string will be "Invalid Command" and the status code will be 1 */ public static Vector<String> shellTestExecution(String... commandlines) { underTest = true; Shell shell = new Shell(); ExecutorService executorSvc = Executors.newSingleThreadExecutor(); Vector<String> resultArray = new Vector<String>(commandlines.length + 1); for (int i = 0; i < commandlines.length; ++i) { ITool tool = shell.parse(commandlines[i]); if (tool == null) { resultArray.add("Invalid Command"); return resultArray; } Runnable runnable = shell.execute(tool); Future<?> taskThread = executorSvc.submit(runnable); while (!taskThread.isDone()) {} resultArray.add(Integer.toString(tool.getStatusCode())); } resultArray.add(0, result); underTest = false; return resultArray; }