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

📄 reflectionutilstests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
字号:
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed 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.springframework.util;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import junit.framework.TestCase;

import org.springframework.beans.TestBean;

/**
 * @author Rob Harrop
 */
public class ReflectionUtilsTests extends TestCase {

	public void testInvokeMethod() throws Exception {
		String rob = "Rob Harrop";
		String juergen = "Juergen Hoeller";

		TestBean bean = new TestBean();
		bean.setName(rob);

		Method getName = TestBean.class.getMethod("getName", (Class[]) null);
		Method setName = TestBean.class.getMethod("setName", new Class[] { String.class });

		Object name = ReflectionUtils.invokeMethod(getName, bean);
		assertEquals("Incorrect name returned", rob, name);

		ReflectionUtils.invokeMethod(setName, bean, new Object[] { juergen });
		assertEquals("Incorrect name set", juergen, bean.getName());
	}

	public void testCopySrcToDestinationOfIncorrectClass() {
		TestBean src = new TestBean();
		String dest = new String();
		try {
			ReflectionUtils.shallowCopyFieldState(src, dest);
			fail();
		}
		catch (IllegalArgumentException ex) {
			// Ok
		}
	}

	public void testRejectsNullSrc() {
		TestBean src = null;
		String dest = new String();
		try {
			ReflectionUtils.shallowCopyFieldState(src, dest);
			fail();
		}
		catch (IllegalArgumentException ex) {
			// Ok
		}
	}

	public void testRejectsNullDest() {
		TestBean src = new TestBean();
		String dest = null;
		try {
			ReflectionUtils.shallowCopyFieldState(src, dest);
			fail();
		}
		catch (IllegalArgumentException ex) {
			// Ok
		}
	}

	public void testValidCopy() {
		TestBean src = new TestBean();
		TestBean dest = new TestBean();
		testValidCopy(src, dest);
	}

	public static class TestBeanSubclassWithNewField extends TestBean {
		private int magic;

		protected String prot = "foo";
	}

	public static class TestBeanSubclassWithFinalField extends TestBean {
		private final String foo = "will break naive copy that doesn't exclude statics";
	}

	public void testValidCopyOnSubTypeWithNewField() {
		TestBeanSubclassWithNewField src = new TestBeanSubclassWithNewField();
		TestBeanSubclassWithNewField dest = new TestBeanSubclassWithNewField();
		src.magic = 11;

		// Will check inherited fields are copied
		testValidCopy(src, dest);

		// Check subclass fields were copied
		assertEquals(src.magic, dest.magic);
		assertEquals(src.prot, dest.prot);
	}

	public void testValidCopyToSubType() {
		TestBean src = new TestBean();
		TestBeanSubclassWithNewField dest = new TestBeanSubclassWithNewField();
		dest.magic = 11;
		testValidCopy(src, dest);
		// Should have left this one alone
		assertEquals(11, dest.magic);
	}

	public void testValidCopyToSubTypeWithFinalField() {
		TestBeanSubclassWithFinalField src = new TestBeanSubclassWithFinalField();
		TestBeanSubclassWithFinalField dest = new TestBeanSubclassWithFinalField();
		// Check that this doesn't fail due to attempt to assign final
		testValidCopy(src, dest);
	}

	private void testValidCopy(TestBean src, TestBean dest) {
		src.setName("freddie");
		src.setAge(15);
		src.setSpouse(new TestBean());
		assertFalse(src.getAge() == dest.getAge());

		ReflectionUtils.shallowCopyFieldState(src, dest);
		assertEquals(src.getAge(), dest.getAge());
		assertEquals(src.getSpouse(), dest.getSpouse());
		assertEquals(src.getDoctor(), dest.getDoctor());
	}

	static class ListSavingMethodCallback implements ReflectionUtils.MethodCallback {
		private List methodNames = new LinkedList();

		private List methods = new LinkedList();

		public void doWith(Method m) throws IllegalArgumentException, IllegalAccessException {
			methodNames.add(m.getName());
			methods.add(m);
		}

		public List getMethodNames() {
			return methodNames;
		}

		public List getMethods() {
			return methods;
		}
	};

	public void testDoWithProtectedMethods() {
		ListSavingMethodCallback mc = new ListSavingMethodCallback();
		ReflectionUtils.doWithMethods(TestBean.class, mc, new ReflectionUtils.MethodFilter() {
			public boolean matches(Method m) {
				return Modifier.isProtected(m.getModifiers());
			}
		});
		assertFalse(mc.getMethodNames().isEmpty());
		assertTrue("Must find protected method on Object", mc.getMethodNames().contains("clone"));
		assertTrue("Must find protected method on Object", mc.getMethodNames().contains("finalize"));
		assertFalse("Public, not protected", mc.getMethodNames().contains("hashCode"));
		assertFalse("Public, not protected", mc.getMethodNames().contains("absquatulate"));
	}

	public static class TestBeanSubclass extends TestBean {
		public void absquatulate() {
			throw new UnsupportedOperationException();
		}
	}

	public void testDuplicatesFound() {
		ListSavingMethodCallback mc = new ListSavingMethodCallback();
		ReflectionUtils.doWithMethods(TestBeanSubclass.class, mc);
		int absquatulateCount = 0;
		for (Iterator it = mc.getMethodNames().iterator(); it.hasNext();) {
			String name = (String) it.next();
			if (name.equals("absquatulate")) {
				++absquatulateCount;
			}
		}
		assertEquals("Found 2 absquatulates", 2, absquatulateCount);
	}

	public void testFindMethod() throws Exception {
	  assertNotNull(ReflectionUtils.findMethod(B.class, "bar", new Class[]{String.class}));
	  assertNotNull(ReflectionUtils.findMethod(B.class, "foo", new Class[]{Integer.class}));
	}
	private static class A {
		private void foo(Integer i){}
	}

	private static class B extends A{
		void bar(String s) {}
	}
}

⌨️ 快捷键说明

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