001: /*
002: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
003: Licensed under the Academic Free License version 3.0
004: */
005:
006: package joptsimple;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
012: * @version $Id: ArgumentAcceptingOptionSpecTest.java,v 1.11 2007/04/10 20:06:26 pholser Exp $
013: */
014: public class ArgumentAcceptingOptionSpecTest extends TestCase {
015: public void testRequiredArgOfNullType() {
016: try {
017: new RequiredArgumentOptionSpec("a").ofType(null);
018: fail();
019: } catch (NullPointerException expected) {
020: assertTrue(expected.getMessage(), true);
021: }
022: }
023:
024: public void testOptionalArgOfNullType() {
025: try {
026: new OptionalArgumentOptionSpec("verbose").ofType(null);
027: fail();
028: } catch (NullPointerException expected) {
029: assertTrue(expected.getMessage(), true);
030: }
031: }
032:
033: public void testRequiredArgOfNonValueType() {
034: try {
035: new RequiredArgumentOptionSpec("threshold")
036: .ofType(Object.class);
037: fail();
038: } catch (IllegalArgumentException expected) {
039: assertTrue(expected.getMessage(), true);
040: }
041: }
042:
043: public void testOptionalArgOfNonValueType() {
044: try {
045: new OptionalArgumentOptionSpec("max").ofType(Object.class);
046: fail();
047: } catch (IllegalArgumentException expected) {
048: assertTrue(expected.getMessage(), true);
049: }
050: }
051:
052: public void testRequiredArgOfValueTypeBasedOnValueOf() {
053: final RequiredArgumentOptionSpec spec = new RequiredArgumentOptionSpec(
054: "threshold");
055:
056: assertNoException(new Runnable() {
057: public void run() {
058: spec.ofType(ValueTypeBasedOnValueOf.class);
059: }
060: });
061: }
062:
063: public void testOptionalArgOfValueTypeBasedOnValueOf() {
064: final OptionalArgumentOptionSpec spec = new OptionalArgumentOptionSpec(
065: "abc");
066:
067: assertNoException(new Runnable() {
068: public void run() {
069: spec.ofType(ValueTypeBasedOnValueOf.class);
070: }
071: });
072: }
073:
074: public void testRequiredArgOfValueTypeBasedOnCtor() {
075: final RequiredArgumentOptionSpec spec = new RequiredArgumentOptionSpec(
076: "threshold");
077:
078: assertNoException(new Runnable() {
079: public void run() {
080: spec.ofType(ValueTypeBasedOnCtor.class);
081: }
082: });
083: }
084:
085: public void testOptionalArgOfValueTypeBasedOnCtor() {
086: final OptionalArgumentOptionSpec spec = new OptionalArgumentOptionSpec(
087: "abc");
088:
089: assertNoException(new Runnable() {
090: public void run() {
091: spec.ofType(ValueTypeBasedOnCtor.class);
092: assertEquals("foo", ((ValueTypeBasedOnCtor) spec
093: .convert("foo")).getS());
094: }
095: });
096: }
097:
098: static class ValueTypeBasedOnValueOf {
099: public static ValueTypeBasedOnValueOf valueOf(String s) {
100: return null;
101: }
102: }
103:
104: static class ValueTypeBasedOnCtor {
105: private String s;
106:
107: public ValueTypeBasedOnCtor(String s) {
108: this .s = s;
109: }
110:
111: public String getS() {
112: return s;
113: }
114:
115: static ValueTypeBasedOnCtor valueOf(String s) {
116: return null;
117: }
118: }
119:
120: private void assertNoException(Runnable block) {
121: try {
122: block.run();
123: } catch (RuntimeException ex) {
124: fail("unexpected exception of " + ex.getClass() + " caught");
125: }
126: }
127: }
|