1 19 20 package org.netbeans.modules.sendopts; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.text.MessageFormat ; 26 import java.util.Arrays ; 27 import java.util.HashSet ; 28 import java.util.Map ; 29 import java.util.ResourceBundle ; 30 import java.util.Set ; 31 import org.netbeans.api.sendopts.CommandException; 32 import org.netbeans.api.sendopts.Processor; 33 import org.netbeans.junit.MockServices; 34 import org.netbeans.junit.NbTestCase; 35 import org.netbeans.spi.sendopts.Env; 36 import org.netbeans.spi.sendopts.Option; 37 import org.netbeans.spi.sendopts.OptionProcessor; 38 import org.openide.util.Lookup; 39 40 44 public class HandlerImplTest extends NbTestCase { 45 static Object key; 46 static Object [] args; 47 ByteArrayInputStream is = new ByteArrayInputStream (new byte[0]); 48 ByteArrayOutputStream os = new ByteArrayOutputStream (); 49 ByteArrayOutputStream err = new ByteArrayOutputStream (); 50 static ResourceBundle bundle; 51 52 public HandlerImplTest(String testName) { 53 super(testName); 54 } 55 56 protected void setUp() throws Exception { 57 OP.arr = new Option[] { Option.withoutArgument(Option.NO_SHORT_NAME, "haha") }; 58 MockServices.setServices(OP.class); 59 bundle = ResourceBundle.getBundle("org.netbeans.modules.sendopts.TestBundle"); 60 } 61 62 public void testErrorMessageIsPrinted() { 63 key = "SIMPLEERROR"; 64 65 int ret = HandlerImpl.execute(new String [] { "--haha" }, is, os, err, new File (".")); 66 67 assertEquals("Execution returns 337", 337, ret); 68 assertEquals("No out", 0, os.toByteArray().length); 69 70 String msg = bundle.getString("SIMPLEERROR"); 71 assertEquals("error is as expected", msg, err.toString().replace("\n", "").replace("\r", "")); 72 } 73 public void testErrorMessageIsPrintedWithArgs() { 74 key = "ARGS"; 75 args = new Object [] { "Y" }; 76 77 int ret = HandlerImpl.execute(new String [] { "--haha" }, is, os, err, new File (".")); 78 79 assertEquals("Execution returns 337", 337, ret); 80 assertEquals("No out", 0, os.toByteArray().length); 81 82 assertEquals("error is as expected", "XYZ", err.toString().replace("\n", "").replace("\r", "")); 83 } 84 public void testErrorMessageForInlinedThrowable() { 85 key = new Exception () { 86 public String getLocalizedMessage() { 87 return "LOC"; 88 } 89 }; 90 91 int ret = HandlerImpl.execute(new String [] { "--haha" }, is, os, err, new File (".")); 92 93 assertEquals("Execution returns 221", 221, ret); 94 assertEquals("No out", 0, os.toByteArray().length); 95 96 assertEquals("error is as expected", "LOC", err.toString().replace("\n", "").replace("\r", "")); 97 } 98 99 public static final class OP extends OptionProcessor { 100 static Option[] arr; 101 static Map <Option, String []> values; 102 103 protected Set <Option> getOptions() { 104 return new HashSet <Option>(Arrays.asList(arr)); 105 } 106 107 protected void process(Env env, Map <Option, String []> optionValues) throws CommandException { 108 values = optionValues; 109 assertNotNull("each test needs to assign a key", key); 110 if (key instanceof Throwable ) { 111 CommandException ex = new CommandException(221); 112 ex.initCause((Throwable )key); 113 throw ex; 114 } 115 116 String locMsg = MessageFormat.format(bundle.getString((String ) key), args); 117 throw new CommandException(337, locMsg); 118 } 119 120 } 121 122 } 123 | Popular Tags |