根据XML配置规则导入Excel数据(七)测试

  测试代码
 
 
package com.randy;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.commons.beanutils.PropertyUtils;

import com.ivfly.xlsbean.BeanSpecification;
import com.ivfly.xlsbean.BeansSpecification;
import com.ivfly.xlsbean.ExcelAble;
import com.ivfly.xlsbean.ExcelReader;
import com.ivfly.xlsbean.test.Bean;

public class Test extends TestCase {
   public void test1(){
    String xlsdir=ClassLoader.getSystemResource( "com/ivfly/xlsbean/test/").getPath();
     //根据配置文件获得描述
    InputStream xmlStream = ClassLoader.getSystemResourceAsStream( "com/ivfly/xlsbean/test/bean.xml");
    BeansSpecification beans = BeansSpecification.getInstance(xmlStream);
    BeanSpecification bean = beans.getBeanByClassName(Bean. class.getName());
    List<ExcelAble> list = ExcelReader.parseXls2Beans(bean,xlsdir, "测试用户数据.xls");
    List<String> pl = bean.getPropertyList();
    Map<String,String> head = bean.getNv();
    Iterator<String> iter = head.keySet().iterator();
     while(iter.hasNext()){
      String key = iter.next();
      String value = head.get(key);
      System.out.print( "|"+value+"");
    }
    
    System.out.println( "---------------------");
     for(ExcelAble o : list){
       if(!o.isCheck()){
         continue;
      }
       for(String p : pl){
         try {
          Object value = null;
          value =PropertyUtils.getProperty(o,p);
          System.out.print( "|p="+p+ ";v="+value+"");
        } catch (Exception e) {
          e.printStackTrace();
        }    
        
        
      }
//      while(iter2.hasNext()){
//        String key = iter2.next();
//        Object value = null;
//        try {
//          value =PropertyUtils.getProperty(o,key);
//        } catch (Exception e) {
//          e.printStackTrace();
//        }    
//        System.out.print("|"+value+"");
        
//      }    
      System.out.println();
    }
    
    
     for(ExcelAble o : list){
       if(!o.isCheck()){
        System.out.println(o.getMessage());
      }        
    }
  }
}
 
 
结果
 
|编号|用户名|QQ号|年龄|出生日期---------------------
|p=id;v=wl002|p=name;v=null|p=qq;v=20316558|p=age;v=23|p=birthDate;v=Fri Dec 23 00:00:00 CST 2011
第4行数据:年龄 数据格式不正确

第6行数据:年龄 数据格式不正确

Excel数据
 
最后。
这是自己在元旦假期,闲来无事。花了一整天的时间,研究的结果。
比较仓促。还存在一些问题。比如:
1.这只是针对列表的导入
2.如果是对单个对象的导入则不支持
3.没有字符串长度的验证
4.只是简单验证了下年龄有效性的验证,其他的均没有验证。
5.时间的导入,是否支持多种类型
7.没有提供字典信息编码的支持。
 
以后自己再慢慢 丰富吧。
今天自己又复习了下
Digester
BeanUtils
lang
 

你可能感兴趣的:(职场,测试,休闲)