KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > sendopts > HandlerImplTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.sendopts;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.util.Set JavaDoc;
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 /**
41  *
42  * @author jarda
43  */

44 public class HandlerImplTest extends NbTestCase {
45     static Object JavaDoc key;
46     static Object JavaDoc[] args;
47     ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(new byte[0]);
48     ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
49     ByteArrayOutputStream JavaDoc err = new ByteArrayOutputStream JavaDoc();
50     static ResourceBundle JavaDoc bundle;
51     
52     public HandlerImplTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
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 JavaDoc[] { "--haha" }, is, os, err, new File JavaDoc("."));
66
67         assertEquals("Execution returns 337", 337, ret);
68         assertEquals("No out", 0, os.toByteArray().length);
69
70         String JavaDoc 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 JavaDoc[] { "Y" };
76
77         int ret = HandlerImpl.execute(new String JavaDoc[] { "--haha" }, is, os, err, new File JavaDoc("."));
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 JavaDoc() {
86             public String JavaDoc getLocalizedMessage() {
87                 return "LOC";
88             }
89         };
90
91         int ret = HandlerImpl.execute(new String JavaDoc[] { "--haha" }, is, os, err, new File JavaDoc("."));
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 JavaDoc<Option, String JavaDoc[]> values;
102         
103         protected Set JavaDoc<Option> getOptions() {
104             return new HashSet JavaDoc<Option>(Arrays.asList(arr));
105         }
106
107         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
108             values = optionValues;
109             assertNotNull("each test needs to assign a key", key);
110             if (key instanceof Throwable JavaDoc) {
111                 CommandException ex = new CommandException(221);
112                 ex.initCause((Throwable JavaDoc)key);
113                 throw ex;
114             }
115
116             String JavaDoc locMsg = MessageFormat.format(bundle.getString((String JavaDoc) key), args);
117             throw new CommandException(337, locMsg);
118         }
119         
120     }
121     
122 }
123
Popular Tags