fork download
  1. import static java.lang.System.in;
  2. import static java.lang.System.setIn;
  3. import static org.junit.Assert.fail;
  4.  
  5. import java.io.BufferedOutputStream;
  6. import java.io.BufferedReader;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.PipedInputStream;
  12. import java.io.PipedOutputStream;
  13. import java.io.PrintStream;
  14. import java.nio.channels.Pipe;
  15.  
  16. import org.jmock.Expectations;
  17. import org.jmock.Mockery;
  18. import org.jmock.lib.legacy.ClassImposteriser;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23.  
  24. public class UnitTests {
  25. private Mockery context = new Mockery() {{
  26. setImposteriser(ClassImposteriser.INSTANCE);
  27. }};
  28. private main mn;
  29. private InputStream oldIn;
  30. private PrintStream oldOut;
  31. private InputStream mockIn;
  32. private InputStreamReader mockinputStream;
  33. private PrintStream mockOut;
  34. private BufferedReader reader;
  35.  
  36.  
  37. @Before
  38. public void setMinimalMockingExpectations() throws IOException {
  39. mn = context.mock(main.class);
  40. reader = context.mock(BufferedReader.class);
  41. mockinputStream = context.mock(InputStreamReader.class);
  42. oldIn = System.in;
  43. oldOut = System.out;
  44. mockIn = context.mock(InputStream.class);
  45. mockOut = context.mock(PrintStream.class);
  46. System.setOut(mockOut);
  47. //System.setIn(mockIn);
  48. }
  49.  
  50. @After
  51. public void reset() {
  52. System.setOut(oldOut);
  53. //System.setIn(oldIn);
  54. }
  55.  
  56. @Test
  57. public void EnsureFileCheckExists() throws IOException {
  58. InputStream originalIn = System.in;
  59. BufferedReader reader = new BufferedReader(new InputStreamReader(pipeInput));
  60. final PipedOutputStream out = new PipedOutputStream(pipeInput);
  61. System.setIn(pipeInput);
  62. out.write("F\n".getBytes());
  63. pipeInput.mark(2);
  64. out.write("DNE!\n".getBytes());
  65.  
  66. Expectations exp;
  67. exp = new Expectations() {{
  68. one(mockOut).println("Do you want to process standard (I)nput, or a (F)ile? I/F");
  69. one(mockOut).println("Enter filename: ");
  70.  
  71. //System.setIn(new ByteArrayInputStream("DNE".getBytes()));
  72. //one(mn).readInput(); will(returnValue("-1"));
  73. //one(mn).readInput(); will(returnValue(DNE));
  74. //one(mn).ensureFileExists(DNE); will(returnValue(false));
  75. }};
  76. context.checking(exp);
  77. main.main(null);
  78. System.setIn(originalIn);
  79. }
  80. }
  81.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty