⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clutiltestcase.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 * 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.cli.avalon;

// Renamed from org.apache.avalon.excalibur.cli

import java.util.List;

import junit.framework.TestCase;

/**
 * 
 */
public final class ClutilTestCase extends TestCase {
	private static final String[] ARGLIST1 = new String[] { "--you", "are", "--all", "-cler", "kid" };

	private static final String[] ARGLIST2 = new String[] { "-Dstupid=idiot", "are", "--all", "here", "-d" };

	private static final String[] ARGLIST3 = new String[] {
	// duplicates
			"-Dstupid=idiot", "are", "--all", "--all", "here" };

	private static final String[] ARGLIST4 = new String[] {
	// incompatable (blee/all)
			"-Dstupid", "idiot", "are", "--all", "--blee", "here" };

	private static final String[] ARGLIST5 = new String[] { "-f", "myfile.txt" };

	private static final int DEFINE_OPT = 'D';

	private static final int CASE_CHECK_OPT = 'd';

	private static final int YOU_OPT = 'y';

	private static final int ALL_OPT = 'a';

	private static final int CLEAR1_OPT = 'c';

	private static final int CLEAR2_OPT = 'l';

	private static final int CLEAR3_OPT = 'e';

	private static final int CLEAR5_OPT = 'r';

	private static final int BLEE_OPT = 'b';

	private static final int FILE_OPT = 'f';

	private static final int TAINT_OPT = 'T';

	private static final CLOptionDescriptor DEFINE = new CLOptionDescriptor("define",
			CLOptionDescriptor.ARGUMENTS_REQUIRED_2, DEFINE_OPT, "define");

	private static final CLOptionDescriptor DEFINE_MANY = new CLOptionDescriptor("define",
			CLOptionDescriptor.ARGUMENTS_REQUIRED_2 | CLOptionDescriptor.DUPLICATES_ALLOWED, DEFINE_OPT, "define");

	private static final CLOptionDescriptor CASE_CHECK = new CLOptionDescriptor("charCheck",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CASE_CHECK_OPT, "check character case sensitivity");

	private static final CLOptionDescriptor YOU = new CLOptionDescriptor("you", CLOptionDescriptor.ARGUMENT_DISALLOWED,
			YOU_OPT, "you");

	private static final CLOptionDescriptor CLEAR1 = new CLOptionDescriptor("c",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR1_OPT, "c");

	private static final CLOptionDescriptor CLEAR2 = new CLOptionDescriptor("l",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR2_OPT, "l");

	private static final CLOptionDescriptor CLEAR3 = new CLOptionDescriptor("e",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR3_OPT, "e");

	private static final CLOptionDescriptor CLEAR5 = new CLOptionDescriptor("r",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR5_OPT, "r");

	private static final CLOptionDescriptor BLEE = new CLOptionDescriptor("blee",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, BLEE_OPT, "blee");

	private static final CLOptionDescriptor ALL = new CLOptionDescriptor("all",
			CLOptionDescriptor.ARGUMENT_DISALLOWED,
			ALL_OPT, "all", new CLOptionDescriptor[] { BLEE });

	private static final CLOptionDescriptor FILE = new CLOptionDescriptor("file", 
			CLOptionDescriptor.ARGUMENT_REQUIRED, FILE_OPT, "the build file.");

	private static final CLOptionDescriptor TAINT = new CLOptionDescriptor("taint",
			CLOptionDescriptor.ARGUMENT_OPTIONAL, TAINT_OPT, "turn on tainting checks (optional level).");

	private static final CLOptionDescriptor [] OPTIONS = new CLOptionDescriptor [] {
	        new CLOptionDescriptor("none", 
	        		CLOptionDescriptor.ARGUMENT_DISALLOWED | CLOptionDescriptor.DUPLICATES_ALLOWED,
	        		'0', "no parameter"),

	        new CLOptionDescriptor("optional", 
	        		CLOptionDescriptor.ARGUMENT_OPTIONAL | CLOptionDescriptor.DUPLICATES_ALLOWED, 
	        		'?', "optional parameter"),
			
			new CLOptionDescriptor("one", 
					CLOptionDescriptor.ARGUMENT_REQUIRED | CLOptionDescriptor.DUPLICATES_ALLOWED, 
					'1', "one parameter"),
			
			new CLOptionDescriptor("two", 
					CLOptionDescriptor.ARGUMENTS_REQUIRED_2 | CLOptionDescriptor.DUPLICATES_ALLOWED, 
					'2', "two parameters")
	};


	

	public ClutilTestCase() {
		this("Command Line Interpreter Test Case");
	}

	public ClutilTestCase(String name) {
		super(name);
	}

	public void testOptionalArgWithSpace() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(option1.getDescriptor().getId(), CLOption.TEXT_ARGUMENT);
		assertEquals(option1.getArgument(0), "param");

		final CLOption option2 = (CLOption) clOptions.get(2);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testOptionalArgLong() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		// Check that optional args work woth long options
		final String[] args = new String[] { "--taint", "param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(CLOption.TEXT_ARGUMENT, option1.getDescriptor().getId());
		assertEquals("param", option1.getArgument(0));

		final CLOption option2 = (CLOption) clOptions.get(2);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testOptionalArgLongEquals() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		// Check that optional args work woth long options
		final String[] args = new String[] { "--taint=param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 2, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), "param", option0.getArgument(0));

		final CLOption option2 = (CLOption) clOptions.get(1);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testShortOptArgUnenteredBeforeOtherOpt() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 2, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(option1.getDescriptor().getId(), ALL_OPT);
		assertEquals(option1.getArgument(0), null);
	}

	public void testOptionalArgsWithArgShortBeforeOtherOpt() {
		// "-T3","-a"
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T3", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 2);
		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals(option0.getDescriptor().getId(), TAINT_OPT);
		assertEquals(option0.getArgument(0), "3");

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(ALL_OPT, option1.getDescriptor().getId());
		assertEquals(null, option1.getArgument(0));
	}

	public void testOptionalArgsWithArgShortEqualsBeforeOtherOpt() {
		// "-T3","-a"
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T=3", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 2);
		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals(option0.getDescriptor().getId(), TAINT_OPT);
		assertEquals(option0.getArgument(0), "3");

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(ALL_OPT, option1.getDescriptor().getId());
		assertEquals(null, option1.getArgument(0));
	}

	public void testOptionalArgsNoArgShortBeforeOtherOpt() {
		// "-T","-a"
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 2);
		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals(TAINT_OPT, option0.getDescriptor().getId());
		assertEquals(null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(ALL_OPT, option1.getDescriptor().getId());
		assertEquals(null, option1.getArgument(0));
	}

	public void testFullParse() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5 };

		final CLArgsParser parser = new CLArgsParser(ARGLIST1, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 8);
		assertEquals(((CLOption) clOptions.get(0)).getDescriptor().getId(), YOU_OPT);
		assertEquals(((CLOption) clOptions.get(1)).getDescriptor().getId(), 0);
		assertEquals(((CLOption) clOptions.get(2)).getDescriptor().getId(), ALL_OPT);
		assertEquals(((CLOption) clOptions.get(3)).getDescriptor().getId(), CLEAR1_OPT);
		assertEquals(((CLOption) clOptions.get(4)).getDescriptor().getId(), CLEAR2_OPT);
		assertEquals(((CLOption) clOptions.get(5)).getDescriptor().getId(), CLEAR3_OPT);
		assertEquals(((CLOption) clOptions.get(6)).getDescriptor().getId(), CLEAR5_OPT);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -