summaryrefslogtreecommitdiffstats
path: root/args4j/args4j/test/org/kohsuke/args4j/ArgumentTest.java
blob: fffcea1a8ff6eb57929b6cee9419c59ab09e240c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.kohsuke.args4j;

import junit.framework.TestCase;

import java.util.List;

public class ArgumentTest extends TestCase {
    protected static class MultiValueHolder {
		@Argument
		public List<String> things;
	}

	protected static class SingleValueHolder {
		@Argument(metaVar = "thing", required = true)
		public String thing;
	}

	protected static class BooleanValueHolder {
		@Argument(metaVar = "thing", required = true)
		public boolean b;
	}

	public void testMultiValue() throws Exception {
		MultiValueHolder holder = new MultiValueHolder();
		CmdLineParser parser = new CmdLineParser(holder);
		parser.parseArgument(new String[] { "one", "two" });

		assertEquals(2, holder.things.size());
		assertEquals("one", holder.things.get(0));
		assertEquals("two", holder.things.get(1));
	}

	public void testTooFew() throws Exception {
		SingleValueHolder holder = new SingleValueHolder();
		CmdLineParser parser = new CmdLineParser(holder);

		try {
			parser.parseArgument(new String[] {});
		} catch (CmdLineException e) {
			assertEquals("Argument \"thing\" is required", e.getMessage());
			return;
		}
		fail("expected " + CmdLineException.class);
	}

	public void testBoolean() throws Exception {
		BooleanValueHolder holder = new BooleanValueHolder();
		CmdLineParser parser = new CmdLineParser(holder);

		parser.parseArgument(new String[] { "true" });

		assertTrue(holder.b);
	}

	public void testIllegalBoolean() throws Exception {
		BooleanValueHolder holder = new BooleanValueHolder();
		CmdLineParser parser = new CmdLineParser(holder);

		try {
			parser.parseArgument(new String[] { "xyz" });
		} catch (CmdLineException e) {
			assertEquals("\"xyz\" is not a legal boolean value", e.getMessage());
			return;
		}
		fail("expected " + CmdLineException.class);
	}
}