summaryrefslogtreecommitdiffstats
path: root/args4j/args4j/test/org/kohsuke/args4j/PropsTest.java
blob: 06889d9d59d9c80a24b239729ff4aae929edcc61 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.kohsuke.args4j;

import java.util.Map;

public class PropsTest extends Args4JTestBase<Props> {

    @Override
    public Props getTestObject() {
        return new Props();
    }

    public void testDoNothing() {
        // ignore, that the other test cases are commented out
        // JUnit doesnt like TestCases without a test-method.
    }

    public void testNoValues() {
        args = new String[]{};
        try {
            parser.parseArgument(args);
            Map<String,String> map = testObject.props;
            if (map == null) {
            	// as expected
            	return;
            }
            assertTrue("Values illegally arrived.", map.size()==0);
        } catch (CmdLineException e) {
            fail("Call without parameters is valid! " + e.getMessage());
        }
    }

    public void testSinglePair() {
        args = new String[]{"-T", "key1=value1"};
        try {
            parser.parseArgument(args);
            Map<String,String> map = testObject.props;
            assertTrue("The key was not set.", map.containsKey("key1"));
            assertEquals("More keys than expected.", map.size(), 1);
            assertEquals("Key has wrong value", map.get("key1"), "value1");
        } catch (CmdLineException e) {
            fail("Cought an invalid exception: " + e.getMessage());
        }
    }

    public void testMultiplePairs() {
        args = new String[]{"-T", "key1=value1", "-T", "key2=value2", "-T", "key3=value3"};
        try {
            parser.parseArgument(args);
            Map<String,String> map = testObject.props;
            assertTrue("A key was not set.", map.containsKey("key1"));
            assertTrue("A key was not set.", map.containsKey("key2"));
            assertTrue("A key was not set.", map.containsKey("key3"));
            assertEquals("Wrong number of keys.", map.size(), 3);
            assertEquals("Key has wrong value", map.get("key1"), "value1");
            assertEquals("Key has wrong value", map.get("key2"), "value2");
            assertEquals("Key has wrong value", map.get("key3"), "value3");
        } catch (CmdLineException e) {
            fail("Cought an invalid exception: " + e.getMessage());
        }
    }

    public void testDuplicateKey() {
        args = new String[]{"-T", "key1=one", "-T", "key1=two"};
        try {
            parser.parseArgument(args);
            Map<String,String> map = testObject.props;
            assertTrue("A key was not set.", map.containsKey("key1"));
            assertEquals("Wrong number of keys.", map.size(), 1);
            assertEquals("As Map.put() defines: last put wins.", map.get("key1"), "two");
        } catch (CmdLineException e) {
            fail("Cought an invalid exception: " + e.getMessage());
        }
    }

    public void testInitialisation() {
        args = new String[]{"-T", "key1=value1"};
        try {
            testObject.props = null;
            parser.parseArgument(args);
            Map<String,String> map = testObject.props;
            assertNotNull("Map should have been initialized with a new HashMap", map);
            assertEquals("Key has wrong value", map.get("key1"), "value1");
        } catch (CmdLineException e) {
            fail("Cought an invalid exception: " + e.getMessage());
        }
    }

    public void testNoValue() {
        args = new String[]{"-T", "key="};
        try {
            parser.parseArgument(args);
            Map<String,String> map = testObject.props;
            assertNull("Key has wrong value", map.get("key"));
        } catch (CmdLineException e) {
            fail("Cought an invalid exception: " + e.getMessage());
        }
    }

    public void testNoKey() {
        args = new String[]{"-T", "=value"};
        try {
            parser.parseArgument(args);
            fail("An exception should have been thrown.");
        } catch (CmdLineException e) {
        	assertEquals("Wrong error message.", "A key must be set.", e.getMessage());
        }
    }

    public void testNoSplitCharacter() {
        args = new String[]{"-T", "keyvalue"};
        try {
            parser.parseArgument(args);
            fail("An exception should have been thrown.");
        } catch (CmdLineException e) {
        	assertEquals("Wrong error message.", "An argument for setting a Map must contain a \"=\"", e.getMessage());
        }
    }

}