package com.youc.beanutils;
import java.beans.Beans;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.junit.Test;
public class Demo01 {
@Test
public void test1() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
BeanUtils.setProperty(p, "name", "x***");
System.out.println(p.getName());
}
@Test
public void test2() throws IllegalAccessException,
InvocationTargetException {
String name = "aaa";
String password = "124";
String age = "23";
String brithday = "1989-09-09";
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("不是String类型不能成功转换");
}
String str = (String) value;
if (str.equals("")) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
Person p = new Person();
BeanUtils.setProperty(p, "name", "name");
// 只支持8中基本数据类型的自动转换,这里自动转换了age的类型
BeanUtils.setProperty(p, "age", "age");
BeanUtils.setProperty(p, "password", "password");
BeanUtils.setProperty(p, "brithday", "brithday");
System.out.println("姓名:" + p.getName());
System.out.println("年龄:" + p.getAge());
System.out.println("密码:" + p.getPassword());
System.out.println("生日:" + p.getBrithday());
}
}
junit测试错误这个?为啥啊?
java.lang.RuntimeException: java.text.ParseException: Unparseable date: "brithday"
at com.youc.beanutils.Demo01$1.convert(Demo01.java:48)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:470)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
at com.youc.beanutils.Demo01.test2(Demo01.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.text.ParseException: Unparseable date: "brithday"
at java.text.DateFormat.parse(DateFormat.java:337)
at com.youc.beanutils.Demo01$1.convert(Demo01.java:46)
... 28 more
这是我要beanUtils的类
package com.youc.beanutils;
import java.util.Date;
/**
*
* 使用beanUtils操作javabean的属性(第三方jar包)
* @author zs
*
*/
public class Person {// javabean
private String name; // 字段
private String password; // 字段
private int age; // 字段
private Date brithday ;
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public String getAb(){
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
} |