测试abstract class

	public static void  main(String[] args)  {

		stdOut.println("");
		stdOut.println("Testing class Movie...");

		//Movie movie;

		Class classDescriptor = null;

		try {
			classDescriptor  = Class.forName("Movie");//查找Movie类,且赋给变量classDescriptor
		} catch (ClassNotFoundException cnfe) {
			fail("1: Class Movie not found " +
				"(check the name of the class)");
		}

		assertTrue("2: Class Movie is not abstract",
			Modifier.isAbstract(classDescriptor.getModifiers()));//检测是否是抽象类

		Constructor[] constructors = classDescriptor.getConstructors();//获取构造函数
		
		assertTrue("3: Movie should contain one public constructor",
			constructors.length == 1);

		Class[] parameterTypes = constructors[0].getParameterTypes();//获取构造函数的参数类型

		assertTrue(
			"4: the number of parameter of the constructor is incorrect",
			parameterTypes.length == 3);
				
		assertTrue("5: the parameters of the constructor are incorrect ",
			parameterTypes[0].getName().equals("java.lang.String") &&
			parameterTypes[1].getName().equals("[Ljava.lang.String;") &&//注意string数组的表示
			parameterTypes[2].getName().equals("java.lang.String"));//检测参数类型是否一致

		Method[] methods = classDescriptor.getDeclaredMethods();//获取方法

		assertTrue("6: Movie should contain three methods",
			methods.length == 4);

		try {
			Method method = classDescriptor.getMethod("getTitle", new Class[0]);//获取方法
			Class returnType = method.getReturnType();//获取方法的返回类型

			assertTrue("7: the return type of method getTitle is incorrect",
				returnType.getName().equals("java.lang.String"));

		} catch (NoSuchMethodException nsme) {
			fail("5: Method getTitle not found " +
				"(check the name of the method)");
		}

		try {
			Method method =
				classDescriptor.getMethod("getActors", new Class[0]);
			Class returnType = method.getReturnType();

			assertTrue("8: the return type of method getActors is incorrect",
				returnType.getName().equals("[Ljava.lang.String;"));

		} catch (NoSuchMethodException nsme) {
			fail("9: Method getActors not found " +
				"(check the name of the method)");
		}

		try {
			Method method =
				classDescriptor.getMethod("getDirector", new Class[0]);
			Class returnType = method.getReturnType();

			assertTrue("10: the return type of method getDirector is incorrect",
				returnType.getName().equals("java.lang.String"));

		} catch (NoSuchMethodException nsme) {
			fail("11: Method getDirector not found " +
				"(check the name of the method)");
		}

		try {
			Method method = classDescriptor.getMethod("toString", new Class[0]);
			Class returnType = method.getReturnType();

			assertTrue("12: the return type of method toString is incorrect",
				returnType.getName().equals("java.lang.String"));

		} catch (NoSuchMethodException nsme) {
			fail("13: Method toString not found " +
				"(check the name of the method)");
		}


		// Testing constructor
		Constructor[] arrayConstructors = classDescriptor.getConstructors();//获取构造函数

		assertTrue("14: the class should contain one constructor",
			arrayConstructors.length == 1);

		Constructor constructor  = arrayConstructors[0];

		Class[] arrayParameters = constructor.getParameterTypes();//获取构造函数参数类型

		assertTrue("15: the constructor should contain three parameters",
			arrayParameters.length == 3);
		assertTrue("16: the types of the parameters in the constructor are "
			+ "incorrect",
			arrayParameters[0].getName().equals("java.lang.String") &&
			arrayParameters[1].getName().equals("[Ljava.lang.String;") &&
			arrayParameters[2].getName().equals("java.lang.String"));//检测参数类型是否正确

		stdOut.println("done");

	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值