001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.sendopts;
042:
043: import java.io.PrintWriter;
044: import java.io.StringWriter;
045: import java.util.Map;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048: import junit.framework.TestCase;
049: import org.netbeans.modules.sendopts.OptionImpl;
050: import org.netbeans.spi.sendopts.Env;
051: import org.netbeans.spi.sendopts.Option;
052:
053: /** Can we associated short description?
054: *
055: * @author Jaroslav Tulach
056: */
057: public class ShortDescriptionTest extends TestCase implements Processor {
058: private Option help;
059: private Option descr;
060:
061: public ShortDescriptionTest(String s) {
062: super (s);
063: }
064:
065: private void setUpHelp() throws Exception {
066: Provider.clearAll();
067: help = Option.withoutArgument('h', "help");
068: Provider.add(this , help);
069: }
070:
071: private void setUpShort() {
072: Provider.clearAll();
073: help = Option.withoutArgument('h', "help");
074: descr = Option.shortDescription(help,
075: "org.netbeans.api.sendopts.TestBundle", "HELP");
076: assertEquals("Option with description is the same", help, descr);
077: assertEquals("Option with description has the same hashCode",
078: help.hashCode(), descr.hashCode());
079: Provider.add(this , descr);
080: }
081:
082: public void testPrintedUsage() throws Exception {
083: setUpHelp();
084:
085: StringWriter w = new StringWriter();
086: PrintWriter pw = new PrintWriter(w);
087:
088: CommandLine.getDefault().usage(pw);
089:
090: Matcher m = Pattern.compile("-h.*--help").matcher(w.toString());
091: if (!m.find()) {
092: fail("-h, --help should be there:\n" + w.toString());
093: }
094:
095: assertEquals("No help associated", w.toString().indexOf(
096: "shorthelp"), -1);
097: }
098:
099: public void testPrintedUsageEiyhFrdvtipyion() throws Exception {
100: setUpShort();
101:
102: StringWriter w = new StringWriter();
103: PrintWriter pw = new PrintWriter(w);
104:
105: CommandLine.getDefault().usage(pw);
106:
107: Matcher m = Pattern.compile("-h.*--help").matcher(w.toString());
108: if (!m.find()) {
109: fail("-h, --help should be there:\n" + w.toString());
110: }
111:
112: if (w.toString().indexOf("shorthelp") == -1) {
113: fail("shorthelp associated: " + w.toString());
114: }
115: }
116:
117: public void testProvidedOwnDisplayName() throws Exception {
118: Provider.clearAll();
119: help = Option.withoutArgument('h', "help");
120: Option shor = Option.shortDescription(help,
121: "org.netbeans.api.sendopts.TestBundle", "HELP");
122: assertEquals("Option with description is the same", help, shor);
123: assertEquals("Option with description has the same hashCode",
124: help.hashCode(), shor.hashCode());
125: descr = Option.displayName(shor,
126: "org.netbeans.api.sendopts.TestBundle", "NAMEHELP");
127: assertEquals("Option with description is the same", help, descr);
128: assertEquals("Option with description has the same hashCode",
129: help.hashCode(), descr.hashCode());
130: Provider.add(this , descr);
131:
132: StringWriter w = new StringWriter();
133: PrintWriter pw = new PrintWriter(w);
134:
135: CommandLine.getDefault().usage(pw);
136:
137: Matcher m = Pattern.compile("-p.*--pomoc")
138: .matcher(w.toString());
139: if (!m.find()) {
140: fail("--pomoc should be there:\n" + w.toString());
141: }
142:
143: if (w.toString().indexOf("shorthelp") == -1) {
144: fail("shorthelp associated: " + w.toString());
145: }
146: }
147:
148: public void process(Env env, Map<Option, String[]> values)
149: throws CommandException {
150: }
151:
152: }
|