Source Code Cross Referenced for GnuParseTest.java in  » Open-Source-Library » Apache-command-line » org » apache » commons » cli » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. JDK Core
2. JDK Modules
3. JDK Modules com.sun
4. JDK Modules com.sun.java
5. JDK Modules Platform
6. JDK Modules sun
7. Open Source Build
8. Open Source Graphic Library
9. Open Source IDE Eclipse
10. Open Source J2EE
11. Open Source JDBC Driver
12. Open Source Library
13. Open Source Library Database
14. Open Source Net
15. Open Source Script
16. Science
17. Security
18. Sevlet Container
19. SUN GlassFish
20. Swing Library
21. Web Services apache cxf 2.0.1
22. Web Services AXIS2
23. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Source Code / Java Documentation » Open Source Library » Apache command line » org.apache.commons.cli 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.commons.cli;
017:
018:        import junit.framework.Test;
019:        import junit.framework.TestCase;
020:        import junit.framework.TestSuite;
021:
022:        public class GnuParseTest extends TestCase {
023:            private Options _options = null;
024:            private Parser _parser = null;
025:
026:            public static Test suite() {
027:                return new TestSuite(GnuParseTest.class);
028:            }
029:
030:            public GnuParseTest(String name) {
031:                super (name);
032:            }
033:
034:            public void setUp() {
035:                _options = new Options().addOption("a", "enable-a", false,
036:                        "turn [a] on or off").addOption("b", "bfile", true,
037:                        "set the value of [b]").addOption("c", "copt", false,
038:                        "turn [c] on or off");
039:
040:                _parser = new GnuParser();
041:            }
042:
043:            public void tearDown() {
044:
045:            }
046:
047:            public void testSimpleShort() {
048:                String[] args = new String[] { "-a", "-b", "toast", "foo",
049:                        "bar" };
050:
051:                try {
052:                    CommandLine cl = _parser.parse(_options, args);
053:
054:                    assertTrue("Confirm -a is set", cl.hasOption("a"));
055:                    assertTrue("Confirm -b is set", cl.hasOption("b"));
056:                    assertTrue("Confirm arg of -b", cl.getOptionValue("b")
057:                            .equals("toast"));
058:                    assertTrue("Confirm size of extra args", cl.getArgList()
059:                            .size() == 2);
060:                } catch (ParseException e) {
061:                    fail(e.toString());
062:                }
063:            }
064:
065:            public void testSimpleLong() {
066:                String[] args = new String[] { "--enable-a", "--bfile",
067:                        "toast", "foo", "bar" };
068:
069:                try {
070:                    CommandLine cl = _parser.parse(_options, args);
071:
072:                    assertTrue("Confirm -a is set", cl.hasOption("a"));
073:                    assertTrue("Confirm -b is set", cl.hasOption("b"));
074:                    assertTrue("Confirm arg of -b", cl.getOptionValue("b")
075:                            .equals("toast"));
076:                    assertTrue("Confirm size of extra args", cl.getArgList()
077:                            .size() == 2);
078:                } catch (ParseException e) {
079:                    fail(e.toString());
080:                }
081:            }
082:
083:            public void testExtraOption() {
084:                String[] args = new String[] { "-a", "-d", "-b", "toast",
085:                        "foo", "bar" };
086:
087:                boolean caught = false;
088:
089:                try {
090:                    CommandLine cl = _parser.parse(_options, args);
091:
092:                    assertTrue("Confirm -a is set", cl.hasOption("a"));
093:                    assertTrue("Confirm -b is set", cl.hasOption("b"));
094:                    assertTrue("confirm arg of -b", cl.getOptionValue("b")
095:                            .equals("toast"));
096:                    assertTrue("Confirm size of extra args", cl.getArgList()
097:                            .size() == 3);
098:                } catch (UnrecognizedOptionException e) {
099:                    caught = true;
100:                } catch (ParseException e) {
101:                    fail(e.toString());
102:                }
103:                assertTrue("Confirm UnrecognizedOptionException caught", caught);
104:            }
105:
106:            public void testMissingArg() {
107:
108:                String[] args = new String[] { "-b" };
109:
110:                boolean caught = false;
111:
112:                try {
113:                    CommandLine cl = _parser.parse(_options, args);
114:                } catch (MissingArgumentException e) {
115:                    caught = true;
116:                } catch (ParseException e) {
117:                    fail(e.toString());
118:                }
119:
120:                assertTrue("Confirm MissingArgumentException caught", caught);
121:            }
122:
123:            public void testStop() {
124:                String[] args = new String[] { "-c", "foober", "-b", "toast" };
125:
126:                try {
127:                    CommandLine cl = _parser.parse(_options, args, true);
128:                    assertTrue("Confirm -c is set", cl.hasOption("c"));
129:                    assertTrue("Confirm  3 extra args: "
130:                            + cl.getArgList().size(),
131:                            cl.getArgList().size() == 3);
132:                } catch (ParseException e) {
133:                    fail(e.toString());
134:                }
135:            }
136:
137:            public void testMultiple() {
138:                String[] args = new String[] { "-c", "foobar", "-b", "toast" };
139:
140:                try {
141:                    CommandLine cl = _parser.parse(_options, args, true);
142:                    assertTrue("Confirm -c is set", cl.hasOption("c"));
143:                    assertTrue("Confirm  3 extra args: "
144:                            + cl.getArgList().size(),
145:                            cl.getArgList().size() == 3);
146:
147:                    cl = _parser.parse(_options, cl.getArgs());
148:
149:                    assertTrue("Confirm -c is not set", !cl.hasOption("c"));
150:                    assertTrue("Confirm -b is set", cl.hasOption("b"));
151:                    assertTrue("Confirm arg of -b", cl.getOptionValue("b")
152:                            .equals("toast"));
153:                    assertTrue("Confirm  1 extra arg: "
154:                            + cl.getArgList().size(),
155:                            cl.getArgList().size() == 1);
156:                    assertTrue("Confirm  value of extra arg: "
157:                            + cl.getArgList().get(0), cl.getArgList().get(0)
158:                            .equals("foobar"));
159:                } catch (ParseException e) {
160:                    fail(e.toString());
161:                }
162:            }
163:
164:            public void testMultipleWithLong() {
165:                String[] args = new String[] { "--copt", "foobar", "--bfile",
166:                        "toast" };
167:
168:                try {
169:                    CommandLine cl = _parser.parse(_options, args, true);
170:                    assertTrue("Confirm -c is set", cl.hasOption("c"));
171:                    assertTrue("Confirm  3 extra args: "
172:                            + cl.getArgList().size(),
173:                            cl.getArgList().size() == 3);
174:
175:                    cl = _parser.parse(_options, cl.getArgs());
176:
177:                    assertTrue("Confirm -c is not set", !cl.hasOption("c"));
178:                    assertTrue("Confirm -b is set", cl.hasOption("b"));
179:                    assertTrue("Confirm arg of -b", cl.getOptionValue("b")
180:                            .equals("toast"));
181:                    assertTrue("Confirm  1 extra arg: "
182:                            + cl.getArgList().size(),
183:                            cl.getArgList().size() == 1);
184:                    assertTrue("Confirm  value of extra arg: "
185:                            + cl.getArgList().get(0), cl.getArgList().get(0)
186:                            .equals("foobar"));
187:                } catch (ParseException e) {
188:                    fail(e.toString());
189:                }
190:            }
191:
192:            public void testDoubleDash() {
193:                String[] args = new String[] { "--copt", "--", "-b", "toast" };
194:
195:                try {
196:                    CommandLine cl = _parser.parse(_options, args);
197:
198:                    assertTrue("Confirm -c is set", cl.hasOption("c"));
199:                    assertTrue("Confirm -b is not set", !cl.hasOption("b"));
200:                    assertTrue("Confirm 2 extra args: "
201:                            + cl.getArgList().size(),
202:                            cl.getArgList().size() == 2);
203:
204:                } catch (ParseException e) {
205:                    fail(e.toString());
206:                }
207:            }
208:
209:            public void testSingleDash() {
210:                String[] args = new String[] { "--copt", "-b", "-", "-a", "-" };
211:
212:                try {
213:                    CommandLine cl = _parser.parse(_options, args);
214:
215:                    assertTrue("Confirm -a is set", cl.hasOption("a"));
216:                    assertTrue("Confirm -b is set", cl.hasOption("b"));
217:                    assertTrue("Confirm arg of -b", cl.getOptionValue("b")
218:                            .equals("-"));
219:                    assertTrue(
220:                            "Confirm 1 extra arg: " + cl.getArgList().size(),
221:                            cl.getArgList().size() == 1);
222:                    assertTrue("Confirm value of extra arg: "
223:                            + cl.getArgList().get(0), cl.getArgList().get(0)
224:                            .equals("-"));
225:                } catch (ParseException e) {
226:                    fail(e.toString());
227:                }
228:
229:            }
230:        }
w___ww__.__java___2__s.__c_o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.