Example #1
0
  @Test
  public void testParseCommandDelete() {
    String cmd = "delete file1";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("DeleteTool", resultTool.getClass().getSimpleName());
  }
Example #2
0
  @Test
  public void testParseCommandCopy() {
    String cmd = "copy file1 file2";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("CopyTool", resultTool.getClass().getSimpleName());
  }
Example #3
0
  @Test
  public void testParseCommandWc() {
    String cmd = "wc testfile.txt";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("WcTool", resultTool.getClass().getSimpleName());
  }
Example #4
0
  @Test
  public void testParseCommandWithInconsistentSpace() {
    String cmd = "  eChO      sOme   wOrd   ";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("EchoTool", resultTool.getClass().getSimpleName());
  }
Example #5
0
  @Test
  public void testParseCommandPiping() {
    String cmd = "cmd-1 | cmd-2";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("PipingTool", resultTool.getClass().getSimpleName());
  }
Example #6
0
  @Test
  public void testParseCommandMove() {
    String cmd = "move path1 path2";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("MoveTool", resultTool.getClass().getSimpleName());
  }
Example #7
0
  @Test
  public void testParseCommandLs() {
    String cmd = "ls";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("LsTool", resultTool.getClass().getSimpleName());
  }
Example #8
0
  @Test
  public void testParseCommandEcho() {
    String cmd = "echo some word";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("EchoTool", resultTool.getClass().getSimpleName());
  }
Example #9
0
  @Test
  public void testParseCommandCd() {
    String cmd = "cd path/new";
    ITool result = shell.parse(cmd);

    assertNotNull(result);
    assertEquals("CdTool", result.getClass().getSimpleName());
  }
Example #10
0
  @Test
  public void testParseCommandCat() {
    String cmd = "cat path/to/file1";
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);
    assertEquals("CatTool", resultTool.getClass().getSimpleName());
  }
Example #11
0
  @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());
  }
Example #12
0
  /**
   * 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());
  }
Example #13
0
  @Test
  public void testParseCommandWithEscapedPipe2()
      throws IllegalArgumentException, IllegalAccessException {
    String cmd = "cat testCase_3.txt | grep [a|b]";
    ITool resultTool = shell.parse(cmd);

    String[] expected = {"cat testCase_3.txt", "grep [a", "b]"};
    Field fields[] = resultTool.getClass().getSuperclass().getDeclaredFields();
    for (Field field : fields) {
      if (field.getName().equals("args")) {
        field.setAccessible(true);
        List<String> args = Arrays.asList((String[]) field.get(resultTool));
        for (int i = 0; i < expected.length; i++) {
          assertEquals(expected[i], args.get(i));
        }
      }
    }
  }
Example #14
0
  @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());
  }
Example #15
0
  @Test
  public void testParseCommandWithQuotes() throws IllegalArgumentException, IllegalAccessException {
    String cmd = "ls \"normal\" 'test's";
    String[] expected = {"normal", "tests"};
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);

    Field fields[] = resultTool.getClass().getSuperclass().getDeclaredFields();
    for (Field field : fields) {
      if (field.getName().equals("args")) {
        field.setAccessible(true);
        List<String> args = Arrays.asList((String[]) field.get(resultTool));
        for (int i = 0; i < expected.length; i++) {
          assertEquals(expected[i], args.get(i));
        }
      }
    }
  }
Example #16
0
  @Test
  public void testParseCommandWithMixedQuotesandEscapedSpaceandDuplicateMatch()
      throws IllegalArgumentException, IllegalAccessException {
    String cmd = "echo \"normal\" as\"as'\\ r\\$'w\\ e\"d's d's dd\\ sa\\$";
    String[] expected = {"normal", "asas'\\ r$'w\\ eds ds", "dd sa$"};
    ITool resultTool = shell.parse(cmd);

    assertNotNull(resultTool);

    Field fields[] = resultTool.getClass().getSuperclass().getDeclaredFields();
    for (Field field : fields) {
      if (field.getName().equals("args")) {
        field.setAccessible(true);
        List<String> args = Arrays.asList((String[]) field.get(resultTool));
        for (int i = 0; i < expected.length; i++) {
          assertEquals(expected[i], args.get(i));
        }
      }
    }
  }
Example #17
0
  @Override
  public void run() {

    File workingDir = new File(System.getProperty("user.dir"));
    String result = tool.execute(workingDir, stdIn);
    if (!result.isEmpty()) {
      if (!Shell.underTest) System.out.println(result);
      Shell.result = result;
    } else Shell.result = "";
    if (!Shell.underTest) System.out.print(System.getProperty("user.dir") + " >> ");
  }
Example #18
0
  /**
   * 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;
  }
  @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());
  }