FileDocCategorySizeDatePackage
GetOptTest.javaAPI DocExample4926Mon Dec 29 12:47:30 GMT 2003regress

GetOptTest

public class GetOptTest extends TestCase
Some test cases for GetOpt.
XXX TODO - compare with expected 'c' values.
author
Ian F. Darwin, http://www.darwinsys.com/
version
$Id: GetOptTest.java,v 1.15 2003/12/29 18:47:30 ian Exp $

Fields Summary
private String
goodArgChars
private String[]
goodArgs
private String[]
goodLongArgs
private char[]
goodArgsExpectChars
private String
badArgChars
private String[]
badArgs
private char[]
badArgsExpectChars
private com.darwinsys.lang.GetOptDesc[]
options
Constructors Summary
Methods Summary
protected voidnewWayInner(com.darwinsys.lang.GetOpt go, java.util.Map map)

		assertFalse(map.size() == 0);
		if (map.size() == 0) {
			throw new IllegalArgumentException(
				"Unexpected empty map");
		}
		int errs = 0;
		Iterator it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = (String)it.next();
			char c = key.charAt(0);
			String val = (String)map.get(key);
			switch(c) {
				case '?":
					errs++; break;
				case 'o": assertEquals(val, "outfile"); break;
				case 'f":
				case 'h":
				case '1":
					 assertEquals(val, null);
					break;
				default:
					throw new IllegalArgumentException(
						"Unexpected c value " + c);
			}
		}
		assertEquals(1, go.getFilenameList().size());
		assertEquals("infile", go.getFilenameList().get(0));
	
voidprocess1(java.lang.String argChars, java.lang.String[] args, boolean shouldFail)


		System.out.println("** START ** " + argChars);

		GetOpt go = new GetOpt(argChars);

		int errs = 0, ix = 0;

		char c;
		while ((c = go.getopt(args)) != 0) {
			if (c == '?") {
				System.out.print("Bad option");
				++errs;
			} else {
				System.out.print("Found " + c);
				if (go.optarg() != null)
					System.out.print("; Option " + go.optarg());
			}
			System.out.println();
		}

		// Process any filename-like arguments.
		for (int i=go.getOptInd(); i<args.length; i++) {
			System.out.println("Filename-like arg " + args[i]);
		}
	
voidprocess2(java.lang.String argChars, java.lang.String[] args, boolean shouldFail)

		int errs = 0;

		System.out.println("** START NEW WAY ** " + argChars);
		GetOpt go2 = new GetOpt(argChars);
		Map m = go2.parseArguments(args);
		if (m.size() == 0)
			System.out.println("NO ARGS MATCHED");
		Iterator it = m.keySet().iterator();
		while (it.hasNext()) {
			Object key = it.next();
			char c = ((String)key).charAt(0);
			System.out.print("Found " + c);
			if (c == '?")
				errs++;
			String val = (String)m.get(key);
			if (val == null || val.equals(""))
				System.out.print("; (no option)");
			else
				System.out.print("; Option " + val);
			System.out.println();
		}

		List filenames = go2.getFilenameList();
		for (int i = 0; i < filenames.size(); i++) {
			System.out.println("Filename-like arg " + filenames.get(i));
		}

		if (shouldFail) {
			if (errs != 0)
				System.out.println("Expected error(s) found");
			else
				System.out.println("** FAILURE ** Expected errors not found");
		} else {
			if (errs == 0)
				System.out.println("Expected error(s) found");
			else
				System.out.println("** FAILURE ** Expected errors not found");
		}
	
public voidtestBadArgChar()


	   
		String bad = "abc@";
		try {
			new GetOpt(bad);
			fail("GetOpt(" + bad + ") did not throw expected exception");
		} catch (IllegalArgumentException ex) {
			System.err.println("Caught expected exception " + ex);
		}
	
public voidtestConstructorArgs()

		try {
			String bad = null;
			new GetOpt(bad);
			fail("GetOpt(null) did not throw expected exception");
		} catch (IllegalArgumentException ex) {
			System.err.println("Caught expected exception " + ex);
		}
		try {
			new GetOpt("::");
			fail("GetOpt(::) did not throw expected exception");
		} catch (IllegalArgumentException ex) {
			System.err.println("Caught expected exception " + ex);
		}
		new GetOpt("f:c:");		// this failed at one point - multiple : args
		new GetOpt("foo");		// multiple occurrences of same letter - ok?
	
public voidtestNewWayLong()

		GetOpt go = new GetOpt(options);
		Map map = go.parseArguments(goodLongArgs);
		newWayInner(go, map);
	
public voidtestNewWayShort()

		GetOpt go = new GetOpt(options);
		Map map = go.parseArguments(goodArgs);
		newWayInner(go, map);
	
public voidtestOldwayBadCharsBadArgs()

		process1(badArgChars, badArgs, true);
		process2(badArgChars, badArgs, true);
	
public voidtestOldwayBadCharsGoodArgs()

		process1(badArgChars, goodArgs, true);
		process2(badArgChars, goodArgs, true);
	
public voidtestOldwayGood()

		process1(goodArgChars, goodArgs, false);
		process2(goodArgChars, goodArgs, false);