133N/A/*
133N/A * CDDL HEADER START
133N/A *
133N/A * The contents of this file are subject to the terms of the
133N/A * Common Development and Distribution License (the "License").
133N/A * You may not use this file except in compliance with the License.
133N/A *
133N/A * See LICENSE.txt included in this distribution for the specific
133N/A * language governing permissions and limitations under the License.
133N/A *
133N/A * When distributing Covered Code, include this CDDL HEADER in each
133N/A * file and include the License file at LICENSE.txt.
133N/A * If applicable, add the following below this CDDL HEADER, with the
133N/A * fields enclosed by brackets "[]" replaced with your own identifying
133N/A * information: Portions Copyright [yyyy] [name of copyright owner]
133N/A *
133N/A * CDDL HEADER END
133N/A */
133N/A
133N/A/*
133N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
133N/A * Use is subject to license terms.
133N/A */
133N/Apackage org.opensolaris.opengrok.util;
133N/A
133N/Aimport java.text.ParseException;
133N/Aimport org.junit.After;
133N/Aimport org.junit.AfterClass;
133N/Aimport org.junit.Before;
133N/Aimport org.junit.BeforeClass;
133N/Aimport org.junit.Test;
133N/Aimport static org.junit.Assert.*;
133N/A
133N/A/**
133N/A * JUnit test for org.opensolaris.opengrok.util.Getopt
133N/A */
133N/Apublic class GetoptTest {
133N/A
133N/A public GetoptTest() {
133N/A }
133N/A
133N/A @BeforeClass
133N/A public static void setUpClass() throws Exception {
133N/A }
133N/A
133N/A @AfterClass
133N/A public static void tearDownClass() throws Exception {
133N/A }
133N/A
133N/A @Before
133N/A public void setUp() throws Exception {
133N/A }
133N/A
133N/A @After
133N/A public void tearDown() throws Exception {
133N/A }
133N/A
133N/A @Test
133N/A public void testParseNormal() throws Exception {
133N/A String[] argv = new String[]{"-a", "foo", "-bc", "--", "-f" };
133N/A Getopt instance = new Getopt(argv, "a:bcr:f");
133N/A
134N/A instance.parse();
133N/A
135N/A assertEquals('a', (char) instance.getOpt());
135N/A assertEquals("foo", instance.getOptarg());
135N/A assertEquals('b', (char) instance.getOpt());
135N/A assertNull(instance.getOptarg());
135N/A assertEquals('c', (char) instance.getOpt());
135N/A assertNull(instance.getOptarg());
135N/A assertEquals(-1, instance.getOpt());
135N/A assertEquals(4, instance.getOptind());
133N/A assertTrue(instance.getOptind() < argv.length);
135N/A assertEquals("-f", argv[instance.getOptind()]);
133N/A }
133N/A
133N/A @Test
134N/A public void reset() throws ParseException {
133N/A String[] argv = new String[]{"-a", "foo", "-bc", "argument1" };
133N/A Getopt instance = new Getopt(argv, "a:bc");
133N/A
134N/A instance.parse();
133N/A
135N/A assertEquals('a', (char) instance.getOpt());
135N/A assertEquals("foo", instance.getOptarg());
135N/A assertEquals('b', (char) instance.getOpt());
135N/A assertNull(instance.getOptarg());
135N/A assertEquals('c', (char) instance.getOpt());
135N/A assertNull(instance.getOptarg());
135N/A assertEquals(-1, instance.getOpt());
135N/A assertEquals(3, instance.getOptind());
133N/A assertTrue(instance.getOptind() < argv.length);
135N/A assertEquals("argument1", argv[instance.getOptind()]);
133N/A
133N/A instance.reset();
133N/A
135N/A assertEquals('a', (char) instance.getOpt());
135N/A assertEquals("foo", instance.getOptarg());
135N/A assertEquals('b', (char) instance.getOpt());
135N/A assertNull(instance.getOptarg());
135N/A assertEquals('c', (char) instance.getOpt());
135N/A assertNull(instance.getOptarg());
135N/A assertEquals(-1, instance.getOpt());
135N/A assertEquals(3, instance.getOptind());
133N/A assertTrue(instance.getOptind() < argv.length);
135N/A assertEquals("argument1", argv[instance.getOptind()]);
133N/A } /* Test of reset method, of class Getopt. */
133N/A
133N/A @Test
133N/A public void testParseFailure() throws Exception {
1024N/A String[] argv = new String[]{"-a"};
133N/A Getopt instance = new Getopt(argv, "a:");
133N/A
133N/A try {
133N/A instance.parse();
133N/A fail("Parse shall not allow missing arguments");
133N/A } catch (ParseException exp) {
134N/A if (!exp.getMessage().contains("requires an argument")) {
134N/A // not the exception we expected
134N/A throw exp;
134N/A }
133N/A }
133N/A
133N/A instance = new Getopt(argv, "b");
133N/A try {
133N/A instance.parse();
133N/A fail("Parse shall not allow unknown arguments");
133N/A } catch (ParseException exp) {
134N/A if (!exp.getMessage().contains("Unknown argument: ")) {
134N/A // not the exception we expected
134N/A throw exp;
134N/A }
134N/A }
133N/A }
1024N/A}