Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. /**
  2. * mainメソッド実装クラスをテストする基底クラス。
  3. */
  4. public abstract class AbstractMainTester {
  5. private static final String ARGS_DELIMITER = " ";
  6. protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
  7. private static final PrintStream stdOut = System.out;
  8. private static final PrintStream stdErr = System.err;
  9. private static final SecurityManager securityManager = System.getSecurityManager();
  10.  
  11. /**
  12. * 正常終了確認
  13. *
  14. * @return 標準出力
  15. */
  16. protected String invorkeMainNomal(Class<?> cl, String arg, int exitCode) {
  17. ByteArrayOutputStream out = new ByteArrayOutputStream();
  18. System.setOut(new PrintStream(out));
  19.  
  20. invorkeMain(cl, arg, exitCode);
  21.  
  22. System.setOut(stdOut);
  23. return out.toString();
  24. }
  25.  
  26. /**
  27. * 異常終了確認
  28. *
  29. * @return 標準エラー出力
  30. */
  31. protected String invorkeMainError(Class<?> cl, String arg, int exitCode) {
  32. ByteArrayOutputStream out = new ByteArrayOutputStream();
  33. System.setErr(new PrintStream(out));
  34.  
  35. invorkeMain(cl, arg, exitCode);
  36.  
  37. System.setErr(stdOut);
  38. return out.toString();
  39. }
  40.  
  41. /**
  42. * mainメソッド実行
  43. */
  44. private void invorkeMain(Class<?> cl, String arg, int exitCode) {
  45. System.setSecurityManager(new TestSecurityManager());
  46. try {
  47. Method m = cl.getMethod("main", String[].class);
  48. m.invorke(null, new Object[] { arg.split(ARGS_DELIMITER) });
  49. } catch (InvocationTargetException ite) {
  50. assertThat(((ExitException)ite.getTargetException()).getStatus(), is(exitCode));
  51. } catch (Exception e) {
  52. throw RuntimeException(e);
  53. }
  54. System.setSecurityManager(securityManager);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement