KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > sendopts > ShortDescriptionTest


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 package org.netbeans.api.sendopts;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import junit.framework.TestCase;
27 import org.netbeans.modules.sendopts.OptionImpl;
28 import org.netbeans.spi.sendopts.Env;
29 import org.netbeans.spi.sendopts.Option;
30
31 /** Can we associated short description?
32  *
33  * @author Jaroslav Tulach
34  */

35 public class ShortDescriptionTest extends TestCase implements Processor {
36     private Option help;
37     private Option descr;
38     
39     public ShortDescriptionTest(String JavaDoc s) {
40         super(s);
41     }
42
43     private void setUpHelp() throws Exception JavaDoc {
44         Provider.clearAll();
45         help = Option.withoutArgument('h', "help");
46         Provider.add(this, help);
47     }
48
49     private void setUpShort() {
50         Provider.clearAll();
51         help = Option.withoutArgument('h', "help");
52         descr = Option.shortDescription(help, "org.netbeans.api.sendopts.TestBundle", "HELP");
53         assertEquals("Option with description is the same", help, descr);
54         assertEquals("Option with description has the same hashCode", help.hashCode(), descr.hashCode());
55         Provider.add(this, descr);
56     }
57     
58     public void testPrintedUsage() throws Exception JavaDoc {
59         setUpHelp();
60         
61         StringWriter JavaDoc w = new StringWriter JavaDoc();
62         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
63
64         CommandLine.getDefault().usage(pw);
65
66         Matcher JavaDoc m = Pattern.compile("-h.*--help").matcher(w.toString());
67         if (!m.find()) {
68             fail("-h, --help should be there:\n" + w.toString());
69         }
70
71         assertEquals("No help associated", w.toString().indexOf("shorthelp"), -1);
72     }
73     public void testPrintedUsageEiyhFrdvtipyion() throws Exception JavaDoc {
74         setUpShort();
75         
76         StringWriter JavaDoc w = new StringWriter JavaDoc();
77         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
78
79         CommandLine.getDefault().usage(pw);
80
81         Matcher JavaDoc m = Pattern.compile("-h.*--help").matcher(w.toString());
82         if (!m.find()) {
83             fail("-h, --help should be there:\n" + w.toString());
84         }
85
86         if (w.toString().indexOf("shorthelp") == -1) {
87             fail("shorthelp associated: " + w.toString());
88         }
89     }
90     public void testProvidedOwnDisplayName() throws Exception JavaDoc {
91         Provider.clearAll();
92         help = Option.withoutArgument('h', "help");
93         Option shor = Option.shortDescription(help, "org.netbeans.api.sendopts.TestBundle", "HELP");
94         assertEquals("Option with description is the same", help, shor);
95         assertEquals("Option with description has the same hashCode", help.hashCode(), shor.hashCode());
96         descr = Option.displayName(shor, "org.netbeans.api.sendopts.TestBundle", "NAMEHELP");
97         assertEquals("Option with description is the same", help, descr);
98         assertEquals("Option with description has the same hashCode", help.hashCode(), descr.hashCode());
99         Provider.add(this, descr);
100         
101         StringWriter JavaDoc w = new StringWriter JavaDoc();
102         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
103
104         CommandLine.getDefault().usage(pw);
105
106         Matcher JavaDoc m = Pattern.compile("-p.*--pomoc").matcher(w.toString());
107         if (!m.find()) {
108             fail("--pomoc should be there:\n" + w.toString());
109         }
110
111         if (w.toString().indexOf("shorthelp") == -1) {
112             fail("shorthelp associated: " + w.toString());
113         }
114     }
115
116     public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
117     }
118
119 }
120
121
Popular Tags