File: OptionDecoderTest.java

package info (click to toggle)
jtreg 5.1-b01-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 5,432 kB
  • sloc: java: 29,218; xml: 729; sh: 665; makefile: 245
file content (367 lines) | stat: -rw-r--r-- 10,940 bytes parent folder | download | duplicates (11)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

import static java.util.Arrays.asList;

import com.sun.javatest.regtest.BadArgs;
import com.sun.javatest.regtest.tool.Option;
import com.sun.javatest.regtest.tool.Option.ArgType;
import com.sun.javatest.regtest.tool.OptionDecoder;

import static com.sun.javatest.regtest.tool.Option.ArgType.*;

/**
 * Unit test for jtreg command-line option decoding,
 * including positive and negative tests for the different
 * styles of options and the different types of arguments
 * they may take.
 *
 * The test is primarily about ensuring that the argument
 * for an option (if any) is correctly recognized and made
 * available to the option handling code. This is done by
 * analyzing the input array of strings, and building a
 * corresponding output list, containing pairs of option
 * name and it's argument, or NULL,  The output list is
 * compared against a golden list.
 *
 * If the test is run with no args, it will execute the
 * built in test cases.
 * If it is run with args, it will execute a single test
 * case based on those args.
 */
public class OptionDecoderTest {
    public static void main(String... args) throws Exception {
        new OptionDecoderTest().run(args);
    }

    private static final String BADARGS = "BadArgs";
    private static final String FILE = "FILE";
    private static final String NULL = "NULL";

    // ArgType.FILE: file

    @Test
    void testFile() {
        test(asList("file"),
                asList(FILE, "file"));

        test(asList("-o", "file", "-splodge"),
                asList("-o", NULL, FILE, "file", FILE, "-splodge"));
    }

    // ArgType.GNU: --opt arg, --opt=arg, -o arg, -oarg

    @Test
    void testGnu1() {
        test(asList("--long-only", "abc"),
                asList("--long-only", "abc"));

        test(asList("--long-only=abc"),
                asList("--long-only=abc", "abc"));
    }

    @Test
    void testGnu2() {
        test(asList("--long-or-short", "abc"),
                asList("--long-or-short", "abc"));

        test(asList("--long-or-short=abc"),
                asList("--long-or-short=abc", "abc"));

        test(asList("-l", "abc"),
                asList("-l", "abc"));

        test(asList("-labc"),
                asList("-labc", "abc"));

        // no value
        test(asList("--long-or-short"),
                asList(BADARGS, "No value given for option --long-or-short"));

        // wrong separator
        test(asList("--long-or-short:abc"),
                asList(BADARGS, "Bad format for option: --long-or-short:abc"));

        // no separator
        test(asList("--long-or-shortabc"),
                asList(BADARGS, "Invalid option: --long-or-shortabc"));
    }

    // ArgType.NONE: -opt (includes --opt -o)

    @Test
    void testNoArg() {
        test(asList("-noarg"),
                asList("-noarg", NULL));

        test(asList("--no-arg"),
                asList("--no-arg", NULL));

        test(asList("-n"),
                asList("-n", NULL));

        test(asList("-n=abc"),
                asList(BADARGS, "Unexpected value for option -n=abc"));

        test(asList("-n:abc"),
                asList(BADARGS, "Unexpected value for option -n:abc"));
    }

    // ArgType.OLD: -opt:arg or -opt arg

    @Test
    void testOld() {
        test(asList("-old", "abc"),
                asList("-old", "abc"));

        test(asList("-old:abc"),
                asList("-old:abc", "abc"));

        // no value
        test(asList("-old"),
                asList(BADARGS, "No value given for option -old"));

        // wrong separator
        test(asList("-old=abc"),
                asList(BADARGS, "Bad format for option: -old=abc"));
    }

    // ArgType.OPT: -opt or -opt:arg

    @Test
    void testOpt() {
        test(asList("-optional"),
                asList("-optional", NULL));

        test(asList("-optional:arg"),
                asList("-optional:arg", "arg"));

        // wrong separator
        test(asList("-optional=arg"),
                asList(BADARGS, "Bad format for option: -optional=arg"));
    }

    // ArgType.REST:   -opt rest of args

    @Test
    void testRest() {
        test(asList("--rest", "abc", "-def"),
                asList("--rest", "abc -def"));

        test(asList("--rest:arg", "abc", "-def"),
                asList("--rest:arg", "arg abc -def"));

        test(asList("--rest=arg", "abc", "-def"),
                asList("--rest=arg", "arg abc -def"));
    }

    // ArgType.SEP:  -opt arg

    @Test
    void testSep() {
        test(asList("-wsarg", "arg"),
                asList("-wsarg", "arg"));

        // no value
        test(asList("-wsarg"),
                asList(BADARGS, "No value given for option -wsarg"));

        // non-ws separator
        test(asList("-wsarg=arg"),
                asList(BADARGS, "Bad format for option: -wsarg=arg"));

        // non-ws separator
        test(asList("-wsarg:arg"),
                asList(BADARGS, "Bad format for option: -wsarg:arg"));
    }

    // ArgType.STD:   -opt:arg

    @Test
    void testStd() {
        test(asList("-std:arg"),
                asList("-std:arg", "arg"));

        // no value
        test(asList("-std"),
                asList(BADARGS, "No value given for option -std"));

        // white space separator
        test(asList("-std", "arg"),
                asList(BADARGS, "No value given for option -std"));

        // wrong separator
        test(asList("-std=arg"),
                asList(BADARGS, "Bad format for option: -std=arg"));
    }

    // ArgType.WILDCARD:   -optarg

    @Test
    void testWildcard() {
        test(asList("-Xarg"),
                asList("-Xarg", "arg"));

        test(asList("-X:arg"),
                asList("-X:arg", ":arg"));

        test(asList("-X=arg"),
                asList("-X=arg", "=arg"));
    }

    // Errors

    @Test
    void testUnknown() {
        test(asList("-unknown"),
                asList(BADARGS, "Invalid option: -unknown"));
    }



    // -----------------------------------------------------------------

    class SimpleOption extends Option {
        SimpleOption(ArgType a, String g, String ln, String... names) {
            super(a, g, ln, names);
        }

        @Override
        public void process(String opt, String arg) {
            results.add(opt);
            results.add(arg == null ? NULL : arg);
        }
    }

    private final String GROUP = "g";

    List<Option> options = Arrays.<Option>asList(
        new SimpleOption(GNU, GROUP, null, "--long-only") { },
        new SimpleOption(GNU, GROUP, null, "--long-or-short", "-l") { },

        new SimpleOption(NONE, GROUP, null, "-noarg", "--no-arg", "-n") { },

        new SimpleOption(OLD, GROUP, null, "-old") { },

        new SimpleOption(OPT, GROUP, null, "-optional", "-o") { },

        new SimpleOption(OPT, GROUP, null, "-optionalChoice", "-oc") {
            @Override
            public String[] getChoices() {
                return new String[] { "a", "b", "c" };
            }
        },

        new SimpleOption(REST, GROUP, null, "--rest") { },
        new SimpleOption(REST, GROUP, null, "--help", "-help", "-h") { },

        new SimpleOption(SEP, GROUP, null, "-wsarg") { },

        new SimpleOption(STD, GROUP, null, "-standard", "-std") { },

        new SimpleOption(WILDCARD, GROUP, null, "-X") { },

        new SimpleOption(ArgType.FILE, GROUP, null) {
            @Override
            public void process(String opt, String arg) {
                results.add(FILE);
                results.add(arg);
            }
        }
    );

    /** Marker annotation for test cases. */
    @Retention(RetentionPolicy.RUNTIME)
    @interface Test { }

    List<String> results = new ArrayList<>();

    void run(String... args) throws Exception {
        if (args.length == 0) {
            runTests();
        } else {
            OptionDecoder d = new OptionDecoder(options);
            try {
                d.decodeArgs(args);
            } catch (BadArgs e) {
                results.add(BADARGS);
                results.add(e.getMessage());
            }
            System.out.println("Out: " + results);
        }
    }

    /**
     * Combo test to run all test cases in all modes.
     */
    void runTests() throws Exception {
        for (Method m : getClass().getDeclaredMethods()) {
            Annotation a = m.getAnnotation(Test.class);
            if (a != null) {
                try {
                    System.out.println("Test: " + m.getName());
                    m.invoke(this);
                } catch (InvocationTargetException e) {
                    Throwable cause = e.getCause();
                    throw (cause instanceof Exception) ? ((Exception) cause) : e;
                }
                System.err.println();
            }
        }
        System.err.println(testCount + " tests" + ((errorCount == 0) ? "" : ", " + errorCount + " errors"));
        if (errorCount > 0) {
            throw new Exception(errorCount + " errors found");
        }
    }

    void test(List<String> opts, List<String> expect) {
        testCount++;
        results.clear();
        OptionDecoder d = new OptionDecoder(options);
        try {
            d.decodeArgs(opts);
        } catch (BadArgs e) {
            results.add(BADARGS);
            results.add(e.getMessage());
        }
        System.out.println("Options: " + opts);
        System.out.println("Expect:  " + expect);
        System.out.println("Found:   " + results);
        if (!results.equals(expect)) {
            System.out.println("ERROR");
            errorCount++;
        }
    }

    int testCount;
    int errorCount;
}