1 19 20 package org.netbeans; 21 22 import java.io.*; 23 import java.util.logging.Logger ; 24 import org.netbeans.junit.*; 25 import java.util.*; 26 import java.util.logging.Level ; 27 28 32 public class CLIHandlerRemembersSystemInOutErrTest extends NbTestCase { 33 34 private static ByteArrayInputStream in = new ByteArrayInputStream("Ahoj".getBytes()); 35 private static PrintStream out = new PrintStream(new ByteArrayOutputStream()); 36 private static PrintStream err = new PrintStream(new ByteArrayOutputStream()); 37 private static String [] args = { "AnArg" }; 38 private static String curDir = "curDir"; 39 40 private Logger LOG; 41 42 static { 43 System.setIn(in); 44 System.setErr(err); 45 System.setOut(out); 46 } 47 48 public CLIHandlerRemembersSystemInOutErrTest(String name) { 49 super(name); 50 } 51 52 protected void setUp() throws Exception { 53 clearWorkDir(); 54 System.setProperty ("netbeans.user", getWorkDirPath()); 55 LOG = Logger.getLogger("TEST-" + getName()); 56 } 57 58 protected Level logLevel() { 59 return Level.ALL; 60 } 61 62 public void testFileExistsButItCannotBeRead() throws Exception { 63 CLIHandler.Args a = new CLIHandler.Args(args, in, out, err, curDir); 65 66 ArrayList<CLIHandler> arr = new ArrayList<CLIHandler>(); 67 arr.add(new H(H.WHEN_BOOT)); 68 arr.add(new H(H.WHEN_INIT)); 69 70 ByteArrayInputStream in2 = new ByteArrayInputStream("NeverBeSeen".getBytes()); 72 PrintStream out2 = new PrintStream(new ByteArrayOutputStream()); 73 PrintStream err2 = new PrintStream(new ByteArrayOutputStream()); 74 75 System.setIn(in2); 76 System.setErr(err2); 77 System.setOut(out2); 78 79 LOG.info("before initialized"); 80 CLIHandler.initialize(a, null, arr, false, true, null); 81 LOG.info("after initialize"); 82 assertEquals("One H called", 1, H.cnt); 83 LOG.info("before finishInitialization"); 84 CLIHandler.finishInitialization(false); 85 LOG.info("after finishInitialization"); 86 assertEquals("Both Hs called", 2, H.cnt); 87 } 88 89 private static final class H extends CLIHandler { 90 static int cnt; 91 92 public H(int w) { 93 super(w); 94 } 95 96 protected int cli(CLIHandler.Args a) { 97 cnt++; 98 99 assertEquals("Same arg", Arrays.asList(args), Arrays.asList(a.getArguments())); 100 assertEquals("same dir", curDir, a.getCurrentDirectory().toString()); 101 assertEquals("same in", in, a.getInputStream()); 102 assertEquals("same out", out, a.getOutputStream()); 103 assertEquals("same err", err, a.getErrorStream()); 104 105 return 0; 106 } 107 108 protected void usage(PrintWriter w) { 109 } 110 } 111 } 112 | Popular Tags |