1 package org.objectweb.celtix.common.commands; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.net.URL ; 8 9 import junit.framework.TestCase; 10 11 public class ResultBufferedCommandTest extends TestCase { 12 13 private static final String OUT = "Hello World!"; 14 private static final String ERR = "Please contact your administrator."; 15 16 17 public void testStreamsEmpty() throws IOException { 18 URL url = TestCommand.class.getResource("TestCommand.class"); 19 File file = new File (url.getFile()); 20 file = file.getParentFile(); 21 file = new File (file, "../../../../.."); 22 String [] cmd = new String [] { 23 JavaHelper.getJavaCommand(), 24 "-classpath", 25 file.getCanonicalPath(), 26 "org.objectweb.celtix.common.commands.TestCommand", 27 }; 28 ResultBufferedCommand rbc = new ResultBufferedCommand(cmd); 29 assertEquals(0, rbc.execute()); 30 BufferedReader br = rbc.getBufferedOutputReader(); 31 assertNotNull(br); 32 assertNull(br.readLine()); 33 br.close(); 34 br = rbc.getBufferedErrorReader(); 35 assertNotNull(br); 36 assertNull(br.readLine()); 37 br.close(); 38 InputStream is = rbc.getOutput(); 39 assertEquals(0, is.available()); 40 is.close(); 41 is = rbc.getError(); 42 assertEquals(0, is.available()); 43 is.close(); 44 } 45 46 public void testStreamsNotEmpty() throws IOException { 47 URL url = TestCommand.class.getResource("TestCommand.class"); 48 File file = new File (url.getFile()); 49 file = file.getParentFile(); 50 file = new File (file, "../../../../.."); 51 String [] cmd = new String [] { 52 JavaHelper.getJavaCommand(), 53 "-classpath", 54 file.getCanonicalPath(), 55 "org.objectweb.celtix.common.commands.TestCommand", 56 "-out", 57 OUT, 58 "-err", 59 ERR, 60 "-result", 61 "2", 62 }; 63 ResultBufferedCommand rbc = new ResultBufferedCommand(); 64 rbc.setArgs(cmd); 65 assertEquals(2, rbc.execute()); 66 BufferedReader br = rbc.getBufferedOutputReader(); 67 assertNotNull(br); 68 String line = br.readLine(); 69 assertEquals(OUT, line); 70 assertNull(br.readLine()); 71 br.close(); 72 br = rbc.getBufferedErrorReader(); 73 assertNotNull(br); 74 line = br.readLine(); 75 assertEquals(ERR, line); 76 assertNull(br.readLine()); 77 br.close(); 78 InputStream is = rbc.getOutput(); 79 assertTrue(is.available() > 0); 80 is.close(); 81 is = rbc.getError(); 82 assertTrue(is.available() > 0); 83 is.close(); 84 } 85 86 } 87 | Popular Tags |